ibm-cloud-sdk 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 181a2894e2d8045bea0d2a33cf81570480eee1f668017b8ade96b1801d0465b2
4
- data.tar.gz: 5399f7e3e4f4b483a72e673d351605cb1f8bb09f216e5fd1a9439742f736aedf
3
+ metadata.gz: 8fe8cad325a98aabb1bd328c04aec7c1c4b7dbda4859d41a85baa51916a40d80
4
+ data.tar.gz: e40564d9e168b80623282caa7bb1b4efe5519e16a7757f8ac657ddbf57c0976a
5
5
  SHA512:
6
- metadata.gz: 237fd30c5dd4efa622de5be57a2df9e2285dcabc3d15f415d6efb6bec31b36254cbaf6c3cb32a6d8b7113b7e8709e4eddee64736515030e5e3227694dc464b4e
7
- data.tar.gz: ed36bdf032519bc35c1563f62fe279f6744b3f25b97fef4358ebd689958a48a17a5a1eafbf85f72a3c62e372dfb764b35463cc292eede788b3ad127170664ced
6
+ metadata.gz: 2dabd16a4f84e7c7ade80f0a5d36f0796f15681a53f0f271b01ff6d98b41b00ea249f681c9c402961e53c0a94943c32c18d41e6f1421075be15561ce5c369c7e
7
+ data.tar.gz: 90bea5c5ae247170955e937be0fef41c8bd43f0d8749b8a3c08786f25759dd669e5f32e9a35bedada529a5936eb9ae0f06a329df4dcbb99a56c60d2f4dbb7659
@@ -1,6 +1,12 @@
1
1
  # Changelog
2
2
  All notable changes to the gem ibm-cloud-sdk-ruby will be documented here.
3
3
 
4
+ ## v0.1.5 - 2020-09-21
5
+ - Added Enumerable based pagination support
6
+ - Move instance profiles
7
+ - Improve resource lookup by GUID
8
+ - Add support to create a PowerIaas volume
9
+
4
10
  ## v0.1.4 - 2020-09-17
5
11
  - Added support for the IBM Virtual Private Cloud (VPC) service
6
12
 
@@ -120,6 +120,14 @@ module IBM
120
120
  delete("cloud-instances/#{guid}/volumes/#{volume_id}")
121
121
  end
122
122
 
123
+ # Create a volume
124
+ #
125
+ # @param volume_hash [Hash] New volume attributes
126
+ # @return [Hash] Volume
127
+ def create_volume(volume_hash)
128
+ post("cloud-instances/#{guid}/volumes", volume_hash.to_json)
129
+ end
130
+
123
131
  # Get all networks in an IBM Power Cloud instance
124
132
  #
125
133
  # @return [Array<Hash>] all networks for this IBM Power Cloud instance
@@ -2,6 +2,7 @@ module IBM
2
2
  module Cloud
3
3
  module SDK
4
4
  class ResourceController < BaseService
5
+ require "ibm/cloud/sdk/resource_controller/resource"
5
6
  def endpoint
6
7
  "https://resource-controller.cloud.ibm.com/v2"
7
8
  end
@@ -10,15 +11,8 @@ module IBM
10
11
  @token = token
11
12
  end
12
13
 
13
- def get_resources
14
- resources = get("resource_instances")["resources"] || []
15
-
16
- require "ibm/cloud/sdk/resource_controller/resource"
17
- resources.map { |instance| Resource.new(instance) }
18
- end
19
-
20
14
  def get_resource(guid)
21
- get_resources.detect { |resource| resource.guid == guid }
15
+ Resource.new(get("resource_instances/#{guid}"))
22
16
  end
23
17
 
24
18
  private
@@ -1,7 +1,7 @@
1
1
  module IBM
2
2
  module Cloud
3
3
  module SDK
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
6
6
  end
7
7
  end
@@ -9,6 +9,7 @@ require_relative 'vpc/floatingips'
9
9
  require_relative 'vpc/flowlogcollectors'
