Class: PI::Client

Inherits:
Object show all
Defined in:
lib/pi/client.rb

Defined Under Namespace

Classes: AuthError, BadResponse, BadTarget, HTTPException, NotFound, TargetError

Constant Summary collapse

PI_HTTP_ERROR_CODES =

Error codes

[ 400, 500 ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_url = PI::DEFAULT_TARGET, auth_token = nil) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pi/client.rb', line 29

def initialize(target_url=PI::DEFAULT_TARGET, auth_token=nil)
  if target_url =~ /(https?:\/\/)?staging.samsungcloud.org/
    if /^https?/ =~ target_url
      target_url = target_url.split("//", 2)
      target_url = target_url[1]
    end
    target_url = "http://api.#{target_url}"
  else
    target_url = "http://#{target_url}" unless /^https?/ =~ target_url
  end
  target_url = target_url.gsub(/\/+$/, '')
  @target =  target_url
  @auth_token = auth_token
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



14
15
16
# File 'lib/pi/client.rb', line 14

def auth_token
  @auth_token
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/pi/client.rb', line 14

def host
  @host
end

#targetObject (readonly)

Returns the value of attribute target.



14
15
16
# File 'lib/pi/client.rb', line 14

def target
  @target
end

#traceObject

Returns the value of attribute trace.



15
16
17
# File 'lib/pi/client.rb', line 15

def trace
  @trace
end

Class Method Details

.versionObject



10
11
12
# File 'lib/pi/client.rb', line 10

def self.version
  PI::VERSION
end

Instance Method Details

#app_debug_info(appid) ⇒ Object



286
287
288
289
# File 'lib/pi/client.rb', line 286

