Sha256: a4363cffb2de7dd602c92f5f6b7ff3dee9f2b0dfe4182e8e6e3b6b3fe4be40c6

Contents?: true

Size: 1.73 KB

Versions: 42

Compression:

Stored size: 1.73 KB

Contents

require 'pp'
module Fog
  module VcloudDirector
    module Query

      def find_by_query(options={})
        type = options.fetch(:type) { self.query_type }

        results = get_all_results(type, options)
        data = results.map do |query_record|
          model_data = {}
          model_data[:id] = query_record[:href].split('/').last
          model_data[:name] = query_record.fetch(:name) if query_record.key?(:name)
          if self.methods.include?(:populate_model_from_query_record)
            model_data.merge(self.populate_model_from_query_record(query_record))
          else
            model_data
          end
        end
        load(data)
      end

      private

      def get_all_results(type, options)
        results = []
        if options.key?(:page)
          page_range = [ Integer(options[:page]) ]
        else
          page_range = (1..get_num_pages(type, options))
        end
        page_range.each do |page|
          results += get_results_page(page, type, options) || []
        end
        results
      end

      def get_num_pages(type, options)
        body = service.get_execute_query(type, options)
        last_page = body[:lastPage] || 1
        raise "Invalid lastPage (#{last_page}) in query results" unless last_page.is_a? Integer
        last_page.to_i
      end

      def get_results_page(page, type, options)
        body = service.get_execute_query(type, options.merge({:page=>page})).body

        record_key = key_of_first_record_or_reference(body)
        body[record_key] = [body[record_key]] if body[record_key].is_a?(Hash)
        body[record_key]
      end

      def key_of_first_record_or_reference(body)
        body.keys.detect { |key| key.to_s =~ /Record|Reference$/ }
      end

    end
  end
end

Version data entries

42 entries across 40 versions & 6 rubygems

Version Path
fog-2.3.0 lib/fog/vcloud_director/query.rb
fog-ifeel-2.2.0 lib/fog/vcloud_director/query.rb
fog-2.2.0 lib/fog/vcloud_director/query.rb
fog-2.1.0 lib/fog/vcloud_director/query.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-1.42.1/lib/fog/vcloud_director/query.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-1.42.1/lib/fog/vcloud_director/query.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-1.42.1/lib/fog/vcloud_director/query.rb
fog-1.42.1 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.10 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.9 lib/fog/vcloud_director/query.rb
fog-2.0.0 lib/fog/vcloud_director/query.rb
fog-1.42.0 lib/fog/vcloud_director/query.rb
fog-1.41.0 lib/fog/vcloud_director/query.rb
fog-1.40.0 lib/fog/vcloud_director/query.rb
fog-1.39.0 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.8 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.7 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.6 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.5 lib/fog/vcloud_director/query.rb
fog-vcloud-director-0.1.4 lib/fog/vcloud_director/query.rb