ibm-cloud-sdk 0.1.6 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/Gemfile +2 -0
  4. data/ibm-cloud-sdk.gemspec +1 -1
  5. data/lib/ibm/cloud/sdk.rb +3 -1
  6. data/lib/ibm/cloud/sdk/iam.rb +5 -0
  7. data/lib/ibm/cloud/sdk/logging.rb +21 -0
  8. data/lib/ibm/cloud/sdk/null_logger.rb +19 -0
  9. data/lib/ibm/cloud/sdk/power_iaas.rb +117 -9
  10. data/lib/ibm/cloud/sdk/resource_controller.rb +4 -0
  11. data/lib/ibm/cloud/sdk/tags.rb +28 -0
  12. data/lib/ibm/cloud/sdk/tags/http.rb +5 -0
  13. data/lib/ibm/cloud/sdk/tags/http/tag_collection.rb +99 -0
  14. data/lib/ibm/cloud/sdk/version.rb +1 -1
  15. data/lib/ibm/cloud/sdk/vpc.rb +8 -6
  16. data/lib/ibm/cloud/sdk/vpc/floatingips.rb +2 -2
  17. data/lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb +2 -2
  18. data/lib/ibm/cloud/sdk/vpc/http.rb +7 -0
  19. data/lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb +82 -0
  20. data/lib/ibm/cloud/sdk/vpc/http/vpc_http.rb +28 -0
  21. data/lib/ibm/cloud/sdk/vpc/http/vpc_instance.rb +30 -0
  22. data/lib/ibm/cloud/sdk/vpc/ike_policies.rb +2 -2
  23. data/lib/ibm/cloud/sdk/vpc/images.rb +2 -2
  24. data/lib/ibm/cloud/sdk/vpc/instance/actions.rb +77 -2
  25. data/lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb +3 -3
  26. data/lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb +3 -3
  27. data/lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb +3 -3
  28. data/lib/ibm/cloud/sdk/vpc/instance_profiles.rb +3 -3
  29. data/lib/ibm/cloud/sdk/vpc/instances.rb +99 -6
  30. data/lib/ibm/cloud/sdk/vpc/ipsec_policies.rb +2 -2
  31. data/lib/ibm/cloud/sdk/vpc/keys.rb +2 -24
  32. data/lib/ibm/cloud/sdk/vpc/load_balancer.rb +1 -1
  33. data/lib/ibm/cloud/sdk/vpc/load_balancer/listeners.rb +2 -2
  34. data/lib/ibm/cloud/sdk/vpc/load_balancer/members.rb +2 -2
  35. data/lib/ibm/cloud/sdk/vpc/load_balancer/policies.rb +2 -2
  36. data/lib/ibm/cloud/sdk/vpc/load_balancer/pools.rb +2 -2
  37. data/lib/ibm/cloud/sdk/vpc/load_balancer/rules.rb +2 -2
  38. data/lib/ibm/cloud/sdk/vpc/load_balancers.rb +1 -1
  39. data/lib/ibm/cloud/sdk/vpc/network_acls.rb +4 -4
  40. data/lib/ibm/cloud/sdk/vpc/operating_systems.rb +2 -2
  41. data/lib/ibm/cloud/sdk/vpc/public_gateways.rb +2 -2
  42. data/lib/ibm/cloud/sdk/vpc/regions.rb +4 -4
  43. data/lib/ibm/cloud/sdk/vpc/security_groups.rb +6 -6
  44. data/lib/ibm/cloud/sdk/vpc/subnets.rb +2 -2
  45. data/lib/ibm/cloud/sdk/vpc/volume_profiles.rb +2 -2
  46. data/lib/ibm/cloud/sdk/vpc/volumes.rb +2 -2
  47. data/lib/ibm/cloud/sdk/vpc/vpcs.rb +6 -6
  48. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/connections.rb +2 -2
  49. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb +3 -3
  50. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb +3 -3
  51. data/lib/ibm/cloud/sdk/vpc/vpn_gateways.rb +2 -2
  52. data/lib/ibm/cloud/sdk_http.rb +16 -0
  53. data/lib/ibm/cloud/sdk_http/base_collection.rb +121 -0
  54. data/lib/ibm/cloud/sdk_http/base_http_mixin.rb +120 -0
  55. data/lib/ibm/cloud/sdk_http/base_instance.rb +55 -0
  56. data/lib/ibm/cloud/sdk_http/connection.rb +38 -0
  57. data/lib/ibm/cloud/sdk_http/exceptions.rb +31 -0
  58. data/lib/ibm/cloud/sdk_http/has_child.rb +23 -0
  59. data/lib/ibm/cloud/sdk_http/iam_token.rb +53 -0
  60. data/lib/ibm/cloud/sdk_http/sdk_response.rb +132 -0
  61. data/lib/ibm/cloud_sdk.rb +39 -0
  62. metadata +23 -10
  63. data/lib/ibm/cloud/sdk/vpc/base_collection.rb +0 -108
  64. data/lib/ibm/cloud/sdk/vpc/base_instance.rb +0 -23
  65. data/lib/ibm/cloud/sdk/vpc/base_vpc.rb +0 -61
  66. data/lib/ibm/cloud/sdk/vpc/cloud_sdk.rb +0 -33
  67. data/lib/ibm/cloud/sdk/vpc/helpers/connection.rb +0 -66
  68. data/lib/ibm/cloud/sdk/vpc/helpers/response.rb +0 -92