10
10
  require_relative 'vpc/ike_policies'
11
11
  require_relative 'vpc/images'
12
+ require_relative 'vpc/instance_profiles'
12
13
  require_relative 'vpc/instances'
13
14
  require_relative 'vpc/ipsec_policies'
14
15
  require_relative 'vpc/keys'
@@ -32,17 +33,16 @@ module IBM
32
33
  # Create an API Client object for the VPC IaaS service
33
34
  #
34
35
  # @param region [String] the IBM Power Cloud instance region
35
- # @param token [IAMtoken] the IBM Cloud IAM Token object
36
- # @param logger [Logger] an instance of an instanciated logger.
37
- def initialize(region, api_key, logger: nil)
36
+ # @param connection [IBM::Cloud::SDK::VPC::Connection] A connection object.
37
+ # @param logger [Logger] An instance of an instanciated logger.
38
+ def initialize(region, connection, logger: nil)
38
39
  @region = region
39
- @api_key = api_key
40
- @token = api_key
40
+ @connection = connection
41
41
 
42
- @logger = logger || Logger.new($stdout)
42
+ @logger = logger || Logger.new($stdout, level: :warn)
43
43
  end
44
44
 
45
- attr_reader :token, :logger
45
+ attr_reader :connection, :logger
46
46
  attr_accessor :region
47
47
 
48
48
  # The Region API endpoint.
@@ -67,6 +67,10 @@ module IBM
67
67
  VPC::Images.new(self)
68
68
  end
69
69
 
70
+ def instance_profiles
71
+ VPC::InstanceProfiles.new(self)
72
+ end
73
+
70
74
  def instances
71
75
  VPC::Instances.new(self)
72
76
  end
@@ -6,9 +6,13 @@ module IBM
6
6
  module SDK
7
7
  # Container that encapsulates the VPC API.
8
8
  class BaseCollection < BaseVPC
9
+ # This class is used as a base for collection APIs.
10
+ # @param parent [Object] The parent instance in the API chain.
11
+ # @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word.
12
+ # @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used.
13
+ # @param child_class [Object] The Object to be used when instanciating the single instance for this class.
9
14
  def initialize(parent, endpoint, array_key: nil, child_class: nil)
10
15
  # Setup empty base instance variables.
11
- @data = nil
12
16
  @params = nil
13
17
 
14
18
  array_key ||= endpoint
@@ -20,47 +24,83 @@ module IBM
20
24
  super(parent, endpoint)
21
25
  end
22
26
 
27
+ # A chainable method to set query filters on the collection.
28
+ # @example vpc.images.params(limit: 1).all
29
+ #
30
+ # @param start [String] A server-supplied token determining what resource to start the page on.
31
+ # @param limit [Integer] The number of resources to return on a page allowed values are between 1 and 100
32
+ # @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
33
+ # @return [BaseCollection] This class with the param instance variable set.
34
+ def params(start: nil, limit: nil, resource_group: nil)
35
+ @params = {}
36
+ @params[:start] = start if start
37
+ @params[:limit] = limit if limit
38
+ @params[:resource_group] = resource_group if resource_group
39
+ self
40
+ end
41
+
23
42
  # Retrieve the collection from the cloud.
43
+ # @return [IBM::Cloud::SDK::VPC::Response] The http response object.
24
44
  def fetch
25
45
  @data ||= get(params: @params)
26
- @data
27
46
  end
28
47
 
48
+ # Get an iterable for the resource collection.
49
+ # @return [Enumerator] Use standard each, next idioms.
29
50
  def all
30
- response = fetch.subkey(@array_key)
31
- return response if response.is_a?(Array)
51
+ each_resource(url)
52
+ end
32
53
 
33
- raise "#{self.class}.#{__method__} Expecting response to be an Array not a #{response.class}"
54
+ # Fetch all data and return in an array.
55
+ # @return [Array] Hashes of the returned data.
56
+ def data
57
+ all.to_a
34
58
  end
