class IBM::Cloud::SDK::VPC::VPCCollection

Container that encapsulates the VPC API. This class is used as a base for collection APIs. @param parent [Object] The parent instance in the API chain. @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word. @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used. @param child_class [Object] The Object to be used when instanciating the single instance for this class.

Public Instance Methods

count() click to toggle source

Get the total count if it exists in the response. Returns nil otherwise. @return [Integer] The total count reuturned by the server.

# File lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb, line 42
def count
  fetch.json&.[](:total_count)
end
has_count?() click to toggle source

Determine if the collection has a total_count key in its response. @return [Boolean]

# File lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb, line 36
def has_count?
  fetch.json&.key?(:total_count)
end
params(start: nil, limit: nil, resource_group: nil) click to toggle source

A chainable method to set query filters on the collection. @example vpc.images.params(limit: 1).all

@param start [String] A server-supplied token determining what resource to start the page on. @param limit [Integer] The number of resources to return on a page allowed values are between 1 and 100 @param resource_group [String] Filters the collection to resources within one of the resource groups identified in a comma-separated list of resource group identifiers @return [BaseCollection] This class with the param instance variable set.

# File lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb, line 27
def params(start: nil, limit: nil, resource_group: nil)
  @params[:start] = start if start
  @params[:limit] = limit if limit
  @params[:resource_group] = resource_group if resource_group
  self
end

Private Instance Methods

each_resource(url) { |hash_instance(value)| ... } click to toggle source

Create a generator that removes the need for pagination. @param url [String] Full URL to send to server. @return [Enumerator] Object to page through results. @yield [BaseInstance] An instance of the instance class. @yield [Hash] When no VPCInstance set.

# File lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb, line 61
def each_resource(url, &block)
  return enum_for(:each_resource, url) unless block_given?
  return unless url

  response = get(path: url).json
  resources = response.fetch(@array_key.to_sym)

  resources&.each { |value| yield hash_instance(value) }
  # VPC has a next key that holds the next URL.
  return unless response.key?(:next)

  # The :next data structure is a hash with a href member.
  next_url = response.dig(:next, :href)
  return unless next_url

  each_resource(next_url, &block)
end
hash_instance(value) click to toggle source

Return a wrapped instance if set. @param value [Hash] The hash returned from server.

# File lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb, line 50
def hash_instance(value)
  return @instance.new(self, data: value, id_key: @instance_id) if @instance

  value
end