diff --git a/lib/fog/hp/network.rb b/lib/fog/hp/network.rb index f9bae96a1..ea6d4c7a9 100644 --- a/lib/fog/hp/network.rb +++ b/lib/fog/hp/network.rb @@ -30,22 +30,26 @@ module Fog request :create_network request :create_port request :create_router + request :create_security_group request :create_subnet request :disassociate_floating_ip request :delete_floating_ip request :delete_network request :delete_port request :delete_router + request :delete_security_group request :delete_subnet request :get_floating_ip request :get_network request :get_port request :get_router + request :get_security_group request :get_subnet request :list_floating_ips request :list_networks request :list_ports request :list_routers + request :list_security_groups request :list_subnets request :remove_router_interface request :update_network @@ -79,6 +83,7 @@ module Fog }, :ports => {}, :routers => {}, + :security_groups => {}, :subnets => {} } end diff --git a/lib/fog/hp/requests/network/create_security_group.rb b/lib/fog/hp/requests/network/create_security_group.rb new file mode 100644 index 000000000..bf76d4c1b --- /dev/null +++ b/lib/fog/hp/requests/network/create_security_group.rb @@ -0,0 +1,100 @@ +module Fog + module HP + class Network + class Real + + # Create a new security group + # + # ==== Parameters + # * options<~Hash>: + # * 'name'<~String> - Name of the security group + # * 'description'<~String> - Description of the security group + # * 'tenant_id'<~String> - TenantId different than the current user, that should own the security group. Only allowed if user has 'admin' role. + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'security_groups'<~Array>: + # * 'id'<~String> - UUId of the security group + # * 'name'<~String> - Name of the security group + # * 'description'<~String> - Description of the security group + # * 'tenant_id'<~String> - Tenant id that owns the security group + # * 'security_group_rules'<~Array>: - Array of security group rules + # * 'id'<~String> - UUId of the security group rule + # * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress'] + # * 'port_range_min'<~String> - Start port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'port_range_max'<~String> - End port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'protocol'<~String> - IP protocol for rule, must be in ['tcp', 'udp', 'icmp'] + # * 'ethertype'<~String> - Type of ethernet support, must be in ['IPv4', 'IPv6'] + # * 'security_group_id'<~String> - UUId of the parent security group + # * 'remote_group_id'<~String> - UUId of the source security group + # * 'remote_ip_prefix'<~String> - IP cidr range address i.e. '0.0.0.0/0' + # * 'tenant_id'<~String> - Tenant id that owns the security group rule + def create_security_group(options = {}) + data = { 'security_group' => {} } + + l_options = [:name, :description, :tenant_id] + l_options.select{|o| options[o]}.each do |key| + data['security_group'][key] = options[key] + end + + request( + :body => Fog::JSON.encode(data), + :expects => 201, + :method => 'POST', + :path => 'security-groups' + ) + end + + end + + class Mock + + def create_security_group(options = {}) + # Spaces are NOT removed from name and description, as in case of compute sec groups + tenant_id = Fog::Mock.random_numbers(14).to_s + sec_group_id = Fog::HP::Mock.uuid.to_s + + response = Excon::Response.new + response.status = 201 + # by default every security group will come setup with an egress rule to "allow all out" + data = { + 'security_group_rules' => [ + { "remote_group_id" => nil, + "direction" => "egress", + "remote_ip_prefix" => nil, + "protocol" => nil, + "ethertype" => "IPv4", + "tenant_id" => tenant_id, + "port_range_max" => nil, + "port_range_min" => nil, + "id" => Fog::HP::Mock.uuid.to_s, + "security_group_id" => sec_group_id + }, + { "remote_group_id" => nil, + "direction" => "egress", + "remote_ip_prefix" => nil, + "protocol" => nil, + "ethertype" => "IPv6", + "tenant_id" => tenant_id, + "port_range_max" => nil, + "port_range_min" => nil, + "id" => Fog::HP::Mock.uuid.to_s, + "security_group_id" => sec_group_id + } + ], + 'id' => sec_group_id, + 'tenant_id' => tenant_id, + 'name' => options[:name] || "", + 'description' => options[:description] || "" + } + self.data[:security_groups][data['id']] = data + response.body = { 'security_group' => data } + response + end + + end + + end + end +end diff --git a/lib/fog/hp/requests/network/delete_security_group.rb b/lib/fog/hp/requests/network/delete_security_group.rb new file mode 100644 index 000000000..3eadcb11c --- /dev/null +++ b/lib/fog/hp/requests/network/delete_security_group.rb @@ -0,0 +1,36 @@ +module Fog + module HP + class Network + class Real + + # Delete a security group + # + # ==== Parameters + # * 'security_group_id'<~String> - UUId of the security group to delete + def delete_security_group(security_group_id) + request( + :expects => 204, + :method => 'DELETE', + :path => "security-groups/#{security_group_id}" + ) + end + + end + + class Mock + + def delete_security_group(security_group_id) + response = Excon::Response.new + if self.data[:security_groups][security_group_id] + self.data[:security_groups].delete(security_group_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/get_security_group.rb b/lib/fog/hp/requests/network/get_security_group.rb new file mode 100644 index 000000000..b9a058f85 --- /dev/null +++ b/lib/fog/hp/requests/network/get_security_group.rb @@ -0,0 +1,56 @@ +module Fog + module HP + class Network + class Real + + # Get details about a security group + # + # ==== Parameters + # * 'security_group_id'<~String> - UUId of the security group + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'security_group'<~Array>: + # * 'id'<~String> - UUId of the security group + # * 'name'<~String> - Name of the security group + # * 'description'<~String> - Description of the security group + # * 'tenant_id'<~String> - Tenant id that owns the security group + # * 'security_group_rules'<~Array>: - Array of security group rules + # * 'id'<~String> - UUId of the security group rule + # * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress'] + # * 'port_range_min'<~String> - Start port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'port_range_max'<~String> - End port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'protocol'<~String> - IP protocol for rule, must be in ['tcp', 'udp', 'icmp'] + # * 'ethertype'<~String> - Type of ethernet support, must be in ['IPv4', 'IPv6'] + # * 'security_group_id'<~String> - UUId of the parent security group + # * 'remote_group_id'<~String> - UUId of the source security group + # * 'remote_ip_prefix'<~String> - IP cidr range address i.e. '0.0.0.0/0' + # * 'tenant_id'<~String> - Tenant id that owns the security group rule + def get_security_group(security_group_id) + request( + :expects => 200, + :method => 'GET', + :path => "security-groups/#{security_group_id}" + ) + end + + end + + class Mock + + def get_security_group(security_group_id) + response = Excon::Response.new + if sec_group = self.data[:security_groups][security_group_id] + response.status = 200 + response.body = { 'security_group' => sec_group } + response + else + raise Fog::HP::Network::NotFound + end + end + + end + end + end +end diff --git a/lib/fog/hp/requests/network/list_security_groups.rb b/lib/fog/hp/requests/network/list_security_groups.rb new file mode 100644 index 000000000..753a3c046 --- /dev/null +++ b/lib/fog/hp/requests/network/list_security_groups.rb @@ -0,0 +1,57 @@ +module Fog + module HP + class Network + class Real + + # List all security groups + # + # ==== Parameters + # * options<~Hash>: + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'security_groups'<~Array>: + # * 'id'<~String> - UUId of the security group + # * 'name'<~String> - Name of the security group + # * 'description'<~String> - Description of the security group + # * 'tenant_id'<~String> - Tenant id that owns the security group + # * 'security_group_rules'<~Array>: - Array of security group rules + # * 'id'<~String> - UUId of the security group rule + # * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress'] + # * 'port_range_min'<~String> - Start port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'port_range_max'<~String> - End port for rule i.e. 22 (or -1 for ICMP wildcard) + # * 'protocol'<~String> - IP protocol for rule, must be in ['tcp', 'udp', 'icmp'] + # * 'ethertype'<~String> - Type of ethernet support, must be in ['IPv4', 'IPv6'] + # * 'security_group_id'<~String> - UUId of the parent security group + # * 'remote_group_id'<~String> - UUId of the source security group + # * 'remote_ip_prefix'<~String> - IP cidr range address i.e. '0.0.0.0/0' + # * 'tenant_id'<~String> - Tenant id that owns the security group rule + def list_security_groups(options = {}) + request( + :expects => 200, + :method => 'GET', + :path => 'security-groups', + :query => options + ) + end + + end + + class Mock + + def list_security_groups(options = {}) + response = Excon::Response.new + + sec_groups = [] + sec_groups = self.data[:security_groups].values unless self.data[:security_groups].nil? + + response.status = 200 + response.body = { 'security_groups' => sec_groups } + response + end + + end + end + end +end