Class: Solano::SolanoAPI

Inherits:
Object
  • Object
show all
Includes:
SolanoConstant
Defined in:
lib/solano/cli/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scm, tddium_client, api_config, options = {}) ⇒ SolanoAPI

Returns a new instance of SolanoAPI.



9
10
11
12
13
14
# File 'lib/solano/cli/api.rb', line 9

def initialize(scm, tddium_client, api_config, options={})
  @scm = scm
  @api_config = api_config
  @tddium_client = tddium_client
  @tddium_clientv3 = options[:v3]
end

Instance Attribute Details

#scmObject (readonly)

rspec



7
8
9
# File 'lib/solano/cli/api.rb', line 7

def scm
  @scm
end

Instance Method Details

#call_api(method, api_path, params = {}, api_key = nil, show_error = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/solano/cli/api.rb', line 16

def call_api(method, api_path, params = {}, api_key = nil, show_error = true)
  api_key ||= @api_config.get_api_key unless api_key == false

  if params[:v3]
    api = @tddium_clientv3
    params.delete(:v3)
  end
  api ||= @tddium_client

  begin
    result = api.call_api(method, api_path, params, api_key)
  rescue TddiumClient::Error::UpgradeRequired => e
    abort e.message
  rescue TddiumClient::Error::APICert => e
    abort e.message
  rescue TddiumClient::Error::Base => e
    say e.message.dup if show_error
    raise e
  end
  result
end

#check_session_done(session_id) ⇒ Object



364
365
366
# File 'lib/solano/cli/api.rb', line 364

def check_session_done(session_id)
  call_api(:get, "#{Api::Path::SESSIONS}/#{session_id}/check_done")
end

#create_session(suite_id, params = {}) ⇒ Object



290
291
292
293
# File 'lib/solano/cli/api.rb', line 290

def create_session(suite_id, params = {})
  new_session = call_api(:post, Api::Path::SESSIONS, params.merge(:suite_id=>suite_id))
  return new_session['session'], new_session['manager']
end

#create_suite(params) ⇒ Object



264
265
266
267
268
# File 'lib/solano/cli/api.rb', line 264

def create_suite(params)
   = params.delete(:account_id)
  new_suite = call_api(:post, Api::Path::SUITES, {:suite => params, :account_id => })
  new_suite["suite"]
end

#current_branchObject



213
214
215
# File 'lib/solano/cli/api.rb', line 213

def current_branch
  @current_branch ||= @scm.current_branch
end

#current_repo_id(options = {}) ⇒ Object



217
218
219
220
# File 'lib/solano/cli/api.rb', line 217

def current_repo_id(options={})
  # api_config.get_branch will query the server if there is no locally cached data
  @api_config.get_branch(current_branch, 'repo_id', options)
end

#current_suite_id(options = {}) ⇒ Object



222
223
224
225
# File 'lib/solano/cli/api.rb', line 222

def current_suite_id(options={})
  # api_config.get_branch will query the server if there is no locally cached data
  @api_config.get_branch(current_branch, 'id', options)
end

#current_suite_options(options = {}) ⇒ Object



227
228
229
# File 'lib/solano/cli/api.rb', line 227

def current_suite_options(options={})
  @api_config.get_branch(current_branch, 'options', options)
end

#default_branchObject



209
210
211
# File 'lib/solano/cli/api.rb', line 209

def default_branch
  @default_branch ||= @scm.default_branch
end

#default_suite_id(options = {}) ⇒ Object



231
232
233
234
# File 'lib/solano/cli/api.rb', line 231

def default_suite_id(options={})
  # api_config.get_branch will query the server if there is no locally cached data
  @api_config.get_branch(default_branch, 'id', options)
end

#default_suite_options(options = {}) ⇒ Object



236
237
238
# File 'lib/solano/cli/api.rb', line 236

def default_suite_options(options={})
  @api_config.get_branch(default_branch, 'options', options)
end

#delete_config_key(scope, key) ⇒ Object



104
105
106
107
# File 'lib/solano/cli/api.rb', line 104

def delete_config_key(scope, key)
  path = env_path(scope, key)
  call_api(:delete, path)
end

#delete_keys(name, params = {}) ⇒ Object



205
206
207
# File 'lib/solano/cli/api.rb', line 205

