From 0b3630f1245082d128dd92397f75a4230909c49b Mon Sep 17 00:00:00 2001 From: Lincoln Stoll Date: Wed, 11 May 2011 15:59:08 -0700 Subject: [PATCH] [ninefold|compute] NAT functionality. --- lib/fog/compute/ninefold.rb | 6 ++ .../ninefold/create_ip_forwarding_rule.rb | 23 +++++ .../ninefold/delete_ip_forwarding_rule.rb | 23 +++++ .../requests/ninefold/disable_static_nat.rb | 23 +++++ .../requests/ninefold/enable_static_nat.rb | 23 +++++ .../ninefold/list_ip_forwarding_rules.rb | 23 +++++ tests/compute/requests/ninefold/helper.rb | 23 +++++ tests/compute/requests/ninefold/nat_tests.rb | 92 +++++++++++++++++++ 8 files changed, 236 insertions(+) create mode 100644 lib/fog/compute/requests/ninefold/create_ip_forwarding_rule.rb create mode 100644 lib/fog/compute/requests/ninefold/delete_ip_forwarding_rule.rb create mode 100644 lib/fog/compute/requests/ninefold/disable_static_nat.rb create mode 100644 lib/fog/compute/requests/ninefold/enable_static_nat.rb create mode 100644 lib/fog/compute/requests/ninefold/list_ip_forwarding_rules.rb create mode 100644 tests/compute/requests/ninefold/nat_tests.rb diff --git a/lib/fog/compute/ninefold.rb b/lib/fog/compute/ninefold.rb index 229f0207c..1262cbfee 100644 --- a/lib/fog/compute/ninefold.rb +++ b/lib/fog/compute/ninefold.rb @@ -56,6 +56,12 @@ module Fog request :associate_ip_address request :list_public_ip_addresses request :disassociate_ip_address + # NAT + request :enable_static_nat + request :disable_static_nat + request :create_ip_forwarding_rule + request :delete_ip_forwarding_rule + request :list_ip_forwarding_rules class Mock diff --git a/lib/fog/compute/requests/ninefold/create_ip_forwarding_rule.rb b/lib/fog/compute/requests/ninefold/create_ip_forwarding_rule.rb new file mode 100644 index 000000000..5e148cfbd --- /dev/null +++ b/lib/fog/compute/requests/ninefold/create_ip_forwarding_rule.rb @@ -0,0 +1,23 @@ +module Fog + module Ninefold + class Compute + class Real + + def create_ip_forwarding_rule(options = {}) + request('createIpForwardingRule', options, :expects => [200], + :response_prefix => 'createipforwardingruleresponse', :response_type => Hash) + end + + end + + class Mock + + def create_ip_forwarding_rule(*args) + Fog::Mock.not_implemented + end + + end + + end + end +end diff --git a/lib/fog/compute/requests/ninefold/delete_ip_forwarding_rule.rb b/lib/fog/compute/requests/ninefold/delete_ip_forwarding_rule.rb new file mode 100644 index 000000000..bc63732ae --- /dev/null +++ b/lib/fog/compute/requests/ninefold/delete_ip_forwarding_rule.rb @@ -0,0 +1,23 @@ +module Fog + module Ninefold + class Compute + class Real + + def delete_ip_forwarding_rule(options = {}) + request('deleteIpForwardingRule', options, :expects => [200], + :response_prefix => 'deleteipforwardingruleresponse', :response_type => Hash) + end + + end + + class Mock + + def delete_ip_forwarding_rule(*args) + Fog::Mock.not_implemented + end + + end + + end + end +end diff --git a/lib/fog/compute/requests/ninefold/disable_static_nat.rb b/lib/fog/compute/requests/ninefold/disable_static_nat.rb new file mode 100644 index 000000000..f1651b638 --- /dev/null +++ b/lib/fog/compute/requests/ninefold/disable_static_nat.rb @@ -0,0 +1,23 @@ +module Fog + module Ninefold + class Compute + class Real + + def disable_static_nat(options = {}) + request('disableStaticNat', options, :expects => [200], + :response_prefix => 'disablestaticnatresponse', :response_type => Hash) + end + + end + + class Mock + + def disable_static_nat(*args) + Fog::Mock.not_implemented + end + + end + + end + end +end diff --git a/lib/fog/compute/requests/ninefold/enable_static_nat.rb b/lib/fog/compute/requests/ninefold/enable_static_nat.rb new file mode 100644 index 000000000..a09fb0fcc --- /dev/null +++ b/lib/fog/compute/requests/ninefold/enable_static_nat.rb @@ -0,0 +1,23 @@ +module Fog + module Ninefold + class Compute + class Real + + def enable_static_nat(options = {}) + request('enableStaticNat', options, :expects => [200], + :response_prefix => 'enablestaticnatresponse', :response_type => Hash) + end + + end + + class Mock + + def enable_static_nat(*args) + Fog::Mock.not_implemented + end + + end + + end + end +end diff --git a/lib/fog/compute/requests/ninefold/list_ip_forwarding_rules.rb b/lib/fog/compute/requests/ninefold/list_ip_forwarding_rules.rb new file mode 100644 index 000000000..aa3ae2d0a --- /dev/null +++ b/lib/fog/compute/requests/ninefold/list_ip_forwarding_rules.rb @@ -0,0 +1,23 @@ +module Fog + module Ninefold + class Compute + class Real + + def list_ip_forwarding_rules(options = {}) + request('listIpForwardingRules', options, :expects => [200], + :response_prefix => 'listipforwardingrulesresponse/ipforwardingrule', :response_type => Hash) + end + + end + + class Mock + + def list_ip_forwarding_rules(*args) + Fog::Mock.not_implemented + end + + end + + end + end +end diff --git a/tests/compute/requests/ninefold/helper.rb b/tests/compute/requests/ninefold/helper.rb index 4f4828060..a686bab24 100644 --- a/tests/compute/requests/ninefold/helper.rb +++ b/tests/compute/requests/ninefold/helper.rb @@ -318,6 +318,29 @@ class Ninefold ADDRESSES = [ADDRESS] DISASSOC_ADDRESS = {"jobid"=>Integer} end + module Nat + ENABLE_NAT_RESPONSE = { + 'success' => String + } + DISABLE_NAT_RESPONSE = { + 'success' => Fog::Boolean + } + DELETE_RULE_RESPONSE = { + 'success' => Fog::Boolean + } + FORWARDING_RULE = { + "id"=>Integer, + "protocol"=>String, + "virtualmachineid"=>Integer, + "virtualmachinename"=>String, + "ipaddressid"=>Integer, + "ipaddress"=>String, + "startport"=>Integer, + "endport"=>Integer, + "state"=>String + } + FORWARDING_RULES = [FORWARDING_RULE] + end end end end diff --git a/tests/compute/requests/ninefold/nat_tests.rb b/tests/compute/requests/ninefold/nat_tests.rb new file mode 100644 index 000000000..98eb09f8d --- /dev/null +++ b/tests/compute/requests/ninefold/nat_tests.rb @@ -0,0 +1,92 @@ +## Note: +# If needed, these tests will create a new VM and public IP address. Because this is expensive, you +# can optionally specify VM_ID and IP_ID as environment variables, and we will use those. Note: +# The IP must not already have static nat enabled or any port mappings. + +Shindo.tests('Ninefold::Compute | server requests', ['ninefold']) do + + if ENV['VM_ID'] && ENV['IP_ID'] + @ipid, @vmid = ENV['IP_ID'], ENV['VM_ID'] + else + begin + # Create a VM to work with + networks = Ninefold[:compute].list_networks + vm_job = Ninefold[:compute].deploy_virtual_machine(:serviceofferingid => Ninefold::Compute::TestSupport::SERVICE_OFFERING, + :templateid => Ninefold::Compute::TestSupport::TEMPLATE_ID, + :zoneid => Ninefold::Compute::TestSupport::ZONE_ID, + :networkids => networks[0]['id']) + @vm = Ninefold::Compute::TestSupport.wait_for_job(vm_job)['jobresult']['virtualmachine'] + @vmid = @vm['id'] + + # Allocate a public IP to work with + ip_job = Ninefold[:compute].associate_ip_address(:zoneid => Ninefold::Compute::TestSupport::ZONE_ID) + @ip = Ninefold::Compute::TestSupport.wait_for_job(ip_job)['jobresult']['ipaddress'] + @ipid = @ip['id'] + rescue => e + puts "*** CREATING VM OR IP FAILED - PLEASE TEST AND CORRECT THIS FIRST" + raise e + end + end + + tests('success') do + + tests("#enable_static_nat()").formats(Ninefold::Compute::Formats::Nat::ENABLE_NAT_RESPONSE) do + pending if Fog.mocking? + Ninefold[:compute].enable_static_nat(:ipaddressid => @ipid, :virtualmachineid => @vmid) + end + + tests("#create_ip_forwarding_rule()").formats(Ninefold::Compute::Formats::Nat::FORWARDING_RULE) do + pending if Fog.mocking? + job = Ninefold[:compute].create_ip_forwarding_rule(:ipaddressid => @ipid, + :protocol => 'TCP', + :startport => 22) + result = Ninefold::Compute::TestSupport.wait_for_job(job)['jobresult']['ipforwardingrule'] + @fwd_rule_id = result['id'] + result + end + + tests("#list_ip_forwarding_rules()").formats(Ninefold::Compute::Formats::Nat::FORWARDING_RULES) do + pending if Fog.mocking? + Ninefold[:compute].list_ip_forwarding_rules + end + + tests("#delete_ip_forwarding_rule()").formats(Ninefold::Compute::Formats::Nat::DELETE_RULE_RESPONSE) do + pending if Fog.mocking? + job = Ninefold[:compute].delete_ip_forwarding_rule(:id => @fwd_rule_id) + Ninefold::Compute::TestSupport.wait_for_job(job)['jobresult'] + end + + + tests("#disable_static_nat()").formats(Ninefold::Compute::Formats::Nat::DISABLE_NAT_RESPONSE) do + pending if Fog.mocking? + job = Ninefold[:compute].disable_static_nat(:ipaddressid => @ipid) + Ninefold::Compute::TestSupport.wait_for_job(job)['jobresult'] + end + + end + + tests('failure') do + + tests("#associate_ip_address()").raises(Excon::Errors::HTTPStatusError) do + pending if Fog.mocking? + Ninefold[:compute].associate_ip_address + end + + end + + unless ENV['VM_ID'] && ENV['IP_ID'] + begin + # Kill test VM + vm_job = Ninefold[:compute].destroy_virtual_machine(:id => @vmid) + Ninefold::Compute::TestSupport.wait_for_job(vm_job) + + # Disassociate public IP + ip_job = Ninefold[:compute].disassociate_ip_address(:id => @ipid) + Ninefold::Compute::TestSupport.wait_for_job(ip_job) + rescue => e + puts "*** DESTROYING VM OR IP FAILED - PLEASE TEST AND CORRECT THIS FIRST" + raise e + end + end + +end