ibm-cloud-sdk 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/ibm-cloud-sdk.gemspec +1 -1
  4. data/lib/ibm/cloud/sdk.rb +2 -1
  5. data/lib/ibm/cloud/sdk/iam.rb +5 -0
  6. data/lib/ibm/cloud/sdk/resource_controller.rb +4 -0
  7. data/lib/ibm/cloud/sdk/tags.rb +28 -0
  8. data/lib/ibm/cloud/sdk/tags/http.rb +5 -0
  9. data/lib/ibm/cloud/sdk/tags/http/tag_collection.rb +99 -0
  10. data/lib/ibm/cloud/sdk/version.rb +1 -1
  11. data/lib/ibm/cloud/sdk/vpc.rb +8 -7
  12. data/lib/ibm/cloud/sdk/vpc/floatingips.rb +2 -2
  13. data/lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb +2 -2
  14. data/lib/ibm/cloud/sdk/vpc/http.rb +7 -0
  15. data/lib/ibm/cloud/sdk/vpc/http/vpc_collection.rb +82 -0
  16. data/lib/ibm/cloud/sdk/vpc/http/vpc_http.rb +29 -0
  17. data/lib/ibm/cloud/sdk/vpc/http/vpc_instance.rb +30 -0
  18. data/lib/ibm/cloud/sdk/vpc/ike_policies.rb +2 -2
  19. data/lib/ibm/cloud/sdk/vpc/images.rb +2 -2
  20. data/lib/ibm/cloud/sdk/vpc/instance/actions.rb +1 -1
  21. data/lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb +2 -2
  22. data/lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb +2 -2
  23. data/lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb +2 -2
  24. data/lib/ibm/cloud/sdk/vpc/instance_profiles.rb +3 -3
  25. data/lib/ibm/cloud/sdk/vpc/instances.rb +2 -2
  26. data/lib/ibm/cloud/sdk/vpc/ipsec_policies.rb +2 -2
  27. data/lib/ibm/cloud/sdk/vpc/keys.rb +2 -24
  28. data/lib/ibm/cloud/sdk/vpc/load_balancer.rb +1 -1
  29. data/lib/ibm/cloud/sdk/vpc/load_balancer/listeners.rb +2 -2
  30. data/lib/ibm/cloud/sdk/vpc/load_balancer/members.rb +2 -2
  31. data/lib/ibm/cloud/sdk/vpc/load_balancer/policies.rb +2 -2
  32. data/lib/ibm/cloud/sdk/vpc/load_balancer/pools.rb +2 -2
  33. data/lib/ibm/cloud/sdk/vpc/load_balancer/rules.rb +2 -2
  34. data/lib/ibm/cloud/sdk/vpc/load_balancers.rb +1 -1
  35. data/lib/ibm/cloud/sdk/vpc/network_acls.rb +4 -4
  36. data/lib/ibm/cloud/sdk/vpc/operating_systems.rb +2 -2
  37. data/lib/ibm/cloud/sdk/vpc/public_gateways.rb +2 -2
  38. data/lib/ibm/cloud/sdk/vpc/regions.rb +4 -4
  39. data/lib/ibm/cloud/sdk/vpc/security_groups.rb +6 -6
  40. data/lib/ibm/cloud/sdk/vpc/subnets.rb +2 -2
  41. data/lib/ibm/cloud/sdk/vpc/volume_profiles.rb +2 -2
  42. data/lib/ibm/cloud/sdk/vpc/volumes.rb +2 -2
  43. data/lib/ibm/cloud/sdk/vpc/vpcs.rb +6 -6
  44. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/connections.rb +2 -2
  45. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb +2 -2
  46. data/lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb +2 -2
  47. data/lib/ibm/cloud/sdk/vpc/vpn_gateways.rb +2 -2
  48. data/lib/ibm/cloud/sdk_http.rb +16 -0
  49. data/lib/ibm/cloud/sdk_http/base_collection.rb +121 -0
  50. data/lib/ibm/cloud/sdk_http/base_http_mixin.rb +63 -0
  51. data/lib/ibm/cloud/sdk_http/base_instance.rb +55 -0
  52. data/lib/ibm/cloud/sdk_http/connection.rb +32 -0
  53. data/lib/ibm/cloud/sdk_http/exceptions.rb +31 -0
  54. data/lib/ibm/cloud/{sdk/vpc/base_mixins → sdk_http}/has_child.rb +3 -7
  55. data/lib/ibm/cloud/sdk_http/iam_token.rb +53 -0
  56. data/lib/ibm/cloud/sdk_http/sdk_response.rb +132 -0
  57. data/lib/ibm/cloud_sdk.rb +39 -0
  58. metadata +21 -12
  59. data/lib/ibm/cloud/sdk/vpc/base_collection.rb +0 -107
  60. data/lib/ibm/cloud/sdk/vpc/base_instance.rb +0 -23
  61. data/lib/ibm/cloud/sdk/vpc/base_vpc.rb +0 -61
  62. data/lib/ibm/cloud/sdk/vpc/cloud_sdk.rb +0 -33
  63. data/lib/ibm/cloud/sdk/vpc/exceptions.rb +0 -33
  64. data/lib/ibm/cloud/sdk/vpc/helpers/connection.rb +0 -96
  65. data/lib/ibm/cloud/sdk/vpc/helpers/response.rb +0 -134