def delete_keys(name, params={})
  call_api(:delete, "#{Api::Path::KEYS}/#{name}", params)
end

#delete_memberships(email, params = {}) ⇒ Object



187
188
189
# File 'lib/solano/cli/api.rb', line 187

def delete_memberships(email, params={})
  call_api(:delete, "#{Api::Path::MEMBERSHIPS}/#{email}", params)
end

#demand_repoman_account(id, params = {}) ⇒ Object



278
279
280
# File 'lib/solano/cli/api.rb', line 278

def (id, params={})
  call_api(:post, "#{Api::Path::ACCOUNTS}/#{id}/demand_repoman", params)
end

#env_path(scope, key = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/solano/cli/api.rb', line 65

def env_path(scope, key=nil)
   = nil
  if @api_config.cli_options[:account] then
     = (@api_config.cli_options[:account])
  end
  if .nil? then
     = 
  end
  path = ['']

  case scope
  when "suite"
    path << 'suites'
    path << current_suite_id
  when "repo"
    path << 'repos'
    path << current_repo_id
  when "org"
    path << 'accounts'
    path << 
  else
    raise "Unrecognized scope. Use 'suite', 'repo', 'org'."
  end

  path << 'env'
  path << key if key
  path.join('/')
end

#get_account_id(acct_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/solano/cli/api.rb', line 54

def (acct_name)
  user_details = user_logged_in?(true, false)
  return nil unless user_details
  accts = user_details["participating_accounts"]
  acct = accts.select{|acct| acct["account"] == acct_name}.first
  if acct.nil?
    raise "You aren't a member of organization '%s'." % acct_name
  end
  acct["account_id"]
end

#get_config_key(scope, key = nil) ⇒ Object



94
95
96
97
# File 'lib/solano/cli/api.rb', line 94

def get_config_key(scope, key=nil)
  path = env_path(scope, key)
  call_api(:get, path)
end

#get_keys(params = {}) ⇒ Object



196
197
198
199
# File 'lib/solano/cli/api.rb', line 196

def get_keys(params={})
  result = call_api(:get, Api::Path::KEYS)
  result['keys']|| []
end

#get_memberships(params = {}) ⇒ Object



177
178
179
180
# File 'lib/solano/cli/api.rb', line 177

def get_memberships(params={})
  result = call_api(:get, Api::Path::MEMBERSHIPS)
  result['account_roles'] || []
end

#get_sessions(params = {}) ⇒ Object



282
283
284
285
286
287
288
# File 'lib/solano/cli/api.rb', line 282

def get_sessions(params={})
  begin
    call_api(:get, Api::Path::SESSIONS, params)['sessions']
  rescue TddiumClient::Error::Base
    []
  end
end

#get_single_account_idObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solano/cli/api.rb', line 38

def 
  user_details = user_logged_in?(true, false)
  return nil unless user_details
  accounts = user_details["participating_accounts"]
  unless accounts.length == 1
    msg = "You are a member of more than one organization.\n"
    msg << "Please specify the organization you want to operate on with "
    msg << "'--org NAME'.\n"
    accounts.each do |acct|
      msg << "  #{acct["account"]}\n"
    end
    raise msg
  end
  accounts.first["account_id"]
end

#get_snapshot_commit(params = {}) ⇒ Object



295
296
297
298
# File 'lib/solano/cli/api.rb', line 295

def get_snapshot_commit(params={})
  params.merge!({:v3 => true})
  call_api(:get, "#{Api::Path::REPO_SNAPSHOT}/commit_id", params)
end

#get_suite_by_id(id, params = {}) ⇒ Object



258
259
260
261
262
# File 'lib/solano/cli/api.rb', line 258

def get_suite_by_id(id, params={})
  current_suites = call_api(:get, "#{Api::Path::SUITES}/#{id}", params)
  current_suites ||= {}
  current_suites['suite']
end

#get_suites(params = {}) ⇒ Object

suites/user_suites returns: [

'account',
'account_id',
'branch',
'ci_ssh_pubkey',
'git_repo_uri',
'id',
'org_name',
'repo_name',
'repo_url'

]



252
253
254
255
256
# File 'lib/solano/cli/api.rb', line 252