@@ -0,0 +1,99 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../http'
5
+
6
+ module IBM
7
+ module Cloud
8
+ module SDK
9
+ module TAG
10
+ # The Base Collection for the Tags API.
11
+ class TagCollection < SDKHTTP::BaseCollection
12
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Layout/LineLength
13
+
14
+ # Set params on the next request.
15
+ # @param account_id [String>] The ID of the billing account to list the tags for. If it is not set, then it is taken from the authorization token. This parameter is required if tag_type is set to service.
16
+ # @param tag_type [String] The type of the tag you want to list. Supported values are user and service.
17
+ # @param full_data [Boolean] If set to true, this query returns the provider, ghost, ims or ghost,ims, where the tag exists and the number of attached resources.
18
+ # @param providers [String] Select a provider. Supported values are ghost and ims.
19
+ # @param attached_to [String] If you want to return only the list of tags attached to a specified resource, pass the ID of the resource on this parameter. For GhoST onboarded resources, the resource ID is the CRN; for IMS resources, it is the IMS ID. When using this parameter, you must specify the appropriate provider (ims or ghost).
20
+ # @param offset [Integer] The offset is the index of the item from which you want to start returning data from.
21
+ # @param limit [Integer] The number of tags to return (1 <> 1000). Default: 100
22
+ # @param timeout [Integer] The search timeout bounds the search request to be executed within the specified time value. It returns the hits accumulated until time runs out (0 <> 60000).
23
+ # @param order_by_name [String] Order the output by tag name (asc,desc).
24
+ # @param attached_only [Boolean] Filter on attached tags. If true, it returns only tags that are attached to one or more resources.
25
+ def params(
26
+ account_id: nil,
27
+ tag_type: nil,
28
+ full_data: false,
29
+ providers: nil,
30
+ attached_to: nil,
31
+ offset: 0,
32
+ limit: 100,
33
+ timeout: nil,
34
+ order_by_name: 'asc',
35
+ attached_only: false
36
+ )
37
+ @params ||= {}
38
+ @params[:account_id] = account_id if account_id
39
+ @params[:tag_type] = tag_type if tag_type && %w[user service].include?(tag_type)
40
+ @params[:full_data] = full_data if full_data.instance_of?(TrueClass)
41
+ @params[:providers] = providers if providers && %w[ghost ims].include?(providers)
42
+ @params[:attached_to] = attached_to if attached_to
43
+ @params[:offset] = offset if offset.positive?
44
+ @params[:limit] = limit if limit && (1..1000).include?(limit) && limit != 100
45
+ @params[:timeout] = timeout if timeout
46
+ @params[:order_by_name] = order_by_name if order_by_name && %w[desc].include?(order_by_name)
47
+ @params[:attached_only] = attached_only if attached_only.instance_of?(TrueClass)
48
+ self
49
+ end
50
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Layout/LineLength
51
+
52
+ def reset_params
53
+ @params = {}
54
+ end
55
+
56
+ # Return the total count as retrieved from the API.
57
+ # @return [Integer] value in total_count
58
+ def total_count
59
+ fetch.json.[](:total_count)
60
+ end
61
+
62
+ # Get all tags attached to a CRN.
63
+ # @param crn [String] The CRN for the instance.
64
+ def attached_to(crn)
65
+ return [] if crn.to_s.empty?
66
+
67
+ params(attached_to: crn).all
68
+ end
69
+
70
+ private
71
+
72
+ # Create a generator that removes the need for pagination.
73
+ # @param url [String] Full URL to send to server.
74
+ # @return [Enumerator] Object to page through results.
75
+ # @yield [BaseInstance] An instance of the instance class.
76
+ # @yield [Hash] When no BaseInstance set.
77
+ def each_resource(url, offset = 0, &block)
78
+ return enum_for(:each_resource, url) unless block_given?
79
+ return unless url
80
+
81
+ response = params(offset: offset).get(path: url).json
82
+
83
+ resources = response.fetch(@array_key.to_sym)
84
+ current_count = response.fetch(:offset) + response.fetch(:limit)
85
+
86
+ resources&.each { |value| yield value } # rubocop:disable Style/ExplicitBlockArgument
87
+
88
+ if resources.empty?
89
+ reset_params
90
+ return
91
+ end
92
+
93
+ each_resource(url, current_count, &block)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,7 +1,7 @@
1
1
  module IBM
