From 4421d869539e9d5d9241965b8b8297a279801a7b Mon Sep 17 00:00:00 2001 From: "geemus (Wesley Beary)" Date: Mon, 5 Apr 2010 17:18:50 -0700 Subject: [PATCH] [terremark] cleanup existing and add stuff for connecting ips/services and delete methods --- lib/fog/terremark.rb | 65 +++++++++++++++++++ lib/fog/terremark/parsers/get_public_Ips.rb | 28 ++++++++ lib/fog/terremark/parsers/internet_service.rb | 63 ++++++++++++++++++ lib/fog/terremark/parsers/node_service.rb | 30 +++++++++ .../requests/add_internet_service.rb | 63 ++++++++++++++++++ .../terremark/requests/add_node_service.rb | 63 ++++++++++++++++++ .../requests/create_internet_service.rb | 63 ++++++++++++++++++ .../requests/delete_internet_service.rb | 40 ++++++++++++ .../terremark/requests/delete_node_service.rb | 40 ++++++++++++ lib/fog/terremark/requests/deploy_vapp.rb | 4 +- lib/fog/terremark/requests/get_public_ips.rb | 37 +++++++++++ lib/fog/terremark/requests/get_task.rb | 2 +- lib/fog/terremark/requests/get_tasks_list.rb | 2 +- lib/fog/terremark/requests/get_vapp.rb | 2 +- .../requests/instantiate_vapp_template.rb | 12 ++-- lib/fog/terremark/requests/power_off.rb | 2 +- lib/fog/terremark/requests/power_on.rb | 2 +- lib/fog/terremark/requests/reset.rb | 2 +- lib/fog/terremark/requests/shutdown.rb | 2 +- 19 files changed, 505 insertions(+), 17 deletions(-) create mode 100644 lib/fog/terremark/parsers/get_public_Ips.rb create mode 100644 lib/fog/terremark/parsers/internet_service.rb create mode 100644 lib/fog/terremark/parsers/node_service.rb create mode 100644 lib/fog/terremark/requests/add_internet_service.rb create mode 100644 lib/fog/terremark/requests/add_node_service.rb create mode 100644 lib/fog/terremark/requests/create_internet_service.rb create mode 100644 lib/fog/terremark/requests/delete_internet_service.rb create mode 100644 lib/fog/terremark/requests/delete_node_service.rb create mode 100644 lib/fog/terremark/requests/get_public_ips.rb diff --git a/lib/fog/terremark.rb b/lib/fog/terremark.rb index e22f39e80..737c75a0e 100644 --- a/lib/fog/terremark.rb +++ b/lib/fog/terremark.rb @@ -3,12 +3,18 @@ module Fog def self.new(options={}) + require 'fog/terremark/requests/add_internet_service' + require 'fog/terremark/requests/add_node_service' + require 'fog/terremark/requests/create_internet_service' + require 'fog/terremark/requests/delete_internet_service' + require 'fog/terremark/requests/delete_node_service' require 'fog/terremark/requests/delete_vapp' require 'fog/terremark/requests/deploy_vapp' require 'fog/terremark/requests/get_catalog' require 'fog/terremark/requests/get_catalog_item' require 'fog/terremark/requests/get_organization' require 'fog/terremark/requests/get_organizations' + require 'fog/terremark/requests/get_public_ips' require 'fog/terremark/requests/get_task' require 'fog/terremark/requests/get_tasks_list' require 'fog/terremark/requests/get_vapp' @@ -66,6 +72,65 @@ module Fog @cookie = get_organizations.headers['Set-Cookie'] end + # TODO: bust cache on organization creation? + def default_organization_id + @default_organization_id ||= begin + org_list = get_organizations.body['OrgList'] + if org_list.length == 1 + org_list.first['href'].split('/').last.to_i + else + nil + end + end + end + + def default_vdc_id + if default_organization_id + @default_vdc_id ||= begin + vdcs = get_organization(default_organization_id).body['Links'].select {|link| + link['type'] == 'application/vnd.vmware.vcloud.vdc+xml' + } + if vdcs.length == 1 + vdcs.first['href'].split('/').last.to_i + else + nil + end + end + else + nil + end + end + + def default_network_id + if default_vdc_id + @default_network_id ||= begin + networks = get_vdc(default_vdc_id).body['AvailableNetworks'] + if networks.length == 1 + networks.first['href'].split('/').last.to_i + else + nil + end + end + else + nil + end + end + + def default_public_ip_id + if default_vdc_id + @default_public_ip_id ||= begin + ips = get_public_ips(default_vdc_id).body['PublicIpAddresses'] + if ips.length == 1 + ips.first['Href'].split('/').last.to_i + else + nil + end + end + else + nil + end + end + private def request(params) diff --git a/lib/fog/terremark/parsers/get_public_Ips.rb b/lib/fog/terremark/parsers/get_public_Ips.rb new file mode 100644 index 000000000..2edc60770 --- /dev/null +++ b/lib/fog/terremark/parsers/get_public_Ips.rb @@ -0,0 +1,28 @@ +module Fog + module Parsers + module Terremark + + class GetPublicIps< Fog::Parsers::Base + + def reset + @ip_address = {} + @response = { 'PublicIpAddresses' => [] } + end + + def end_element(name) + case name + when 'Href', 'Name' + @ip_address[name] = @value + when 'Id' + @ip_address[name] = @value.to_i + when 'PublicIPAddress' + @response['PublicIpAddresses'] << @ip_address + @ip_address = {} + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/parsers/internet_service.rb b/lib/fog/terremark/parsers/internet_service.rb new file mode 100644 index 000000000..8dc8ffb8e --- /dev/null +++ b/lib/fog/terremark/parsers/internet_service.rb @@ -0,0 +1,63 @@ +module Fog + module Parsers + module Terremark + + class InternetService < Fog::Parsers::Base + + def reset + @in_public_ip_address = false + @response = { 'PublicIpAddress' => {} } + end + + def start_element(name, attributes) + @value = '' + case name + when 'Href' + data = {} + until attributes.empty? + data[attributes.shift] = attributes.shift + end + if @in_public_ip_address + @response['PublicIpAddress'][name] = data + else + @response[name] = data + end + when 'PublicIpAddress' + @in_public_ip_address = true + end + end + + def end_element(name) + case name + when 'Description', 'Protocol' + @response[name] = @value + when 'Enabled' + if @value == 'false' + @response[name] = false + else + @response[name] = true + end + when 'Id' + if @in_public_ip_address + @response['PublicIpAddress'][name] = @value.to_i + else + @response[name] = @value.to_i + end + when 'Name' + if @in_public_ip_address + @response['PublicIpAddress'][name] = @value + else + @response[name] = @value + end + when 'Port', 'Timeout' + @response[name] = @value.to_i + when 'PublicIpAddress' + @in_public_ip_address = false + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/parsers/node_service.rb b/lib/fog/terremark/parsers/node_service.rb new file mode 100644 index 000000000..5bc7f2640 --- /dev/null +++ b/lib/fog/terremark/parsers/node_service.rb @@ -0,0 +1,30 @@ +module Fog + module Parsers + module Terremark + + class NodeService < Fog::Parsers::Base + + def reset + @response = {} + end + + def end_element(name) + case name + when 'Description', 'Href', 'IpAddress', 'Name', 'Protocol' + @response[name] = @value + when 'Enabled' + if @value == 'false' + @response[name] = false + else + @response[name] = true + end + when 'Id', 'Port' + @response[name] = @value.to_i + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/requests/add_internet_service.rb b/lib/fog/terremark/requests/add_internet_service.rb new file mode 100644 index 000000000..b01883915 --- /dev/null +++ b/lib/fog/terremark/requests/add_internet_service.rb @@ -0,0 +1,63 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/internet_service' + + # Reserve requested resources and deploy vApp + # + # ==== Parameters + # * ip_id<~Integer> - Id of ip to add service to + # * name<~String> - Name of service + # * protocol<~String> - Protocol of service + # * port<~Integer> - Port of service + # * options<~Hash>: + # * Enabled<~Boolean>: defaults to true + # * Description<~String>: optional description + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'endTime'<~String> - endTime of task + # * 'href'<~String> - link to task + # * 'startTime'<~String> - startTime of task + # * 'status'<~String> - status of task + # * 'type'<~String> - type of task + # * 'Owner'<~String> - + # * 'href'<~String> - href of owner + # * 'name'<~String> - name of owner + # * 'type'<~String> - type of owner + def add_internet_service(ip_id, name, protocol, port, options = {}) + unless options.has_key?('Enabled') + options['Enabled'] = true + end + data = <<-DATA + + #{name} + #{protocol.upcase} + #{port} + #{options['Enabled']} + #{options['Description']} + +DATA + request( + :body => data, + :expects => 200, + :headers => {'Content-Type' => 'application/xml'}, + :method => 'POST', + :parser => Fog::Parsers::Terremark::InternetService.new, + :path => "publicIps/#{ip_id}/internetServices" + ) + end + + end + + class Mock + + def add_internet_service(ip_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/add_node_service.rb b/lib/fog/terremark/requests/add_node_service.rb new file mode 100644 index 000000000..d57b6e474 --- /dev/null +++ b/lib/fog/terremark/requests/add_node_service.rb @@ -0,0 +1,63 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/node_service' + + # Reserve requested resources and deploy vApp + # + # ==== Parameters + # * service_id<~String> - Id of service to add node to + # * ip<~String> - Private ip of server to add to node + # * name<~String> - Name of service + # * port<~Integer> - Port of service + # * options<~Hash>: + # * Enabled<~Boolean>: defaults to true + # * Description<~String>: optional description + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'endTime'<~String> - endTime of task + # * 'href'<~String> - link to task + # * 'startTime'<~String> - startTime of task + # * 'status'<~String> - status of task + # * 'type'<~String> - type of task + # * 'Owner'<~String> - + # * 'href'<~String> - href of owner + # * 'name'<~String> - name of owner + # * 'type'<~String> - type of owner + def add_node_service(service_id, ip, name, port, options = {}) + unless options.has_key?('Enabled') + options['Enabled'] = true + end + data = <<-DATA + + #{ip} + #{name} + #{port} + #{options['Enabled']} + #{options['Description']} + +DATA + request( + :body => data, + :expects => 200, + :headers => {'Content-Type' => 'application/xml'}, + :method => 'POST', + :parser => Fog::Parsers::Terremark::InternetService.new, + :path => "internetServices/#{service_id}/nodes" + ) + end + + end + + class Mock + + def add_node_service(ip) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/create_internet_service.rb b/lib/fog/terremark/requests/create_internet_service.rb new file mode 100644 index 000000000..adfc39877 --- /dev/null +++ b/lib/fog/terremark/requests/create_internet_service.rb @@ -0,0 +1,63 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/internet_service' + + # Reserve requested resources and deploy vApp + # + # ==== Parameters + # * vdc_id<~Integer> - Id of vDc to add internet service to + # * name<~String> - Name of service + # * protocol<~String> - Protocol of service + # * port<~Integer> - Port of service + # * options<~Hash>: + # * Enabled<~Boolean>: defaults to true + # * Description<~String>: optional description + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'endTime'<~String> - endTime of task + # * 'href'<~String> - link to task + # * 'startTime'<~String> - startTime of task + # * 'status'<~String> - status of task + # * 'type'<~String> - type of task + # * 'Owner'<~String> - + # * 'href'<~String> - href of owner + # * 'name'<~String> - name of owner + # * 'type'<~String> - type of owner + def create_internet_service(vdc_id, name, protocol, port, options = {}) + unless options.has_key?('Enabled') + options['Enabled'] = true + end + data = <<-DATA + + #{name} + #{protocol.upcase} + #{port} + #{options['Enabled']} + #{options['Description']} + +DATA + request( + :body => data, + :expects => 200, + :headers => {'Content-Type' => 'application/xml'}, + :method => 'POST', + :parser => Fog::Parsers::Terremark::InternetService.new, + :path => "vdc/#{vdc_id}/internetServices" + ) + end + + end + + class Mock + + def create_internet_service(vdc_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/delete_internet_service.rb b/lib/fog/terremark/requests/delete_internet_service.rb new file mode 100644 index 000000000..0047553f7 --- /dev/null +++ b/lib/fog/terremark/requests/delete_internet_service.rb @@ -0,0 +1,40 @@ +module Fog + module Terremark + class Real + + # Destroy an internet service + # + # ==== Parameters + # * internet_service_id<~Integer> - Id of service to destroy + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + + # FIXME + + # * 'CatalogItems'<~Array> + # * 'href'<~String> - linke to item + # * 'name'<~String> - name of item + # * 'type'<~String> - type of item + # * 'description'<~String> - Description of catalog + # * 'name'<~String> - Name of catalog + def delete_internet_service(internet_service_id) + request( + :expects => 200, + :method => 'DELETE', + :path => "InternetServices/#{internet_service_id}" + ) + end + + end + + class Mock + + def delete_internet_service(internet_service_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/delete_node_service.rb b/lib/fog/terremark/requests/delete_node_service.rb new file mode 100644 index 000000000..e199132ad --- /dev/null +++ b/lib/fog/terremark/requests/delete_node_service.rb @@ -0,0 +1,40 @@ +module Fog + module Terremark + class Real + + # Destroy a node + # + # ==== Parameters + # * node_service_id<~Integer> - Id of node to destroy + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + + # FIXME + + # * 'CatalogItems'<~Array> + # * 'href'<~String> - linke to item + # * 'name'<~String> - name of item + # * 'type'<~String> - type of item + # * 'description'<~String> - Description of catalog + # * 'name'<~String> - Name of catalog + def delete_node_service(node_service_id) + request( + :expects => 200, + :method => 'DELETE', + :path => "nodeServices/#{node_service_id}" + ) + end + + end + + class Mock + + def delete_node_service(node_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/deploy_vapp.rb b/lib/fog/terremark/requests/deploy_vapp.rb index 7ec6512eb..e0fbc174c 100644 --- a/lib/fog/terremark/requests/deploy_vapp.rb +++ b/lib/fog/terremark/requests/deploy_vapp.rb @@ -26,7 +26,7 @@ module Fog :expects => 202, :method => 'POST', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/vApp/#{vapp_id}/action/deploy" + :path => "vApp/#{vapp_id}/action/deploy" ) end @@ -34,7 +34,7 @@ module Fog class Mock - def deploy_vapp(task_id) + def deploy_vapp(vapp_id) raise MockNotImplemented.new("Contributions welcome!") end diff --git a/lib/fog/terremark/requests/get_public_ips.rb b/lib/fog/terremark/requests/get_public_ips.rb new file mode 100644 index 000000000..5663905ce --- /dev/null +++ b/lib/fog/terremark/requests/get_public_ips.rb @@ -0,0 +1,37 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/get_public_ips' + + # Get list of public ips + # + # ==== Parameters + # * vdc_id<~Integer> - Id of vdc to find public ips for + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'PublicIpAddresses'<~Array> + # * 'href'<~String> - linke to item + # * 'name'<~String> - name of item + def get_public_ips(vdc_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Terremark::GetPublicIps.new, + :path => "vdc/#{vdc_id}/publicIps" + ) + end + + end + + class Mock + + def get_public_ips(vdc_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/get_task.rb b/lib/fog/terremark/requests/get_task.rb index ffa284016..38e0718e5 100644 --- a/lib/fog/terremark/requests/get_task.rb +++ b/lib/fog/terremark/requests/get_task.rb @@ -30,7 +30,7 @@ module Fog :expects => 200, :method => 'GET', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/task/#{task_id}" + :path => "task/#{task_id}" ) end diff --git a/lib/fog/terremark/requests/get_tasks_list.rb b/lib/fog/terremark/requests/get_tasks_list.rb index 1c56c4ff6..233c44636 100644 --- a/lib/fog/terremark/requests/get_tasks_list.rb +++ b/lib/fog/terremark/requests/get_tasks_list.rb @@ -23,7 +23,7 @@ module Fog :expects => 200, :method => 'GET', :parser => Fog::Parsers::Terremark::GetTasksList.new, - :path => "/tasksList/#{tasks_list_id}" + :path => "tasksList/#{tasks_list_id}" ) end diff --git a/lib/fog/terremark/requests/get_vapp.rb b/lib/fog/terremark/requests/get_vapp.rb index 5264d7422..648620e27 100644 --- a/lib/fog/terremark/requests/get_vapp.rb +++ b/lib/fog/terremark/requests/get_vapp.rb @@ -33,7 +33,7 @@ module Fog :expects => 200, :method => 'GET', :parser => Fog::Parsers::Terremark::Vapp.new, - :path => "/vapp/#{vapp_id}" + :path => "vapp/#{vapp_id}" ) end diff --git a/lib/fog/terremark/requests/instantiate_vapp_template.rb b/lib/fog/terremark/requests/instantiate_vapp_template.rb index b0671e51d..432835f42 100644 --- a/lib/fog/terremark/requests/instantiate_vapp_template.rb +++ b/lib/fog/terremark/requests/instantiate_vapp_template.rb @@ -23,13 +23,9 @@ module Fog # * 'name'<~String> - Name of catalog def instantiate_vapp_template(name) # FIXME: much cheating to commence - - organization_id = get_organizations.body['OrgList'].first['href'].split('/').last - organization = get_organization(organization_id).body - vdc_id = organization['Links'].select {|link| link['type'] == 'application/vnd.vmware.vcloud.vdc+xml'}.first['href'].split('/').last - vdc = get_vdc(vdc_id).body - network_id = vdc['AvailableNetworks'].first['href'].split('/').last - catalog_item = 12 # Ubuntu JeOS 9.10 (64-bit) + vdc_id = default_vdc_id + network_id = default_network_id + catalog_item = 12 # Ubuntu JeOS 9.10 (64-bit) # case UNRESOLVED: # return "0"; @@ -101,7 +97,7 @@ DATA :headers => { 'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml' }, :method => 'POST', :parser => Fog::Parsers::Terremark::InstantiateVappTemplate.new, - :path => "/vdc/#{vdc_id}/action/instantiatevAppTemplate" + :path => "vdc/#{vdc_id}/action/instantiatevAppTemplate" ) end diff --git a/lib/fog/terremark/requests/power_off.rb b/lib/fog/terremark/requests/power_off.rb index 1ae2da687..053978fc5 100644 --- a/lib/fog/terremark/requests/power_off.rb +++ b/lib/fog/terremark/requests/power_off.rb @@ -26,7 +26,7 @@ module Fog :expects => 202, :method => 'POST', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/vApp/#{vapp_id}/power/action/powerOff" + :path => "vApp/#{vapp_id}/power/action/powerOff" ) end diff --git a/lib/fog/terremark/requests/power_on.rb b/lib/fog/terremark/requests/power_on.rb index a2edc669a..9498b5ecf 100644 --- a/lib/fog/terremark/requests/power_on.rb +++ b/lib/fog/terremark/requests/power_on.rb @@ -26,7 +26,7 @@ module Fog :expects => 202, :method => 'POST', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/vApp/#{vapp_id}/power/action/powerOn" + :path => "vApp/#{vapp_id}/power/action/powerOn" ) end diff --git a/lib/fog/terremark/requests/reset.rb b/lib/fog/terremark/requests/reset.rb index 681e6ba72..4342e462c 100644 --- a/lib/fog/terremark/requests/reset.rb +++ b/lib/fog/terremark/requests/reset.rb @@ -26,7 +26,7 @@ module Fog :expects => 202, :method => 'POST', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/vApp/#{vapp_id}/power/action/reset" + :path => "vApp/#{vapp_id}/power/action/reset" ) end diff --git a/lib/fog/terremark/requests/shutdown.rb b/lib/fog/terremark/requests/shutdown.rb index 57c8c0294..e5211eeb1 100644 --- a/lib/fog/terremark/requests/shutdown.rb +++ b/lib/fog/terremark/requests/shutdown.rb @@ -26,7 +26,7 @@ module Fog :expects => 202, :method => 'POST', :parser => Fog::Parsers::Terremark::Task.new, - :path => "/vApp/#{vapp_id}/power/action/shutdown" + :path => "vApp/#{vapp_id}/power/action/shutdown" ) end