diff --git a/lib/fog/hp/network.rb b/lib/fog/hp/network.rb index 64472b4cd..c40caba3c 100644 --- a/lib/fog/hp/network.rb +++ b/lib/fog/hp/network.rb @@ -12,15 +12,21 @@ module Fog secrets :hp_secret_key request_path 'fog/hp/requests/network' + request :associate_floating_ip + request :create_floating_ip request :create_port request :create_network request :create_subnet + request :disassociate_floating_ip + request :delete_floating_ip request :delete_port request :delete_network request :delete_subnet + request :get_floating_ip request :get_port request :get_network request :get_subnet + request :list_floating_ips request :list_ports request :list_networks request :list_subnets @@ -28,6 +34,7 @@ module Fog request :update_network request :update_subnet + module Utils end @@ -40,7 +47,8 @@ module Fog hash[key] = { :networks => {}, :subnets => {}, - :ports => {} + :ports => {}, + :floating_ips => {} } end end diff --git a/lib/fog/hp/requests/network/associate_floating_ip.rb b/lib/fog/hp/requests/network/associate_floating_ip.rb new file mode 100644 index 000000000..b79b0d518 --- /dev/null +++ b/lib/fog/hp/requests/network/associate_floating_ip.rb @@ -0,0 +1,71 @@ +module Fog + module HP + class Network + + class Real + # Associate port with floating ip + # + # ==== Parameters + # * 'floating_ip_id'<~String>: - UUId of the floating IP address to associate with + # * 'port_id'<~String>: - Port to associate with the floating IP + # * options<~Hash>: + # * 'fixed_ip_address'<~String>: - Fixed IP address to associate with the floating IP. Mandatory, if the port has multiple IP addresses + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * floatingip<~Array>: + # * 'id'<~String>: - UUId for the floating ip + # * 'tenant_id'<~String>: - TenantId that owns the floating ip + # * 'floating_network_id'<~String>: - UUId of the external network + # * 'router_id'<~String>: - Id of the router, null if not assigned + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, null if not assigned + # * 'floating_ip_address'<~String>: - Floating IP address + # * 'port_id'<~String>: - Port associated to the floating IP, null if not assigned + def associate_floating_ip(floating_ip_id, port_id, options = {}) + data = { + 'floatingip' => { + 'port_id' => port_id + } + } + l_options = [:fixed_ip_address] + l_options.select{|o| options[o]}.each do |key| + data['floatingip'][key] = options[key] + end + + request( + :body => Fog::JSON.encode(data), + :expects => 200, + :method => 'PUT', + :path => "floatingips/#{floating_ip_id}" + ) + end + end + + class Mock + def associate_floating_ip(floating_ip_id, port_id, options = {}) + response = Excon::Response.new + if list_floating_ips.body['floatingips'].detect {|_| _['id'] == floating_ip_id} + response.status = 201 + data = { + 'id' => floating_ip_id, + 'port_id' => port_id, + 'router_id' => Fog::HP::Mock.uuid.to_s, + 'tenant_id' => Fog::Mock.random_numbers(14).to_s, + 'floating_network_id' => Fog::HP::Mock.uuid.to_s, + 'fixed_ip_address' => options[:fixed_ip_address] || Fog::HP::Mock.ip_address.to_s, + 'floating_ip_address' => Fog::HP::Mock.ip_address.to_s + } + + self.data[:floating_ips][data['id']] = data + response.body = { 'floatingip' => data } + response + else + raise Fog::HP::Network::NotFound + end + end + end + + end + end +end diff --git a/lib/fog/hp/requests/network/create_floating_ip.rb b/lib/fog/hp/requests/network/create_floating_ip.rb new file mode 100644 index 000000000..5658eda08 --- /dev/null +++ b/lib/fog/hp/requests/network/create_floating_ip.rb @@ -0,0 +1,69 @@ +module Fog + module HP + class Network + + class Real + # Create a new floating ip + # + # ==== Parameters + # * 'floating_network_id'<~String>: - UUId of the external network + # * options<~Hash>: + # * 'port_id'<~String>: - Port to associate with the floating IP + # * 'tenant_id'<~String>: - TenantId that owns the floating IP + # * 'fixed_ip_address'<~String>: - Fixed IP address to associate with the floating IP. Mandatory, if the port has multiple IP addresses + # * 'floating_ip_address'<~String>: - Specific floating IP address to allocate, otherwise automatically allocated + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * floatingip<~Array>: + # * 'id'<~String>: - UUId for the floating ip + # * 'tenant_id'<~String>: - TenantId that owns the floating ip + # * 'floating_network_id'<~String>: - UUId of the external network + # * 'router_id'<~String>: - Id of the router, null if not assigned + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, null if not assigned + # * 'floating_ip_address'<~String>: - Floating IP address + # * 'port_id'<~String>: - Port associated to the floating IP, null if not assigned + def create_floating_ip(floating_network_id, options = {}) + data = { + 'floatingip' => { + 'floating_network_id' => floating_network_id + } + } + + l_options = [:port_id, :fixed_ip_address, :floating_ip_address, :tenant_id] + l_options.select{|o| options[o]}.each do |key| + data['floatingip'][key] = options[key] + end + + request( + :body => Fog::JSON.encode(data), + :expects => 201, + :method => 'POST', + :path => 'floatingips' + ) + end + end + + class Mock + def create_floating_ip(floating_network_id, options = {}) + response = Excon::Response.new + response.status = 201 + data = { + 'id' => Fog::HP::Mock.uuid.to_s, + 'floating_network_id' => floating_network_id, + 'port_id' => options[:port_id] || nil, + 'tenant_id' => options[:tenant_id] || Fog::Mock.random_numbers(14).to_s, + 'fixed_ip_address' => options[:fixed_ip_address] || nil, + 'floating_ip_address' => options[:floating_ip_address] || Fog::HP::Mock.ip_address.to_s, + 'router_id' => Fog::HP::Mock.uuid.to_s + } + self.data[:floating_ips][data['id']] = data + response.body = { 'floatingip' => data } + response + end + end + + end + end +end diff --git a/lib/fog/hp/requests/network/delete_floating_ip.rb b/lib/fog/hp/requests/network/delete_floating_ip.rb new file mode 100644 index 000000000..353075336 --- /dev/null +++ b/lib/fog/hp/requests/network/delete_floating_ip.rb @@ -0,0 +1,34 @@ +module Fog + module HP + class Network + + class Real + # Delete an existing floating ip + # + # ==== Parameters + # * 'floating_ip_id'<~String>: - UUId of the floating IP address to delete + def delete_floating_ip(floating_ip_id) + request( + :expects => 204, + :method => 'DELETE', + :path => "floatingips/#{floating_ip_id}" + ) + end + end + + class Mock + def delete_floating_ip(floating_ip_id) + response = Excon::Response.new + if list_floating_ips.body['floatingips'].detect {|_| _['id'] == floating_ip_id} + self.data[:floating_ips].delete(floating_ip_id) + response.status = 204 + response + else + raise Fog::HP::Network::NotFound + end + end + end + + end + end +end diff --git a/lib/fog/hp/requests/network/disassociate_floating_ip.rb b/lib/fog/hp/requests/network/disassociate_floating_ip.rb new file mode 100644 index 000000000..c0940b780 --- /dev/null +++ b/lib/fog/hp/requests/network/disassociate_floating_ip.rb @@ -0,0 +1,71 @@ +module Fog + module HP + class Network + + class Real + # Associate port with floating ip + # + # ==== Parameters + # * 'floating_ip_id'<~String>: - UUId of the floating IP address to associate with + # * options<~Hash>: + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, nil to disassociate + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * floatingip<~Array>: + # * 'id'<~String>: - UUId for the floating ip + # * 'tenant_id'<~String>: - TenantId that owns the floating ip + # * 'floating_network_id'<~String>: - UUId of the external network + # * 'router_id'<~String>: - Id of the router, null if not assigned + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, null if not assigned + # * 'floating_ip_address'<~String>: - Floating IP address + # * 'port_id'<~String>: - Port associated to the floating IP, null if not assigned + def disassociate_floating_ip(floating_ip_id, options = {}) + data = { + 'floatingip' => { + 'port_id' => nil # nil, to disassociate + } + } + + l_options = [:fixed_ip_address] + l_options.select{|o| options[o]}.each do |key| + data['floatingip'][key] = nil # nil, to disassociate + end + + request( + :body => Fog::JSON.encode(data), + :expects => 200, + :method => 'PUT', + :path => "floatingips/#{floating_ip_id}" + ) + end + end + + class Mock + def disassociate_floating_ip(floating_ip_id, options = {}) + response = Excon::Response.new + if list_floating_ips.body['floatingips'].detect {|_| _['id'] == floating_ip_id} + response.status = 200 + data = { + 'id' => floating_ip_id, + 'port_id' => nil, + 'router_id' => Fog::HP::Mock.uuid.to_s, + 'tenant_id' => Fog::Mock.random_numbers(14).to_s, + 'floating_network_id' => Fog::HP::Mock.uuid.to_s, + 'fixed_ip_address' => nil, + 'floating_ip_address' => Fog::HP::Mock.ip_address.to_s + } + + self.data[:floating_ips][data['id']] = data + response.body = { 'floatingip' => data } + response + else + raise Fog::HP::Network::NotFound + end + end + end + + end + end +end diff --git a/lib/fog/hp/requests/network/get_floating_ip.rb b/lib/fog/hp/requests/network/get_floating_ip.rb new file mode 100644 index 000000000..0f2991fea --- /dev/null +++ b/lib/fog/hp/requests/network/get_floating_ip.rb @@ -0,0 +1,47 @@ +module Fog + module HP + class Network + + class Real + # Get an existing floating ip by id + # + # ==== Parameters + # * 'id'<~String>: - UUId for the floating ip + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * floatingip<~Array>: + # * 'id'<~String>: - UUId for the floating ip + # * 'tenant_id'<~String>: - TenantId that owns the floating ip + # * 'floating_network_id'<~String>: - UUId of the external network + # * 'router_id'<~String>: - Id of the router, null if not assigned + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, null if not assigned + # * 'floating_ip_address'<~String>: - Floating IP address + # * 'port_id'<~String>: - Port associated to the floating IP, null if not assigned + def get_floating_ip(floating_ip_id) + request( + :expects => 200, + :method => 'GET', + :path => "floatingips/#{floating_ip_id}" + ) + end + end + + class Mock + def get_floating_ip(floating_ip_id) + response = Excon::Response.new + if floating_ip = list_floating_ips.body['floatingips'].detect {|_| _['id'] == floating_ip_id} + response.status = 200 + response.body = { 'floatingip' => floating_ip } + response + else + raise Fog::HP::Network::NotFound + end + end + end + + + end + end +end diff --git a/lib/fog/hp/requests/network/list_floating_ips.rb b/lib/fog/hp/requests/network/list_floating_ips.rb new file mode 100644 index 000000000..2ff9b89a2 --- /dev/null +++ b/lib/fog/hp/requests/network/list_floating_ips.rb @@ -0,0 +1,46 @@ +module Fog + module HP + class Network + + class Real + # List existing floating ips + # + # ==== Parameters + # * options<~Hash>: + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * floatingips<~Array>: + # * 'id'<~String>: - UUId for the floating ip + # * 'tenant_id'<~String>: - TenantId that owns the floating ip + # * 'floating_network_id'<~String>: - UUId of the external network + # * 'router_id'<~String>: - Id of the router, null if not assigned + # * 'fixed_ip_address'<~String>: - Fixed IP address associated to the floating IP, null if not assigned + # * 'floating_ip_address'<~String>: - Floating IP address + # * 'port_id'<~String>: - Port associated to the floating IP, null if not assigned + + def list_floating_ips(options = {}) + request( + :expects => 200, + :method => 'GET', + :path => 'floatingips', + :query => options + ) + end + end + + class Mock + def list_floating_ips(options = {}) + response = Excon::Response.new + + floatingips = self.data[:floating_ips].values + response.status = 200 + response.body = { 'floatingips' => floatingips } + response + end + end + + end + end +end diff --git a/tests/hp/requests/network/floating_ip_tests.rb b/tests/hp/requests/network/floating_ip_tests.rb new file mode 100644 index 000000000..37416807c --- /dev/null +++ b/tests/hp/requests/network/floating_ip_tests.rb @@ -0,0 +1,70 @@ +Shindo.tests("HP::Network | floating ip requests", ['hp', 'floatingip']) do + + @floating_ip_format = { + 'id' => String, + 'tenant_id' => String, + 'floating_network_id' => String, + 'router_id' => Fog::Nullable::String, + 'fixed_ip_address' => Fog::Nullable::String, + 'floating_ip_address' => String, + 'port_id' => Fog::Nullable::String + } + + @ext_network_id = "ca07e0fd-16f3-4536-a67d-048a279083e1" + s_data = HP[:network].create_port(@network_id, {:name => 'fog_port'}).body['port'] + @port_id = s_data['id'] + + tests('success') do + + @floating_ip_id = nil + + tests("#create_floating_ip(#{@ext_network_id})").formats(@floating_ip_format) do + data = HP[:network].create_floating_ip(@ext_network_id).body['floatingip'] + @floating_ip_id = data['id'] + data + end + + tests('#list_floating_ips').formats({'floatingips' => [@floating_ip_format]}) do + HP[:network].list_floating_ips.body + end + + tests("#get_floating_ip(#{@floating_ip_id})").formats({'floatingip' => @floating_ip_format}) do + HP[:network].get_floating_ip(@floating_ip_id).body + end + + tests("#associate_floating_ip(#{@floating_ip_id}, #{@port_id})").formats({'floatingip' => @floating_ip_format}) do + HP[:network].associate_floating_ip(@floating_ip_id, @port_id).body + end + + tests("#disassociate_floating_ip(#{@floating_ip_id}, nil)").formats({'floatingip' => @floating_ip_format}) do + HP[:network].disassociate_floating_ip(@floating_ip_id, nil).body + end + + tests("#delete_floating_ip(#{@floating_ip_id})").succeeds do + HP[:network].delete_floating_ip(@floating_ip_id) + end + + end + + tests('failure') do + tests('#get_floating_ip("0")').raises(Fog::HP::Network::NotFound) do + HP[:network].get_floating_ip(0) + end + + tests("#associate_floating_ip('0', #{@port_id})").raises(Fog::HP::Network::NotFound) do + HP[:network].associate_floating_ip('0', @port_id) + end + + tests('#disassociate_floating_ip("0")').raises(Fog::HP::Network::NotFound) do + HP[:network].disassociate_floating_ip("0") + end + + tests('#delete_floating_ip("0")').raises(Fog::HP::Network::NotFound) do + HP[:network].delete_floating_ip("0") + end + end + + # cleanup + HP[:network].delete_port(@port_id) + +end \ No newline at end of file