bmc-sdk 0.1.0

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 (5) hide show
  1. checksums.yaml +7 -0
  2. data/lib/bmc-sdk.rb +73 -0
  3. data/lib/commands.rb +283 -0
  4. data/lib/dtos.rb +106 -0
  5. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1e5d542a5466d08fe7c79b45c4d1a318153873c44d67e8b813e0d00fe371647
4
+ data.tar.gz: c0fbeef4be689e9c1290d3d2e2c8cef956db9b92bd4296ff0ec56087527937c6
5
+ SHA512:
6
+ metadata.gz: 5ea5ce3b12fc00a83eaea18790a20f0b04aa176507a9c83b88b112230ed4fc0c50c28a54fd6b71ebfdb6d7907509884605417a6f779df06219e12f127db279f1
7
+ data.tar.gz: 4661731d08c7d774af649c8c4d448ef1577886864753698d1d42a62ae94a3c3a7baf6c8490d95e3e993a9da1db606ba6a1daedfbf989a4a4954d8fcd1a97042e
data/lib/bmc-sdk.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'os'
2
+ require 'oauth2'
3
+ require 'yaml'
4
+
5
+ module Bmc
6
+ end
7
+
8
+ ##
9
+ # This module provides commands and DTOs for working with the BMC API documented at https://developers.phoenixnap.com/docs/bmc/1/overview.
10
+ # Module functions provided here which load configuration assume that client credentials are located in a YAML file at $HOME/.pnap/config.
11
+ #
12
+
13
+ module Bmc::Sdk
14
+
15
+ ##
16
+ # The API entrypoint URL.
17
+ Entrypoint = "https://api.phoenixnap.com/bmc/v1/"
18
+
19
+ ##
20
+ # The OIDC token URL
21
+ TokenURL = "https://auth.phoenixnap.com/auth/realms/BMC/protocol/openid-connect/token"
22
+
23
+ ##
24
+ # The default configuration file location on POSIX systems
25
+ POSIXPath = "/.pnap/config"
26
+
27
+ ##
28
+ # The default configuration file location on Windows systems
29
+ WindowsPath = '\\AppData\\Roaming\\pnap\\config'
30
+
31
+ ##
32
+ # The computed configuration file location based on the current system
33
+ ConfigPath = OS.windows? ? Dir.home + WindowsPath : Dir.home + POSIXPath
34
+
35
+ require 'commands.rb'
36
+ require 'dtos.rb'
37
+
38
+ ##
39
+ # This function creates a new HTTP client with integrated OAuth2 with the provided configuration.
40
+ #
41
+
42
+ def new_client(config)
43
+ cfg = config.dup
44
+ client = OAuth2::Client.new(
45
+ cfg[:client_id],
46
+ cfg[:client_secret],
47
+ token_url: TokenURL)
48
+ return client.client_credentials.get_token
49
+ end
50
+ module_function :new_client
51
+
52
+ ##
53
+ # load_client is a shortcut function which loads configuration from the expected location and uses that configuration to produce a new client.
54
+ #
55
+
56
+ def load_client
57
+ cfg = load_config
58
+ return new_client({
59
+ client_id: cfg['client_id'],
60
+ client_secret: cfg['client_secret']
61
+ })
62
+ end
63
+ module_function :load_client
64
+
65
+ ##
66
+ # load_config loads a YAML file from the expected file path.
67
+
68
+ def load_config
69
+ return YAML.load_file(ConfigPath)
70
+ end
71
+ module_function :load_config
72
+
73
+ end
data/lib/commands.rb ADDED
@@ -0,0 +1,283 @@
1
+ require 'json'
2
+
3
+ module Bmc::Sdk
4
+
5
+ ##
6
+ # CreateServer command create (request) a new server for account. Server DNS will be configured to access Google's public DNS at 8.8.8.8.
7
+ #
8
+
9
+ class CreateServer
10
+ attr_accessor :server
11
+
12
+ # server is a ProvisionedServer
13
+ def initialize(client, server)
14
+ @client = client
15
+ @server = server
16
+ end
17
+
18
+ def execute
19
+ return @client.post(
20
+ "#{Entrypoint}servers",
21
+ headers: {'Content-Type': 'application/json'},
22
+ body: @server.to_json)
23
+ end
24
+ end
25
+
26
+ ##
27
+ # DeleteServer command will deprovision specific server with the given server ID.
28
+ #
29
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/delete
30
+
31
+ class DeleteServer
32
+ attr_accessor :serverID
33
+
34
+ def initialize(client, serverID)
35
+ @client = client
36
+ @serverID = serverID
37
+ end
38
+
39
+ def execute
40
+ return @client.delete(
41
+ "#{Entrypoint}servers/#{serverID}",
42
+ headers: {'Content-Type': 'application/json'})
43
+ end
44
+ end
45
+
46
+ ##
47
+ # GetServer command will get server properties for a given server ID.
48
+ #
49
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/get
50
+
51
+ class GetServer
52
+ attr_accessor :serverID
53
+
54
+ def initialize(client, serverID)
55
+ @client = client
56
+ @serverID = serverID
57
+ end
58
+
59
+ def execute
60
+ return @client.get(
61
+ "#{Entrypoint}servers/#{serverID}",
62
+ headers: {'Content-Type': 'application/json'})
63
+ end
64
+ end
65
+
66
+ ##
67
+ # GetServers command will get server properties for all servers in an account.
68
+ #
69
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/get
70
+
71
+ class GetServers
72
+ def initialize(client)
73
+ @client = client
74
+ end
75
+
76
+ def execute
77
+ return @client.get(
78
+ "#{Entrypoint}servers",
79
+ headers: {'Content-Type': 'application/json'})
80
+ end
81
+ end
82
+
83
+ ##
84
+ # PowerOff command will power off a specific server.
85
+ #
86
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/actions/power-off/post
87
+
88
+ class PowerOff
89
+ attr_accessor :serverID
90
+
91
+ def initialize(client, serverID)
92
+ @client = client
93
+ @serverID = serverID
94
+ end
95
+
96
+ def execute
97
+ return @client.post(
98
+ "#{Entrypoint}servers/#{serverID}/actions/power-off",
99
+ headers: {'Content-Type': 'application/json'})
100
+ end
101
+ end
102
+
103
+ ##
104
+ # PowerOn command will power on a specific server.
105
+ #
106
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/actions/power-on/post
107
+
108
+ class PowerOn
109
+ attr_accessor :serverID
110
+
111
+ def initialize(client, serverID)
112
+ @client = client
113
+ @serverID = serverID
114
+ end
115
+
116
+ def execute
117
+ return @client.post(
118
+ "#{Entrypoint}servers/#{serverID}/actions/power-on",
119
+ headers: {'Content-Type': 'application/json'})
120
+ end
121
+ end
122
+
123
+ ##
124
+ # Reboot command will reboot a specific server.
125
+ #
126
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/actions/reboot/post
127
+
128
+ class Reboot
129
+ attr_accessor :serverID
130
+
131
+ def initialize(client, serverID)
132
+ @client = client
133
+ @serverID = serverID
134
+ end
135
+
136
+ def execute
137
+ return @client.post(
138
+ "#{Entrypoint}servers/#{serverID}/actions/reboot",
139
+ headers: {'Content-Type': 'application/json'})
140
+ end
141
+ end
142
+
143
+ ##
144
+ # Reset command will reset the configuration for a specific server.
145
+ #
146
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/actions/reset/post
147
+
148
+ class Reset
149
+ attr_accessor :server
150
+
151
+ # server is a ProvisionedServer
152
+ def initialize(client, server)
153
+ @client = client
154
+ @server = server
155
+ end
156
+
157
+ def execute
158
+ return @client.post(
159
+ "#{Entrypoint}servers/#{@server.id}/actions/reset",
160
+ headers: {'Content-Type': 'application/json'},
161
+ body: @server.to_json)
162
+ end
163
+ end
164
+
165
+ ##
166
+ # Shutdown command will shutdown a specific server
167
+ #
168
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/servers/%7Bserver_id%7D/actions/shutdown/post
169
+
170
+ class Shutdown
171
+ attr_accessor :serverID
172
+
173
+ def initialize(client, serverID)
174
+ @client = client
175
+ @serverID = serverID
176
+ end
177
+
178
+ def execute
179
+ return @client.post(
180
+ "#{Entrypoint}servers/#{serverID}/actions/shutdown",
181
+ headers: {'Content-Type': 'application/json'})
182
+ end
183
+ end
184
+
185
+ ##
186
+ # GetSSHKeys command will list all SSH keys associated with an account
187
+ #
188
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/ssh-keys/get
189
+
190
+ class GetSSHKeys
191
+ def initialize(client)
192
+ @client = client
193
+ end
194
+
195
+ def execute
196
+ return @client.get(
197
+ "#{Entrypoint}ssh-keys",
198
+ headers: {'Content-Type': 'application/json'})
199
+ end
200
+ end
201
+
202
+ ##
203
+ # CreateSSHKey command will create a new SSH key with the provided properties.
204
+ #
205
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/ssh-keys/post
206
+
207
+ class CreateSSHKey
208
+ attr_accessor :sshKey
209
+
210
+ def initialize(client, sshKey)
211
+ @client = client
212
+ @sshKey = sshKey
213
+ end
214
+
215
+ def execute
216
+ return @client.post(
217
+ "#{Entrypoint}ssh-keys",
218
+ headers: {'Content-Type': 'application/json'},
219
+ body: @sshKey.to_json)
220
+ end
221
+ end
222
+
223
+ ##
224
+ # DeleteSSHKey command will delete the SSH key with the specified ID.
225
+ #
226
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/ssh-keys/%7Bssh_key_id%7D/delete
227
+
228
+ class DeleteSSHKey
229
+ attr_accessor :sshKeyID
230
+
231
+ def initialize(client, sshKeyID)
232
+ @client = client
233
+ @sshKeyID = sshKeyID
234
+ end
235
+
236
+ def execute
237
+ return @client.delete(
238
+ "#{Entrypoint}ssh-keys/#{sshKeyID}",
239
+ headers: {'Content-Type': 'application/json'})
240
+ end
241
+ end
242
+
243
+ ##
244
+ # GetSSHKey command will retrieve the SSH key with the specified ID.
245
+ #
246
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/ssh-keys/%7Bssh_key_id%7D/get
247
+
248
+ class GetSSHKey
249
+ attr_accessor :sshKeyID
250
+
251
+ def initialize(client, sshKeyID)
252
+ @client = client
253
+ @sshKeyID = sshKeyID
254
+ end
255
+
256
+ def execute
257
+ return @client.get(
258
+ "#{Entrypoint}ssh-keys/#{sshKeyID}",
259
+ headers: {'Content-Type': 'application/json'})
260
+ end
261
+ end
262
+
263
+ ##
264
+ # GetSSHKey command will update the SSH key with the specified ID.
265
+ #
266
+ # @see https://developers.phoenixnap.com/docs/bmc/1/routes/ssh-keys/%7Bssh_key_id%7D/put
267
+
268
+ class EditSSHKey
269
+ attr_accessor :sshKey
270
+
271
+ def initialize(client, sshKey)
272
+ @client = client
273
+ @sshKey = sshKey
274
+ end
275
+
276
+ def execute
277
+ return @client.put(
278
+ "#{Entrypoint}ssh-keys/#{@sshKey.id}",
279
+ headers: {'Content-Type': 'application/json'},
280
+ body: @sshKey.to_json)
281
+ end
282
+ end
283
+ end
data/lib/dtos.rb ADDED
@@ -0,0 +1,106 @@
1
+ module Bmc::Sdk
2
+
3
+ ##
4
+ # ErrorMessage is used to transmit details on error responses.
5
+ #
6
+ # @see https://developers.phoenixnap.com/docs/bmc/1/types/Error
7
+
8
+ class ErrorMessage
9
+ attr_accessor :message, :validationErrors
10
+ def initialize(message, validationErrors)
11
+ @message = message
12
+ @validationErrors = validationErrors
13
+ end
14
+ def to_json(*a)
15
+ {message: @message, validationErrors: @validationErrors}.to_json(*a)
16
+ end
17
+ end
18
+
19
+ ##
20
+ # LongServer is used to transmit details on fully specified server responses.
21
+ #
22
+ # @see https://developers.phoenixnap.com/docs/bmc/1/types/Server
23
+
24
+ class LongServer
25
+ attr_accessor :id, :status, :hostname, :description, :privateIPAddresses, :publicIPAddresses, :os, :type, :location, :cpu, :ram, :storage
26
+ def initialize(id, status, hostname, description, privateIPAddresses, publicIPAddresses, os, type, location, cpu, ram, storage)
27
+ @id = id
28
+ @status = status
29
+ @hostname = hostname
30
+ @description = description
31
+ @privateIPAddresses = privateIPAddresses
32
+ @publicIPAddresses = publicIPAddresses
33
+ @os = os
34
+ @type = type
35
+ @location = location
36
+ @cpu = cpu
37
+ @ram = ram
38
+ @storage = storage
39
+ end
40
+ def to_json(*a)
41
+ {id: @id, status: @status, hostname: @hostname, description: @description, privateIpAddresses: @privateIPAddresses, publicIPAddresses: @publicIpAddresses, os: @os, type: @type, location: @location, cpu: @cpu, ram: @ram, storage: @storage }.to_json(*a)
42
+ end
43
+ end
44
+
45
+ ##
46
+ # ProvisionedServer is used to transmit details on CreateServer commands.
47
+ #
48
+ # @see https://developers.phoenixnap.com/docs/bmc/1/types/ServerCreate
49
+
50
+ class ProvisionedServer
51
+ attr_accessor :id, :status, :hostname, :description, :os, :type, :location, :sshKeys, :sshKeyIds
52
+ def initialize(id, status, hostname, description, os, type, location, sshKeys, sshKeyIds)
53
+ @id = id
54
+ @status = status
55
+ @hostname = hostname
56
+ @description = description
57
+ @os = os
58
+ @type = type
59
+ @location = location
60
+ @sshKeys = sshKeys
61
+ @sshKeyIds = sshKeyIds
62
+ end
63
+ def to_json(*a)
64
+ {id: @id, status: @status, hostname: @hostname, description: @description, os: @os, type: @type, location: @location, sshKeys: @sshKeys, sshKeyIds: @sshKeyIds}.to_json(*a)
65
+ end
66
+ end
67
+
68
+ ##
69
+ # ServerResetSpec is used to transmit details on ServerReset commands.
70
+ #
71
+ # @see https://developers.phoenixnap.com/docs/bmc/1/types/ServerReset
72
+
73
+ class ServerResetSpec
74
+ attr_accessor :id, :sshKeys, :sshKeyIds, :installDefaultSshKeys
75
+ def initialize(id, sshKeys, sshKeyIds, installDefaultSshKeys)
76
+ @id = id
77
+ @sshKeys = sshKeys
78
+ @sshKeyIds = sshKeyIds
79
+ @installDefaultSshKeys = installDefaultSshKeys
80
+ end
81
+ def to_json(*a)
82
+ {id: @id, status: @status, sshKeys: @sshKeys, sshKeyIds: @sshKeyIds, installDefaultSshKeys: @installDefaultSshKeys}.to_json(*a)
83
+ end
84
+ end
85
+
86
+ ##
87
+ # SSHKeySpec is used to transmit details for BMC SSH Key objects.
88
+ #
89
+ # @see https://developers.phoenixnap.com/docs/bmc/1/types/SshKey
90
+
91
+ class SSHKeySpec
92
+ attr_accessor :id, :default, :name, :key, :fingerprint, :createdOn, :lastUpdatedOn
93
+ def initialize(id, default, name, key, fingerprint, createdOn, lastUpdatedOn)
94
+ @id = id
95
+ @default = default
96
+ @name = name
97
+ @key = key
98
+ @fingerprint = fingerprint
99
+ @createdOn = createdOn
100
+ @lastUpdatedOn = lastUpdatedOn
101
+ end
102
+ def to_json(*a)
103
+ {id: @id, default: @default, name: @name, key: @key, fingerprint: @fingerprint, createdOn: @createdOn, lastUpdatedOn: @lastUpdatedOn}.to_json(*a)
104
+ end
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bmc-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PhoenixNAP
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: os
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oauth2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.4
55
+ description: An SDK for interacting with the BMC API. See https://developers.phoenixnap.com/docs/bmc/1/overview
56
+ email: support@phoenixnap.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/bmc-sdk.rb
62
+ - lib/commands.rb
63
+ - lib/dtos.rb
64
+ homepage: https://github.com/phoenixnap/bmc-sdk-ruby
65
+ licenses:
66
+ - MPL-2.0
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.0.8
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: An SDK for interacting with the BMC API.
87
+ test_files: []