From 7979363d37bba6ee386452a0e438ed0737003721 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Thu, 10 Jun 2010 07:20:14 +0800 Subject: [PATCH] Network(s) and Ip(s) models --- lib/fog/vcloud/collection.rb | 3 +- lib/fog/vcloud/terremark/ecloud.rb | 4 ++ .../ecloud/models/internet_services.rb | 7 +- lib/fog/vcloud/terremark/ecloud/models/ip.rb | 18 +++++ lib/fog/vcloud/terremark/ecloud/models/ips.rb | 36 ++++++++++ .../vcloud/terremark/ecloud/models/network.rb | 32 +++++++++ .../terremark/ecloud/models/networks.rb | 38 +++++++++++ lib/fog/vcloud/terremark/ecloud/models/vdc.rb | 6 ++ .../vcloud/terremark/ecloud/models/ip_spec.rb | 35 ++++++++++ .../terremark/ecloud/models/ips_spec.rb | 28 ++++++++ .../terremark/ecloud/models/network_spec.rb | 65 +++++++++++++++++++ .../terremark/ecloud/models/networks_spec.rb | 26 ++++++++ .../terremark/ecloud/models/public_ip_spec.rb | 2 - 13 files changed, 296 insertions(+), 4 deletions(-) create mode 100644 lib/fog/vcloud/terremark/ecloud/models/ip.rb create mode 100644 lib/fog/vcloud/terremark/ecloud/models/ips.rb create mode 100644 lib/fog/vcloud/terremark/ecloud/models/network.rb create mode 100644 lib/fog/vcloud/terremark/ecloud/models/networks.rb create mode 100644 spec/vcloud/terremark/ecloud/models/ip_spec.rb create mode 100644 spec/vcloud/terremark/ecloud/models/ips_spec.rb create mode 100644 spec/vcloud/terremark/ecloud/models/network_spec.rb create mode 100644 spec/vcloud/terremark/ecloud/models/networks_spec.rb diff --git a/lib/fog/vcloud/collection.rb b/lib/fog/vcloud/collection.rb index d45aeaa44..f6593b9b4 100644 --- a/lib/fog/vcloud/collection.rb +++ b/lib/fog/vcloud/collection.rb @@ -18,7 +18,8 @@ module Fog @all_request = all_request class_eval <<-EOS, __FILE__, __LINE__ def all - data = self.class.all_request.call(self).body.links.select do |link| + raw_all = self.class.all_request.call(self) + data = (raw_all.is_a?(Array) ? raw_all : raw_all.body.links).select do |link| link.type == self.class.vcloud_type end.map { |link| {:href => link.href, :name => link.name } } load(data) diff --git a/lib/fog/vcloud/terremark/ecloud.rb b/lib/fog/vcloud/terremark/ecloud.rb index 571cdedfc..aa1de7d9a 100644 --- a/lib/fog/vcloud/terremark/ecloud.rb +++ b/lib/fog/vcloud/terremark/ecloud.rb @@ -31,6 +31,10 @@ module Fog require 'fog/vcloud/terremark/all' require 'fog/vcloud/terremark/ecloud/models/internet_service' require 'fog/vcloud/terremark/ecloud/models/internet_services' + require 'fog/vcloud/terremark/ecloud/models/ip' + require 'fog/vcloud/terremark/ecloud/models/ips' + require 'fog/vcloud/terremark/ecloud/models/network' + require 'fog/vcloud/terremark/ecloud/models/networks' require 'fog/vcloud/terremark/ecloud/models/public_ip' require 'fog/vcloud/terremark/ecloud/models/public_ips' require 'fog/vcloud/terremark/ecloud/models/vdc' diff --git a/lib/fog/vcloud/terremark/ecloud/models/internet_services.rb b/lib/fog/vcloud/terremark/ecloud/models/internet_services.rb index 336e393f3..006e0308b 100644 --- a/lib/fog/vcloud/terremark/ecloud/models/internet_services.rb +++ b/lib/fog/vcloud/terremark/ecloud/models/internet_services.rb @@ -33,10 +33,15 @@ module Fog raw_results.body.links.detect { |link| link.href.to_s == uri.to_s } end + def reload + super + @raw_results = nil + end + private def raw_results - connection.get_internet_services(self.href) + @raw_results ||= connection.get_internet_services(self.href) end end diff --git a/lib/fog/vcloud/terremark/ecloud/models/ip.rb b/lib/fog/vcloud/terremark/ecloud/models/ip.rb new file mode 100644 index 000000000..453025cd2 --- /dev/null +++ b/lib/fog/vcloud/terremark/ecloud/models/ip.rb @@ -0,0 +1,18 @@ +require 'fog/model' + +module Fog + module Vcloud + module Terremark + module Ecloud + class Ip < Fog::Vcloud::Model + + identity :name + + attribute :status + attribute :server + + end + end + end + end +end diff --git a/lib/fog/vcloud/terremark/ecloud/models/ips.rb b/lib/fog/vcloud/terremark/ecloud/models/ips.rb new file mode 100644 index 000000000..91d54da49 --- /dev/null +++ b/lib/fog/vcloud/terremark/ecloud/models/ips.rb @@ -0,0 +1,36 @@ +module Fog + module Vcloud + module Terremark + module Ecloud + + class Ips < Fog::Vcloud::Collection + + model Fog::Vcloud::Terremark::Ecloud::Ip + + undef_method :create + + def all + load(connection.get_network_ips(href).body.addresses. + map { |address| { :name => address.name, :status => address.status, :server => address.server } }) + end + + def get_raw(name) + raw_results.detect { |address| address.name == name } + end + + def reload + super + @raw_results = nil + end + + private + + def raw_results + @raw_results ||= connection.get_network_ips(href).body.addresses + end + + end + end + end + end +end diff --git a/lib/fog/vcloud/terremark/ecloud/models/network.rb b/lib/fog/vcloud/terremark/ecloud/models/network.rb new file mode 100644 index 000000000..26239cda9 --- /dev/null +++ b/lib/fog/vcloud/terremark/ecloud/models/network.rb @@ -0,0 +1,32 @@ +require 'fog/model' + +module Fog + module Vcloud + module Terremark + module Ecloud + class Network < Fog::Vcloud::Model + + identity :href + + attribute :name + attribute :features + attribute :configuration + attribute :ips_link + attribute :type + attribute :xmlns + + def ips + unless @loaded + reload + end + @ips ||= Fog::Vcloud::Terremark::Ecloud::Ips. + new( :connection => connection, + :href => ips_link.href ) + end + end + end + end + end +end + + diff --git a/lib/fog/vcloud/terremark/ecloud/models/networks.rb b/lib/fog/vcloud/terremark/ecloud/models/networks.rb new file mode 100644 index 000000000..63bb76ec1 --- /dev/null +++ b/lib/fog/vcloud/terremark/ecloud/models/networks.rb @@ -0,0 +1,38 @@ +module Fog + module Vcloud + module Terremark + module Ecloud + + module Mock + def networks(options = {}) + @networks ||= Fog::Vcloud::Terremark::Ecloud::Networks.new(options.merge(:connection => self)) + end + end + + module Real + def networks(options = {}) + @networks ||= Fog::Vcloud::Terremark::Ecloud::Networks.new(options.merge(:connection => self)) + end + end + + class Networks < Fog::Vcloud::Collection + + undef_method :create + + model Fog::Vcloud::Terremark::Ecloud::Network + + get_request :get_network + vcloud_type "application/vnd.vmware.vcloud.network+xml" + all_request lambda { |networks| networks.connection.get_vdc(networks.href).body.networks } + + #def all + # pp connection.get_vdc(href).body.networks + # load(connection.get_vdc(href).body.networks.map { |network| { } } ) + #end + + end + end + end + end +end + diff --git a/lib/fog/vcloud/terremark/ecloud/models/vdc.rb b/lib/fog/vcloud/terremark/ecloud/models/vdc.rb index 0a2f1de51..d5520bcdf 100644 --- a/lib/fog/vcloud/terremark/ecloud/models/vdc.rb +++ b/lib/fog/vcloud/terremark/ecloud/models/vdc.rb @@ -27,6 +27,12 @@ module Fog :href => href.to_s.gsub('vdc','extensions/vdc') + "/internetServices" ) end + def networks + @networks ||= Fog::Vcloud::Terremark::Ecloud::Networks. + new( :connection => connection, + :href => href ) + end + end end end diff --git a/spec/vcloud/terremark/ecloud/models/ip_spec.rb b/spec/vcloud/terremark/ecloud/models/ip_spec.rb new file mode 100644 index 000000000..9874f5a44 --- /dev/null +++ b/spec/vcloud/terremark/ecloud/models/ip_spec.rb @@ -0,0 +1,35 @@ +require File.join(File.dirname(__FILE__),'..','..','..','spec_helper') + +describe "Fog::Vcloud::Terremark::Ecloud::Ip", :type => :tmrk_ecloud_model do + subject { @vcloud } + + describe :class do + subject { Fog::Vcloud::Terremark::Ecloud::Ip } + + it { should have_identity :name } + it { should have_only_these_attributes [:name, :status, :server] } + end + + context "with no uri" do + + subject { Fog::Vcloud::Terremark::Ecloud::Ip.new() } + + its(:name) { should be_nil } + its(:status) { should be_nil } + its(:server) { should be_nil } + end + + context "as a collection member" do + subject { @vcloud.vdcs[0].networks[0].ips[0] } + let(:status) { @mock_network[:ips].keys.include?(@vcloud.vdcs[0].networks[0].ips[0].name) ? "Assigned" : nil } + let(:server) { @mock_network[:ips][@vcloud.vdcs[0].networks[0].ips[0].name] } + + it { should be_an_instance_of Fog::Vcloud::Terremark::Ecloud::Ip } + + its(:name) { should == IPAddr.new(@mock_network[:name]).to_range.to_a[3].to_s } + its(:status) { should == status } + its(:server) { should == server } + + end +end + diff --git a/spec/vcloud/terremark/ecloud/models/ips_spec.rb b/spec/vcloud/terremark/ecloud/models/ips_spec.rb new file mode 100644 index 000000000..6a592f7d6 --- /dev/null +++ b/spec/vcloud/terremark/ecloud/models/ips_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__),'..','..','..','spec_helper') + +describe "Fog::Vcloud::Terremark::Ecloud::Ips", :type => :tmrk_ecloud_model do + subject { @vcloud } + + it { should_not respond_to :ips } + + describe :class do + subject { @vcloud.vdcs[0].networks[0].ips.class } + its(:model) { should == Fog::Vcloud::Terremark::Ecloud::Ip } + its(:get_request) { should be_nil } + its(:all_request) { should be_nil } + its(:vcloud_type) { should be_nil } + it { should_not respond_to :create } + end + + describe :ips do + subject { @vcloud.vdcs[0].networks[0].ips } + + it { should be_an_instance_of Fog::Vcloud::Terremark::Ecloud::Ips } + + its(:length) { should == 252 } + + it { should have_members_of_the_right_model } + end +end + + diff --git a/spec/vcloud/terremark/ecloud/models/network_spec.rb b/spec/vcloud/terremark/ecloud/models/network_spec.rb new file mode 100644 index 000000000..2d362bd51 --- /dev/null +++ b/spec/vcloud/terremark/ecloud/models/network_spec.rb @@ -0,0 +1,65 @@ +require File.join(File.dirname(__FILE__),'..','..','..','spec_helper') + +describe "Fog::Vcloud::Terremark::Ecloud::Network", :type => :tmrk_ecloud_model do + subject { @vcloud } + + describe :class do + subject { Fog::Vcloud::Terremark::Ecloud::Network } + + it { should have_identity :href } + it { should have_only_these_attributes [:href, :name, :features, :configuration, :ips_link, :type, :xmlns] } + end + + context "with no uri" do + + subject { Fog::Vcloud::Terremark::Ecloud::Network.new() } + + its(:href) { should be_nil } + its(:identity) { should be_nil } + its(:name) { should be_nil } + its(:type) { should be_nil } + its(:features) { should be_nil } + its(:configuration) { should be_nil } + its(:ips_link) { should be_nil } + its(:xmlns) { should be_nil } + end + + context "as a collection member" do + subject { @vcloud.vdcs[0].networks[0] } + + it { should be_an_instance_of Fog::Vcloud::Terremark::Ecloud::Network } + + it_should_behave_like "it has a vcloud v0.8 xmlns" + + its(:href) { should == URI.parse(@mock_network[:href]) } + its(:identity) { should == URI.parse(@mock_network[:href]) } + its(:name) { should == @mock_network[:name] } + its(:type) { should == "application/vnd.vmware.vcloud.network+xml" } + + it { should have(1).features } + + describe :features do + let(:feature) { subject.features[0] } + specify { feature.should be_an_instance_of Struct::VcloudNetworkFenceMode } + specify { feature.mode.should == "isolated" } + end + + describe :configurations do + let(:configuration) { subject.configuration } + specify { configuration.should be_an_instance_of Struct::VcloudNetworkConfiguration } + specify { configuration.gateway.should == @mock_network[:gateway] } + specify { configuration.netmask.should == @mock_network[:netmask] } + specify { configuration.dns.should be_nil } + end + + describe :ips_link do + let(:ips_link) { subject.ips_link } + specify { ips_link.rel.should == "down" } + specify { ips_link.href.should == URI.parse(@mock_network[:href] + "/ips") } + specify { ips_link.type.should == "application/xml" } + specify { ips_link.name.should == "IP Addresses" } + end + + end +end + diff --git a/spec/vcloud/terremark/ecloud/models/networks_spec.rb b/spec/vcloud/terremark/ecloud/models/networks_spec.rb new file mode 100644 index 000000000..43dd7be05 --- /dev/null +++ b/spec/vcloud/terremark/ecloud/models/networks_spec.rb @@ -0,0 +1,26 @@ +require File.join(File.dirname(__FILE__),'..','..','..','spec_helper') + +describe "Fog::Vcloud::Terremark::Ecloud::Networks", :type => :tmrk_ecloud_model do + subject { @vcloud } + + it { should respond_to :networks } + + describe :class do + subject { @vcloud.networks.class } + its(:model) { should == Fog::Vcloud::Terremark::Ecloud::Network } + its(:get_request) { should == :get_network } + its(:all_request) { should be_an_instance_of Proc } + its(:vcloud_type) { should == "application/vnd.vmware.vcloud.network+xml" } + end + + describe :networks do + subject { @vcloud.vdcs[0].networks } + + it { should be_an_instance_of Fog::Vcloud::Terremark::Ecloud::Networks } + + its(:length) { should == 2 } + + it { should have_members_of_the_right_model } + end +end + diff --git a/spec/vcloud/terremark/ecloud/models/public_ip_spec.rb b/spec/vcloud/terremark/ecloud/models/public_ip_spec.rb index 16f7144f2..327c77815 100644 --- a/spec/vcloud/terremark/ecloud/models/public_ip_spec.rb +++ b/spec/vcloud/terremark/ecloud/models/public_ip_spec.rb @@ -8,8 +8,6 @@ describe "Fog::Vcloud::Terremark::Ecloud::PublicIp", :type => :tmrk_ecloud_model subject { @vcloud } - it { should respond_to :get_public_ip } - describe :class do subject { Fog::Vcloud::Terremark::Ecloud::PublicIp }