morpheus-cli 4.1.1 → 4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/morpheus/api/api_client.rb +70 -1
- data/lib/morpheus/api/auth_interface.rb +11 -2
- data/lib/morpheus/cli/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7753eaeda16637f0e5dc4789805e09872fa92cc7bd487f6abebdbc30f021211c
|
4
|
+
data.tar.gz: 44b028c23aa86ca2bf4a4d648930985fd55c7050713ad483ea0ae9f1419b4283
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 577cd169ab4f3235e536d46603f744b39c233a09664fbe4b81294c662ffbeceb5f4402c763b3653446e17ad021b31ac0bc58c96232fe2b27ce17c6d0dd5f03f4
|
7
|
+
data.tar.gz: 0e185c2007c4f5c7ca8c7158f0ecc872faeecdc34773e19bb61983c45238a798038652df07a0ea3dfde0c75bf5dfae4c2c04347841b1d730f70524aa664373e5
|
@@ -3,17 +3,48 @@ require 'uri'
|
|
3
3
|
require 'rest-client'
|
4
4
|
|
5
5
|
class Morpheus::APIClient
|
6
|
+
# Initialize a new APIClient
|
7
|
+
# client = APIClient.new(url:"https://morpheus.yourcompany.com", verify_ssl:false)
|
8
|
+
# This old method signature is being deprecated:
|
9
|
+
# client = APIClient.new(access_token, refresh_token, expires_in, base_url, verify_ssl, options={})
|
10
|
+
#
|
6
11
|
def initialize(access_token, refresh_token=nil,expires_in = nil, base_url=nil, verify_ssl=true, options={})
|
12
|
+
if access_token.is_a?(Hash)
|
13
|
+
client_opts = access_token.clone()
|
14
|
+
access_token = client_opts[:access_token]
|
15
|
+
refresh_token = client_opts[:refresh_token]
|
16
|
+
base_url = client_opts[:url] || client_opts[:base_url]
|
17
|
+
expires_in = client_opts[:expires_in]
|
18
|
+
verify_ssl = client_opts.key?(:verify_ssl) ? client_opts[:verify_ssl] : true
|
19
|
+
options = refresh_token.is_a?(Hash) ? refresh_token.clone() : {}
|
20
|
+
end
|
7
21
|
@access_token = access_token
|
8
22
|
@refresh_token = refresh_token
|
9
23
|
@base_url = base_url
|
24
|
+
if @base_url.to_s.empty?
|
25
|
+
raise "#{self.class} requires option :url"
|
26
|
+
end
|
27
|
+
@base_url = @base_url.chomp("/")
|
28
|
+
# todo: validate URI
|
10
29
|
if expires_in != nil
|
11
|
-
@expires_at =
|
30
|
+
@expires_at = Time.now + expires_in
|
12
31
|
end
|
13
32
|
set_ssl_verification_enabled(verify_ssl)
|
14
33
|
setopts(options)
|
15
34
|
end
|
16
35
|
|
36
|
+
def url
|
37
|
+
@base_url
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
"<##{self.class}:#{self.object_id.to_s(8)} @url=#{@base_url} @verify_ssl=#{@verify_ssl} @access_token=#{@access_token ? '************' : nil} @refresh_token=#{@access_token ? '************' : nil} @expires_at=#{@expires_at} @options=#{@options}>"
|
42
|
+
end
|
43
|
+
|
44
|
+
def inspect
|
45
|
+
to_s
|
46
|
+
end
|
47
|
+
|
17
48
|
def dry_run(val=true)
|
18
49
|
@dry_run = !!val
|
19
50
|
self
|
@@ -184,6 +215,44 @@ class Morpheus::APIClient
|
|
184
215
|
end
|
185
216
|
end
|
186
217
|
|
218
|
+
def logged_in?
|
219
|
+
!!@access_token
|
220
|
+
end
|
221
|
+
|
222
|
+
def login(username, password)
|
223
|
+
@access_token, @refresh_token, @expires_at = nil, nil, nil
|
224
|
+
response = auth.login(username, password)
|
225
|
+
@access_token = response['access_token']
|
226
|
+
@refresh_token = response['refresh_token']
|
227
|
+
if response['expires_in'] != nil
|
228
|
+
@expires_at = Time.now + response['expires_in']
|
229
|
+
end
|
230
|
+
# return response
|
231
|
+
return self
|
232
|
+
end
|
233
|
+
|
234
|
+
def refresh_token()
|
235
|
+
if @refresh_token.nil?
|
236
|
+
raise "#{self.class} does not currently have a refresh_token"
|
237
|
+
end
|
238
|
+
response = auth.use_refresh_token(@refresh_token)
|
239
|
+
@access_token = response['access_token']
|
240
|
+
@refresh_token = response['refresh_token']
|
241
|
+
if response['expires_in'] != nil
|
242
|
+
@expires_at = Time.now + response['expires_in']
|
243
|
+
end
|
244
|
+
@access_token = response['access_token']
|
245
|
+
# return response
|
246
|
+
return self
|
247
|
+
end
|
248
|
+
|
249
|
+
def logout
|
250
|
+
@access_token = nil
|
251
|
+
@refresh_token = nil
|
252
|
+
@expires_at = nil
|
253
|
+
return self
|
254
|
+
end
|
255
|
+
|
187
256
|
def auth
|
188
257
|
Morpheus::AuthInterface.new(@base_url, @access_token).setopts(@options)
|
189
258
|
end
|
@@ -2,7 +2,7 @@ require 'morpheus/api/api_client'
|
|
2
2
|
|
3
3
|
class Morpheus::AuthInterface < Morpheus::APIClient
|
4
4
|
|
5
|
-
attr_reader :access_token
|
5
|
+
#attr_reader :access_token, :refresh_token, :expires_at
|
6
6
|
|
7
7
|
def initialize(base_url, access_token=nil)
|
8
8
|
@base_url = base_url
|
@@ -10,7 +10,7 @@ class Morpheus::AuthInterface < Morpheus::APIClient
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def login(username, password)
|
13
|
-
@access_token = nil
|
13
|
+
@access_token, @refresh_token, @expires_at = nil, nil, nil
|
14
14
|
url = "#{@base_url}/oauth/token"
|
15
15
|
params = {grant_type: 'password', scope:'write', client_id: 'morph-cli', username: username}
|
16
16
|
payload = {password: password}
|
@@ -18,6 +18,10 @@ class Morpheus::AuthInterface < Morpheus::APIClient
|
|
18
18
|
response = execute(opts)
|
19
19
|
return response if @dry_run
|
20
20
|
@access_token = response['access_token']
|
21
|
+
@refresh_token = response['refresh_token']
|
22
|
+
if response['expires_in'] != nil
|
23
|
+
@expires_at = Time.now + response['expires_in']
|
24
|
+
end
|
21
25
|
return response
|
22
26
|
end
|
23
27
|
|
@@ -31,10 +35,15 @@ class Morpheus::AuthInterface < Morpheus::APIClient
|
|
31
35
|
response = execute(opts)
|
32
36
|
return response if @dry_run
|
33
37
|
@access_token = response['access_token']
|
38
|
+
@refresh_token = response['refresh_token']
|
39
|
+
if response['expires_in'] != nil
|
40
|
+
@expires_at = Time.now + response['expires_in']
|
41
|
+
end
|
34
42
|
return response
|
35
43
|
end
|
36
44
|
|
37
45
|
def logout()
|
46
|
+
# super.logout()
|
38
47
|
if @access_token
|
39
48
|
# todo: expire the token
|
40
49
|
end
|
data/lib/morpheus/cli/version.rb
CHANGED