def app_debug_info(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/instances")
end

#app_dns(appid) ⇒ Object



375
376
377
378
# File 'lib/pi/client.rb', line 375

def app_dns(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/url/map")
end

#app_env(appid) ⇒ Object



264
265
266
267
# File 'lib/pi/client.rb', line 264

def app_env(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/env/list")
end

#app_get_byid(appid) ⇒ Object

applications



191
192
193
194
# File 'lib/pi/client.rb', line 191

def app_get_byid(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}")
end

#app_log(appid, filepath, instanceindex) ⇒ Object



257
258
259
260
261
262
# File 'lib/pi/client.rb', line 257

def app_log(appid, filepath, instanceindex)
  filepath = uri_encode(filepath)
  appid = uri_encode(appid)
  instanceindex = uri_encode(instanceindex)
  http_get("#{PI::APP_PATH}/#{appid}/logs?filePath=#{filepath}&instanceIndex=#{instanceindex}")
end

#app_message(uuid) ⇒ Object



216
217
218
# File 'lib/pi/client.rb', line 216

def app_message(uuid)
  json_get("#{PI::APP_PATH}/message/#{uuid}")
end

#app_search(targetname, appname) ⇒ Object



196
197
198
199
200
# File 'lib/pi/client.rb', line 196

def app_search(targetname, appname)
  targetname = uri_encode(targetname)
  appname = uri_encode(appname)
  json_get("#{PI::APP_PATH}/search/#{targetname}/#{appname}")
end

#app_search_target_is_all(appname) ⇒ Object



202
203
204
205
# File 'lib/pi/client.rb', line 202

def app_search_target_is_all(appname)    
  appname = uri_encode(appname)
  json_get("#{PI::PROJECT_PATH}/apps?appName=#{appname}")
end

#app_service(appid) ⇒ Object



300
301
302
303
# File 'lib/pi/client.rb', line 300

def app_service(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/service/bind")
end

#app_sum(projectid = 0, targetname = nil) ⇒ Object



181
182
183
184
185
# File 'lib/pi/client.rb', line 181

def app_sum(projectid=0, targetname=nil)
  projectid = uri_encode(projectid)
  targetname = uri_encode(targetname)
  http_get("#{PI::PROJECT_PATH}/apps/sum?projectId=#{projectid}&targetName=#{targetname}")
end

#apps(projectid = 0, targetname = nil, pageNum = -1,, numPerPage = -1)) ⇒ Object



175
176
177
178
179
# File 'lib/pi/client.rb', line 175

def apps(projectid=0, targetname=nil, pageNum=-1, numPerPage=-1)    
  projectid = uri_encode(projectid)
  targetname = uri_encode(targetname)
  json_get("#{PI::PROJECT_PATH}/apps?projectId=#{projectid}&targetName=#{targetname}&pageNum=#{pageNum}&numPerPage=#{numPerPage}")
end

#bind_service(appid, manifest = {}) ⇒ Object



305
306
307
308
# File 'lib/pi/client.rb', line 305

def bind_service(appid, manifest={})
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/service/bind",manifest)
end

#check_login_statusObject

Raises:



63
64
65
# File 'lib/pi/client.rb', line 63

def 
  raise AuthError unless @auth_token
end

#check_service(serviceid) ⇒ Object



334
335
336
337
# File 'lib/pi/client.rb', line 334

def check_service(serviceid)
  serviceid = uri_encode(serviceid)
  http_get("#{PI::SERVICE_PATH}/check/#{serviceid}")
end

#create_app(manifest = {}) ⇒ Object



207
208
209
# File 'lib/pi/client.rb', line 207

def create_app(manifest={})
  status, body, headers = json_post("#{PI::APP_PATH}/", manifest)
end

#create_dns(manifest = {}) ⇒ Object



395
396
397
# File 'lib/pi/client.rb', line 395

def create_dns(manifest={})
  json_post("#{PI::DNS_PATH}/", manifest)
end

#create_env(appid, manifest = {}) ⇒ Object



269
270
271
272
# File 'lib/pi/client.rb', line 269

def create_env(appid, manifest={})
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/env", manifest)
end

#create_project(name, manifest = {}) ⇒ Object



123
124
125
# File 'lib/pi/client.rb', line 123

def create_project(name, manifest={})
  status, body, headers = json_post("#{PI::PROJECT_PATH}/", manifest)
end

#delete_app(appid) ⇒ Object



220
221
222
223
# File 'lib/pi/client.rb', line 220

def delete_app(appid)
  appid = uri_encode(appid)
  http_delete("#{PI::APP_PATH}/#{appid}",'application/json')
end

#delete_binary(binaryid) ⇒ Object



137
138
139
140
# File 'lib/pi/client.rb', line 137

def delete_binary(binaryid)
  binaryid = uri_encode(binaryid)
  http_delete("#{PI::PROJECT_PATH}/binary/#{binaryid}",'application/json')
end

#delete_dns(dnsid) ⇒ Object



409
410
411
412
# File 'lib/pi/client.rb', line 409

def delete_dns(dnsid)
  dnsid = uri_encode(dnsid)
  http_delete("#{PI::DNS_PATH}/#{dnsid}",'application/json')
end

#delete_env(appid, env) ⇒ Object



274
275
276
277
278
# File 'lib/pi/client.rb', line 274

def delete_env(appid, env)
  env = uri_encode(env)
  appid = uri_encode(appid)
  http_delete("#{PI::APP_PATH}/#{appid}/env/#{env}",'application/json')
end

#delete_project(projectid) ⇒ Object



127
128
129
130
# File 'lib/pi/client.rb', line 127

def delete_project(projectid)
  projectid = uri_encode(projectid)
  http_delete("#{PI::PROJECT_PATH}/#{projectid}",'application/json')
end

#delete_service(serviceid) ⇒ Object



339
340
341
342
# File 'lib/pi/client.rb', line 339

def delete_service(serviceid)
  serviceid = uri_encode(serviceid)
  http_delete("#{PI::SERVICE_PATH}/#{serviceid}", 'application/json')
end

#deregister_dns(recordid) ⇒ Object



434
435
436
437
# File 'lib/pi/client.rb', line 434

def deregister_dns(recordid)
  recordid = uri_encode(recordid)
  http_delete("#{PI::DNS_PATH}/record/#{recordid}",'application/json')
end

#deregister_service(servicetargetid) ⇒ Object



361
362
363
364
# File 'lib/pi/client.rb', line 361

def deregister_service(servicetargetid)
  servicetargetid = uri_encode(servicetargetid)
  http_delete("#{PI::SERVICE_PATH}/unregister/#{servicetargetid}",'application/json')
end

#dnsObject



391
392
393
# File 'lib/pi/client.rb', line 391

def dns
  json_get("#{PI::DNS_PATH}/search")
end

#dns_continentsObject



419
420
421
# File 'lib/pi/client.rb', line 419

def dns_continents
  json_get("#{PI::DNS_PATH}/continents")
end

#dns_country(continents) ⇒ Object



423
424
425
426
# File 'lib/pi/client.rb', line 423

def dns_country(continents)
  continents = uri_encode(continents)
  json_get("#{PI::DNS_PATH}/country/#{continents}")
end

#dns_zone_listObject



399
400
401
# File 'lib/pi/client.rb', line 399

def dns_zone_list
  json_get("#{PI::DNS_PATH}/dnszones")
end

#env_verify(appid, name) ⇒ Object



280
281
282
283
284
# File 'lib/pi/client.rb', line 280

def env_verify(appid, name)
  name = uri_encode(name)
  appid = uri_encode(appid)
  http_get("#{PI::APP_PATH}/#{appid}/env/verify?name=#{name}")
end

#export_service(serviceid) ⇒ Object



329
330
331
332
# File 'lib/pi/client.rb', line 329

def export_service(serviceid)
  serviceid = uri_encode(serviceid)
  http_get("#{PI::SERVICE_PATH}/exporturl/#{serviceid}")
end

#frameworks(runtime) ⇒ Object



103
104
105
# File 'lib/pi/client.rb', line 103

def frameworks(runtime)
  json_get("#{PI::INFO_PATH}/frameworks?runtime=#{runtime}")
end

#github(manifest = {}) ⇒ Object



95
96
97
# File 'lib/pi/client.rb', line 95

def github(manifest={})
  json_post("#{PI::USER_PATH}/repository/bind?type=github",manifest)
end

#github_infoObject



83
84
85
# File 'lib/pi/client.rb', line 83

def github_info
  json_get("#{PI::USER_PATH}/repository/bind?type=github")
end

#import_service(manifest = {}) ⇒ Object



325
326
327
# File 'lib/pi/client.rb', line 325

def import_service(manifest={})
  http_post("#{PI::SERVICE_PATH}/import",manifest)
end

#infoObject



75
76
77
# File 'lib/pi/client.rb', line 75

def info
  json_get(PI::INFO_PATH)
end

#login(user, password) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/pi/client.rb', line 67

def (user, password)
  status, body, headers = json_post("#{PI::TOKEN_PATH}", {:password => password,:userName => user})
  response_info = json_parse(body)
  if response_info
    @auth_token = response_info[:token]
  end
end

#map_dns(appid, manifest = {}) ⇒ Object



380
381
382
383
# File 'lib/pi/client.rb', line 380

def map_dns(appid, manifest={})
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/url/map", manifest)
end

#password(manifest = {}) ⇒ Object



91
92
93
# File 'lib/pi/client.rb', line 91

def password(manifest={})
  status, body, headers = json_post("#{PI::USER_PATH}/password", manifest)
end

#project_binary(projectid) ⇒ Object



164
165
166
167
# File 'lib/pi/client.rb', line 164

def project_binary(projectid)
  projectid = uri_encode(projectid)
  json_get("#{PI::PROJECT_PATH}/binary/list/#{projectid}")
end

#project_binary_existed(projectid, versionname) ⇒ Object



169
170
171
172
173
# File 'lib/pi/client.rb', line 169

def project_binary_existed(projectid,versionname)
  versionname = uri_encode(versionname)
  projectid = uri_encode(projectid)
  http_get("#{PI::PROJECT_PATH}/binary/existed/#{projectid}/#{versionname}")
end

#project_commits_bybranch(projectid, branch = "master") ⇒ Object



158
159
160
161
162
# File 'lib/pi/client.rb', line 158

def project_commits_bybranch(projectid,branch="master")
  projectid = uri_encode(projectid)
  branch = uri_encode(branch)
  json_get("#{PI::PROJECT_PATH}/commitinfo/branch/#{projectid}/#{branch}")
end

#project_commits_bytag(projectid, tag) ⇒ Object



152
153
154
155
156
# File 'lib/pi/client.rb', line 152

def project_commits_bytag(projectid,tag)
  projectid = uri_encode(projectid)
  tag = uri_encode(tag)
  json_get("#{PI::PROJECT_PATH}/commitinfo/#{projectid}/#{tag}")
end

#project_events(projectid) ⇒ Object



142
143
144
145
# File 'lib/pi/client.rb', line 142

def project_events(projectid)
  projectid = uri_encode(projectid)
  json_get("#{PI::PROJECT_PATH}/events/#{projectid}")
end

#project_search(projectname = nil) ⇒ Object

projects



111
112
113
# File 'lib/pi/client.rb', line 111

def project_search(projectname=nil)
  json_get("#{PI::PROJECT_PATH}/search/#{projectname}")
end

#project_sumObject



119
120
121
# File 'lib/pi/client.rb', line 119

def project_sum
  http_get("#{PI::PROJECTS_PATH}/sum")
end

#project_tags(projectid) ⇒ Object



147
148
149
150
# File 'lib/pi/client.rb', line 147

def project_tags(projectid)
  projectid = uri_encode(projectid)
  json_get("#{PI::PROJECT_PATH}/tags/#{projectid}")
end

#projects(pageNum = -1,, numPerPage = -1)) ⇒ Object



115
116
117
# File 'lib/pi/client.rb', line 115

def projects(pageNum=-1, numPerPage=-1)
  json_get("#{PI::PROJECTS_PATH}?pageNum=#{pageNum}&numPerPage=#{numPerPage}")
end

#raw_infoObject



59
60
61
# File 'lib/pi/client.rb', line 59

def raw_info
  http_get(PI::INFO_PATH)
end

#register_dns(dnsid, target, manifest = {}) ⇒ Object



428
429
430
431
432
# File 'lib/pi/client.rb', line 428

def register_dns(dnsid, target, manifest={})
  dnsid = uri_encode(dnsid)
  target = uri_encode(target)
  json_post("#{PI::DNS_PATH}/view/#{dnsid}/#{target}", manifest)
end

#register_service(serviceid, targetname, manifest = {}) ⇒ Object



354
355
356
357
358
359
# File 'lib/pi/client.rb', line 354

def register_service(serviceid, targetname, manifest={})
  serviceid = uri_encode(serviceid)
  targetname = uri_encode(targetname)
  # servicetype = uri_encode(servicetype)
  json_post("#{PI::SERVICE_PATH}/register/#{targetname}/#{serviceid}?serviceType=1",manifest)
end

#restart_app(appid, graceful) ⇒ Object



235
236
237
238
# File 'lib/pi/client.rb', line 235

def restart_app(appid, graceful)
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/restart?graceful=#{graceful}",manifest={})
end

#runtimesObject



99
100
101
# File 'lib/pi/client.rb', line 99

def runtimes
  json_get("#{PI::INFO_PATH}/runtimes")
end

#scale_app(appid, instance) ⇒ Object



240
241
242
243
244
# File 'lib/pi/client.rb', line 240

def scale_app(appid, instance)
  appid = uri_encode(appid)
  instance = uri_encode(instance)
  json_post("#{PI::APP_PATH}/#{appid}/scale?instance=#{instance}", manifest={})
end

#service(serviceid) ⇒ Object



320
321
322
323
# File 'lib/pi/client.rb', line 320

def service(serviceid)
  serviceid = uri_encode(serviceid)
  json_get("#{PI::SERVICE_PATH}/#{serviceid}")
end

#service_target(serviceid) ⇒ Object



349
350
351
352
# File 'lib/pi/client.rb', line 349

def service_target(serviceid)
  serviceid = uri_encode(serviceid)
  json_get("#{PI::SERVICE_PATH}/servicetarget/#{serviceid}")
end

#servicesObject



316
317
318
# File 'lib/pi/client.rb', line 316

def services
  json_get("#{PI::SERVICE_PATH}/search")
end

#start_app(appid) ⇒ Object



225
226
227
228
# File 'lib/pi/client.rb', line 225

def start_app(appid)
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/start",manifest={})
end

#status(appid) ⇒ Object



252
253
254
255
# File 'lib/pi/client.rb', line 252

def status(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/monitor")
end

#stop_app(appid, graceful) ⇒ Object



230
231
232
233
# File 'lib/pi/client.rb', line 230

def stop_app(appid, graceful)
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/stop?graceful=#{graceful}",manifest={})
end

#target_memory(targetname) ⇒ Object



211
212
213
214
# File 'lib/pi/client.rb', line 211

def target_memory(targetname)
  targetname = uri_encode(targetname)
  http_get("#{PI::APP_PATH}/target/#{targetname}/memory")
end

#target_valid?Boolean

Users

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/pi/client.rb', line 48

def target_valid?
  return false unless descr = info
  return false unless descr[:name]
  return false unless descr[:build]
  return false unless descr[:version]
  return false unless descr[:description]
  true
rescue
  false
end

#targetsObject



87
88
89
# File 'lib/pi/client.rb', line 87

def targets
  json_get("#{PI::USER_PATH}/targets")
end

#unbind_service(appid, servicename) ⇒ Object



310
311
312
313
314
# File 'lib/pi/client.rb', line 310

def unbind_service(appid, servicename)
  appid = uri_encode(appid)
  servicename = uri_encode(servicename)
  http_delete("#{PI::APP_PATH}/#{appid}/service/unbind/#{servicename}",'application/json')
end

#unmap_dns(appid, url) ⇒ Object



385
386
387
388
389
# File 'lib/pi/client.rb', line 385

def unmap_dns(appid, url)
  appid = uri_encode(appid)
  url = uri_encode(url)
  http_delete("#{PI::APP_PATH}/#{appid}/url/unmap/#{url}", 'application/json')
end

#update_app(appid, tag, graceful) ⇒ Object



246
247
248
249
250
# File 'lib/pi/client.rb', line 246

def update_app(appid, tag, graceful)
  tag = uri_encode(tag)
  appid = uri_encode(appid)
  json_post("#{PI::APP_PATH}/#{appid}/rollback/#{tag}?graceful=#{graceful}", manifest={})
end

#upload(manifest = {}) ⇒ Object



132
133
134
135
# File 'lib/pi/client.rb', line 132

def upload(manifest={})
  status, body, headers = http_post("#{PI::PROJECT_PATH}/upload", manifest)
  response_info = json_parse(body)
end

#uri_encode(name) ⇒ Object



441
442
443
# File 'lib/pi/client.rb', line 441

def uri_encode(name)
  URI.encode name.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
end

#usable_dns(appid) ⇒ Object

dns



370
371
372
373
# File 'lib/pi/client.rb', line 370

def usable_dns(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/url/usable")
end

#usable_dns_target(dnsid) ⇒ Object



414
415
416
417
# File 'lib/pi/client.rb', line 414

def usable_dns_target(dnsid)
  dnsid = uri_encode(dnsid)
  json_get("#{PI::DNS_PATH}/dnstarget/#{dnsid}")
end

#usable_service_target(serviceid) ⇒ Object



344
345
346
347
# File 'lib/pi/client.rb', line 344

def usable_service_target(serviceid)
  serviceid = uri_encode(serviceid)
  json_get("#{PI::SERVICE_PATH}/target/usable/#{serviceid}")
end

#usable_services(appid) ⇒ Object

services



295
296
297
298
# File 'lib/pi/client.rb', line 295

def usable_services(appid)
  appid = uri_encode(appid)
  json_get("#{PI::APP_PATH}/#{appid}/service/usable")
end

#user_infoObject



79
80
81
# File 'lib/pi/client.rb', line 79

def 
  json_get(PI::USER_PATH)
end

#verify_dns(dnsname, zoneid) ⇒ Object



403
404
405
406
407
# File 'lib/pi/client.rb', line 403

def verify_dns(dnsname, zoneid)
  dnsname = uri_encode(dnsname)
  zoneid = uri_encode(zoneid)
  http_get("#{PI::DNS_PATH}/verify?dnsName=#{dnsname}&zoneId=#{zoneid}")
end