Skip to content

Commit

Permalink
Merge pull request fog#3567 from Ladas/openstack_add_nova_service_sup…
Browse files Browse the repository at this point in the history
…port

OpenStack add nova service support
  • Loading branch information
geemus committed Jun 1, 2015
2 parents 6c477e9 + e8fc08f commit 1f390d0
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/fog/openstack/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class OpenStack < Fog::Service
model_path 'fog/openstack/models/compute'
model :server
collection :servers
model :service
collection :services
model :image
collection :images
model :flavor
Expand Down Expand Up @@ -85,6 +87,13 @@ class OpenStack < Fog::Service
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
Expand Down
48 changes: 48 additions & 0 deletions lib/fog/openstack/models/compute/service.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions lib/fog/openstack/models/compute/services.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions lib/fog/openstack/requests/compute/delete_service.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions lib/fog/openstack/requests/compute/disable_service.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions lib/fog/openstack/requests/compute/disable_service_log_reason.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions lib/fog/openstack/requests/compute/enable_service.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 72 additions & 0 deletions lib/fog/openstack/requests/compute/list_services.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions tests/openstack/requests/compute/service_tests.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1f390d0

Please sign in to comment.