From edb5bdc1b783eaac83bd2eb9039e996ad8ae8c2a Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Fri, 12 Apr 2013 00:52:06 -0400 Subject: [PATCH] [hp|network] Add security group rules model, along with tests. --- .../hp/models/network/security_group_rule.rb | 35 +++++++++++++++++++ .../hp/models/network/security_group_rules.rb | 35 +++++++++++++++++++ lib/fog/hp/network.rb | 2 ++ .../network/security_group_rule_tests.rb | 25 +++++++++++++ .../network/security_group_rules_tests.rb | 23 ++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 lib/fog/hp/models/network/security_group_rule.rb create mode 100644 lib/fog/hp/models/network/security_group_rules.rb create mode 100644 tests/hp/models/network/security_group_rule_tests.rb create mode 100644 tests/hp/models/network/security_group_rules_tests.rb diff --git a/lib/fog/hp/models/network/security_group_rule.rb b/lib/fog/hp/models/network/security_group_rule.rb new file mode 100644 index 000000000..b8f192d42 --- /dev/null +++ b/lib/fog/hp/models/network/security_group_rule.rb @@ -0,0 +1,35 @@ +require 'fog/core/model' + +module Fog + module HP + class Network + + class SecurityGroupRule < Fog::Model + identity :id + + attribute :security_group_id + attribute :direction + attribute :protocol + attribute :port_range_min + attribute :port_range_max + attribute :remote_ip_prefix + attribute :ethertype + attribute :remote_group_id + attribute :tenant_id + + def destroy + requires :id + service.delete_security_group_rule(id) + true + end + + def save + raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted? + merge_attributes(service.create_security_group_rule(security_group_id, direction, attributes).body['security_group_rule']) + true + end + + end + end + end +end diff --git a/lib/fog/hp/models/network/security_group_rules.rb b/lib/fog/hp/models/network/security_group_rules.rb new file mode 100644 index 000000000..4c26b746e --- /dev/null +++ b/lib/fog/hp/models/network/security_group_rules.rb @@ -0,0 +1,35 @@ +require 'fog/core/collection' +require 'fog/hp/models/network/security_group_rule' + +module Fog + module HP + class Network + + class SecurityGroupRules < Fog::Collection + + attribute :filters + + model Fog::HP::Network::SecurityGroupRule + + def initialize(attributes) + self.filters ||= {} + super + end + + def all(filters = filters) + self.filters = filters + load(service.list_security_group_rules(filters).body['security_group_rules']) + end + + def get(sec_group_rule_id) + if sec_group_rule = service.get_security_group_rule(sec_group_rule_id).body['security_group_rule'] + new(sec_group_rule) + end + rescue Fog::HP::Network::NotFound + nil + end + + end + end + end +end diff --git a/lib/fog/hp/network.rb b/lib/fog/hp/network.rb index 613a8bf91..80682a226 100644 --- a/lib/fog/hp/network.rb +++ b/lib/fog/hp/network.rb @@ -22,6 +22,8 @@ module Fog collection :routers model :security_group collection :security_groups + model :security_group_rule + collection :security_group_rules model :subnet collection :subnets diff --git a/tests/hp/models/network/security_group_rule_tests.rb b/tests/hp/models/network/security_group_rule_tests.rb new file mode 100644 index 000000000..1259269d1 --- /dev/null +++ b/tests/hp/models/network/security_group_rule_tests.rb @@ -0,0 +1,25 @@ +Shindo.tests('HP::Network | networking security group rule model', ['hp', 'networking', 'securitygroup']) do + + @secgroup = HP[:network].security_groups.create({:name => 'fogsecgroup'}) + + attributes = {:security_group_id => @secgroup.id, :direction => 'ingress'} + model_tests(HP[:network].security_group_rules, attributes, true) + + tests('success') do + + tests('#create').succeeds do + attributes = {:security_group_id => @secgroup.id, :direction => 'ingress', :protocol => 'tcp', + :port_range_min => 22, :port_range_max => 22, :remote_ip_prefix => '0.0.0.0/0'} + @secgrouprule = HP[:network].security_group_rules.create(attributes) + @secgrouprule.wait_for { ready? } unless Fog.mocking? + !@secgrouprule.id.nil? + end + + tests('#destroy').succeeds do + @secgrouprule.destroy + end + + end + + @secgroup.destroy +end diff --git a/tests/hp/models/network/security_group_rules_tests.rb b/tests/hp/models/network/security_group_rules_tests.rb new file mode 100644 index 000000000..96ec33740 --- /dev/null +++ b/tests/hp/models/network/security_group_rules_tests.rb @@ -0,0 +1,23 @@ +Shindo.tests('HP::Network | networking security group rules collection', ['hp', 'networking', 'securitygroup']) do + + @secgroup = HP[:network].security_groups.create({:name => 'my_secgroup'}) + + attributes = {:security_group_id => @secgroup.id, :direction => 'ingress'} + collection_tests(HP[:network].security_group_rules, attributes, true) + + tests('success') do + + attributes = {:security_group_id => @secgroup.id, :direction => 'ingress', :protocol => 'tcp', + :port_range_min => 22, :port_range_max => 22, :remote_ip_prefix => '0.0.0.0/0'} + @secgrouprule = HP[:network].security_group_rules.create(attributes) + + tests('#all(filter)').succeeds do + secgrouprule = HP[:network].security_group_rules.all({:direction => 'ingress'}) + secgrouprule.first.direction == 'ingress' + end + + @secgrouprule.destroy + end + + @secgroup.destroy +end \ No newline at end of file