2
2
  module Cloud
3
3
  module SDK
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.11"
5
5
  end
6
6
  end
7
7
  end
@@ -1,9 +1,8 @@
1
1
  # typed: true
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative 'vpc/base_vpc'
5
- require_relative 'vpc/base_collection'
6
- require_relative 'vpc/base_instance'
4
+ require 'ibm/cloud/sdk_http'
5
+ require_relative 'vpc/http'
7
6
 
8
7
  require_relative 'vpc/floatingips'
9
8
  require_relative 'vpc/flowlogcollectors'
@@ -29,20 +28,23 @@ module IBM
29
28
  module Cloud
30
29
  module SDK
31
30
  # Container that encapsulates the VPC API.
32
- class Vpc < BaseVPC
31
+ class Vpc
32
+ include VPC::VpcHTTP
33
+
33
34
  # Create an API Client object for the VPC IaaS service
34
35
  #
35
36
  # @param region [String] the IBM Power Cloud instance region
36
37
  # @param connection [IBM::Cloud::SDK::VPC::Connection] A connection object.
37
38
  # @param logger [Logger] An instance of an instanciated logger.
38
- def initialize(region, connection, logger: nil)
39
+ def initialize(region, connection, token, logger: nil)
39
40
  @region = region
41
+ @token = token
40
42
  @connection = connection
41
43
 
42
44
  @logger = logger || Logger.new($stdout, level: :warn)
43
45
  end
44
46
 
45
- attr_reader :connection, :logger
47
+ attr_reader :logger, :token, :connection
46
48
  attr_accessor :region
47
49
 
48
50
  # The Region API endpoint.
@@ -6,14 +6,14 @@ module IBM
6
6
  module SDK
7
7
  module VPC
8
8
  # Class that deals with groups of floating IPs.
9
- class FloatingIPs < BaseCollection
9
+ class FloatingIPs < VPCCollection
10
10
  def initialize(parent)
11
11
  super(parent, 'floating_ips', child_class: FloatingIP)
12
12
  end
13
13
  end
14
14
 
15
15
  # Class that deals with a single floating IP.
16
- class FloatingIP < BaseInstance; end
16
+ class FloatingIP < VPCInstance; end
17
17
  end
18
18
  end
19
19
  end
@@ -6,14 +6,14 @@ module IBM
6
6
  module SDK
7
7
  module VPC
8
8
  # A list of subnets
9
- class FlowLogCollectors < BaseCollection
9
+ class FlowLogCollectors < VPCCollection
10
10
  def initialize(parent)
11
11
  super(parent, 'flow_log_collectors', child_class: FlowLogCollectors)
12
12
  end
13
13
  end
14
14
 
15
15
  # A single subnet
16
- class FlowLogCollector < BaseInstance; end
16
+ class FlowLogCollector < VPCInstance; end
17
17
  end
18
18
  end
