Skip to content

Commit

Permalink
get_public_ip(s) w/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Muller authored and Wesley Beary committed Jun 5, 2010
1 parent 93ea9de commit c85ce34
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/fog/vcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,50 +127,54 @@ def do_request(params)
end

class Mock < Real
def self._base_url
"https://fakey.com/api/v0.8"
end

DATA =
{
:versions => [
{ :version => "v0.8", :login_url => "https://fakey.com/api/v0.8/login", :supported => true }
{ :version => "v0.8", :login_url => "#{_base_url}/login", :supported => true }
],
:vdc_resources => [
{
:type => "application/vnd.vmware.vcloud.vApp+xml",
:href => "https://fakey.com/api/v0.8/vapp/61",
:href => "#{_base_url}/vapp/61",
:name => "Foo App 1"
},
{
:type => "application/vnd.vmware.vcloud.vApp+xml",
:href => "https://fakey.com/api/v0.8/vapp/62",
:href => "#{_base_url}/vapp/62",
:name => "Bar App 1"
},
{
:type => "application/vnd.vmware.vcloud.vApp+xml",
:href => "https://fakey.com/api/v0.8/vapp/63",
:href => "#{_base_url}/vapp/63",
:name => "Bar App 2"
}
],
:organizations =>
[
{
:info => {
:href => "https://fakey.com/api/v0.8/org/1",
:href => "#{_base_url}/org/1",
:name => "Boom Inc.",
},
:vdcs => [
{ :href => "https://fakey.com/api/v0.8/vdc/21",
{ :href => "#{_base_url}/vdc/21",
:name => "Boomstick",
:storage => { :used => 105, :allocated => 200 },
:cpu => { :allocated => 10000 },
:memory => { :allocated => 20480 },
:networks => [
{ :href => "https://fakey.com/api/v0.8/network/31",
{ :href => "#{_base_url}/network/31",
:name => "1.2.3.0/24",
:subnet => "1.2.3.0/24",
:gateway => "1.2.3.1",
:netmask => "255.255.255.0",
:fencemode => "isolated"
},
{ :href => "https://fakey.com/api/v0.8/network/32",
{ :href => "#{_base_url}/network/32",
:name => "4.5.6.0/24",
:subnet => "4.5.6.0/24",
:gateway => "4.5.6.1",
Expand All @@ -179,13 +183,13 @@ class Mock < Real
},
],
:vms => [
{ :href => "https://fakey.com/api/v0.8/vap/41",
{ :href => "#{_base_url}/vap/41",
:name => "Broom 1"
},
{ :href => "https://fakey.com/api/v0.8/vap/42",
{ :href => "#{_base_url}/vap/42",
:name => "Broom 2"
},
{ :href => "https://fakey.com/api/v0.8/vap/43",
{ :href => "#{_base_url}/vap/43",
:name => "Email!"
}
],
Expand All @@ -201,13 +205,13 @@ class Mock < Real
}
]
},
{ :href => "https://fakey.com/api/v0.8/vdc/22",
{ :href => "#{_base_url}/vdc/22",
:storage => { :used => 40, :allocated => 150 },
:cpu => { :allocated => 1000 },
:memory => { :allocated => 2048 },
:name => "Rock-n-Roll",
:networks => [
{ :href => "https://fakey.com/api/v0.8/network/33",
{ :href => "#{_base_url}/network/33",
:name => "7.8.9.0/24",
:subnet => "7.8.9.0/24",
:gateway => "7.8.9.1",
Expand All @@ -216,7 +220,7 @@ class Mock < Real
}
],
:vms => [
{ :href => "https://fakey.com/api/v0.8/vap/44",
{ :href => "#{_base_url}/vap/44",
:name => "Master Blaster"
}
],
Expand Down
8 changes: 8 additions & 0 deletions lib/fog/vcloud/terremark/ecloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ def self.extended(klass)
#Do anything we need to do here that's specific to ecloud
unless @required
require 'fog/vcloud/terremark/all'
require 'fog/vcloud/terremark/ecloud/models/public_ip'
require 'fog/vcloud/terremark/ecloud/models/public_ips'
require 'fog/vcloud/terremark/ecloud/models/vdc'
require 'fog/vcloud/terremark/ecloud/models/vdcs'
require 'fog/vcloud/terremark/ecloud/parsers/get_public_ip'
require 'fog/vcloud/terremark/ecloud/parsers/get_public_ips'
require 'fog/vcloud/terremark/ecloud/parsers/get_vdc'
require 'fog/vcloud/terremark/ecloud/requests/get_public_ip'
require 'fog/vcloud/terremark/ecloud/requests/get_public_ips'
require 'fog/vcloud/terremark/ecloud/requests/login'
require 'fog/vcloud/terremark/ecloud/requests/get_vdc'
Struct.new("TmrkEcloudVdc", :links, :resource_entities, :networks,
:cpu_capacity, :storage_capacity, :memory_capacity, :deployed_vm_quota, :instantiated_vm_quota,
:href, :type, :name, :xmlns, :description)
Struct.new("TmrkEcloudPublicIpList", :links)
Struct.new("TmrkEcloudPublicIp", :type, :href, :name, :id)
@required = true
end
if Fog.mocking?
Expand Down
19 changes: 19 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/models/public_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'fog/model'

module Fog
module Vcloud
module Terremark
module Ecloud
class PublicIp < Fog::Vcloud::Model

identity :href

attribute :name
attribute :type
attribute :id
end
end
end
end
end

32 changes: 32 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/models/public_ips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module Vcloud
module Terremark
module Ecloud

module Mock
def public_ips(options = {})
@public_ips ||= Fog::Vcloud::Terremark::Ecloud::PublicIps.new(options.merge(:connection => self))
end
end

module Real
def public_ips(options = {})
@public_ips ||= Fog::Vcloud::Terremark::Ecloud::PublicIps.new(options.merge(:connection => self))
end
end

class PublicIps < Fog::Vcloud::Collection

attr_accessor :href

model Fog::Vcloud::Terremark::Ecloud::PublicIp

get_request :get_public_ip
vcloud_type "application/vnd.tmrk.ecloud.publicIp+xml"
all_request lambda { |public_ips| public_ips.connection.get_public_ips(public_ips.href) }

end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/models/vdc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Vdc < Fog::Vcloud::Vdc
attribute :deployed_vm_quota
attribute :instantiated_vm_quota

def public_ips
@public_ips ||= Fog::Vcloud::Terremark::Ecloud::PublicIps.new( :connection => connection,
:href => other_links.detect { |link| link.type == "application/vnd.tmrk.ecloud.publicIpsList+xml" }.href )
end

end
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/parsers/get_public_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Fog
module Parsers
module Vcloud
module Terremark
module Ecloud

class GetPublicIp < Fog::Parsers::Vcloud::Base

def reset
@response = Struct::TmrkEcloudPublicIp.new("application/vnd.tmrk.ecloud.publicIp+xml")
end

def end_element(name)
case name
when 'Href'
@response[name.downcase] = URI.parse(@value)
when 'Name'
@response[name.downcase] = @value
when 'Id'
@response['id'] = @value.to_i
end
end

end
end
end
end
end
end

40 changes: 40 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/parsers/get_public_ips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Fog
module Parsers
module Vcloud
module Terremark
module Ecloud

class GetPublicIps < Fog::Parsers::Vcloud::Base

def reset
@response = Struct::TmrkEcloudPublicIpList.new([])
end

def start_element(name, attributes)
@value = nil
case name
when 'PublicIPAddress'
@ip_address = Struct::TmrkEcloudPublicIp.new("application/vnd.tmrk.ecloud.publicIp+xml")
end
end

def end_element(name)
case name
when 'Href'
@ip_address[name.downcase] = URI.parse(@value)
when 'Name'
@ip_address[name.downcase] = @value
when 'Id'
@ip_address[name.downcase] = @value.to_i
when 'PublicIPAddress'
@response.links << @ip_address
end
end

end
end
end
end
end
end

49 changes: 49 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/requests/get_public_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Fog
module Vcloud
module Terremark
module Ecloud

module Real

def get_public_ip(public_ip_uri)
opts = {
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Vcloud::Terremark::Ecloud::GetPublicIp.new,
:uri => public_ip_uri
}
if self.class == Fog::Terremark::Ecloud::Real
opts[:path] = "extensions/publicIp/#{public_ip_id}"
end
request(opts)
end

end

module Mock
#
#Based off of:
#http://support.theenterprisecloud.com/kb/default.asp?id=567&Lang=1&SID=
#

def get_public_ip(public_ip_uri)
ip_id = public_ip_uri.to_s.split('/')[-1]
if ip = mock_data[:organizations].map { |org| org[:vdcs] }.flatten.map { |vdc| vdc[:public_ips] }.flatten.detect { |ip| ip[:id].to_s == ip_id }
xml = Builder::XmlMarkup.new
mock_it(Fog::Parsers::Vcloud::Terremark::Ecloud::GetPublicIp.new, 200,
xml.PublicIp(:xmlns => "urn:tmrk:eCloudExtensions-2.0", :"xmlns:i" => "http://www.w3.org/2001/XMLSchema-instance") {
xml.Id(ip[:id])
xml.Href("https://fakey.com/api/v0.8/extensions/publicIp/#{ip[:id]}")
xml.Name(ip[:name])
}, { 'Content-Type' => 'application/vnd.tmrk.ecloud.publicIp+xml' }
)
else
mock_error 200, "401 Unauthorized"
end
end

end
end
end
end
end
47 changes: 47 additions & 0 deletions lib/fog/vcloud/terremark/ecloud/requests/get_public_ips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Fog
module Vcloud
module Terremark
module Ecloud

module Real

def get_public_ips(public_ips_uri)
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Vcloud::Terremark::Ecloud::GetPublicIps.new,
:uri => public_ips_uri
)
end
end

module Mock

#
#Based off of:
#http://support.theenterprisecloud.com/kb/default.asp?id=577&Lang=1&SID=
#
def get_public_ips(public_ips_uri)
if vdc = mock_data[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| vdc[:href].split('/').last == public_ips_uri.to_s.split('/')[-2] }
xml = Builder::XmlMarkup.new
mock_it Fog::Parsers::Vcloud::Terremark::Ecloud::GetPublicIps.new, 200,
xml.PublicIPAddresses {
vdc[:public_ips].each do |ip|
xml.PublicIPAddress {
xml.Id(ip[:id])
xml.Href("https://fakey.com/api/v0.8/extensions/publicIp/#{ip[:id]}")
xml.Name(ip[:name])
}
end
}, { 'Content-Type' => 'application/vnd.tmrk.ecloud.publicIpsList+xml'}
else
mock_error 200, "401 Unauthorized"
end
end

end
end
end
end
end

1 change: 1 addition & 0 deletions spec/vcloud/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
Spec::Runner.configure do |config|
config.before(:all) do
@mock_data = Fog::Vcloud::Mock::DATA
@base_url = Fog::Vcloud::Mock._base_url
@mock_version = @mock_data[:versions][0]
@mock_organization = @mock_data[:organizations][0]
@mock_vdc = @mock_organization[:vdcs][0]
Expand Down
Loading

0 comments on commit c85ce34

Please sign in to comment.