@@ -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
@@ -3,23 +3,19 @@
3
3
 
4
4
  module IBM
5
5
  module Cloud
6
- module SDK
6
+ module SDKHTTP
7
7
  # Access a specific instance by either id or name depending on API.
8
8
  module ChildMixin
9
9
  # Get an instance of the collection object.
10
10
  # @param id [String] ID of Name to search on depending on API.
11
11
  # @return [] The instance object.
12
12
  def instance(id)
13
- @instance.new(self, id)
13
+ @instance.new(self, endpoint: id).refresh
14
14
  end
15
15
 
16
16
  # Return the first_instance returned from a collection get.
17
17
  def first_instance
18
- result = params(limit: 1).all.first
19
- return nil unless result
20
- return nil unless result.key?(:id)
21
-
22
- instance(result.fetch(:id))
18
+ params(limit: 1).all.first
23
19
  end
24
20
  end
25
21
  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.9
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-10-02 00:00:00.000000000 Z
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: http
28
+ name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -65,18 +65,17 @@ files:
65
65
  - lib/ibm/cloud/sdk/power_iaas.rb
66
66
  - lib/ibm/cloud/sdk/resource_controller.rb
67
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
68
71
  - lib/ibm/cloud/sdk/version.rb
69
72
  - lib/ibm/cloud/sdk/vpc.rb
70
- - lib/ibm/cloud/sdk/vpc/base_collection.rb
71
- - lib/ibm/cloud/sdk/vpc/base_instance.rb
72
- - lib/ibm/cloud/sdk/vpc/base_mixins/has_child.rb
73
- - lib/ibm/cloud/sdk/vpc/base_vpc.rb
74
- - lib/ibm/cloud/sdk/vpc/cloud_sdk.rb
75
- - lib/ibm/cloud/sdk/vpc/exceptions.rb
76
73
  - lib/ibm/cloud/sdk/vpc/floatingips.rb
77
74
  - lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb
78
- - lib/ibm/cloud/sdk/vpc/helpers/connection.rb
79
- - lib/ibm/cloud/sdk/vpc/helpers/response.rb
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
80
79
  - lib/ibm/cloud/sdk/vpc/ike_policies.rb
81
80
  - lib/ibm/cloud/sdk/vpc/images.rb
82
81
  - lib/ibm/cloud/sdk/vpc/instance/actions.rb
@@ -107,12 +106,22 @@ files:
107
106
  - lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb
108
107
  - lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb
109
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
110
119
  homepage: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