19
19
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ibm/cloud/sdk_http'
4
+ require_relative 'http/vpc_http'
5
+
6
+ require_relative 'http/vpc_collection'
7
+ require_relative 'http/vpc_instance'
@@ -0,0 +1,82 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../http'
5
+ require_relative 'vpc_http'
6
+
7
+ module IBM
8
+ module Cloud
9
+ module SDK
10
+ module VPC
11
+ # Container that encapsulates the VPC API.
12
+ # This class is used as a base for collection APIs.
13
+ # @param parent [Object] The parent instance in the API chain.
14
+ # @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word.
15
+ # @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used.
16
+ # @param child_class [Object] The Object to be used when instanciating the single instance for this class.
17
+ class VPCCollection < SDKHTTP::BaseCollection
18
+ include VpcHTTP
19
+
20
+ # A chainable method to set query filters on the collection.
21
+ # @example vpc.images.params(limit: 1).all
22
+ #
23
+ # @param start [String] A server-supplied token determining what resource to start the page on.
24
+ # @param limit [Integer] The number of resources to return on a page allowed values are between 1 and 100
25
+ # @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
26
+ # @return [BaseCollection] This class with the param instance variable set.
27
+ def params(start: nil, limit: nil, resource_group: nil)
28
+ @params[:start] = start if start
29
+ @params[:limit] = limit if limit
30
+ @params[:resource_group] = resource_group if resource_group
31
+ self
32
+ end
33
+
34
+ # Determine if the collection has a total_count key in its response.
35
+ # @return [Boolean]
36
+ def has_count?
37
+ fetch.json&.key?(:total_count)
38
+ end
39
+
40
+ # Get the total count if it exists in the response. Returns nil otherwise.
41
+ # @return [Integer] The total count reuturned by the server.
42
+ def count
43
+ fetch.json&.[](:total_count)
44
+ end
45
+
46
+ private
47
+
48
+ # Return a wrapped instance if set.
49
+ # @param value [Hash] The hash returned from server.
50
+ def hash_instance(value)
51
+ return @instance.new(self, data: value, id_key: @instance_id) if @instance
52
+
53
+ value
54
+ end
55
+
56
+ # Create a generator that removes the need for pagination.
57
+ # @param url [String] Full URL to send to server.
58
+ # @return [Enumerator] Object to page through results.
59
+ # @yield [BaseInstance] An instance of the instance class.
60
+ # @yield [Hash] When no VPCInstance set.
61
+ def each_resource(url, &block)
62
+ return enum_for(:each_resource, url) unless block_given?
63
+ return unless url
64
+
65
+ response = get(path: url).json
66
+ resources = response.fetch(@array_key.to_sym)
67
+
68
+ resources&.each { |value| yield hash_instance(value) }
69
+ # VPC has a next key that holds the next URL.
70
+ return unless response.key?(:next)
71
+
72
+ # The :next data structure is a hash with a href member.
73
+ next_url = response.dig(:next, :href)
74
+ return unless next_url
75
+
76
+ each_resource(next_url, &block)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../http'
4
+
5
+ module IBM
6
+ module Cloud
7
+ module SDK
8
+ # Module holds basic HTTP functionality.
9
+ module VPC
10
+ # Generic methods for accessing VPC.
11
+ module VpcHTTP
12
+ include SDKHTTP::BaseHTTPMixin
13
+
14
+ # Preprocess request parameters, add required version and generation parameters to query.
15
+ # @param query [Hash] A hash of query parameters.
16
+ # @param payload [Hash] A hash to send as the body.
17
+ # @param payload_type [Hash] If json then convert to json string, else send as form data.
18
+ # @return [Hash]
19
+ def metadata(query = nil, payload = nil, payload_type = 'json')
20
+ default_params = { version: '2020-08-01', generation: 2 }
21
+ default_params.merge!(query) if query
22
+ super(default_params, payload, payload_type)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ibm/cloud/sdk/tags'
4
+
5
+ require_relative 'vpc_http'
6
+
7
+ module IBM
8
+ module Cloud
9
+ module SDK
10
+ module VPC
11
+ # Container that encapsulates the VPC API.
12
+ class VPCInstance < SDKHTTP::BaseInstance
13
+ include VpcHTTP
14
+
15
+ # Get the CRN for the resource.
16
+ # @return [String]
17
+ def crn
18
+ self[:crn]
19
+ end
20
+
21
+ # Get the tags associated with this resource.
22
+ # @return [Enumerator] An enumerator with the results.
23
+ def tags
24
+ IBM::Cloud::SDK::Tags.new(@connection, @token, logger: @logger).attached_to(crn)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -6,14 +6,14 @@ module IBM
6
6
  module SDK
