From 34e3b781234be6eb9c348e63056835e87377eea9 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 26 Apr 2012 18:14:53 +0800 Subject: [PATCH] [openstack|compute] Added list/get support for /os-hosts --- lib/fog/openstack/compute.rb | 7 ++ lib/fog/openstack/models/compute/host.rb | 28 +++++++ lib/fog/openstack/models/compute/hosts.rb | 32 ++++++++ .../requests/compute/get_host_details.rb | 74 +++++++++++++++++++ .../openstack/requests/compute/list_hosts.rb | 32 ++++++++ 5 files changed, 173 insertions(+) create mode 100644 lib/fog/openstack/models/compute/host.rb create mode 100644 lib/fog/openstack/models/compute/hosts.rb create mode 100644 lib/fog/openstack/requests/compute/get_host_details.rb create mode 100644 lib/fog/openstack/requests/compute/list_hosts.rb diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index c5eceae5b..7812c31d7 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -37,6 +37,8 @@ module Fog collection :networks model :snapshot collection :snapshots + model :host + collection :hosts ## REQUESTS # @@ -148,6 +150,11 @@ module Fog request :get_quota_defaults request :update_quota + # Hosts + request :list_hosts + request :get_host_details + + class Mock def self.data diff --git a/lib/fog/openstack/models/compute/host.rb b/lib/fog/openstack/models/compute/host.rb new file mode 100644 index 000000000..aeda3fe0c --- /dev/null +++ b/lib/fog/openstack/models/compute/host.rb @@ -0,0 +1,28 @@ +require 'fog/compute/models/server' +require 'fog/openstack/models/compute/metadata' + +module Fog + module Compute + class OpenStack + + class Host < Fog::Model + + attribute :host_name + attribute :service + attribute :details + + def initialize(attributes) + @connection = attributes[:connection] + super + end + + def details + connection.get_host_details(self.host_name).body['host'] + end + + end + + end + end + +end diff --git a/lib/fog/openstack/models/compute/hosts.rb b/lib/fog/openstack/models/compute/hosts.rb new file mode 100644 index 000000000..d56804167 --- /dev/null +++ b/lib/fog/openstack/models/compute/hosts.rb @@ -0,0 +1,32 @@ +require 'fog/core/collection' +require 'fog/openstack/models/compute/host' + +module Fog + module Compute + class OpenStack + + class Hosts < Fog::Collection + + model Fog::Compute::OpenStack::Host + + def all + data = connection.list_hosts.body['hosts'] + load(data) + end + + def get(host_name) + if host = connection.get_host_details(host_name).body['host'] + new({ + 'host_name' => host_name, + 'details' => host} + ) + end + rescue Fog::Compute::OpenStack::NotFound + nil + end + + end + + end + end +end diff --git a/lib/fog/openstack/requests/compute/get_host_details.rb b/lib/fog/openstack/requests/compute/get_host_details.rb new file mode 100644 index 000000000..ba8d73166 --- /dev/null +++ b/lib/fog/openstack/requests/compute/get_host_details.rb @@ -0,0 +1,74 @@ +module Fog + module Compute + class OpenStack + class Real + + def get_host_details(host) + request( + :expects => [200, 203], + :method => 'GET', + :path => "os-hosts/#{host}.json" + ) + end + + end + + class Mock + + def get_host_details(host) + + response = Excon::Response.new + response.status = 200 + response.body = { "host" => [ + { "resource" => { + "project" => "(total)", + "memory_mb" => 64427, + "host" => "cn28.la-1-3.morphcloud.net", + "cpu" => 12, + "disk_gb" => 1608 + } + }, + { "resource" => { + "project" => "(used_now)", + "memory_mb" => 1753, + "host" => "cn28.la-1-3.morphcloud.net", + "cpu" => 3, + "disk_gb" => 33 + } + }, + { "resource" => { + "project" => "(used_max)", + "memory_mb" => 7168, + "host" => "cn28.la-1-3.morphcloud.net", + "cpu" => 3, + "disk_gb" => 45 + } + }, + { "resource" => { + "project" => "bf8301f5164f4790889a1bc2bfb16d99", + "memory_mb" => 5120, + "host" => "cn28.la-1-3.morphcloud.net", + "cpu" => 2, + "disk_gb" => 35 + } + }, + { "resource" => { + "project" => "3bb4d0301c5f47d5b4d96a361fcf96f4", + "memory_mb" => 2048, + "host" => "cn28.la-1-3.morphcloud.net", + "cpu" => 1, + "disk_gb" => 10 + } + } + ] + } + response + + + end + + + end # mock + end # openstack + end # compute +end # fog diff --git a/lib/fog/openstack/requests/compute/list_hosts.rb b/lib/fog/openstack/requests/compute/list_hosts.rb new file mode 100644 index 000000000..4b8435455 --- /dev/null +++ b/lib/fog/openstack/requests/compute/list_hosts.rb @@ -0,0 +1,32 @@ +module Fog + module Compute + class OpenStack + class Real + + def list_hosts + request( + :expects => [200, 203], + :method => 'GET', + :path => 'os-hosts.json' + ) + end + + end + + class Mock + + def list_hosts + response = Excon::Response.new + response.status = 200 + response.body = { "hosts" => [ + {"host_name" => "host.test.net", "service"=>"compute"} + ] + } + response + end + + + end # mock + end # openstack + end # compute +end # fog \ No newline at end of file