ibm-cloud-sdk 0.1.6 → 0.1.11

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 (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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ require_relative 'base_http_mixin'
6
+
7
+ module IBM
8
+ module Cloud
9
+ # Module holds basic HTTP functionality.
10
+ module SDKHTTP
11
+ # Container that encapsulates the VPC API.
12
+ class BaseInstance
13
+ include BaseHTTPMixin
14
+
15
+ def initialize(parent, data: {}, id_key: 'id', endpoint: nil)
16
+ @data = data
17
+
18
+ endpoint ||= data[id_key.to_sym]
19
+ @connection = parent.connection
20
+ @token = parent.token
21
+ @endpoint = parent.url(endpoint)
22
+ @logger = parent.logger
23
+ end
24
+
25
+ attr_reader :logger, :endpoint, :token, :connection
26
+
27
+ # Fetch the instance details for this resource.
28
+ # @return [SDKResponse] The HTTP response object.
29
+ def details
30
+ get.hash_response
31
+ end
32
+
33
+ # Fetch the instance details from the server. Clear current data and save the results.
34
+ # @return [BaseInstance] This instance for chaining.
35
+ def refresh
36
+ @data.replace(details)
37
+ self
38
+ end
39
+
40
+ # Send an update to the server for this resource.
41
+ def update(payload)
42
+ patch(payload: payload)
43
+ end
44
+
45
+ # Send a delete request to the server for this resource.
46
+ def remove
47
+ delete
48
+ end
49
+
50
+ extend Forwardable
51
+ def_delegators :@data, :[], :dig, :each, :each_pair, :fetch, :has_key?, :has_value?, :include?, :index, :inspect, :key?, :keys, :length, :merge, :merge!, :clear, :to_h, :value?, :values, :pretty_print
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+ require_relative 'sdk_response'
5
+
6
+ module IBM
7
+ module Cloud
8
+ module SDKHTTP
9
+ # Class to wrap the HTTP Provider implementation.
10
+ class HTTPConnection
11
+ include HTTParty
12
+
13
+ def initialize(logger)
14
+ @logger = logger
15
+ self.class.logger(logger, :debug, :apache)
16
+ end
17
+
18
+ # Set the logger type to curl which has more information.
19
+ def verbose_logger=(verbosity)
20
+ default_options[:log_format] = verbosity ? :curl : :apache
21
+ end
22
+
23
+ def default_options
24
+ self.class.default_options
25
+ end
26
+
27
+ def add_proxy(host, port, user, password)
28
+ self.class.http_proxy(host, port, user, password)
29
+ end
30
+
31
+ def request(method, path, params)
32
+ response = self.class.send(method.to_sym, path, params)
33
+ SDKResponse.new(response)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -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.6
4
+ version: 0.1.11
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-23 00:00:00.000000000 Z
11
+ date: 2020-10-28 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
  - - ">="
@@ -60,19 +60,22 @@ 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/helpers/connection.rb
75
- - 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
76
79
  - lib/ibm/cloud/sdk/vpc/ike_policies.rb
77
80
  - lib/ibm/cloud/sdk/vpc/images.rb
78
81
  - lib/ibm/cloud/sdk/vpc/instance/actions.rb
@@ -103,12 +106,22 @@ files:
103
106
  - lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb
104
107
  - lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb
105
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
106
119
  homepage: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
107
120
  licenses:
108
121
  - '"Apache-2.0"'
109
122
  metadata:
110
123
  homepage_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby
111
- changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.6/CHANGELOG.md
124
+ changelog_uri: https://github.com/IBM-Cloud/ibm-cloud-sdk-ruby/blob/v0.1.11/CHANGELOG.md
112
125
  post_install_message:
113
126
  rdoc_options: []
114
127
  require_paths: