ibm-cloud-sdk 0.1.2 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -0
- data/Gemfile +2 -0
- data/ibm-cloud-sdk.gemspec +3 -1
- data/lib/ibm/cloud/sdk.rb +3 -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 +181 -4
- data/lib/ibm/cloud/sdk/resource_controller.rb +2 -8
- data/lib/ibm/cloud/sdk/version.rb +1 -1
- data/lib/ibm/cloud/sdk/vpc.rb +133 -0
- data/lib/ibm/cloud/sdk/vpc/base_collection.rb +108 -0
- data/lib/ibm/cloud/sdk/vpc/base_instance.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/base_vpc.rb +61 -0
- data/lib/ibm/cloud/sdk/vpc/cloud_sdk.rb +33 -0
- data/lib/ibm/cloud/sdk/vpc/exceptions.rb +33 -0
- data/lib/ibm/cloud/sdk/vpc/floatingips.rb +20 -0
- data/lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb +20 -0
- data/lib/ibm/cloud/sdk/vpc/helpers/connection.rb +96 -0
- data/lib/ibm/cloud/sdk/vpc/helpers/response.rb +134 -0
- data/lib/ibm/cloud/sdk/vpc/ike_policies.rb +24 -0
- data/lib/ibm/cloud/sdk/vpc/images.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/instance/actions.rb +19 -0
- data/lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb +28 -0
- data/lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/instance_profiles.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/instances.rb +64 -0
- data/lib/ibm/cloud/sdk/vpc/ipsec_policies.rb +24 -0
- data/lib/ibm/cloud/sdk/vpc/keys.rb +43 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/listeners.rb +30 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/members.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/policies.rb +30 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/pools.rb +28 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/rules.rb +25 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancers.rb +19 -0
- data/lib/ibm/cloud/sdk/vpc/network_acls.rb +39 -0
- data/lib/ibm/cloud/sdk/vpc/operating_systems.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/public_gateways.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/regions.rb +35 -0
- data/lib/ibm/cloud/sdk/vpc/security_groups.rb +48 -0
- data/lib/ibm/cloud/sdk/vpc/subnets.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/volume_profiles.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/volumes.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/vpcs.rb +60 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/connections.rb +35 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb +32 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb +32 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateways.rb +25 -0
- metadata +60 -2
@@ -0,0 +1,133 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'vpc/base_vpc'
|
5
|
+
require_relative 'vpc/base_collection'
|
6
|
+
require_relative 'vpc/base_instance'
|
7
|
+
require_relative 'vpc/exceptions'
|
8
|
+
|
9
|
+
require_relative 'vpc/floatingips'
|
10
|
+
require_relative 'vpc/flowlogcollectors'
|
11
|
+
require_relative 'vpc/ike_policies'
|
12
|
+
require_relative 'vpc/images'
|
13
|
+
require_relative 'vpc/instance_profiles'
|
14
|
+
require_relative 'vpc/instances'
|
15
|
+
require_relative 'vpc/ipsec_policies'
|
16
|
+
require_relative 'vpc/keys'
|
17
|
+
require_relative 'vpc/load_balancers'
|
18
|
+
require_relative 'vpc/network_acls'
|
19
|
+
require_relative 'vpc/operating_systems'
|
20
|
+
require_relative 'vpc/public_gateways'
|
21
|
+
require_relative 'vpc/regions'
|
22
|
+
require_relative 'vpc/security_groups'
|
23
|
+
require_relative 'vpc/subnets'
|
24
|
+
require_relative 'vpc/volume_profiles'
|
25
|
+
require_relative 'vpc/volumes'
|
26
|
+
require_relative 'vpc/vpcs'
|
27
|
+
require_relative 'vpc/vpn_gateways'
|
28
|
+
|
29
|
+
module IBM
|
30
|
+
module Cloud
|
31
|
+
module SDK
|
32
|
+
# Container that encapsulates the VPC API.
|
33
|
+
class Vpc < BaseVPC
|
34
|
+
# Create an API Client object for the VPC IaaS service
|
35
|
+
#
|
36
|
+
# @param region [String] the IBM Power Cloud instance region
|
37
|
+
# @param connection [IBM::Cloud::SDK::VPC::Connection] A connection object.
|
38
|
+
# @param logger [Logger] An instance of an instanciated logger.
|
39
|
+
def initialize(region, connection, logger: nil)
|
40
|
+
@region = region
|
41
|
+
@connection = connection
|
42
|
+
|
43
|
+
@logger = logger || Logger.new($stdout, level: :warn)
|
44
|
+
end
|
45
|
+
|
46
|
+
attr_reader :connection, :logger
|
47
|
+
attr_accessor :region
|
48
|
+
|
49
|
+
# The Region API endpoint.
|
50
|
+
def endpoint
|
51
|
+
"https://#{region.sub(/-\d$/, '')}.iaas.cloud.ibm.com/v1"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Entry point to the Floating IPs API.
|
55
|
+
def floating_ips
|
56
|
+
VPC::FloatingIPs.new(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
def flowlog_collectors
|
60
|
+
VPC::FlowLogCollectors.new(self)
|
61
|
+
end
|
62
|
+
|
63
|
+
def ike_policies
|
64
|
+
VPC::IKEPolicies.new(self)
|
65
|
+
end
|
66
|
+
|
67
|
+
def images
|
68
|
+
VPC::Images.new(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
def instance_profiles
|
72
|
+
VPC::InstanceProfiles.new(self)
|
73
|
+
end
|
74
|
+
|
75
|
+
def instances
|
76
|
+
VPC::Instances.new(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
def ipsec_policies
|
80
|
+
VPC::IPSecPolicies.new(self)
|
81
|
+
end
|
82
|
+
|
83
|
+
def keys
|
84
|
+
VPC::Keys.new(self)
|
85
|
+
end
|
86
|
+
|
87
|
+
def load_balancers
|
88
|
+
VPC::LoadBalancers.new(self)
|
89
|
+
end
|
90
|
+
|
91
|
+
def network_acls
|
92
|
+
VPC::NetworkACLs.new(self)
|
93
|
+
end
|
94
|
+
|
95
|
+
def operating_systems
|
96
|
+
VPC::OperatingSystems.new(self)
|
97
|
+
end
|
98
|
+
|
99
|
+
def public_gateways
|
100
|
+
VPC::PublicGateways.new(self)
|
101
|
+
end
|
102
|
+
|
103
|
+
def regions
|
104
|
+
VPC::Regions.new(self)
|
105
|
+
end
|
106
|
+
|
107
|
+
def security_groups
|
108
|
+
VPC::SecurityGroups.new(self)
|
109
|
+
end
|
110
|
+
|
111
|
+
def subnets
|
112
|
+
VPC::Subnets.new(self)
|
113
|
+
end
|
114
|
+
|
115
|
+
def volumes_profiles
|
116
|
+
VPC::VolumeProfiles.new(self)
|
117
|
+
end
|
118
|
+
|
119
|
+
def volumes
|
120
|
+
VPC::Volumes.new(self)
|
121
|
+
end
|
122
|
+
|
123
|
+
def vpcs
|
124
|
+
VPC::Vpcs.new(self)
|
125
|
+
end
|
126
|
+
|
127
|
+
def vpn_gateways
|
128
|
+
VPC::VPNGateways.new(self)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,108 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
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 BaseInstance < BaseVPC
|
9
|
+
def details
|
10
|
+
get.hash_response
|
11
|
+
end
|
12
|
+
|
13
|
+
def update(payload)
|
14
|
+
patch(payload)
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove
|
18
|
+
delete
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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 BaseVPC
|
9
|
+
def initialize(parent, endpoint = nil)
|
10
|
+
@endpoint = parent.url(endpoint)
|
11
|
+
@connection = parent.connection
|
12
|
+
@logger = parent.logger
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :endpoint, :connection, :logger
|
16
|
+
|
17
|
+
def adhoc(method: 'get', path: nil, params: {}, payload_type: 'json', payload: {})
|
18
|
+
@connection.adhoc(method.to_sym, url(path), metadata(params, payload_type, payload))
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(path: nil, params: {})
|
22
|
+
adhoc(method: 'get', path: path, params: params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def post(payload = {}, path: nil, params: {}, type: 'json')
|
26
|
+
adhoc(method: 'post', path: path, params: params, payload: payload, payload_type: type)
|
27
|
+
end
|
28
|
+
|
29
|
+
def put(payload = {}, path: nil, params: {})
|
30
|
+
adhoc(method: 'put', path: path, params: params, payload: payload)
|
31
|
+
end
|
32
|
+
|
33
|
+
def patch(payload = {}, path: nil, params: {})
|
34
|
+
adhoc(method: 'patch', path: path, params: params, payload: payload)
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(path: nil, params: {})
|
38
|
+
adhoc(method: 'delete', path: path, params: params)
|
39
|
+
end
|
40
|
+
|
41
|
+
def url(path = nil)
|
42
|
+
return endpoint unless path
|
43
|
+
|
44
|
+
"#{endpoint}/#{path}"
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def metadata(params = {}, payload_type = 'json', payload = {})
|
50
|
+
params ||= {}
|
51
|
+
pt = {
|
52
|
+
params: { version: '2020-08-01', generation: 2 }.merge(params)
|
53
|
+
}
|
54
|
+
|
55
|
+
pt[payload_type.to_sym] = payload unless payload.empty?
|
56
|
+
pt
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'http'
|
4
|
+
require 'ibm/cloud/sdk/vpc/helpers/connection'
|
5
|
+
require 'ibm/cloud/sdk/vpc'
|
6
|
+
|
7
|
+
module IBM
|
8
|
+
# Holds the SDK pieces.
|
9
|
+
class CloudSDK
|
10
|
+
def initialize(api_key, logger: nil, options: {})
|
11
|
+
@logger = logger
|
12
|
+
@logger ||= Logger.new($stdout, level: :warn)
|
13
|
+
|
14
|
+
@client = HTTP.use(http_options(options))
|
15
|
+
@connection = IBM::Cloud::SDK::VPC::Connection.new(api_key, logger: @logger, client: @client)
|
16
|
+
end
|
17
|
+
|
18
|
+
def http_options(options = {})
|
19
|
+
options.merge(
|
20
|
+
{
|
21
|
+
logging: { logger: @logger },
|
22
|
+
auto_deflate: {}
|
23
|
+
}
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :logger, :connection
|
28
|
+
|
29
|
+
def vpc(region = 'us-east')
|
30
|
+
IBM::Cloud::SDK::Vpc.new(region, @connection, logger: @logger)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IBM
|
4
|
+
module Cloud
|
5
|
+
module SDK
|
6
|
+
module VPC
|
7
|
+
# Module to contain all custom exception classes.
|
8
|
+
module Exceptions
|
9
|
+
# An exception for http with a response attribute.
|
10
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
11
|
+
# @param msg [String] A human readable message.
|
12
|
+
class ExceptionWithResponse < RuntimeError
|
13
|
+
def initialize(response, msg)
|
14
|
+
@response = response
|
15
|
+
super(msg)
|
16
|
+
end
|
17
|
+
# @return [IBM::Cloud::SDK::VPC::Response] The response
|
18
|
+
attr_reader :response
|
19
|
+
end
|
20
|
+
|
21
|
+
# An exception for http status errors with a response attribute.
|
22
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
23
|
+
class HttpStatusError < ExceptionWithResponse
|
24
|
+
def initialize(response)
|
25
|
+
msg = "Invalid status #{response.code} for url \"#{response.url}\", #{response.reason}. #{response.body}"
|
26
|
+
super(response, msg)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# typed: false
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
module VPC
|
8
|
+
# Class that deals with groups of floating IPs.
|
9
|
+
class FloatingIPs < BaseCollection
|
10
|
+
def initialize(parent)
|
11
|
+
super(parent, 'floating_ips', child_class: FloatingIP)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Class that deals with a single floating IP.
|
16
|
+
class FloatingIP < BaseInstance; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
module VPC
|
8
|
+
# A list of subnets
|
9
|
+
class FlowLogCollectors < BaseCollection
|
10
|
+
def initialize(parent)
|
11
|
+
super(parent, 'flow_log_collectors', child_class: FlowLogCollectors)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# A single subnet
|
16
|
+
class FlowLogCollector < BaseInstance; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|