def get_suites(params={})
  current_suites = call_api(:get, "#{Api::Path::SUITES}/user_suites", params)
  current_suites ||= {}
  current_suites['suites'] || []
end

#get_usage(params = {}) ⇒ Object



191
192
193
194
# File 'lib/solano/cli/api.rb', line 191

def get_usage(params={})
  result = call_api(:get, Api::Path::ACCOUNT_USAGE_BY_ACCOUNT)
  result['usage'] || []
end

#get_user(api_key = nil) ⇒ Object



109
110
111
112
# File 'lib/solano/cli/api.rb', line 109

def get_user(api_key=nil)
  result = call_api(:get, Api::Path::USERS, {}, api_key, false)
  result && result['user']
end

#get_user_credentials(options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/solano/cli/api.rb', line 122

def get_user_credentials(options = {})
  params = {}

  if options[:cli_token]
    params[:cli_token] = options[:cli_token]
  elsif options[:invited]
    # prompt for email/invitation and password
    token = options[:invitation_token] || ask(Text::Prompt::INVITATION_TOKEN)
    params[:invitation_token] = token.strip
    params[:password] = options[:password] || HighLine.ask(Text::Prompt::NEW_PASSWORD) { |q| q.echo = "*" }
  else
    say Text::Warning::USE_PASSWORD_TOKEN
    params[:email] = options[:email] || HighLine.ask(Text::Prompt::EMAIL)
    params[:password] = options[:password] || HighLine.ask(Text::Prompt::PASSWORD) { |q| q.echo = "*" }
  end
  params
end

#login_user(options = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/solano/cli/api.rb', line 140

def (options = {})
  # POST (email, password) to /users/sign_in to retrieve an API key
  begin
    user = options[:params]
     = call_api(:post, Api::Path::SIGN_IN, {:user => user}, false, options[:show_error])
    @api_config.set_api_key(["api_key"], user[:email])
  rescue TddiumClient::Error::Base => e
  end
  
end

#permanent_destroy_suite(id, params = {}) ⇒ Object



274
275
276
# File 'lib/solano/cli/api.rb', line 274

def permanent_destroy_suite(id, params={})
  call_api(:delete, "#{Api::Path::SUITES}/#{id}/permanent_destroy", params)
end

#poll_session(session_id, params = {}) ⇒ Object



352
353
354
# File 'lib/solano/cli/api.rb', line 352

def poll_session(session_id, params={})
  call_api(:get, "#{Api::Path::SESSIONS}/#{session_id}/#{Api::Path::TEST_EXECUTIONS}")
end

#query_session(session_id, params = {}) ⇒ Object



356
357
358
# File 'lib/solano/cli/api.rb', line 356

def query_session(session_id, params={})
  call_api(:get, "#{Api::Path::SESSIONS}/#{session_id}")
end

#query_session_tests(session_id, params = {}) ⇒ Object



360
361
362
# File 'lib/solano/cli/api.rb', line 360

def query_session_tests(session_id, params={})
  call_api(:get, "#{Api::Path::SESSIONS}/#{session_id}/#{Api::Path::QUERY_TEST_EXECUTIONS}")
end

#register_session(session_id, suite_id, test_pattern, test_exclude_pattern = nil) ⇒ Object



330
331
332
333
334
335
336
337
# File 'lib/solano/cli/api.rb', line 330

def register_session(session_id, suite_id, test_pattern, test_exclude_pattern=nil)
  args = {:suite_id => suite_id, :test_pattern => test_pattern}
  if test_exclude_pattern
    args[:test_exclude_pattern] = test_exclude_pattern
  end

  call_api(:post, "#{Api::Path::SESSIONS}/#{session_id}/#{Api::Path::REGISTER_TEST_EXECUTIONS}", args)
end

#request_patch_url(params = {}) ⇒ Object



315
316
317
318
# File 'lib/solano/cli/api.rb', line 315

def request_patch_url(params={})
  params.merge!({:v3 => true})
  call_api(:post, "#{Api::Path::SESSION_PATCH}/request_url", params)
end

#request_snapshot_url(params = {}) ⇒ Object



305
306
307
308
# File 'lib/solano/cli/api.rb', line 305

def request_snapshot_url(params={})
  params.merge!({:v3 => true})
  call_api(:post, "#{Api::Path::REPO_SNAPSHOT}/request_upload_url", params)
