diff --git a/lib/fog/terremark.rb b/lib/fog/terremark.rb index e25fcd0d6..dd2c31d52 100644 --- a/lib/fog/terremark.rb +++ b/lib/fog/terremark.rb @@ -1,9 +1,19 @@ +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_task' +require 'fog/terremark/requests/get_tasks_list' +require 'fog/terremark/requests/get_vapp' require 'fog/terremark/requests/get_vapp_template' require 'fog/terremark/requests/get_vdc' +require 'fog/terremark/requests/instantiate_vapp_template' +require 'fog/terremark/requests/reset' +require 'fog/terremark/requests/power_off' +require 'fog/terremark/requests/power_on' +require 'fog/terremark/requests/shutdown' module Fog module Terremark diff --git a/lib/fog/terremark/parsers/get_catalog.rb b/lib/fog/terremark/parsers/get_catalog.rb index 4a36e4daa..ff6e000f8 100644 --- a/lib/fog/terremark/parsers/get_catalog.rb +++ b/lib/fog/terremark/parsers/get_catalog.rb @@ -21,7 +21,8 @@ module Fog catalog = {} until attributes.empty? if attributes.first.is_a?(Array) - catalog[attributes.first.first] = attributes.shift.last + attribute = attributes.shift + catalog[attribute.first] = attribute.last else catalog[attributes.shift] = attributes.shift end diff --git a/lib/fog/terremark/parsers/get_catalog_item.rb b/lib/fog/terremark/parsers/get_catalog_item.rb index 4fec18bf5..82ed775b9 100644 --- a/lib/fog/terremark/parsers/get_catalog_item.rb +++ b/lib/fog/terremark/parsers/get_catalog_item.rb @@ -5,7 +5,6 @@ module Fog class GetCatalogItem < Fog::Parsers::Base def reset - @property_key @response = { 'Entity' => {}, 'Properties' => {} } end @@ -20,7 +19,8 @@ module Fog catalog_item = {} until attributes.empty? if attributes.first.is_a?(Array) - catalog_item[attributes.first.first] = attributes.shift.last + attribute = attributes.shift + catalog_item[attribute.first] = attribute.last else catalog_item[attributes.shift] = attributes.shift end diff --git a/lib/fog/terremark/parsers/get_organization.rb b/lib/fog/terremark/parsers/get_organization.rb index d8036d704..8e6894815 100644 --- a/lib/fog/terremark/parsers/get_organization.rb +++ b/lib/fog/terremark/parsers/get_organization.rb @@ -21,7 +21,8 @@ module Fog org = {} until attributes.empty? if attributes.first.is_a?(Array) - org[attributes.first.first] = attributes.shift.last + attribute = attributes.shift + org[attribute.first] = attribute.last else org[attributes.shift] = attributes.shift end diff --git a/lib/fog/terremark/parsers/get_tasks_list.rb b/lib/fog/terremark/parsers/get_tasks_list.rb new file mode 100644 index 000000000..65e45fd0f --- /dev/null +++ b/lib/fog/terremark/parsers/get_tasks_list.rb @@ -0,0 +1,50 @@ +module Fog + module Parsers + module Terremark + + class GetTasksList < Fog::Parsers::Base + + def reset + @response = { 'Tasks' => [] } + @task = {} + end + + def start_element(name, attributes) + @value = '' + case name + when 'Owner', 'Result' + data = {} + until attributes.empty? + data[attributes.shift] = attributes.shift + end + @task[name] = data + when 'Task' + until attributes.empty? + @task[attributes.shift] = attributes.shift + end + when 'TasksList' + tasks_list = {} + until attributes.empty? + if attributes.first.is_a?(Array) + attribute = attributes.shift + tasks_list[attribute.first] = attribute.last + else + tasks_list[attributes.shift] = attributes.shift + end + end + @response['href'] = tasks_list['href'] + end + end + + def end_element(name) + if name == 'Task' + @response['Tasks'] << @task + @task = {} + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/parsers/get_vapp_template.rb b/lib/fog/terremark/parsers/get_vapp_template.rb index 55d54f1cd..af6a80a64 100644 --- a/lib/fog/terremark/parsers/get_vapp_template.rb +++ b/lib/fog/terremark/parsers/get_vapp_template.rb @@ -5,7 +5,6 @@ module Fog class GetVappTemplate < Fog::Parsers::Base def reset - @property_key @response = { 'Links' => [] } end @@ -22,7 +21,8 @@ module Fog vapp_template = {} until attributes.empty? if attributes.first.is_a?(Array) - vapp_template[attributes.first.first] = attributes.shift.last + attribute = attributes.shift + vapp_template[attribute.first] = attribute.last else vapp_template[attributes.shift] = attributes.shift end diff --git a/lib/fog/terremark/parsers/get_vdc.rb b/lib/fog/terremark/parsers/get_vdc.rb index b78f02972..eb887e236 100644 --- a/lib/fog/terremark/parsers/get_vdc.rb +++ b/lib/fog/terremark/parsers/get_vdc.rb @@ -20,7 +20,7 @@ module Fog 'Memory' => {} }, 'StorageCapacity' => {}, - 'ResourceEntities' => {} + 'ResourceEntities' => [] } end @@ -52,14 +52,15 @@ module Fog until attributes.empty? resource_entity[attributes.shift] = attributes.shift end - @response['ResourceEntity'] << resource_entity + @response['ResourceEntities'] << resource_entity when 'StorageCapacity' @in_storage_capacity = true when 'Vdc' vdc = {} until attributes.empty? if attributes.first.is_a?(Array) - vdc[attributes.first.first] = attributes.shift.last + attribute = attributes.shift + vdc[attribute.first] = attribute.last else vdc[attributes.shift] = attributes.shift end diff --git a/lib/fog/terremark/parsers/instantiate_vapp_template.rb b/lib/fog/terremark/parsers/instantiate_vapp_template.rb new file mode 100644 index 000000000..b7d412c70 --- /dev/null +++ b/lib/fog/terremark/parsers/instantiate_vapp_template.rb @@ -0,0 +1,39 @@ +module Fog + module Parsers + module Terremark + + class InstantiateVappTemplate < Fog::Parsers::Base + + def reset + @property_key + @response = { 'Links' => [] } + end + + def start_element(name, attributes) + @value = '' + case name + when 'Link' + link = {} + until attributes.empty? + link[attributes.shift] = attributes.shift + end + @response['Links'] << link + when 'VApp' + vapp_template = {} + until attributes.empty? + if attributes.first.is_a?(Array) + attribute = attributes.shift + vapp_template[attribute.first] = attribute.last + else + vapp_template[attributes.shift] = attributes.shift + end + end + @response.merge!(vapp_template.reject {|key, value| !['href', 'name', 'size', 'status', 'type'].include?(key)}) + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/parsers/task.rb b/lib/fog/terremark/parsers/task.rb new file mode 100644 index 000000000..f7a19e73b --- /dev/null +++ b/lib/fog/terremark/parsers/task.rb @@ -0,0 +1,38 @@ +module Fog + module Parsers + module Terremark + + class Task < Fog::Parsers::Base + + def reset + @response = {} + end + + def start_element(name, attributes) + @value = '' + case name + when 'Owner', 'Result' + data = {} + until attributes.empty? + data[attributes.shift] = attributes.shift + end + @response[name] = data + when 'Task' + task = {} + until attributes.empty? + if attributes.first.is_a?(Array) + attribute = attributes.shift + task[attribute.first] = attribute.last + else + task[attributes.shift] = attributes.shift + end + end + @response.merge!(task.reject {|key,value| !['endTime', 'href', 'startTime', 'status', 'type'].include?(key)}) + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/parsers/vapp.rb b/lib/fog/terremark/parsers/vapp.rb new file mode 100644 index 000000000..94f498cd4 --- /dev/null +++ b/lib/fog/terremark/parsers/vapp.rb @@ -0,0 +1,45 @@ +module Fog + module Parsers + module Terremark + + class Vapp < Fog::Parsers::Base + + def reset + @response = { 'Links' => [] } + end + + def start_element(name, attributes) + @value = '' + case name + when 'Link' + link = {} + until attributes.empty? + link[attributes.shift] = attributes.shift + end + @response['Links'] << link + when 'Vapp' + vapp = {} + until attributes.empty? + if attributes.first.is_a?(Array) + attribute = attributes.shift + vapp[attribute.first] = attribute.last + else + vapp[attributes.shift] = attributes.shift + end + end + @response.merge!(task.reject {|key,value| !['href', 'name', 'size', 'status', 'type'].include?(key)}) + end + end + + def end_element(name) + case name + when 'IpAddress' + @response['IpAddress'] = @value + end + end + + end + + end + end +end diff --git a/lib/fog/terremark/requests/delete_vapp.rb b/lib/fog/terremark/requests/delete_vapp.rb new file mode 100644 index 000000000..c5c4c1f95 --- /dev/null +++ b/lib/fog/terremark/requests/delete_vapp.rb @@ -0,0 +1,40 @@ +module Fog + module Terremark + class Real + + # Destroy a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp 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_vapp(vapp_id) + request( + :expects => 202, + :method => 'DELETE', + :path => "vApp/#{vapp_id}" + ) + end + + end + + class Mock + + def delete_vapp(vapp_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 new file mode 100644 index 000000000..7ec6512eb --- /dev/null +++ b/lib/fog/terremark/requests/deploy_vapp.rb @@ -0,0 +1,43 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Reserve requested resources and deploy vApp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vApp to deploy + # + # ==== 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 deploy_vapp(vapp_id) + request( + :expects => 202, + :method => 'POST', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/vApp/#{vapp_id}/action/deploy" + ) + end + + end + + class Mock + + def deploy_vapp(task_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/get_organizations.rb b/lib/fog/terremark/requests/get_organizations.rb index 60daa336b..4fe613b28 100644 --- a/lib/fog/terremark/requests/get_organizations.rb +++ b/lib/fog/terremark/requests/get_organizations.rb @@ -14,7 +14,6 @@ module Fog # * 'name'<~String> - Name of organization def get_organizations request({ - :body => '', :expects => 200, :headers => { 'Authorization' => "Basic #{Base64.encode64("#{@terremark_username}:#{@terremark_password}").chomp!}" diff --git a/lib/fog/terremark/requests/get_task.rb b/lib/fog/terremark/requests/get_task.rb new file mode 100644 index 000000000..ffa284016 --- /dev/null +++ b/lib/fog/terremark/requests/get_task.rb @@ -0,0 +1,47 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Get details of a task + # + # ==== Parameters + # * task_id<~Integer> - Id of task to lookup + # + # ==== 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 + # * 'Result'<~String> - + # * 'href'<~String> - href of result + # * 'name'<~String> - name of result + # * 'type'<~String> - type of result + def get_task(task_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/task/#{task_id}" + ) + end + + end + + class Mock + + def get_task(task_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/get_tasks_list.rb b/lib/fog/terremark/requests/get_tasks_list.rb new file mode 100644 index 000000000..1c56c4ff6 --- /dev/null +++ b/lib/fog/terremark/requests/get_tasks_list.rb @@ -0,0 +1,40 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/get_tasks_list' + + # Get list of tasks + # + # ==== Parameters + # * tasks_list_id<~Integer> - Id of tasks lists to view + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * '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 get_tasks_list(tasks_list_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Terremark::GetTasksList.new, + :path => "/tasksList/#{tasks_list_id}" + ) + end + + end + + class Mock + + def get_tasks_list(tasks_list_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/get_vapp.rb b/lib/fog/terremark/requests/get_vapp.rb new file mode 100644 index 000000000..5264d7422 --- /dev/null +++ b/lib/fog/terremark/requests/get_vapp.rb @@ -0,0 +1,50 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/vapp' + + # Get details of a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp to lookup + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + + # FIXME + + # * '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 + # * 'Result'<~String> - + # * 'href'<~String> - href of result + # * 'name'<~String> - name of result + # * 'type'<~String> - type of result + def get_vapp(vapp_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Terremark::Vapp.new, + :path => "/vapp/#{vapp_id}" + ) + end + + end + + class Mock + + def get_vapp(vapp_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/instantiate_vapp_template.rb b/lib/fog/terremark/requests/instantiate_vapp_template.rb new file mode 100644 index 000000000..b0671e51d --- /dev/null +++ b/lib/fog/terremark/requests/instantiate_vapp_template.rb @@ -0,0 +1,118 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/instantiate_vapp_template' + + # Instatiate a vapp template + # + # ==== Parameters + # * vdc_id<~Integer> - Id of vdc to instantiate template in + # + # ==== 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 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) + + # case UNRESOLVED: + # return "0"; + # case RESOLVED: + # return "1"; + # case OFF: + # return "2"; + # case SUSPENDED: + # return "3"; + # case ON: + # return "4"; + # default: + # + # /** + # * The vApp is unresolved (one or more file references are unavailable in the cloud) + # */ + # UNRESOLVED, + # /** + # * The vApp is resolved (all file references are available in the cloud) but not deployed + # */ + # RESOLVED, + # /** + # * The vApp is deployed and powered off + # */ + # OFF, + # /** + # * The vApp is deployed and suspended + # */ + # SUSPENDED, + # /** + # * The vApp is deployed and powered on + # */ + # ON; + + data = <<-DATA + + + + + + + + 1 + 3 + 1 + + + 2 + 4 + 512 + + + + + + allowInOut + true + + + + + + +DATA + + request( + :body => data, + :expects => 200, + :headers => { 'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml' }, + :method => 'POST', + :parser => Fog::Parsers::Terremark::InstantiateVappTemplate.new, + :path => "/vdc/#{vdc_id}/action/instantiatevAppTemplate" + ) + end + + end + + class Mock + + def instatiate_vapp_template(vapp_template_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/power_off.rb b/lib/fog/terremark/requests/power_off.rb new file mode 100644 index 000000000..1ae2da687 --- /dev/null +++ b/lib/fog/terremark/requests/power_off.rb @@ -0,0 +1,43 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Power off a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp to power off + # + # ==== 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 power_off(vapp_id) + request( + :expects => 202, + :method => 'POST', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/vApp/#{vapp_id}/power/action/powerOff" + ) + end + + end + + class Mock + + def power_off(vapp_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/power_on.rb b/lib/fog/terremark/requests/power_on.rb new file mode 100644 index 000000000..a2edc669a --- /dev/null +++ b/lib/fog/terremark/requests/power_on.rb @@ -0,0 +1,43 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Power on a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp to power on + # + # ==== 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 power_on(vapp_id) + request( + :expects => 202, + :method => 'POST', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/vApp/#{vapp_id}/power/action/powerOn" + ) + end + + end + + class Mock + + def power_on(vapp_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/reset.rb b/lib/fog/terremark/requests/reset.rb new file mode 100644 index 000000000..681e6ba72 --- /dev/null +++ b/lib/fog/terremark/requests/reset.rb @@ -0,0 +1,43 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Reset a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp to reset + # + # ==== 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 reset(vapp_id) + request( + :expects => 202, + :method => 'POST', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/vApp/#{vapp_id}/power/action/reset" + ) + end + + end + + class Mock + + def reset(vapp_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end diff --git a/lib/fog/terremark/requests/shutdown.rb b/lib/fog/terremark/requests/shutdown.rb new file mode 100644 index 000000000..57c8c0294 --- /dev/null +++ b/lib/fog/terremark/requests/shutdown.rb @@ -0,0 +1,43 @@ +module Fog + module Terremark + class Real + + require 'fog/terremark/parsers/task' + + # Shutdown a vapp + # + # ==== Parameters + # * vapp_id<~Integer> - Id of vapp to shutdown + # + # ==== 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 shutdown(vapp_id) + request( + :expects => 202, + :method => 'POST', + :parser => Fog::Parsers::Terremark::Task.new, + :path => "/vApp/#{vapp_id}/power/action/shutdown" + ) + end + + end + + class Mock + + def shutdown(vapp_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end +end