Class: Tddium::TddiumAPI

Inherits:
Object
  • Object
show all
Includes:
TddiumConstant
Defined in:
lib/tddium/cli/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_config, tddium_client, scm) ⇒ TddiumAPI

Returns a new instance of TddiumAPI.



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

def initialize(api_config, tddium_client, scm)
  @scm = scm
  @api_config = api_config
  @tddium_client = tddium_client
end

Instance Attribute Details

#scmObject (readonly)

rspec



7
8
9
# File 'lib/tddium/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



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tddium/cli/api.rb', line 15

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

  begin
    result = @tddium_client.call_api(method, api_path, params, api_key)
  rescue TddiumClient::Error::UpgradeRequired => 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



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

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

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



280
281
282
283
# File 'lib/tddium/cli/api.rb', line 280

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

#create_suite(params) ⇒ Object



254
255
256
257
258
# File 'lib/tddium/cli/api.rb', line 254

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



203
204
205
# File 'lib/tddium/cli/api.rb', line 203

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

#current_repo_id(options = {}) ⇒ Object



207
208
209
210
# File 'lib/tddium/cli/api.rb', line 207

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



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

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



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

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

#default_branchObject



199
200
201
# File 'lib/tddium/cli/api.rb', line 199

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

#default_suite_id(options = {}) ⇒ Object



221
222
223
224
# File 'lib/tddium/cli/api.rb', line 221

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



226
227
228
# File 'lib/tddium/cli/api.rb', line 226

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

#delete_config_key(scope, key) ⇒ Object



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

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

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



195
196
197
# File 'lib/tddium/cli/api.rb', line 195

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

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



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

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

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



268
269
270
# File 'lib/tddium/cli/api.rb', line 268

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

#env_path(scope, key = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tddium/cli/api.rb', line 56

def env_path(scope, key=nil)
  path = ['']

  case scope
  when "suite"
    path << 'suites'
    path << current_suite_id
  when "repo"
    path << 'repos'
    path << current_repo_id
  when "account", "org"
    path << 'accounts'
    path << 
  when /\Aaccount:/
    path << 'accounts'
    path << (scope.sub(/\Aaccount:/, ''))
  when /\Aorg:/
    path << 'accounts'
    path << (scope.sub(/\Aorg:/, ''))
  else
    raise "Unrecognized scope. Use 'suite', 'repo', 'org', or 'org:an_organization_name'."
  end

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

#get_account_id(acct_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/tddium/cli/api.rb', line 45

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



84
85
86
87
# File 'lib/tddium/cli/api.rb', line 84

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

#get_keys(params = {}) ⇒ Object



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

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

#get_memberships(params = {}) ⇒ Object



167
168
169
170
# File 'lib/tddium/cli/api.rb', line 167

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

#get_sessions(params = {}) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/tddium/cli/api.rb', line 272

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

#get_single_account_idObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tddium/cli/api.rb', line 29

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:an_organization_name'.\n"
    accounts.each do |acct|
      msg << "  #{acct["account"]}\n"
    end
    raise msg
  end
  accounts.first["account_id"]
end

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



248
249
250
251
252
# File 'lib/tddium/cli/api.rb', line 248

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'

]



242
243
244
245
246
# File 'lib/tddium/cli/api.rb', line 242

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



181
182
183
184
# File 'lib/tddium/cli/api.rb', line 181

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

#get_user(api_key = nil) ⇒ Object



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

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

#get_user_credentials(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tddium/cli/api.rb', line 112

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/tddium/cli/api.rb', line 130

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



264
265
266
# File 'lib/tddium/cli/api.rb', line 264

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

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



307
308
309
# File 'lib/tddium/cli/api.rb', line 307

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



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

def query_session(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



290
291
292
293
294
295
296
297
# File 'lib/tddium/cli/api.rb', line 290

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

#set_config_key(scope, key, value) ⇒ Object



89
90
91
92
# File 'lib/tddium/cli/api.rb', line 89

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

#set_keys(params) ⇒ Object



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

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

#set_memberships(params = {}) ⇒ Object



172
173
174
175
# File 'lib/tddium/cli/api.rb', line 172

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

#set_user(params) ⇒ Object



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

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

#start_session(session_id, params) ⇒ Object



299
300
301
# File 'lib/tddium/cli/api.rb', line 299

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



303
304
305
# File 'lib/tddium/cli/api.rb', line 303

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

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



285
286
287
288
# File 'lib/tddium/cli/api.rb', line 285

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

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



260
261
262
# File 'lib/tddium/cli/api.rb', line 260

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

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



108
109
110
# File 'lib/tddium/cli/api.rb', line 108

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

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

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/tddium/cli/api.rb', line 141

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