ibm-cloud-sdk 0.1.5 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/Gemfile +2 -0
- data/ibm-cloud-sdk.gemspec +1 -1
- data/lib/ibm/cloud/sdk.rb +3 -1
- data/lib/ibm/cloud/sdk/iam.rb +5 -0
- data/lib/ibm/cloud/sdk/logging.rb +21 -0
- data/lib/ibm/cloud/sdk/null_logger.rb +19 -0
- data/lib/ibm/cloud/sdk/power_iaas.rb +125 -10
- data/lib/ibm/cloud/sdk/resource_controller.rb +4 -0
- data/lib/ibm/cloud/sdk/tags.rb +28 -0
- data/lib/ibm/cloud/sdk/tags/http.rb +5 -0
- data/lib/ibm/cloud/sdk/tags/http/tag_collection.rb +99 -0
- data/lib/ibm/cloud/sdk/version.rb +1 -1
- data/lib/ibm/cloud/sdk/vpc.rb +8 -6
- data/lib/ibm/cloud/sdk/vpc/floatingips.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/http.rb +7 -0
- data/lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb +82 -0
- data/lib/ibm/cloud/sdk/vpc/http/vpc_http.rb +29 -0
- data/lib/ibm/cloud/sdk/vpc/http/vpc_instance.rb +30 -0
- data/lib/ibm/cloud/sdk/vpc/ike_policies.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/images.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/instance/actions.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb +3 -3
- data/lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb +4 -29
- data/lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb +4 -27
- data/lib/ibm/cloud/sdk/vpc/instance_profiles.rb +3 -3
- data/lib/ibm/cloud/sdk/vpc/instances.rb +43 -3
- data/lib/ibm/cloud/sdk/vpc/ipsec_policies.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/keys.rb +2 -24
- data/lib/ibm/cloud/sdk/vpc/load_balancer.rb +1 -1
- data/lib/ibm/cloud/sdk/vpc/load_balancer/listeners.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/load_balancer/members.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/load_balancer/policies.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/load_balancer/pools.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/load_balancer/rules.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/load_balancers.rb +1 -1
- data/lib/ibm/cloud/sdk/vpc/network_acls.rb +4 -4
- data/lib/ibm/cloud/sdk/vpc/operating_systems.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/public_gateways.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/regions.rb +4 -4
- data/lib/ibm/cloud/sdk/vpc/security_groups.rb +6 -6
- data/lib/ibm/cloud/sdk/vpc/subnets.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/volume_profiles.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/volumes.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/vpcs.rb +6 -6
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/connections.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb +2 -2
- data/lib/ibm/cloud/sdk/vpc/vpn_gateways.rb +2 -2
- data/lib/ibm/cloud/sdk_http.rb +16 -0
- data/lib/ibm/cloud/sdk_http/base_collection.rb +121 -0
- data/lib/ibm/cloud/sdk_http/base_http_mixin.rb +63 -0
- data/lib/ibm/cloud/sdk_http/base_instance.rb +55 -0
- data/lib/ibm/cloud/sdk_http/connection.rb +32 -0
- data/lib/ibm/cloud/sdk_http/exceptions.rb +31 -0
- data/lib/ibm/cloud/sdk_http/has_child.rb +23 -0
- data/lib/ibm/cloud/sdk_http/iam_token.rb +53 -0
- data/lib/ibm/cloud/sdk_http/sdk_response.rb +132 -0
- data/lib/ibm/cloud_sdk.rb +39 -0
- metadata +23 -11
- data/lib/ibm/cloud/sdk/vpc/base_collection.rb +0 -108
- data/lib/ibm/cloud/sdk/vpc/base_instance.rb +0 -23
- data/lib/ibm/cloud/sdk/vpc/base_vpc.rb +0 -61
- data/lib/ibm/cloud/sdk/vpc/cloud_sdk.rb +0 -33
- data/lib/ibm/cloud/sdk/vpc/helpers/connection.rb +0 -66
- data/lib/ibm/cloud/sdk/vpc/helpers/response.rb +0 -92
- data/lib/ibm/cloud/sdk/vpc/instance.rb +0 -35
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IBM
|
4
|
+
module Cloud
|
5
|
+
module SDKHTTP
|
6
|
+
# Module to contain all custom exception classes.
|
7
|
+
module Exceptions
|
8
|
+
# An exception for http with a response attribute.
|
9
|
+
# @param msg [String] A human readable message.
|
10
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
11
|
+
class ExceptionWithResponse < RuntimeError
|
12
|
+
def initialize(msg, response)
|
13
|
+
@response = response
|
14
|
+
super(msg)
|
15
|
+
end
|
16
|
+
# @return [IBM::Cloud::SDK::VPC::Response] The response
|
17
|
+
attr_reader :response
|
18
|
+
end
|
19
|
+
|
20
|
+
# An exception for http status errors with a response attribute.
|
21
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
22
|
+
class HttpStatusError < ExceptionWithResponse
|
23
|
+
def initialize(response)
|
24
|
+
msg = "Invalid status #{response.code} for url \"#{response.url}\", #{response.reason}. #{response.body}"
|
25
|
+
super(msg, response)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDKHTTP
|
7
|
+
# Access a specific instance by either id or name depending on API.
|
8
|
+
module ChildMixin
|
9
|
+
# Get an instance of the collection object.
|
10
|
+
# @param id [String] ID of Name to search on depending on API.
|
11
|
+
# @return [] The instance object.
|
12
|
+
def instance(id)
|
13
|
+
@instance.new(self, endpoint: id).refresh
|
14
|
+
end
|
15
|
+
|
16
|
+
# Return the first_instance returned from a collection get.
|
17
|
+
def first_instance
|
18
|
+
params(limit: 1).all.first
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IBM
|
4
|
+
module Cloud
|
5
|
+
# Module holds basic HTTP functionality.
|
6
|
+
module SDKHTTP
|
7
|
+
# Used to authenticate with IAM.
|
8
|
+
class IAMToken
|
9
|
+
include BaseHTTPMixin
|
10
|
+
|
11
|
+
def initialize(api_key, connection, logger: nil)
|
12
|
+
@api_key = api_key
|
13
|
+
@logger = logger
|
14
|
+
@connection = connection
|
15
|
+
@response = nil
|
16
|
+
@data = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :response, :connection
|
20
|
+
|
21
|
+
def fetch
|
22
|
+
payload = {
|
23
|
+
body: {
|
24
|
+
grant_type: 'urn:ibm:params:oauth:grant-type:apikey',
|
25
|
+
apikey: @api_key
|
26
|
+
}
|
27
|
+
}
|
28
|
+
@response = @connection.request('post', 'https://iam.cloud.ibm.com/identity/token', payload)
|
29
|
+
end
|
30
|
+
|
31
|
+
def data
|
32
|
+
fetch unless @response
|
33
|
+
@response.raise_for_status!.json
|
34
|
+
end
|
35
|
+
|
36
|
+
# Check to see if the access_token is expired. Fetch a new token if none exists.
|
37
|
+
# @return [IBM::Cloud::SDK::VPC::Response] Wrapped response to query.
|
38
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
39
|
+
def expired?
|
40
|
+
data.fetch(:expiration, 0).to_i <= Time.now.to_i + 600
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get a Bearer token string. Before returning check to see if token is expired.
|
44
|
+
# @return [String] The Bearer token header used in subsequent requests.
|
45
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
46
|
+
def authorization_header
|
47
|
+
fetch if expired?
|
48
|
+
"#{data.fetch(:token_type)} #{data.fetch(:access_token)}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module IBM
|
6
|
+
module Cloud
|
7
|
+
# Module holds basic HTTP functionality.
|
8
|
+
module SDKHTTP
|
9
|
+
# Encapsulate the HTTP response.
|
10
|
+
# @param response [HTTP::Response] The HTTP response object.
|
11
|
+
class SDKResponse
|
12
|
+
def initialize(response)
|
13
|
+
@response = response
|
14
|
+
end
|
15
|
+
|
16
|
+
# The raw HTTP response.
|
17
|
+
# @return [HTTP::Response]
|
18
|
+
attr_reader :response
|
19
|
+
|
20
|
+
# Return the response in a hash or array.
|
21
|
+
# @return [Hash] When response is a hash.
|
22
|
+
# @return [Array] When response is an array.
|
23
|
+
# @raise [Exceptions::ExceptionWithResponse] Contents of body is not properly formatted json.
|
24
|
+
def json
|
25
|
+
JSON.parse(body, symbolize_names: true)
|
26
|
+
rescue StandardError
|
27
|
+
raise Exceptions::ExceptionWithResponse.new("#{url} Error while parsing response body. #{response.body}", self)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return the raw response string.
|
31
|
+
# @return [String]
|
32
|
+
# @return [nil] Response does not have body method.
|
33
|
+
def body
|
34
|
+
response&.body.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
# Return the response code.
|
38
|
+
# @return [Integer] Response has code method.
|
39
|
+
# @return [nil] Response does not have code method.
|
40
|
+
def code
|
41
|
+
response&.code
|
42
|
+
end
|
43
|
+
|
44
|
+
alias status code
|
45
|
+
|
46
|
+
# Return the raw connection object.
|
47
|
+
# @return [HTTP::Connection]
|
48
|
+
# @return [nil] Response does not have a connection method.
|
49
|
+
def connection
|
50
|
+
response&.request
|
51
|
+
end
|
52
|
+
|
53
|
+
# Chainable method to verify the status code. Raise an exception for non 200-series or 404 status codes.
|
54
|
+
# @return [Response] Allows for method to be chainable.
|
55
|
+
# @raise [Exceptions::HttpStatusError] Raise if status checks failed.
|
56
|
+
def raise_for_status!
|
57
|
+
return self if (200..299).include?(code)
|
58
|
+
return self if code == 404
|
59
|
+
|
60
|
+
raise Exceptions::HttpStatusError.new(self)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Return the content type of the response.
|
64
|
+
# @return [String] The mimetype of the response.
|
65
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
66
|
+
def content_type
|
67
|
+
response&.response&.content_type
|
68
|
+
end
|
69
|
+
|
70
|
+
# Return the textual reason.
|
71
|
+
# @return [String] HTTP Reason
|
72
|
+
# @return [nil] Response does not have reaspn method that responds.
|
73
|
+
def reason
|
74
|
+
response&.response&.msg
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return the sent url as a string.
|
78
|
+
# @return [String] Full URL sent
|
79
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
80
|
+
def url
|
81
|
+
uri.to_s
|
82
|
+
end
|
83
|
+
|
84
|
+
# Return the sent url as a URI class.
|
85
|
+
# @see https://github.com/httprb/http/blob/master/lib/http/uri.rb URI Class doc.
|
86
|
+
# @return [HTTP::URI]
|
87
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
88
|
+
def uri
|
89
|
+
connection&.uri
|
90
|
+
end
|
91
|
+
|
92
|
+
# Verify that the json response is a hash.
|
93
|
+
# @return [Hash] Response from JSON
|
94
|
+
# @raise [RuntimeError] JSON object is not a Hash.
|
95
|
+
def hash_response
|
96
|
+
check_object(Hash)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Verify that the json response is an array.
|
100
|
+
# @return [Array] Response from JSON
|
101
|
+
# @raise [RuntimeError] JSON object is not a Array.
|
102
|
+
def array_response
|
103
|
+
check_object(Array)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Find a subkey within the returned response.
|
107
|
+
# @param key [String] Name of a first level key.
|
108
|
+
# @return [Any] Response from JSON
|
109
|
+
# @raise [RuntimeError] JSON object is not a Array.
|
110
|
+
def subkey(key)
|
111
|
+
ret = hash_response
|
112
|
+
sym_key = key.to_sym
|
113
|
+
return ret.fetch(sym_key) if ret.key?(sym_key)
|
114
|
+
|
115
|
+
msg = "Key #{key} not found in #{ret}."
|
116
|
+
raise Exceptions::ExceptionWithResponse.new(msg, self)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Check to see if the returned object is the expected object.
|
120
|
+
# @param obj [Object] The object to test the response against.
|
121
|
+
# @raise [Exceptions::ExceptionWithResponse] Parsed JSON is not the expecte class.
|
122
|
+
def check_object(obj)
|
123
|
+
ret = json
|
124
|
+
return ret if ret.instance_of?(obj)
|
125
|
+
|
126
|
+
msg = "Expected #{obj} in response for #{url}. The returned object is a #{ret.class}."
|
127
|
+
raise Exceptions::ExceptionWithResponse.new(msg, self)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
require_relative 'cloud/sdk_http'
|
6
|
+
require_relative 'cloud/sdk/vpc'
|
7
|
+
require_relative 'cloud/sdk/tags'
|
8
|
+
|
9
|
+
module IBM
|
10
|
+
# Holds the SDK pieces.
|
11
|
+
class CloudSDK
|
12
|
+
def initialize(api_key, logger: nil)
|
13
|
+
@logger = logger || Logger.new($stdout, level: :warn)
|
14
|
+
@connection = IBM::Cloud::SDKHTTP::HTTPConnection.new(@logger)
|
15
|
+
@token = IBM::Cloud::SDKHTTP::IAMToken.new(api_key, @connection, logger: @logger)
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :logger, :token, :connection
|
19
|
+
|
20
|
+
# Add a proxy to the configuration.
|
21
|
+
# @param uri [URI::Generic] A URI object with proxy configuration.
|
22
|
+
def add_proxy(uri)
|
23
|
+
raise "URI #{uri} is not set properly. Requires a full url, and optional user & password" if uri.relative?
|
24
|
+
|
25
|
+
path = "#{uri.scheme}://#{uri.hostname}"
|
26
|
+
@connection.class.http_proxy(path, uri.port, uri.user, uri.password)
|
27
|
+
end
|
28
|
+
|
29
|
+
def tags
|
30
|
+
@tags ||= IBM::Cloud::SDK::Tags.new(@connection, @token, logger: @logger)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get an instance of the VPC API.
|
34
|
+
# @param region [String] The region to use for the VPC connection.
|
35
|
+
def vpc(region = 'us-east')
|
36
|
+
@vpc ||= IBM::Cloud::SDK::Vpc.new(region, @connection, @token, logger: @logger)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
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
|
+
version: 0.1.10
|
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-
|
11
|
+
date: 2020-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: httparty
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -60,22 +60,24 @@ files:
|
|
60
60
|
- lib/ibm/cloud/sdk/base_service.rb
|
61
61
|
- lib/ibm/cloud/sdk/iam.rb
|
62
62
|
- lib/ibm/cloud/sdk/iam/token.rb
|
63
|
+
- lib/ibm/cloud/sdk/logging.rb
|
64
|
+
- lib/ibm/cloud/sdk/null_logger.rb
|
63
65
|
- lib/ibm/cloud/sdk/power_iaas.rb
|
64
66
|
- lib/ibm/cloud/sdk/resource_controller.rb
|
65
67
|
- lib/ibm/cloud/sdk/resource_controller/resource.rb
|
68
|
+
- lib/ibm/cloud/sdk/tags.rb
|
69
|
+
- lib/ibm/cloud/sdk/tags/http.rb
|
70
|
+
- lib/ibm/cloud/sdk/tags/http/tag_collection.rb
|
66
71
|
- lib/ibm/cloud/sdk/version.rb
|
67
72
|
- lib/ibm/cloud/sdk/vpc.rb
|
68
|
-
- lib/ibm/cloud/sdk/vpc/base_collection.rb
|
69
|
-
- lib/ibm/cloud/sdk/vpc/base_instance.rb
|
70
|
-
- lib/ibm/cloud/sdk/vpc/base_vpc.rb
|
71
|
-
- lib/ibm/cloud/sdk/vpc/cloud_sdk.rb
|
72
73
|
- lib/ibm/cloud/sdk/vpc/floatingips.rb
|
73
74
|
- lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb
|
74
|
-
- lib/ibm/cloud/sdk/vpc/
|
75
|
-
- lib/ibm/cloud/sdk/vpc/
|
75
|
+
- lib/ibm/cloud/sdk/vpc/http.rb
|
76
|
+
- lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb
|
77
|
+
- lib/ibm/cloud/sdk/vpc/http/vpc_http.rb
|
78
|
+
- lib/ibm/cloud/sdk/vpc/http/vpc_instance.rb
|
76
79
|
- lib/ibm/cloud/sdk/vpc/ike_policies.rb
|
77
80
|
- lib/ibm/cloud/sdk/vpc/images.rb
|
78
|
-
- lib/ibm/cloud/sdk/vpc/instance.rb
|
79
81
|
- lib/ibm/cloud/sdk/vpc/instance/actions.rb
|
80
82
|
- lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb
|
81
83
|
- lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb
|
@@ -104,12 +106,22 @@ files:
|
|
104
106
|
- lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb
|
105
107
|
- lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb
|
106
108
|
- lib/ibm/cloud/sdk/vpc/vpn_gateways.rb
|
109
|
+
- lib/ibm/cloud/sdk_http.rb
|
110
|
+
- lib/ibm/cloud/sdk_http/base_collection.rb
|
111
|
+
- lib/ibm/cloud/sdk_http/base_http_mixin.rb
|
112
|
+
- lib/ibm/cloud/sdk_http/base_instance.rb
|
113
|
+
- lib/ibm/cloud/sdk_http/connection.rb
|
114
|
+
- lib/ibm/cloud/sdk_http/exceptions.rb
|
115
|
+
- lib/ibm/cloud/sdk_http/has_child.rb
|
116
|
+
- lib/ibm/cloud/sdk_http/iam_token.rb
|
117
|
+
- lib/ibm/cloud/sdk_http/sdk_response.rb
|
118
|
+
- lib/ibm/cloud_sdk.rb
|
107
119
|
homepage: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
|
108
120
|
licenses:
|
109
121
|
- '"Apache-2.0"'
|
110
122
|
metadata:
|
111
123
|
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.
|
124
|
+
changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.10/CHANGELOG.md
|
113
125
|
post_install_message:
|
114
126
|
rdoc_options: []
|
115
127
|
require_paths:
|
@@ -1,108 +0,0 @@
|
|
1
|
-
# typed: true
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module IBM
|
5
|
-
module Cloud
|
6
|
-
module SDK
|
7
|
-
# Container that encapsulates the VPC API.
|
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.
|
14
|
-
def initialize(parent, endpoint, array_key: nil, child_class: nil)
|
15
|
-
# Setup empty base instance variables.
|
16
|
-
@params = nil
|
17
|
-
|
18
|
-
array_key ||= endpoint
|
19
|
-
|
20
|
-
# Set the array key and child class.
|
21
|
-
@array_key ||= array_key
|
22
|
-
@instance ||= child_class
|
23
|
-
|
24
|
-
super(parent, endpoint)
|
25
|
-
end
|
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
|
-
|
42
|
-
# Retrieve the collection from the cloud.
|
43
|
-
# @return [IBM::Cloud::SDK::VPC::Response] The http response object.
|
44
|
-
def fetch
|
45
|
-
@data ||= get(params: @params)
|
46
|
-
end
|
47
|
-
|
48
|
-
# Get an iterable for the resource collection.
|
49
|
-
# @return [Enumerator] Use standard each, next idioms.
|
50
|
-
def all
|
51
|
-
each_resource(url)
|
52
|
-
end
|
53
|
-
|
54
|
-
# Fetch all data and return in an array.
|
55
|
-
# @return [Array] Hashes of the returned data.
|
56
|
-
def data
|
57
|
-
all.to_a
|
58
|
-
end
|
59
|
-
|
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)
|
64
|
-
end
|
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.
|
68
|
-
def count
|
69
|
-
fetch.json&.fetch(:total_count)
|
70
|
-
end
|
71
|
-
|
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)
|
78
|
-
end
|
79
|
-
|
80
|
-
# Access a specific instance by either id or name depending on API.
|
81
|
-
def instance(id)
|
82
|
-
@instance.new(self, id)
|
83
|
-
end
|
84
|
-
|
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
|
102
|
-
|
103
|
-
each_resource(next_url, &block)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|