end

#set_config_key(scope, key, value) ⇒ Object



99
100
101
102
# File 'lib/solano/cli/api.rb', line 99

def set_config_key(scope, key, value)
  path = env_path(scope)
  call_api(:post, path, :env=>{key=>value})
end

#set_keys(params) ⇒ Object



201
202
203
# File 'lib/solano/cli/api.rb', line 201

def set_keys(params)
  call_api(:post, Api::Path::KEYS, params)
end

#set_memberships(params = {}) ⇒ Object



182
183
184
185
# File 'lib/solano/cli/api.rb', line 182

def set_memberships(params={})
  result = call_api(:post, Api::Path::MEMBERSHIPS, params)
  result['memberships'] || []
end

#set_user(params) ⇒ Object



114
115
116
# File 'lib/solano/cli/api.rb', line 114

def set_user(params)
  call_api(:post, Api::Path::USERS, {:user => params}, false, false)
end

#start_console(session_id, suite_id) ⇒ Object



343
344
345
346
# File 'lib/solano/cli/api.rb', line 343

def start_console(session_id, suite_id)
  path = "#{Api::Path::SESSIONS}/#{session_id}/#{Api::Path::TEST_EXECUTIONS}/console"
  call_api(:post, path, {suite_id: suite_id})
end

#start_destrofree_session(session_id, params = {}) ⇒ Object



300
301
302
303
# File 'lib/solano/cli/api.rb', line 300

def start_destrofree_session(session_id, params={})
  params.merge!({:v3 => true})
  call_api(:post, "#{Api::Path::SESSIONS}/#{session_id}/start", params)
end

#start_session(session_id, params) ⇒ Object



339
340
341
# File 'lib/solano/cli/api.rb', line 339

def start_session(session_id, params)
  call_api(:post, "#{Api::Path::SESSIONS}/#{session_id}/#{Api::Path::START_TEST_EXECUTIONS}", params)
end

#stop_session(ls_id, params = {}) ⇒ Object



348
349
350
# File 'lib/solano/cli/api.rb', line 348

def stop_session(ls_id, params = {})
  call_api(:post, "#{Api::Path::SESSIONS}/#{ls_id}/stop", params)
end

#update_session(session_id, params = {}) ⇒ Object



325
326
327
328
# File 'lib/solano/cli/api.rb', line 325

def update_session(session_id, params={})
  result = call_api(:put, "#{Api::Path::SESSIONS}/#{session_id}", params)
  result['session']
end

#update_snapshot(params = {}) ⇒ Object



310
311
312
313
# File 'lib/solano/cli/api.rb', line 310

def update_snapshot(params={})
  params.merge!({:v3 => true})
  call_api(:post, "#{Api::Path::REPO_SNAPSHOT}", params)
end

#update_suite(id, params = {}) ⇒ Object



270
271
272
# File 'lib/solano/cli/api.rb', line 270

def update_suite(id, params={})
  call_api(:put, "#{Api::Path::SUITES}/#{id}", params)
end

#update_user(user_id, params, api_key = nil) ⇒ Object



118
119
120
# File 'lib/solano/cli/api.rb', line 118

def update_user(user_id, params, api_key=nil)
  call_api(:put, "#{Api::Path::USERS}/#{user_id}/", params, api_key, false)
end

#upload_session_patch(params = {}) ⇒ Object



320
321
322
323
# File 'lib/solano/cli/api.rb', line 320

def upload_session_patch(params={})
  params.merge!({:v3 => true})
  call_api(:post, "#{Api::Path::SESSION_PATCH}", params)
end

#user_logged_in?(active = true, message = false) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/solano/cli/api.rb', line 151

def user_logged_in?(active = true, message = false)
  global_api_key = @api_config.get_api_key(:global => true)
  repo_api_key = @api_config.get_api_key(:repo => true)

  if (global_api_key && repo_api_key && global_api_key != repo_api_key)
    say Text::Error::INVALID_CREDENTIALS if message
    return
  end

  result = repo_api_key || global_api_key

  if message && result.nil? then
    say Text::Error::NOT_INITIALIZED
  end

  if result && active
    u = get_user
    if message && u.nil?
      say Text::Error::INVALID_CREDENTIALS
    end
    u
  else
    result
  end
end