From a16a9fe12df1638d92a1a364f5a7ab49907ed036 Mon Sep 17 00:00:00 2001 From: Nelvin Driz Date: Fri, 23 Nov 2012 15:49:50 +0800 Subject: [PATCH] [openstack|compute] Add `get_limits` request Signed-off-by: Nelvin Driz --- lib/fog/openstack/compute.rb | 1 + .../openstack/requests/compute/get_limits.rb | 97 +++++++++++++++++++ .../openstack/requests/compute/limit_tests.rb | 64 ++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 lib/fog/openstack/requests/compute/get_limits.rb create mode 100644 tests/openstack/requests/compute/limit_tests.rb diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index 9e0481f4a..ae381a55a 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -126,6 +126,7 @@ module Fog # Tenant request :list_tenants request :set_tenant + request :get_limits # Volume request :list_volumes diff --git a/lib/fog/openstack/requests/compute/get_limits.rb b/lib/fog/openstack/requests/compute/get_limits.rb new file mode 100644 index 000000000..8f22e3253 --- /dev/null +++ b/lib/fog/openstack/requests/compute/get_limits.rb @@ -0,0 +1,97 @@ +module Fog + module Compute + class OpenStack + + # http://docs.openstack.org/api/openstack-compute/2/content/ProgramaticLimits.html + # + class Real + def get_limits + request( + :expects => 200, + :method => 'GET', + :path => '/limits.json' + ) + end + end + + + class Mock + def get_limits + rate_limits = [ + { 'regex' => '.*', + 'limit' => [ + { 'next-available' => '2012-11-22T16:13:44Z', + 'unit' => 'MINUTE', + 'verb' => 'POST', + 'remaining' => 9, + 'value' => 10 }, + { 'next-available' => '2012-11-23T00:46:14Z', + 'unit' => 'MINUTE', + 'verb' => 'PUT', + 'remaining' => 10, + 'value' => 10 }, + { 'next-available' => '2012-11-22T16:14:30Z', + 'unit' => 'MINUTE', + 'verb' => 'DELETE', + 'remaining' => 99, + 'value' => 100 } ], + 'uri' => '*' }, + { 'regex' => '^/servers', + 'limit' => [ + { 'next-available' => '2012-11-23T00:46:14Z', + 'unit' => 'DAY', + 'verb' => 'POST', + 'remaining' => 50, + 'value' => 50} ], + 'uri'=>'*/servers' }, + { 'regex' => '.*changes-since.*', + 'limit' => [ + { 'next-available' => '2012-11-23T00:46:14Z', + 'unit' => 'MINUTE', + 'verb' => 'GET', + 'remaining' => 3, + 'value' => 3 } ], + 'uri' => '*changes-since*' } + ] + + absolute_limits = { + # Max + 'maxServerMeta' => 128, + 'maxTotalInstances' => 10, + 'maxPersonality' => 5, + 'maxImageMeta' => 128, + 'maxPersonalitySize' => 10240, + 'maxSecurityGroupRules' => 20, + 'maxTotalKeypairs' => 100, + 'maxTotalVolumes' => 10, + 'maxSecurityGroups' => 10, + 'maxTotalCores' => 20, + 'maxTotalFloatingIps' => 10, + 'maxTotalVolumeGigabytes' => 1000, + 'maxTotalRAMSize' => 51200, + + # Used + 'totalVolumesUsed' => 0, + 'totalCoresUsed' => -1, + 'totalRAMUsed' => -2048, + 'totalInstancesUsed' => -1, + 'totalVolumeGigabytesUsed' => 0, + 'totalSecurityGroupsUsed' => 0, + 'totalKeyPairsUsed' => 0 + } + + + Excon::Response.new( + :status => 200, + :body => { + 'limits' => { + 'rate' => rate_limits, + 'absolute' => absolute_limits } + } + ) + end + end + + end + end +end diff --git a/tests/openstack/requests/compute/limit_tests.rb b/tests/openstack/requests/compute/limit_tests.rb new file mode 100644 index 000000000..b4f038089 --- /dev/null +++ b/tests/openstack/requests/compute/limit_tests.rb @@ -0,0 +1,64 @@ +Shindo.tests('Fog::Compute[:openstack] | limits requests', ['openstack']) do + @rate_limit_format = { + 'regex' => String, + 'uri' => String, + 'limit' => Array + } + + @rate_limit_usage_format = { + 'next-available' => String, + 'unit' => String, + 'verb' => String, + 'remaining' => Fixnum, + 'value' => Fixnum + } + + @absolute_limits_format = { + 'maxServerMeta' => Fixnum, + 'maxTotalInstances' => Fixnum, + 'maxPersonality' => Fixnum, + 'maxImageMeta' => Fixnum, + 'maxPersonalitySize' => Fixnum, + 'maxSecurityGroupRules' => Fixnum, + 'maxTotalKeypairs' => Fixnum, + 'maxTotalVolumes' => Fixnum, + 'maxSecurityGroups' => Fixnum, + 'maxTotalCores' => Fixnum, + 'maxTotalFloatingIps' => Fixnum, + 'maxTotalVolumeGigabytes' => Fixnum, + 'maxTotalRAMSize' => Fixnum, + 'totalVolumesUsed' => Fixnum, + 'totalCoresUsed' => Fixnum, + 'totalRAMUsed' => Fixnum, + 'totalInstancesUsed' => Fixnum, + 'totalVolumeGigabytesUsed' => Fixnum, + 'totalSecurityGroupsUsed' => Fixnum, + 'totalKeyPairsUsed' => Fixnum + } + + @limits_format = { + 'rate' => Array, + 'absolute' => Hash + } + + tests('success') do + tests('#get_limits') do + tests('format').formats(@limits_format) do + Fog::Compute[:openstack].get_limits.body['limits'] + end + + tests('rate limit format').formats(@rate_limit_format) do + Fog::Compute[:openstack].get_limits.body['limits']['rate'].first + end + + tests('rate limit usage format').formats(@rate_limit_usage_format) do + Fog::Compute[:openstack].get_limits.body['limits']['rate'].first['limit'].first + end + + tests('absolute limits format').formats(@absolute_limits_format) do + Fog::Compute[:openstack].get_limits.body['limits']['absolute'] + end + end + end +end +