rightscale_selfservice 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE +22 -0
- data/README.md +4 -0
- data/bin/rightscale_selfservice +24 -0
- data/lib/rightscale_selfservice/api/client.rb +188 -0
- data/lib/rightscale_selfservice/api/interface.json +15984 -0
- data/lib/rightscale_selfservice/api/resource.rb +72 -0
- data/lib/rightscale_selfservice/api/service.rb +57 -0
- data/lib/rightscale_selfservice/cli/base.rb +52 -0
- data/lib/rightscale_selfservice/cli/main.rb +29 -0
- data/lib/rightscale_selfservice/cli/template.rb +45 -0
- data/lib/rightscale_selfservice/utilities/template.rb +92 -0
- data/lib/rightscale_selfservice.rb +23 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NmYzNzk0MWJmMzc4MTIxNGIyOWZkMDJiMTUxMTczNDVhOTg0OGNhMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YzA5YjA2NjAyMWE2ODliY2RmMTE3ZjliMmVlYzY3ZGNjNDY4NzVlNA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Y2Q1OTJlNGY4YTQ4NjNjZjU5NTZlYzEyMTBjMjIzZTViNzFkYTM1MDA5N2Rl
|
10
|
+
YzliM2E2MTI2Yjc5MThmYmJhYjg5Yjg4ODE5OTFiMjNhZTE4OGIzNjdiZTg2
|
11
|
+
MTgwYzJjOWU1Mjk0MGNmOTczOGNiZDY4NzU4ZTBjMmI5MTQ2ODM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTNkNGI2ZmU2ZmI4MzRmYjdiZDkzZDQ5MzU4MDBkNDY0NjRlMTQ5OTdhNDUz
|
14
|
+
MjFlM2E1OGNlOTMxYjQyNWM4MmI3OTY4Yzg1NzNjOGNhNWU0M2ZhZjRjYmFm
|
15
|
+
ZjY0Yjc4YzU0NzA4MDkyNGQ5ZmExY2MxMDA0ZTc3OTY1NjkwZTE=
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Ryan Geyer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright (c) 2014 Ryan Geyer
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
# this software and associated documentation files (the "Software"), to deal in
|
7
|
+
# the Software without restriction, including without limitation the rights to
|
8
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
# subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "rightscale_selfservice"))
|
23
|
+
|
24
|
+
RightScaleSelfService::Cli::Main.start(ARGV)
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# Copyright (c) 2014 Ryan Geyer
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
# this software and associated documentation files (the "Software"), to deal in
|
5
|
+
# the Software without restriction, including without limitation the rights to
|
6
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
# subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
11
|
+
# copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
|
+
|
20
|
+
require 'rest-client'
|
21
|
+
|
22
|
+
module RightScaleSelfService
|
23
|
+
module Api
|
24
|
+
class Client
|
25
|
+
|
26
|
+
def self.get_known_account_id_tokens
|
27
|
+
[":account_id",":catalog_id",":collection_id",":project_id"]
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_accessor :selfservice_url
|
31
|
+
|
32
|
+
attr_accessor :api_url
|
33
|
+
|
34
|
+
attr_accessor :logger
|
35
|
+
|
36
|
+
attr_accessor :account_id
|
37
|
+
|
38
|
+
attr_reader :interface
|
39
|
+
|
40
|
+
def initialize(params)
|
41
|
+
@services = {}
|
42
|
+
@auth = {"cookie" => {}, "authorization" => {}}
|
43
|
+
required_params = [:account_id,:selfservice_url,:api_url]
|
44
|
+
# allowed_params = [
|
45
|
+
# :access_token,
|
46
|
+
# :refresh_token,
|
47
|
+
# :account_id,
|
48
|
+
# :selfservice_url,
|
49
|
+
# :api_url,
|
50
|
+
# :email,
|
51
|
+
# :password,
|
52
|
+
# :logger
|
53
|
+
# ]
|
54
|
+
|
55
|
+
# Use the defined logger, or log to a blackhole
|
56
|
+
if params.include?(:logger)
|
57
|
+
@logger = params[:logger]
|
58
|
+
else
|
59
|
+
@logger = NullLogger.new
|
60
|
+
end
|
61
|
+
|
62
|
+
# Validate required properties
|
63
|
+
unless (required_params - params.keys()).length == 0
|
64
|
+
raise "RightScaleSelfService::Api requires the following parameters (#{required_params.join(',')}) but only these were supplied (#{params.keys().join(',')})"
|
65
|
+
end
|
66
|
+
|
67
|
+
@account_id = params[:account_id]
|
68
|
+
@selfservice_url = params[:selfservice_url]
|
69
|
+
@api_url = params[:api_url]
|
70
|
+
|
71
|
+
if params.include?(:access_token)
|
72
|
+
@logger.info("Using pre-authenticated access token")
|
73
|
+
@logger.info("Logging into self service @ #{@selfservice_url}")
|
74
|
+
ss_login_req = RestClient::Request.new(
|
75
|
+
:method => :get,
|
76
|
+
:url => "#{@selfservice_url}/api/catalog/new_session?account_id=#{@account_id}",
|
77
|
+
:headers => {"Authorization" => "Bearer #{params[:access_token]}"}
|
78
|
+
)
|
79
|
+
ss_login_resp = ss_login_req.execute
|
80
|
+
@auth["authorization"] = {"Authorization" => "Bearer #{params[:access_token]}"}
|
81
|
+
end
|
82
|
+
|
83
|
+
if params.include?(:refresh_token)
|
84
|
+
# OAuth
|
85
|
+
@logger.info("Logging into RightScale API 1.5 using OAuth @ #{@api_url}")
|
86
|
+
cm_login_req = RestClient::Request.new(
|
87
|
+
:method => :post,
|
88
|
+
:payload => URI.encode_www_form({
|
89
|
+
:grant_type => "refresh_token",
|
90
|
+
:refresh_token => params[:refresh_token]
|
91
|
+
}),
|
92
|
+
:url => "#{@api_url}/api/oauth2",
|
93
|
+
:headers => {"X-API-VERSION" => "1.5"}
|
94
|
+
)
|
95
|
+
cm_login_resp = cm_login_req.execute
|
96
|
+
oauth_token = JSON.parse(cm_login_resp.to_s)["access_token"]
|
97
|
+
@logger.info("Logging into self service @ #{@selfservice_url}")
|
98
|
+
ss_login_req = RestClient::Request.new(
|
99
|
+
:method => :get,
|
100
|
+
:url => "#{@selfservice_url}/api/catalog/new_session?account_id=#{@account_id}",
|
101
|
+
:headers => {"Authorization" => "Bearer #{oauth_token}"}
|
102
|
+
)
|
103
|
+
ss_login_resp = ss_login_req.execute
|
104
|
+
@auth["authorization"] = {"Authorization" => "Bearer #{oauth_token}"}
|
105
|
+
end
|
106
|
+
|
107
|
+
if params.include?(:email) && params.include?(:password)
|
108
|
+
@logger.info("Logging into RightScale Cloud Management API 1.5 @ #{@api_url}")
|
109
|
+
cm_login_req = RestClient::Request.new(
|
110
|
+
:method => :post,
|
111
|
+
:payload => URI.encode_www_form({
|
112
|
+
:email => params[:email],
|
113
|
+
:password => params[:password],
|
114
|
+
:account_href => "/api/accounts/#{@account_id}"
|
115
|
+
}),
|
116
|
+
:url => "#{@api_url}/api/session",
|
117
|
+
:headers => {"X-API-VERSION" => "1.5"}
|
118
|
+
)
|
119
|
+
cm_login_resp = cm_login_req.execute
|
120
|
+
|
121
|
+
@logger.info("Logging into self service @ #{@selfservice_url}")
|
122
|
+
ss_login_req = RestClient::Request.new(
|
123
|
+
:method => :get,
|
124
|
+
:url => "#{@selfservice_url}/api/catalog/new_session?account_id=#{@account_id}",
|
125
|
+
:cookies => {"rs_gbl" => cm_login_resp.cookies["rs_gbl"]}
|
126
|
+
)
|
127
|
+
ss_login_resp = ss_login_req.execute
|
128
|
+
@auth["cookie"] = cm_login_resp.cookies
|
129
|
+
end
|
130
|
+
|
131
|
+
if @auth == {"cookie" => {}, "authorization" => {}}
|
132
|
+
raise "RightScaleSelfService::Api did not authenticate with #{@selfservice_url}. Make sure you supplied valid login details"
|
133
|
+
end
|
134
|
+
|
135
|
+
interface_filepath = File.expand_path(File.join(File.dirname(__FILE__),"interface.json"))
|
136
|
+
interface_file = File.open(interface_filepath, "rb")
|
137
|
+
begin
|
138
|
+
@interface = JSON.parse(interface_file.read)
|
139
|
+
rescue Exception => e
|
140
|
+
raise e
|
141
|
+
ensure
|
142
|
+
interface_file.close
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def get_authorized_rest_client_request(request_params)
|
147
|
+
if @auth["cookie"].length > 0
|
148
|
+
request_params[:cookies] = @auth["cookie"]
|
149
|
+
end
|
150
|
+
|
151
|
+
if @auth["authorization"].length > 0
|
152
|
+
if request_params.has_key?(:headers)
|
153
|
+
request_params[:headers].merge!(@auth["authorization"])
|
154
|
+
else
|
155
|
+
request_params[:headers] = @auth["authorization"]
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
RestClient::Request.new(request_params)
|
160
|
+
end
|
161
|
+
|
162
|
+
def method_missing(name, *args)
|
163
|
+
unless interface["services"].has_key?(name.to_s)
|
164
|
+
raise "No service named \"#{name}\" can not be found. Available services are [#{interface["services"].keys.join(',')}]"
|
165
|
+
end
|
166
|
+
version = ""
|
167
|
+
if args.length > 0
|
168
|
+
version = args.first
|
169
|
+
unless interface["services"][name.to_s].has_key? version
|
170
|
+
raise "Version #{version} of service \"#{name}\" can not be found. Available versions are [#{interface["services"][name.to_s].keys.join(',')}]"
|
171
|
+
end
|
172
|
+
else
|
173
|
+
version = interface["services"][name.to_s].keys.sort.last
|
174
|
+
end
|
175
|
+
service_hash_key = "#{name}::#{version}"
|
176
|
+
|
177
|
+
if @services.has_key?(service_hash_key)
|
178
|
+
@services[service_hash_key]
|
179
|
+
else
|
180
|
+
base_url = selfservice_url.gsub(/\/*$/,"")
|
181
|
+
base_url += "/api/#{name}"
|
182
|
+
service = RightScaleSelfService::Api::Service.new(name.to_s,version,base_url,self)
|
183
|
+
@services[service_hash_key] = service
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|