lib/fog/vcloud_director/requests/compute/get_vms_in_lease_from_query.rb
module Fog
module Compute
class VcloudDirector
class Real
# Retrieves a list of VMs in lease by using REST API general
# QueryHandler.
#
# @param [Hash] options
# @option options [String] :sortAsc (Sorted by database ID) Sort
# results by attribute-name in ascending order. attribute-name cannot
# include metadata.
# @option options [String] :sortDesc (Sorted by database ID) Sort
# results by attribute-name in descending order. attribute-name
# cannot include metadata.
# @option options [Integer] :page (1) If the query results span
# multiple pages, return this page.
# @option options [Integer] :pageSize (25) Number of results per page,
# to a maximum of 128.
# @option options [String] :format (records) One of the following
# types:
# - *references* Returns a reference to each object, including its
# :name, :type, and :href attributes.
# - *records* Returns all database records for each object, with each
# record as an attribute.
# - *idrecords* Identical to the records format, except that object
# references are returned in :id format rather than :href format.
# @option options [Array<String>] :fields (all static attribute names)
# List of attribute names or metadata key names to return.
# @option options [Integer] :offset (0) Integer value specifying the
# first record to return. Record numbers < offset are not returned.
# @option options [String] :filter (none) Filter expression.
# @return [Excon::Response]
# * hash<~Hash>:
# * :href<~String> - The URI of the entity.
# * :type<~String> - The MIME type of the entity.
# * :name<~String> - Query name that generated this result set.
# * :page<~String> - Page of the result set that this container
# holds. The first page is page number 1.
# * :pageSize<~String> - Page size, as a number of records or
# references.
# * :total<~String> - Total number of records or references in the
# container.
# * :Link<~Array<Hash>>:
# * :href<~String> - Contains the URI to the entity.
# * :type<~String> - Contains the type of the entity.
# * :rel<~String> - Defines the relationship of the link to the
# object that contains it.
# * :VMRecord<~Array<Hash>>:
# * TODO
# * :firstPage<~Integer> - First page in the result set.
# * :previousPage<~Integer> - Previous page in the result set.
# * :nextPage<~Integer> - Next page in the result set.
# * :lastPage<~Integer> - Last page in the result set.
#
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/GET-VMsInLeaseFromQuery.html
# @since vCloud API version 1.5
def get_vms_in_lease_from_query(options={})
query = []
query << "sortAsc=#{options[:sortAsc]}" if options[:sortAsc]
query << "sortDesc=#{options[:sortDesc]}" if options[:sortDesc]
query << "page=#{options[:page]}" if options[:page]
query << "pageSize=#{options[:pageSize]}" if options[:pageSize]
query << "format=#{options[:format]}" if options[:format]
query << "fields=#{Array(options[:fields]).join(',')}" if options[:fields]
query << "offset=#{options[:offset]}" if options[:offset]
query << "filter=#{options[:filter]}" if options[:filter]
response = request(
:expects => 200,
:idempotent => true,
:method => 'GET',
:parser => Fog::ToHashDocument.new,
:path => 'vms/query',
:query => query.map {|q| URI.escape(q)}.join('&')
)
ensure_list! response.body, :Link
ensure_list! response.body,
response.body[:type] == 'application/vnd.vmware.vcloud.query.references+xml' ?
:VMReference : :VMRecord
%w[firstPage previousPage nextPage lastPage].each do |rel|
if link = response.body[:Link].find {|l| l[:rel] == rel}
href = Nokogiri::XML.fragment(link[:href])
query = CGI.parse(URI.parse(href.text).query)
response.body[rel.to_sym] = query['page'].first.to_i
response.body[:pageSize] ||= query['pageSize'].first.to_i
end
end
response
end
end
class Mock
def get_vms_in_lease_from_query(options={})
if options.key?(:filter) && options[:filter] =~ /^href==([^;,]+)$/
href = $1
id = href.split('/').last
else
Fog::Mock.not_implemented("Filter by href is currently the only option implemented in get_vms_in_lease_from_query Mock")
end
vm = data[:vms][id]
parent_vapp_id = vm[:parent_vapp]
vdc_id = data[:vapps][parent_vapp_id][:vdc_id]
body = {
:xmlns=>xmlns,
:xmlns_xsi=>xmlns_xsi,
:href=>make_href('query'),
:name=>"vm",
:type=>"application/vnd.vmware.vcloud.query.records+xml",
:xsi_schemaLocation=>xsi_schema_location,
:total=>"1",
:pageSize=>"25",
:page=>"1",
:Link=>
[{:rel=>"alternate",
:type=>"application/vnd.vmware.vcloud.query.references+xml",
:href=>make_href('query')},
{:rel=>"alternate",
:type=>"application/vnd.vmware.vcloud.query.idrecords+xml",
:href=>make_href('query')}],
:VMRecord=>
[{:vdc=>make_href(vdc_id),
:numberOfCpus=>vm[:cpu_count],
:name=>vm[:name],
:containerName=>data[:vapps][parent_vapp_id][:name],
:memoryMB=>vm[:memory_in_mb],
:isVAppTemplate=>"false",
:href=>make_href(id),
:taskStatus=>"success",
:task=>make_href("task/#{uuid}"),
:taskDetails=>" ",
:taskStatusName=>"vappUpdateVm"}]
}
Excon::Response.new(
:status => 200,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:body => body
)
end
end
end
end
end