35
59
 
36
- def params(start: nil, limit: nil, resource_group: nil)
37
- @params = {}
38
- @params['start'] = start if start
39
- @params['limit'] = limit if limit
40
- @params['resource_group'] = resource_group if resource_group
41
- self
60
+ # Determine if the collection has a total_count key in its response.
61
+ # @return [Boolean]
62
+ def has_count?
63
+ fetch.json&.key?(:total_count)
42
64
  end
43
65
 
66
+ # Get the total count if it exists in the response. Returns nil otherwise.
67
+ # @return [Integer] The total count reuturned by the server.
44
68
  def count
45
- fetch.subkey('total_count')
69
+ fetch.json&.fetch(:total_count)
46
70
  end
47
71
 
48
- def limit
49
- fetch.subkey('limit')
72
+ # A generic post method to create a resource on the collection.
73
+ # @param payload [Hash] A hash of parameters to send to the server.
74
+ # @param payload_type [String] One of the following options json, form, or body.
75
+ # @return [IBM::Cloud::SDK::VPC::Response] The http response object.
76
+ def create(payload, payload_type = 'json')
77
+ adhoc(method: 'post', payload_type: payload_type, payload: payload)
50
78
  end
51
79
 
80
+ # Access a specific instance by either id or name depending on API.
52
81
  def instance(id)
53
82
  @instance.new(self, id)
54
83
  end
55
84
 
56
- def next
57
- fetch.subkey('next')
58
- rescue RuntimeError
59
- nil
60
- end
85
+ private
86
+
87
+ # Create a generator that removes the need for pagination.
88
+ def each_resource(url, &block)
89
+ return enum_for(:each_resource, url) unless block_given?
90
+ return unless url
91
+
92
+ response = @connection.adhoc('get', url, metadata(@params)).json
93
+ resources = response.fetch(@array_key.to_sym)
94
+
95
+ resources&.each do |value|
96
+ yield value
97
+ end
98
+ return unless response.key?(:next)
99
+
100
+ next_url = response.dig(:next, :href)
101
+ return unless next_url
61
102
 
62
- def create(payload)
63
- post(payload)
103
+ each_resource(next_url, &block)
64
104
  end
65
105
  end
66
106
  end
@@ -7,7 +7,7 @@ module IBM
7
7
  # Container that encapsulates the VPC API.
8
8
  class BaseInstance < BaseVPC
9
9
  def details
10
- get.hash
10
+ get.hash_response
11
11
  end
12
12
 
13
13
  def update(payload)
@@ -8,14 +8,14 @@ module IBM
8
8
  class BaseVPC
9
9
  def initialize(parent, endpoint = nil)
10
10
  @endpoint = parent.url(endpoint)
11
- @token = parent.token
11
+ @connection = parent.connection
12
12
  @logger = parent.logger
13
13
  end
14
14
 
15
- attr_reader :endpoint, :token, :logger
15
+ attr_reader :endpoint, :connection, :logger
16
16
 
17
17
  def adhoc(method: 'get', path: nil, params: {}, payload_type: 'json', payload: {})
18
- @token.adhoc(method.to_sym, url(path), metadata(params, payload_type, payload))
18
+ @connection.adhoc(method.to_sym, url(path), metadata(params, payload_type, payload))
19
19
  end
20
20
 
21
21
  def get(path: nil, params: {})
@@ -23,7 +23,7 @@ module IBM
23
23
  end
24
24
 
25
25
  def post(payload = {}, path: nil, params: {}, type: 'json')
26
- adhoc(method: 'post', path: path, params: params, payload: payload)
26
+ adhoc(method: 'post', path: path, params: params, payload: payload, payload_type: type)
27
27
  end
28
28
 
29
29
  def put(payload = {}, path: nil, params: {})
@@ -12,7 +12,7 @@ module IBM
12
12
  @logger ||= Logger.new($stdout, level: :warn)
