From b282f5d5a0db9e9a714c2eca90b0e4c090326fea Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Fri, 29 Mar 2013 14:37:05 -0400 Subject: [PATCH] [hp|network] Add models for subnets, along with tests. --- lib/fog/hp/models/network/subnet.rb | 49 ++++++++++++++++++++++++ lib/fog/hp/models/network/subnets.rb | 35 +++++++++++++++++ lib/fog/hp/network.rb | 2 + tests/hp/models/network/subnet_tests.rb | 30 +++++++++++++++ tests/hp/models/network/subnets_tests.rb | 22 +++++++++++ 5 files changed, 138 insertions(+) create mode 100644 lib/fog/hp/models/network/subnet.rb create mode 100644 lib/fog/hp/models/network/subnets.rb create mode 100644 tests/hp/models/network/subnet_tests.rb create mode 100644 tests/hp/models/network/subnets_tests.rb diff --git a/lib/fog/hp/models/network/subnet.rb b/lib/fog/hp/models/network/subnet.rb new file mode 100644 index 000000000..69406a841 --- /dev/null +++ b/lib/fog/hp/models/network/subnet.rb @@ -0,0 +1,49 @@ +require 'fog/core/model' + +module Fog + module HP + class Network + + class Subnet < Fog::Model + identity :id + + attribute :name + attribute :network_id + attribute :cidr + attribute :ip_version + attribute :tenant_id + attribute :dns_nameservers + attribute :allocation_pools + attribute :host_routes + attribute :gateway_ip + attribute :enable_dhcp + + def destroy + requires :id + service.delete_subnet(id) + true + end + + def save + requires :network_id, :cidr, :ip_version + identity ? update : create + end + + private + + def create + requires :network_id, :cidr, :ip_version + merge_attributes(service.create_subnet(network_id, cidr, ip_version, attributes).body['subnet']) + true + end + + def update + requires :id + merge_attributes(service.update_subnet(id, attributes).body['subnet']) + true + end + + end + end + end +end diff --git a/lib/fog/hp/models/network/subnets.rb b/lib/fog/hp/models/network/subnets.rb new file mode 100644 index 000000000..fcd15da13 --- /dev/null +++ b/lib/fog/hp/models/network/subnets.rb @@ -0,0 +1,35 @@ +require 'fog/core/collection' +require 'fog/hp/models/network/subnet' + +module Fog + module HP + class Network + + class Subnets < Fog::Collection + + attribute :filters + + model Fog::HP::Network::Subnet + + def initialize(attributes) + self.filters ||= {} + super + end + + def all(filters = filters) + self.filters = filters + load(service.list_subnets(filters).body['subnets']) + end + + def get(subnet_id) + if subnet = service.get_subnet(subnet_id).body['subnet'] + new(subnet) + 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 4ad146651..1a73796c2 100644 --- a/lib/fog/hp/network.rb +++ b/lib/fog/hp/network.rb @@ -14,6 +14,8 @@ module Fog model_path 'fog/hp/models/network' model :network collection :networks + model :subnet + collection :subnets request_path 'fog/hp/requests/network' request :add_router_interface diff --git a/tests/hp/models/network/subnet_tests.rb b/tests/hp/models/network/subnet_tests.rb new file mode 100644 index 000000000..9fbfbab42 --- /dev/null +++ b/tests/hp/models/network/subnet_tests.rb @@ -0,0 +1,30 @@ +Shindo.tests('HP::Network | networking subnet model', ['hp', 'networking', 'subnet']) do + + @network = HP[:network].networks.create(:name => 'my_network') + + attributes = {:name => 'fogsubnet', :network_id => @network.id, :cidr => '11.11.11.11/11', :ip_version => 4} + model_tests(HP[:network].subnets, attributes, true) + + tests('success') do + + tests('#create').succeeds do + attributes = {:name => 'my_subnet', :network_id => @network.id, :cidr => '12.12.12.12/12', :ip_version => 4} + @subnet = HP[:network].subnets.create(attributes) + @subnet.wait_for { ready? } unless Fog.mocking? + !@subnet.id.nil? + end + + tests('#save').succeeds do + @subnet.name = 'my_subnet_upd' + @subnet.save + end + + tests('#destroy').succeeds do + @subnet.destroy + end + + end + + @network.destroy + +end diff --git a/tests/hp/models/network/subnets_tests.rb b/tests/hp/models/network/subnets_tests.rb new file mode 100644 index 000000000..a846b6b88 --- /dev/null +++ b/tests/hp/models/network/subnets_tests.rb @@ -0,0 +1,22 @@ +Shindo.tests('HP::Network | networking subnets model', ['hp', 'networking', 'subnet']) do + + @network = HP[:network].networks.create(:name => 'my_network') + + attributes = {:name => 'my_subnet', :network_id => @network.id, :cidr => '11.11.11.11/11', :ip_version => 4} + collection_tests(HP[:network].subnets, attributes, true) + + tests('success') do + + attributes = {:name => 'fogsubnet', :network_id => @network.id, :cidr => '12.12.12.12/12', :ip_version => 4} + @subnet = HP[:network].subnets.create(attributes) + + tests('#all(filter)').succeeds do + subnets = HP[:network].subnets.all({:cidr => '12.12.12.12/12'}) + subnets.first.cidr == '12.12.12.12/12' + end + + @subnet.destroy + end + + @network.destroy +end \ No newline at end of file