From ca4561e990fd3cdda008cb0f8466d477a2f2ce0d Mon Sep 17 00:00:00 2001 From: Nick Osborn Date: Mon, 14 Oct 2013 00:50:11 +0100 Subject: [PATCH] [vcloud_director] Improve support for query service. --- lib/fog/vcloud_director/compute.rb | 7 +- .../compute/get_catalogs_from_query.rb | 59 +++++++++++++-- .../requests/compute/get_disks_from_query.rb | 59 +++++++++++++-- .../requests/compute/get_groups_from_query.rb | 73 +++++++++++++++++++ .../requests/compute/get_medias_from_query.rb | 59 +++++++++++++-- .../compute/get_organizations_from_query.rb | 73 +++++++++++++++++++ .../requests/compute/get_users_from_query.rb | 73 +++++++++++++++++++ .../compute/get_vapp_templates_from_query.rb | 58 +++++++++++++-- .../compute/get_vapps_in_lease_from_query.rb | 59 +++++++++++++-- .../requests/compute/get_vdcs_from_query.rb | 72 ++++++++++++++++++ .../compute/get_vms_in_lease_from_query.rb | 58 +++++++++++++-- .../requests/compute/catalog_tests.rb | 4 +- .../requests/compute/disk_tests.rb | 15 ++++ .../requests/compute/groups_tests.rb | 10 +++ .../requests/compute/media_tests.rb | 4 +- .../requests/compute/organization_tests.rb | 5 ++ .../requests/compute/schema_helper.rb | 19 ++--- .../requests/compute/users_tests.rb | 10 +++ .../requests/compute/vapp_tests.rb | 4 +- .../requests/compute/vdc_tests.rb | 7 +- .../requests/compute/vm_tests.rb | 4 +- 21 files changed, 673 insertions(+), 59 deletions(-) create mode 100644 lib/fog/vcloud_director/requests/compute/get_groups_from_query.rb create mode 100644 lib/fog/vcloud_director/requests/compute/get_organizations_from_query.rb create mode 100644 lib/fog/vcloud_director/requests/compute/get_users_from_query.rb create mode 100644 lib/fog/vcloud_director/requests/compute/get_vdcs_from_query.rb create mode 100644 tests/vcloud_director/requests/compute/disk_tests.rb create mode 100644 tests/vcloud_director/requests/compute/groups_tests.rb create mode 100644 tests/vcloud_director/requests/compute/users_tests.rb diff --git a/lib/fog/vcloud_director/compute.rb b/lib/fog/vcloud_director/compute.rb index 5c3f5733e..379e7f842 100644 --- a/lib/fog/vcloud_director/compute.rb +++ b/lib/fog/vcloud_director/compute.rb @@ -105,6 +105,7 @@ module Fog request :get_disks_rasd_items_list request :get_edge_gateway request :get_entity + request :get_groups_from_query request :get_guest_customization_system_section_vapp request :get_guest_customization_system_section_vapp_template request :get_href # this is used for manual testing @@ -134,6 +135,7 @@ module Fog request :get_organization_metadata request :get_organization_metadata_item_metadata request :get_organizations + request :get_organizations_from_query request :get_product_sections_vapp request :get_product_sections_vapp_template request :get_request # this is used for manual testing @@ -147,6 +149,7 @@ module Fog request :get_task request :get_task_list request :get_thumbnail + request :get_users_from_query request :get_vapp request :get_vapp_metadata request :get_vapp_metadata_item_metadata @@ -166,6 +169,7 @@ module Fog request :get_vdc_storage_class request :get_vdc_storage_class_metadata request :get_vdc_storage_class_metadata_item_metadata + request :get_vdcs_from_query request :get_virtual_hardware_section request :get_vm request :get_vm_capabilities @@ -378,7 +382,8 @@ module Fog :idempotent => params[:idempotent], :method => params[:method], :parser => params[:parser], - :path => path + :path => path, + :query => params[:query] }) rescue => e raise e unless e.class.to_s =~ /^Excon::Errors/ diff --git a/lib/fog/vcloud_director/requests/compute/get_catalogs_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_catalogs_from_query.rb index 8170092b4..b0a19fb24 100644 --- a/lib/fog/vcloud_director/requests/compute/get_catalogs_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_catalogs_from_query.rb @@ -2,24 +2,69 @@ module Fog module Compute class VcloudDirector class Real - # Retrieves a list of Catalogs by using REST API general QueryHandler; - # If filter is provided it will be applied to the corresponding result - # set. Format determines the elements representation - references or - # records. Default format is references. + # Retrieves a list of Catalogs 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :CatalogRecord<~Array>: + # * 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-CatalogsFromQuery.html # @since vCloud API version 1.5 - def get_catalogs_from_query - request( + def get_catalogs_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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'catalogs/query' + :path => 'catalogs/query', + :query => query ) + response.body[:CatalogRecord] = [response.body[:CatalogRecord]] if response.body[:CatalogRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/lib/fog/vcloud_director/requests/compute/get_disks_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_disks_from_query.rb index 6345401e9..0e309ef15 100644 --- a/lib/fog/vcloud_director/requests/compute/get_disks_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_disks_from_query.rb @@ -2,24 +2,69 @@ module Fog module Compute class VcloudDirector class Real - # Retrieves a disk list by using REST API general QueryHandler; If - # filter is provided it will be applied to the corresponding result - # set. Format determines the elements representation - references or - # records. Default format is references. + # Retrieves a disk list 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :DiskRecord<~Array>: + # * 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-DisksFromQuery.html # @since vCloud API version 1.5 - def get_disks_from_query - request( + def get_disks_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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'disks/query' + :path => 'disks/query', + :query => query ) + response.body[:DiskRecord] = [response.body[:DiskRecord]] if response.body[:DiskRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/lib/fog/vcloud_director/requests/compute/get_groups_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_groups_from_query.rb new file mode 100644 index 000000000..ee4528f4c --- /dev/null +++ b/lib/fog/vcloud_director/requests/compute/get_groups_from_query.rb @@ -0,0 +1,73 @@ +module Fog + module Compute + class VcloudDirector + class Real + # Retrieves a list of groups for organization the org admin belongs to + # 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. + # @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. + # * :GroupRecord<~Array>: + # * 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-GroupsFromQuery.html + # @since vCloud API version 1.5 + def get_groups_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[:offset] = options[:offset] if options[:offset] + + response = request( + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::ToHashDocument.new, + :path => 'admin/groups/query', + :query => query + ) + response.body[:GroupRecord] = [response.body[:GroupRecord]] if response.body[:GroupRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 + end + end +end diff --git a/lib/fog/vcloud_director/requests/compute/get_medias_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_medias_from_query.rb index aaee993cf..76edc000a 100644 --- a/lib/fog/vcloud_director/requests/compute/get_medias_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_medias_from_query.rb @@ -2,24 +2,69 @@ module Fog module Compute class VcloudDirector class Real - # Retrieves a media list by using REST API general QueryHandler; If - # filter is provided it will be applied to the corresponding result - # set. Format determines the elements representation - references or - # records. Default format is references. + # Retrieves a media list 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :MediaRecord<~Array>: + # * 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-MediasFromQuery.html # @since vCloud API version 1.5 - def get_medias_from_query - request( + def get_medias_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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'mediaList/query' + :path => 'mediaList/query', + :query => query ) + response.body[:MediaRecord] = [response.body[:MediaRecord]] if response.body[:MediaRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/lib/fog/vcloud_director/requests/compute/get_organizations_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_organizations_from_query.rb new file mode 100644 index 000000000..a34a4b910 --- /dev/null +++ b/lib/fog/vcloud_director/requests/compute/get_organizations_from_query.rb @@ -0,0 +1,73 @@ +module Fog + module Compute + class VcloudDirector + class Real + # Retrieves a list of organizations 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. + # @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. + # * :OrganizationRecord<~Array>: + # * 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-OrganizationsFromQuery.html + # @since vCloud API version 1.5 + def get_organizations_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[:offset] = options[:offset] if options[:offset] + + response = request( + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::ToHashDocument.new, + :path => 'admin/orgs/query', + :query => query + ) + response.body[:OrganizationRecord] = [response.body[:OrganizationRecord]] if response.body[:OrganizationRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 + end + end +end diff --git a/lib/fog/vcloud_director/requests/compute/get_users_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_users_from_query.rb new file mode 100644 index 000000000..767f92d75 --- /dev/null +++ b/lib/fog/vcloud_director/requests/compute/get_users_from_query.rb @@ -0,0 +1,73 @@ +module Fog + module Compute + class VcloudDirector + class Real + # Retrieves a list of users for organization the org admin belongs to + # 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. + # @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. + # * :UserRecord<~Array>: + # * 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-UsersFromQuery-query.html + # @since vCloud API version 1.5 + def get_users_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[:offset] = options[:offset] if options[:offset] + + response = request( + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::ToHashDocument.new, + :path => 'admin/users/query', + :query => query + ) + response.body[:UserRecord] = [response.body[:UserRecord]] if response.body[:UserRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 + end + end +end diff --git a/lib/fog/vcloud_director/requests/compute/get_vapp_templates_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_vapp_templates_from_query.rb index c7cd30c09..0315c0c95 100644 --- a/lib/fog/vcloud_director/requests/compute/get_vapp_templates_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_vapp_templates_from_query.rb @@ -3,23 +3,69 @@ module Fog class VcloudDirector class Real # Retrieves a list of vAppTemplates using REST API general - # QueryHandler; If filter is provided it will be applied to the - # corresponding result set. Format determines the elements - # representation - references or records. Default format is references. + # 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :VAppTemplateRecord<~Array>: + # * 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-VAppTemplatesFromQuery.html # @since vCloud API version 1.5 - def get_vapp_templates_from_query - request( + def get_vapp_templates_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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'vAppTemplates/query' + :path => 'vAppTemplates/query', + :query => query ) + response.body[:VAppTemplateRecord] = [response.body[:VAppTemplateRecord]] if response.body[:VAppTemplateRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/lib/fog/vcloud_director/requests/compute/get_vapps_in_lease_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_vapps_in_lease_from_query.rb index aeddf2423..01a7b081e 100644 --- a/lib/fog/vcloud_director/requests/compute/get_vapps_in_lease_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_vapps_in_lease_from_query.rb @@ -2,24 +2,69 @@ module Fog module Compute class VcloudDirector class Real - # Retrieves a list of vApps by using REST API general QueryHandler; If - # filter is provided it will be applied to the corresponding result - # set. Format determines the elements representation - references or - # records. Default format is references. + # Retrieves a list of vApps 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :VAppRecord<~Array>: + # * 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-VAppsInLeaseFromQuery.html # @since vCloud API version 1.5 - def get_vapps_in_lease_from_query - request( + def get_vapps_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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'vApps/query' + :path => 'vApps/query', + :query => query ) + response.body[:VAppRecord] = [response.body[:VAppRecord]] if response.body[:VAppRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/lib/fog/vcloud_director/requests/compute/get_vdcs_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_vdcs_from_query.rb new file mode 100644 index 000000000..29be3b4ad --- /dev/null +++ b/lib/fog/vcloud_director/requests/compute/get_vdcs_from_query.rb @@ -0,0 +1,72 @@ +module Fog + module Compute + class VcloudDirector + class Real + # Retrieves a list of vdcs in the organization. + # + # @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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. + # @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. + # * :OrgVdcRecord<~Array>: + # * 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-55/topic/com.vmware.vcloud.api.reference.doc_55/doc/operations/GET-RightsFromQuery-query.html + # @since vCloud API version 1.5 + def get_vdcs_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[:offset] = options[:offset] if options[:offset] + + response = request( + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::ToHashDocument.new, + :path => 'admin/vdcs/query', + :query => query + ) + response.body[:OrgVdcRecord] = [response.body[:OrgVdcRecord]] if response.body[:OrgVdcRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 + end + end +end diff --git a/lib/fog/vcloud_director/requests/compute/get_vms_in_lease_from_query.rb b/lib/fog/vcloud_director/requests/compute/get_vms_in_lease_from_query.rb index f8f7dee15..0c0f3d23f 100644 --- a/lib/fog/vcloud_director/requests/compute/get_vms_in_lease_from_query.rb +++ b/lib/fog/vcloud_director/requests/compute/get_vms_in_lease_from_query.rb @@ -3,23 +3,69 @@ module Fog class VcloudDirector class Real # Retrieves a list of VMs in lease by using REST API general - # QueryHandler; If filter is provided it will be applied to the - # corresponding result set. Format determines the elements - # representation - references or records. Default format is references. + # 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 [Integer] :offset (0) Integer value specifying the + # first record to return. Record numbers < offset are not returned. # @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. + # * :VMRecord<~Array>: + # * 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 - request( + 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[:offset] = options[:offset] if options[:offset] + + response = request( :expects => 200, :idempotent => true, :method => 'GET', :parser => Fog::ToHashDocument.new, - :path => 'vms/query' + :path => 'vms/query', + :query => query ) + response.body[:VMRecord] = [response.body[:VMRecord]] if response.body[:VMRecord].is_a?(Hash) + + %w[firstPage previousPage nextPage lastPage].each do |rel| + if link = response.body[:Link].detect {|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 end diff --git a/tests/vcloud_director/requests/compute/catalog_tests.rb b/tests/vcloud_director/requests/compute/catalog_tests.rb index 2adc97808..afefefb3f 100644 --- a/tests/vcloud_director/requests/compute/catalog_tests.rb +++ b/tests/vcloud_director/requests/compute/catalog_tests.rb @@ -22,9 +22,9 @@ Shindo.tests('Compute::VcloudDirector | catalog requests', ['vclouddirector']) d @service.get_control_access_params_catalog(@org[:href].split('/').last, @catalog_id).body end - tests('#get_catalogs_from_query').returns(Hash) do + tests('#get_catalogs_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do pending if Fog.mocking? - @service.get_catalogs_from_query.body.class + @service.get_catalogs_from_query.body end tests('Retrieve non-existent Catalog').raises(Excon::Errors::Forbidden) do diff --git a/tests/vcloud_director/requests/compute/disk_tests.rb b/tests/vcloud_director/requests/compute/disk_tests.rb new file mode 100644 index 000000000..2f7bed714 --- /dev/null +++ b/tests/vcloud_director/requests/compute/disk_tests.rb @@ -0,0 +1,15 @@ +Shindo.tests('Compute::VcloudDirector | disk requests', ['vclouddirector']) do + + @service = Fog::Compute::VcloudDirector.new + + tests('#get_disks_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do + pending if Fog.mocking? + @service.get_disks_from_query.body + end + + tests('Retrieve non-existent Disk').raises(Excon::Errors::Forbidden) do + pending if Fog.mocking? + @service.get_disk('00000000-0000-0000-0000-000000000000') + end + +end diff --git a/tests/vcloud_director/requests/compute/groups_tests.rb b/tests/vcloud_director/requests/compute/groups_tests.rb new file mode 100644 index 000000000..f4ed323fb --- /dev/null +++ b/tests/vcloud_director/requests/compute/groups_tests.rb @@ -0,0 +1,10 @@ +Shindo.tests('Compute::VcloudDirector | groups requests', ['vclouddirector']) do + + @service = Fog::Compute::VcloudDirector.new + + tests('#get_groups_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do + pending if Fog.mocking? + @service.get_groups_from_query.body + end + +end diff --git a/tests/vcloud_director/requests/compute/media_tests.rb b/tests/vcloud_director/requests/compute/media_tests.rb index d0714c6ea..96f163172 100644 --- a/tests/vcloud_director/requests/compute/media_tests.rb +++ b/tests/vcloud_director/requests/compute/media_tests.rb @@ -153,9 +153,9 @@ Shindo.tests('Compute::VcloudDirector | media requests', ['vclouddirector']) do end end - tests('#get_medias_from_query').returns(Hash) do + tests('#get_medias_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do pending if Fog.mocking? - @service.get_medias_from_query.body.class + @service.get_medias_from_query.body end tests('Upload to non-existent vDC').raises(Excon::Errors::Forbidden) do diff --git a/tests/vcloud_director/requests/compute/organization_tests.rb b/tests/vcloud_director/requests/compute/organization_tests.rb index 26829eea6..bc8ca62da 100644 --- a/tests/vcloud_director/requests/compute/organization_tests.rb +++ b/tests/vcloud_director/requests/compute/organization_tests.rb @@ -17,6 +17,11 @@ Shindo.tests('Compute::VcloudDirector | organization requests', ['vclouddirector @service.get_organization_metadata(@org_uuid).body end + tests('#get_organizations_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do + pending if Fog.mocking? + @service.get_organizations_from_query.body + end + tests('retrieve non-existent Org').raises(Excon::Errors::Forbidden) do @service.get_organization('00000000-0000-0000-0000-000000000000') end diff --git a/tests/vcloud_director/requests/compute/schema_helper.rb b/tests/vcloud_director/requests/compute/schema_helper.rb index 7aa669f2a..b40899964 100644 --- a/tests/vcloud_director/requests/compute/schema_helper.rb +++ b/tests/vcloud_director/requests/compute/schema_helper.rb @@ -84,6 +84,14 @@ class VcloudDirector :File => FILE_TYPE } + # Container for query result sets. + CONTAINER_TYPE = RESOURCE_TYPE.merge({ + :name => String, + :page => String, + :pageSize => String, + :total => String + }) + # Represents an asynchronous operation in vCloud Director. TASK_TYPE = ENTITY_TYPE.merge({ :cancelRequested => Fog::Nullable::String, @@ -348,18 +356,11 @@ class VcloudDirector # Container for query results in records format. # Combine with QUERY_RESULT_RECORD_TYPE subtypes to validate query results - QUERY_RESULT_RECORDS_TYPE = { - :href => Fog::Nullable::String, - :type => Fog::Nullable::String, - :name => Fog::Nullable::String, - :page => Fog::Nullable::String, - :pageSize => Fog::Nullable::String, - :total => Fog::Nullable::String, - } + QUERY_RESULT_RECORDS_TYPE = CONTAINER_TYPE # Base type for a single record from query result in records format. # Subtypes define more specific elements. - QUERY_RESULT_RECORD_TYPE = { + QUERY_RESULT_RECORD_TYPE = { :href => String, :id => Fog::Nullable::String, :type => Fog::Nullable::String diff --git a/tests/vcloud_director/requests/compute/users_tests.rb b/tests/vcloud_director/requests/compute/users_tests.rb new file mode 100644 index 000000000..a69c91b03 --- /dev/null +++ b/tests/vcloud_director/requests/compute/users_tests.rb @@ -0,0 +1,10 @@ +Shindo.tests('Compute::VcloudDirector | users requests', ['vclouddirector']) do + + @service = Fog::Compute::VcloudDirector.new + + tests('#get_users_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do + pending if Fog.mocking? + @service.get_users_from_query.body + end + +end diff --git a/tests/vcloud_director/requests/compute/vapp_tests.rb b/tests/vcloud_director/requests/compute/vapp_tests.rb index 0401073d1..1cf84335e 100644 --- a/tests/vcloud_director/requests/compute/vapp_tests.rb +++ b/tests/vcloud_director/requests/compute/vapp_tests.rb @@ -65,9 +65,9 @@ Shindo.tests('Compute::VcloudDirector | vapp requests', ['vclouddirector']) do end end - tests('#get_vapps_in_lease_from_query').returns(Hash) do + tests('#get_vapps_in_lease_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do pending if Fog.mocking? - @service.get_vapps_in_lease_from_query.body.class + @service.get_vapps_in_lease_from_query.body end tests('Retrieve non-existent vApp').raises(Excon::Errors::Forbidden) do diff --git a/tests/vcloud_director/requests/compute/vdc_tests.rb b/tests/vcloud_director/requests/compute/vdc_tests.rb index d471d4543..3ad5931fe 100644 --- a/tests/vcloud_director/requests/compute/vdc_tests.rb +++ b/tests/vcloud_director/requests/compute/vdc_tests.rb @@ -17,7 +17,12 @@ Shindo.tests('Compute::VcloudDirector | vdc requests', ['vclouddirector']) do tests('#get_vdc_metadata').data_matches_schema(VcloudDirector::Compute::Schema::METADATA_TYPE) do pending if Fog.mocking? - @service.get_vdc_metadata(@vdc_id) + @service.get_vdc_metadata(@vdc_id).body + end + + tests('#get_vdcs_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do + pending if Fog.mocking? + @service.get_vdcs_from_query.body end tests('Retrieve non-existent vDC').raises(Excon::Errors::Forbidden) do diff --git a/tests/vcloud_director/requests/compute/vm_tests.rb b/tests/vcloud_director/requests/compute/vm_tests.rb index 7d18120ee..c685be8e2 100644 --- a/tests/vcloud_director/requests/compute/vm_tests.rb +++ b/tests/vcloud_director/requests/compute/vm_tests.rb @@ -100,9 +100,9 @@ Shindo.tests('Compute::VcloudDirector | vm requests', ['vclouddirector']) do end end - tests('#get_vms_in_lease_from_query').returns(Hash) do + tests('#get_vms_in_lease_from_query').data_matches_schema(VcloudDirector::Compute::Schema::CONTAINER_TYPE) do pending if Fog.mocking? - @service.get_vms_in_lease_from_query.body.class + @service.get_vms_in_lease_from_query.body end #tests('Retrieve non-existent vApp').raises(Excon::Errors::Forbidden) do