13
13
 
14
14
  @client = HTTP.use(http_options(options))
15
- @token = IBM::Cloud::SDK::VPC::Connection.new(api_key, logger: @logger, client: @client)
15
+ @connection = IBM::Cloud::SDK::VPC::Connection.new(api_key, logger: @logger, client: @client)
16
16
  end
17
17
 
18
18
  def http_options(options = {})
@@ -24,10 +24,10 @@ module IBM
24
24
  )
25
25
  end
26
26
 
27
- attr_reader :logger, :token
27
+ attr_reader :logger, :connection
28
28
 
29
29
  def vpc(region = 'us-east')
30
- IBM::Cloud::SDK::Vpc.new(region, @token, logger: @logger)
30
+ IBM::Cloud::SDK::Vpc.new(region, @connection, logger: @logger)
31
31
  end
32
32
  end
33
- end
33
+ end
@@ -32,6 +32,8 @@ module IBM
32
32
  response.code
33
33
  end
34
34
 
35
+ alias_method :status, :code
36
+
35
37
  # Return the raw connection object.
36
38
  def connection
37
39
  response.connection
@@ -73,7 +75,7 @@ module IBM
73
75
  sym_key = key.to_sym
74
76
  return ret.fetch(sym_key) if ret.key?(sym_key)
75
77
 
76
- raise "Key not found in #{ret}."
78
+ raise "Key #{key} not found in #{ret}."
77
79
  end
78
80
 
79
81
  # Check to see if the returned object is the expected object.
@@ -4,7 +4,6 @@
4
4
  require_relative 'instance/actions'
5
5
  require_relative 'instance/network_interfaces'
6
6
  require_relative 'instance/volume_attachments'
7
- require_relative 'instance/profiles'
8
7
 
9
8
  module IBM
10
9
  module Cloud
@@ -0,0 +1,21 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module IBM
5
+ module Cloud
6
+ module SDK
7
+ module VPC
8
+ # Work with multiple profiles.
9
+ class InstanceProfiles < BaseCollection
10
+ def initialize(parent)
11
+ super(parent, 'instance/profiles', child_class: Profile, array_key: 'profiles')
12
+ end
13
+ end
14
+
15
+ # Get a single profile.
16
+ class Profile < BaseInstance
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibm-cloud-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - IBM Cloud Developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -79,8 +79,8 @@ files:
79
79
  - lib/ibm/cloud/sdk/vpc/instance/actions.rb
80
80
  - lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb
81
81
  - lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb
82
- - lib/ibm/cloud/sdk/vpc/instance/profiles.rb
83
82
  - lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb
83
+ - lib/ibm/cloud/sdk/vpc/instance_profiles.rb
84
84
  - lib/ibm/cloud/sdk/vpc/instances.rb
85
85
  - lib/ibm/cloud/sdk/vpc/ipsec_policies.rb
86
86
  - lib/ibm/cloud/sdk/vpc/keys.rb
@@ -109,7 +109,7 @@ licenses:
109
109
  - '"Apache-2.0"'
110
110
  metadata:
111
111
  homepage_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
112
- changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.4/CHANGELOG.md
112
+ changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.5/CHANGELOG.md
113
113
  post_install_message:
114
114
  rdoc_options: []
115
115
  require_paths:
@@ -1,34 +0,0 @@
1
- # typed: true
2
- # frozen_string_literal: true
3
-
4
- module IBM
5
- module Cloud
6
- module SDK
7
- module VPC
8
- module INSTANCES
9
- # Work with multiple profiles.
10
- class Profiles < BaseVPC
11
- def initialize(parent)
12
- super(parent, 'instance/profiles')
13
- end
14
-
15
- def all
16
- get
17
- end
18
-
19
- def instance(name)
20
- Profile.new(self, name)
21
- end
22
- end
23
-
24
- # Get a single profile.
25
- class Profile < BaseVPC
26
- def details
27
- get
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end