7
7
  module VPC
8
8
  # A list of IKEPolicy
9
- class IKEPolicies < BaseCollection
9
+ class IKEPolicies < VPCCollection
10
10
  def initialize(parent)
11
11
  super(parent, 'ike_policies', child_class: IKEPolicy)
12
12
  end
13
13
  end
14
14
 
15
15
  # A single IKEPolicy
16
- class IKEPolicy < BaseInstance
16
+ class IKEPolicy < VPCInstance
17
17
  def connections
18
18
  get('connections')
19
19
  end
@@ -6,14 +6,14 @@ module IBM
6
6
  module SDK
7
7
  module VPC
8
8
  # Work with multiple images.
9
- class Images < BaseCollection
9
+ class Images < VPCCollection
10
10
  def initialize(parent)
11
11
  super(parent, 'images', child_class: Image)
12
12
  end
13
13
  end
14
14
 
15
15
  # Work with a single image.
16
- class Image < BaseInstance
16
+ class Image < VPCInstance
17
17
  end
18
18
  end
19
19
  end
@@ -1,16 +1,91 @@
1
1
  # typed: true
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'date'
5
+ require 'forwardable'
6
+
4
7
  module IBM
5
8
  module Cloud
6
9
  module SDK
7
10
  module VPC
8
- module INSTANCES
11
+ module INSTANCE
9
12
  # Actions for an instance.
10
- class Actions < BaseCollection
13
+ class Actions < VPCCollection
11
14
  def initialize(parent)
12
15
  super(parent, 'actions')
13
16
  end
17
+
18
+ # Send an action request to start the instance.
19
+ def start
20
+ create('start')
21
+ end
22
+
23
+ # Send an action request to stop the instance.
24
+ # @param force [Boolean] Clear the queue and run this action.
25
+ def stop(force: false)
26
+ create('stop', force: force)
27
+ end
28
+
29
+ # Send an action request to reboot the instance.
30
+ # @param force [Boolean] Clear the queue and run this action.
31
+ def reboot(force: false)
32
+ create('reboot', force: force)
33
+ end
34
+
35
+ # Send a custom action request.
36
+ # @param action [String] The type of action. Allowable values: [reboot, start, stop]
37
+ # @param force [Boolean] If set to true, the action will be forced immediately, and all queued actions deleted. Ignored for the start action.
38
+ def create(action, force: false)
39
+ @logger.info("Sending action request for #{action} with force #{force}.")
40
+ payload = { type: action }
41
+ payload[:force] = force if force
42
+ response = post(payload: payload)
43
+ Action.new(response)
44
+ end
45
+ end
46
+
47
+ # A base class that wraps the action response.
48
+ class Action
49
+ def initialize(response)
50
+ @response = response
51
+ @data = response.json
52
+ @keys = %i[type force created_at]
53
+ @deprecated = %i[completed_at started_at status id href]
54
+
55
+ clear_deprecated
56
+ add_inst_vars(@keys)
57
+ end
58
+
59
+ # Return the DateTime the action was created.
60
+ # @return [DateTime]
61
+ def created_at
62
+ return DateTime.parse(@created_at) if @created_at
63
+
64
+ @created_at
65
+ end
66
+
67
+ # The HTTP response object.
68
+ attr_accessor :response, :data
69
+
70
+ extend Forwardable
71
+ def_delegators :@data, :[], :dig, :each_pair, :each, :fetch, :has_key?, :has_value?, :include?, :index, :inspect, :key?, :keys, :length, :merge, :merge!, :clear, :to_h, :value?, :values, :pretty_print
72
+
73
+ private
74
+
75
+ # Iterate through an array of symbols. Set the instance variable and attr_accessor for each.
76
+ def add_inst_vars(keys)
77
+ keys.each do |k|
78
+ instance_variable_set "@#{k}", @data[k]
79
+ self.class.attr_accessor k unless respond_to?(k)
80
+ end
81
+ end
82
+
83
+ # Remove deprecated keys from hash.
84
+ def clear_deprecated
85
+ @data.each do |k, _v|
86
+ @data.delete(k) if @deprecated.include?(k)
87
+ end
88
+ end
14
89
  end
15
90
  end
16
91
  end