mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #3567 from Ladas/openstack_add_nova_service_support
OpenStack add nova service support
This commit is contained in:
commit
1f390d0ad1
9 changed files with 327 additions and 0 deletions
|
@ -18,6 +18,8 @@ module Fog
|
|||
model_path 'fog/openstack/models/compute'
|
||||
model :server
|
||||
collection :servers
|
||||
model :service
|
||||
collection :services
|
||||
model :image
|
||||
collection :images
|
||||
model :flavor
|
||||
|
@ -85,6 +87,13 @@ module Fog
|
|||
request :live_migrate_server
|
||||
request :migrate_server
|
||||
|
||||
# Service CRUD
|
||||
request :list_services
|
||||
request :enable_service
|
||||
request :disable_service
|
||||
request :disable_service_log_reason
|
||||
request :delete_service
|
||||
|
||||
# Image CRUD
|
||||
request :list_images
|
||||
request :list_images_detail
|
||||
|
|
48
lib/fog/openstack/models/compute/service.rb
Normal file
48
lib/fog/openstack/models/compute/service.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Service < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :binary
|
||||
attribute :host
|
||||
attribute :state
|
||||
attribute :status
|
||||
attribute :updated_at
|
||||
attribute :zone
|
||||
|
||||
#detailed
|
||||
attribute :disabled_reason
|
||||
|
||||
def initialize(attributes)
|
||||
# Old 'connection' is renamed as service and should be used instead
|
||||
prepare_service_value(attributes)
|
||||
super
|
||||
end
|
||||
|
||||
def enable
|
||||
requires :binary, :host
|
||||
service.enable_service(self.host, self.binary)
|
||||
end
|
||||
|
||||
def disable
|
||||
requires :binary, :host
|
||||
service.disable_service(self.host, self.binary)
|
||||
end
|
||||
|
||||
def disable_and_log_reason
|
||||
requires :binary, :host, :disabled_reason
|
||||
service.disable_service_log_reason(self.host, self.binary, self.disabled_reason)
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
service.delete_service(self.id)
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
lib/fog/openstack/models/compute/services.rb
Normal file
20
lib/fog/openstack/models/compute/services.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/openstack/models/compute/service'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Services < Fog::Collection
|
||||
model Fog::Compute::OpenStack::Service
|
||||
|
||||
def all(parameters=nil)
|
||||
load(service.list_services(parameters).body['services'])
|
||||
end
|
||||
|
||||
def details(parameters=nil)
|
||||
load(service.list_services(parameters).body['services'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/openstack/requests/compute/delete_service.rb
Normal file
32
lib/fog/openstack/requests/compute/delete_service.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Real
|
||||
def delete_service(uuid, optional_params = nil)
|
||||
# Encode all params
|
||||
optional_params = optional_params.each { |k, v| optional_params[k] = URI::encode(v) } if optional_params
|
||||
|
||||
request(
|
||||
:expects => [202, 204],
|
||||
:method => 'DELETE',
|
||||
:path => "os-services/#{uuid}",
|
||||
:query => optional_params
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def delete_service(host, binary, optional_params = nil)
|
||||
response = Excon::Response.new
|
||||
response.status = 204
|
||||
response.headers = {
|
||||
"Content-Type" => "text/html; charset=UTF-8",
|
||||
"Content-Length" => "0",
|
||||
"Date" => Date.new
|
||||
}
|
||||
response
|
||||
end
|
||||
end # mock
|
||||
end # openstack
|
||||
end # compute
|
||||
end # fog
|
37
lib/fog/openstack/requests/compute/disable_service.rb
Normal file
37
lib/fog/openstack/requests/compute/disable_service.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Real
|
||||
def disable_service(host, binary, optional_params = nil)
|
||||
data = {"host" => host, "binary" => binary}
|
||||
|
||||
# Encode all params
|
||||
optional_params = optional_params.each { |k, v| optional_params[k] = URI::encode(v) } if optional_params
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 200,
|
||||
:method => 'PUT',
|
||||
:path => "os-services/disable",
|
||||
:query => optional_params
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def disable_service(host, binary, optional_params = nil)
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
"service" => {
|
||||
"host" => "host1",
|
||||
"binary" => "nova-compute",
|
||||
"status" => "disabled"
|
||||
}
|
||||
}
|
||||
response
|
||||
end
|
||||
end # mock
|
||||
end # openstack
|
||||
end # compute
|
||||
end # fog
|
|
@ -0,0 +1,38 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Real
|
||||
def disable_service_log_reason(host, binary, disabled_reason, optional_params = nil)
|
||||
data = {"host" => host, "binary" => binary, "disabled_reason" => disabled_reason}
|
||||
|
||||
# Encode all params
|
||||
optional_params = optional_params.each { |k, v| optional_params[k] = URI::encode(v) } if optional_params
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 200,
|
||||
:method => 'PUT',
|
||||
:path => "os-services/disable-log-reason",
|
||||
:query => optional_params
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def disable_service_log_reason(host, binary, disabled_reason, optional_params = nil)
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
"service" => {
|
||||
"host" => "host1",
|
||||
"binary" => "nova-compute",
|
||||
"status" => "disabled",
|
||||
"disabled_reason" => "test2"
|
||||
}
|
||||
}
|
||||
response
|
||||
end
|
||||
end # mock
|
||||
end # openstack
|
||||
end # compute
|
||||
end # fog
|
38
lib/fog/openstack/requests/compute/enable_service.rb
Normal file
38
lib/fog/openstack/requests/compute/enable_service.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Real
|
||||
def enable_service(host, binary, optional_params = nil)
|
||||
data = {"host" => host, "binary" => binary}
|
||||
|
||||
# Encode all params
|
||||
optional_params = optional_params.each { |k, v| optional_params[k] = URI::encode(v) } if optional_params
|
||||
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 200,
|
||||
:method => 'PUT',
|
||||
:path => "os-services/enable",
|
||||
:query => optional_params
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def enable_service(host, binary, optional_params = nil)
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
"service" => {
|
||||
"host" => "host1",
|
||||
"binary" => "nova-compute",
|
||||
"status" => "enabled"
|
||||
}
|
||||
}
|
||||
response
|
||||
end
|
||||
end # mock
|
||||
end # openstack
|
||||
end # compute
|
||||
end # fog
|
72
lib/fog/openstack/requests/compute/list_services.rb
Normal file
72
lib/fog/openstack/requests/compute/list_services.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class OpenStack
|
||||
class Real
|
||||
def list_services(parameters=nil)
|
||||
if parameters
|
||||
query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
|
||||
else
|
||||
query = {}
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => [200, 203],
|
||||
:method => 'GET',
|
||||
:path => 'os-services',
|
||||
:query => query
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def list_services(parameters=nil)
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
"services" => [{
|
||||
"id" => 1,
|
||||
"binary" => "nova-scheduler",
|
||||
"host" => "host1",
|
||||
"state" => "up",
|
||||
"status" => "disabled",
|
||||
"updated_at" => "2012-10-29T13:42:02.000000",
|
||||
"zone" => "internal",
|
||||
"disabled_reason" => "test2"
|
||||
},
|
||||
{
|
||||
"id" => 2,
|
||||
"binary" => "nova-compute",
|
||||
"host" => "host1",
|
||||
"state" => "up",
|
||||
"status" => "disabled",
|
||||
"updated_at" => "2012-10-29T13:42:05.000000",
|
||||
"zone" => "nova",
|
||||
"disabled_reason" => "test2"
|
||||
},
|
||||
{
|
||||
"id" => 3,
|
||||
"binary" => "nova-scheduler",
|
||||
"host" => "host2",
|
||||
"state" => "down",
|
||||
"status" => "enabled",
|
||||
"updated_at" => "2012-09-19T06:55:34.000000",
|
||||
"zone" => "internal",
|
||||
"disabled_reason" => "nil"
|
||||
},
|
||||
{
|
||||
"id" => 4,
|
||||
"binary" => "nova-compute",
|
||||
"host" => "host2",
|
||||
"state" => "down",
|
||||
"status" => "disabled",
|
||||
"updated_at" => "2012-09-18T08:03:38.000000",
|
||||
"zone" => "nova",
|
||||
"disabled_reason" => "test2"
|
||||
}]
|
||||
}
|
||||
response
|
||||
end
|
||||
end # mock
|
||||
end # openstack
|
||||
end # compute
|
||||
end # fog
|
33
tests/openstack/requests/compute/service_tests.rb
Normal file
33
tests/openstack/requests/compute/service_tests.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
Shindo.tests('Fog::Compute[:openstack] | service requests', ['openstack']) do
|
||||
|
||||
@service_format = {
|
||||
"id" => Integer,
|
||||
"binary" => String,
|
||||
"host" => String,
|
||||
"state" => String,
|
||||
"status" => String,
|
||||
"updated_at" => String,
|
||||
"zone" => String,
|
||||
'disabled_reason' => Fog::Nullable::String
|
||||
}
|
||||
|
||||
tests('success') do
|
||||
tests('#list_services').data_matches_schema({'services' => [@service_format]}) do
|
||||
services = Fog::Compute[:openstack].list_services.body
|
||||
@service = services['services'].last
|
||||
services
|
||||
end
|
||||
|
||||
tests('#disable_service').succeeds do
|
||||
Fog::Compute[:openstack].disable_service(@service['host'], @service['binary'])
|
||||
end
|
||||
|
||||
tests('#disable_service_log_reason').succeeds do
|
||||
Fog::Compute[:openstack].disable_service_log_reason(@service['host'], @service['binary'], 'reason')
|
||||
end
|
||||
|
||||
tests('#enable_service').succeeds do
|
||||
Fog::Compute[:openstack].enable_service(@service['host'], @service['binary'])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue