mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[hp|network] Add request methods for networking security group rules, along with tests.
This commit is contained in:
parent
5eba540e2c
commit
ccf7e8e91c
6 changed files with 285 additions and 0 deletions
|
@ -31,6 +31,7 @@ module Fog
|
|||
request :create_port
|
||||
request :create_router
|
||||
request :create_security_group
|
||||
request :create_security_group_rule
|
||||
request :create_subnet
|
||||
request :disassociate_floating_ip
|
||||
request :delete_floating_ip
|
||||
|
@ -38,17 +39,20 @@ module Fog
|
|||
request :delete_port
|
||||
request :delete_router
|
||||
request :delete_security_group
|
||||
request :delete_security_group_rule
|
||||
request :delete_subnet
|
||||
request :get_floating_ip
|
||||
request :get_network
|
||||
request :get_port
|
||||
request :get_router
|
||||
request :get_security_group
|
||||
request :get_security_group_rule
|
||||
request :get_subnet
|
||||
request :list_floating_ips
|
||||
request :list_networks
|
||||
request :list_ports
|
||||
request :list_routers
|
||||
request :list_security_group_rules
|
||||
request :list_security_groups
|
||||
request :list_subnets
|
||||
request :remove_router_interface
|
||||
|
@ -84,6 +88,7 @@ module Fog
|
|||
:ports => {},
|
||||
:routers => {},
|
||||
:security_groups => {},
|
||||
:security_group_rules => {},
|
||||
:subnets => {}
|
||||
}
|
||||
end
|
||||
|
|
83
lib/fog/hp/requests/network/create_security_group_rule.rb
Normal file
83
lib/fog/hp/requests/network/create_security_group_rule.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
module Fog
|
||||
module HP
|
||||
class Network
|
||||
class Real
|
||||
|
||||
# Create a new security group rule
|
||||
#
|
||||
# ==== Parameters
|
||||
# * 'security_group_id'<~String> - UUId of the parent security group
|
||||
# * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress']
|
||||
# * options<~Hash>:
|
||||
# * 'port_range_min'<~Integer> - Start port for rule i.e. 22 (or -1 for ICMP wildcard)
|
||||
# * 'port_range_max'<~Integer> - 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']
|
||||
# * 'remote_group_id'<~String> - UUId of the remote security group
|
||||
# * 'remote_ip_prefix'<~String> - IP cidr range address i.e. '0.0.0.0/0'
|
||||
# * '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_group_rule'<~Hash>:
|
||||
# * '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_rule(security_group_id, direction, options = {})
|
||||
data = { 'security_group_rule' => {
|
||||
'security_group_id' => security_group_id,
|
||||
'direction' => direction
|
||||
}
|
||||
}
|
||||
|
||||
l_options = [:port_range_min, :port_range_max, :protocol, :ethertype,
|
||||
:remote_group_id, :remote_ip_prefix, :tenant_id]
|
||||
l_options.select{|o| options[o]}.each do |key|
|
||||
data['security_group_rule'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 201,
|
||||
:method => 'POST',
|
||||
:path => 'security-group-rules'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def create_security_group_rule(security_group_id, direction, options = {})
|
||||
response = Excon::Response.new
|
||||
data = {
|
||||
"id" => Fog::HP::Mock.uuid.to_s,
|
||||
"remote_group_id" => options[:remote_group_id],
|
||||
"direction" => direction,
|
||||
"remote_ip_prefix" => options[:remote_ip_prefix],
|
||||
"protocol" => options[:protocol],
|
||||
"ethertype" => options[:ethertype] || "IPv4",
|
||||
"tenant_id" => options[:tenant_id] || Fog::Mock.random_numbers(14).to_s,
|
||||
"port_range_max" => options[:port_range_max],
|
||||
"port_range_min" => options[:port_range_min],
|
||||
"security_group_id" => security_group_id
|
||||
}
|
||||
self.data[:security_group_rules][data['id']] = data
|
||||
response.status = 201
|
||||
response.body = { 'security_group_rule' => data }
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
36
lib/fog/hp/requests/network/delete_security_group_rule.rb
Normal file
36
lib/fog/hp/requests/network/delete_security_group_rule.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Fog
|
||||
module HP
|
||||
class Network
|
||||
class Real
|
||||
|
||||
# Delete a security group rule
|
||||
#
|
||||
# ==== Parameters
|
||||
# * 'security_group_rule_id'<~String> - UUId of the security group rule to delete
|
||||
def delete_security_group_rule(security_group_rule_id)
|
||||
request(
|
||||
:expects => 204,
|
||||
:method => 'DELETE',
|
||||
:path => "security-group-rules/#{security_group_rule_id}"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def delete_security_group_rule(security_group_rule_id)
|
||||
response = Excon::Response.new
|
||||
if self.data[:security_group_rules][security_group_rule_id]
|
||||
self.data[:security_group_rules].delete(security_group_rule_id)
|
||||
response.status = 204
|
||||
response
|
||||
else
|
||||
raise Fog::HP::Network::NotFound
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
51
lib/fog/hp/requests/network/get_security_group_rule.rb
Normal file
51
lib/fog/hp/requests/network/get_security_group_rule.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
module Fog
|
||||
module HP
|
||||
class Network
|
||||
class Real
|
||||
|
||||
# Get details about a security group rule
|
||||
#
|
||||
# ==== Parameters
|
||||
# * 'security_group_rule_id'<~String> - UUId of the security group rule
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'security_group_rule'<~Hash>:
|
||||
# * 'id'<~String> - UUId of the security group rule
|
||||
# * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress']
|
||||
# * 'port_range_min'<~Integer> - Start port for rule i.e. 22 (or -1 for ICMP wildcard)
|
||||
# * 'port_range_max'<~Integer> - 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 remote 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_rule(security_group_rule_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "security-group-rules/#{security_group_rule_id}"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def get_security_group_rule(security_group_rule_id)
|
||||
response = Excon::Response.new
|
||||
if sec_group_rule = self.data[:security_group_rules][security_group_rule_id]
|
||||
response.status = 200
|
||||
response.body = { 'security_group_rule' => sec_group_rule }
|
||||
response
|
||||
else
|
||||
raise Fog::HP::Network::NotFound
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
52
lib/fog/hp/requests/network/list_security_group_rules.rb
Normal file
52
lib/fog/hp/requests/network/list_security_group_rules.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
module Fog
|
||||
module HP
|
||||
class Network
|
||||
class Real
|
||||
|
||||
# List all security group rules
|
||||
#
|
||||
# ==== Parameters
|
||||
# * options<~Hash>:
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'security_group_rules'<~Array>:
|
||||
# * 'id'<~String> - UUId of the security group rule
|
||||
# * 'direction'<~String> - Direction of traffic, must be in ['ingress', 'egress']
|
||||
# * 'port_range_min'<~Integer> - Start port for rule i.e. 22 (or -1 for ICMP wildcard)
|
||||
# * 'port_range_max'<~Integer> - 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 remote 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_group_rules(options = {})
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => 'security-group-rules',
|
||||
:query => options
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def list_security_group_rules(options = {})
|
||||
response = Excon::Response.new
|
||||
|
||||
sec_group_rules = []
|
||||
sec_group_rules = self.data[:security_group_rules].values unless self.data[:security_group_rules].nil?
|
||||
|
||||
response.status = 200
|
||||
response.body = { 'security_group_rules' => sec_group_rules }
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
58
tests/hp/requests/network/security_group_rule_tests.rb
Normal file
58
tests/hp/requests/network/security_group_rule_tests.rb
Normal file
|
@ -0,0 +1,58 @@
|
|||
Shindo.tests('HP::Network | networking security group rule requests', ['hp', 'networking', 'securitygroup']) do
|
||||
|
||||
@security_group_rule_format = {
|
||||
'id' => String,
|
||||
'remote_group_id' => Fog::Nullable::String,
|
||||
'direction' => String,
|
||||
'remote_ip_prefix' => Fog::Nullable::String,
|
||||
'protocol' => Fog::Nullable::String,
|
||||
'ethertype' => String,
|
||||
'port_range_max' => Fog::Nullable::Integer,
|
||||
'port_range_min' => Fog::Nullable::Integer,
|
||||
'security_group_id' => String,
|
||||
'tenant_id' => String
|
||||
}
|
||||
|
||||
tests('success') do
|
||||
|
||||
attributes = {:name => 'my_security_group', :description => 'tests group'}
|
||||
data = HP[:network].create_security_group(attributes).body['security_group']
|
||||
@sec_group_id = data['id']
|
||||
|
||||
@sec_group_rule_id = nil
|
||||
|
||||
tests("#create_security_group_rule(#{@sec_group_id}, 'ingress', attributes)").formats(@security_group_rule_format) do
|
||||
attributes = {:remote_ip_prefix => '0.0.0.0/0', :protocol => 'tcp', :port_range_min => 22, :port_range_max => 22}
|
||||
data = HP[:network].create_security_group_rule(@sec_group_id, 'ingress', attributes).body['security_group_rule']
|
||||
@sec_group_rule_id = data['id']
|
||||
data
|
||||
end
|
||||
|
||||
tests("#get_security_group_rule('#{@sec_group_rule_id}')").formats(@security_group_rule_format) do
|
||||
HP[:network].get_security_group_rule(@sec_group_rule_id).body['security_group_rule']
|
||||
end
|
||||
|
||||
tests("#list_security_group_rules").formats('security_group_rules' => [@security_group_rule_format]) do
|
||||
HP[:network].list_security_group_rules.body
|
||||
end
|
||||
|
||||
tests("#delete_security_group_rule('#{@sec_group_rule_id}')").succeeds do
|
||||
HP[:network].delete_security_group_rule(@sec_group_rule_id)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
|
||||
tests('#get_security_group_rule(0)').raises(Fog::HP::Network::NotFound) do
|
||||
HP[:network].get_security_group_rule(0)
|
||||
end
|
||||
|
||||
tests('#delete_security_group_rule(0)').raises(Fog::HP::Network::NotFound) do
|
||||
HP[:network].delete_security_group_rule(0)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
HP[:network].delete_security_group(@sec_group_id)
|
||||
end
|
Loading…
Reference in a new issue