111
120
  licenses:
112
121
  - '"Apache-2.0"'
113
122
  metadata:
114
123
  homepage_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
115
- changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.9/CHANGELOG.md
124
+ changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.10/CHANGELOG.md
116
125
  post_install_message:
117
126
  rdoc_options: []
118
127
  require_paths:
@@ -1,107 +0,0 @@
1
- # typed: true
2
- # frozen_string_literal: true
3
-
4
- require_relative('base_mixins/has_child')
5
-
6
- module IBM
7
- module Cloud
8
- module SDK
9
- # Container that encapsulates the VPC API.
10
- class BaseCollection < BaseVPC
11
- # This class is used as a base for collection APIs.
12
- # @param parent [Object] The parent instance in the API chain.
13
- # @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word.
14
- # @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used.
15
- # @param child_class [Object] The Object to be used when instanciating the single instance for this class.
16
- def initialize(parent, endpoint, array_key: nil, child_class: nil)
17
- # Setup empty base instance variables.
18
- @params = nil
19
-
20
- array_key ||= endpoint
21
-
22
- # Set the array key and child class.
23
- @array_key ||= array_key
24
- @instance ||= child_class
25
-
26
- (class << self; include ChildMixin; end) if child_class
27
-
28
- super(parent, endpoint)
29
- end
30
-
31
- # A chainable method to set query filters on the collection.
32
- # @example vpc.images.params(limit: 1).all
33
- #
34
- # @param start [String] A server-supplied token determining what resource to start the page on.
35
- # @param limit [Integer] The number of resources to return on a page allowed values are between 1 and 100
36
- # @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
37
- # @return [BaseCollection] This class with the param instance variable set.
38
- def params(start: nil, limit: nil, resource_group: nil)
39
- @params = {}
40
- @params[:start] = start if start
41
- @params[:limit] = limit if limit
42
- @params[:resource_group] = resource_group if resource_group
43
- self
44
- end
45
-
46
- # Retrieve the collection from the cloud.
47
- # @return [IBM::Cloud::SDK::VPC::Response] The http response object.
48
- def fetch
49
- @data ||= get(params: @params)
50
- end
51
-
52
- # Get an iterable for the resource collection.
53
- # @return [Enumerator] Use standard each, next idioms.
54
- def all
55
- each_resource(url)
56
- end
57
-
58
- # Fetch all data and return in an array.
59
- # @return [Array] Hashes of the returned data.
60
- def data
61
- all.to_a
62
- end
63
-
64
- # Determine if the collection has a total_count key in its response.
65
- # @return [Boolean]
66
- def has_count?
67
- fetch.json&.key?(:total_count)
68
- end
69
-
70
- # Get the total count if it exists in the response. Returns nil otherwise.
71
- # @return [Integer] The total count reuturned by the server.
72
- def count
73
- fetch.json&.fetch(:total_count, nil)
74
- end
75
-
76
- # A generic post method to create a resource on the collection.
77
- # @param payload [Hash] A hash of parameters to send to the server.
78
- # @param payload_type [String] One of the following options json, form, or body.
79
- # @return [IBM::Cloud::SDK::VPC::Response] The http response object.
80
- def create(payload, payload_type = 'json')
81
- adhoc(method: 'post', payload_type: payload_type, payload: payload)
82
- end
83
-
84
- private
85
-
86
- # Create a generator that removes the need for pagination.
87
- def each_resource(url, &block)
88
- return enum_for(:each_resource, url) unless block_given?
89
- return unless url
90
-
91
- response = @connection.adhoc('get', url, metadata(@params)).json
92
- resources = response.fetch(@array_key.to_sym)
93
-
94
- resources&.each do |value|
95
- yield value
96
- end
97
- return unless response.key?(:next)
98
-
99
- next_url = response.dig(:next, :href)
100
- return unless next_url
101
-
102
- each_resource(next_url, &block)
103
- end
104
- end
105
- end
106
- end
107
- end