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
# File 'lib/pi/client.rb', line 29

def initialize(target_url=PI::DEFAULT_TARGET, auth_token=nil)
  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



277
278
279
280
# File 'lib/pi/client.rb', line 277

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

#app_dns(appid) ⇒ Object



366
367
368
369
# File 'lib/pi/client.rb', line 366

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

#app_env(appid) ⇒ Object



255
256
257
258
# File 'lib/pi/client.rb', line 255

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

#app_get_byid(appid) ⇒ Object

applications



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

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

#app_log(appid, filepath, instanceindex) ⇒ Object



248
249
250
251
252
253
# File 'lib/pi/client.rb', line 248

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



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

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

#app_search(targetname, appname) ⇒ Object



187
188
189
190
191
# File 'lib/pi/client.rb', line 187

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



193
194
195
196
# File 'lib/pi/client.rb', line 193

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

#app_service(appid) ⇒ Object



291
292
293
294
# File 'lib/pi/client.rb', line 291

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

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



172
173
174
175
176
# File 'lib/pi/client.rb', line 172

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



166
167
168
169
170
# File 'lib/pi/client.rb', line 166

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



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

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

#check_login_statusObject

Raises:



54
55
56
# File 'lib/pi/client.rb', line 54

def 
  raise AuthError unless @auth_token
end

#check_service(serviceid) ⇒ Object



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

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

#create_app(manifest = {}) ⇒ Object



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

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

#create_dns(manifest = {}) ⇒ Object



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

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

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



260
261
262
263
# File 'lib/pi/client.rb', line 260

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

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



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

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

#delete_app(appid) ⇒ Object



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

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

#delete_binary(binaryid) ⇒ Object



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

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

#delete_dns(dnsid) ⇒ Object



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

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

#delete_env(appid, env) ⇒ Object



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

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



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

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

#delete_service(serviceid) ⇒ Object



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

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

#deregister_dns(recordid) ⇒ Object



431
432
433
434
# File 'lib/pi/client.rb', line 431

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

#deregister_service(servicetargetid) ⇒ Object



352
353
354
355
# File 'lib/pi/client.rb', line 352

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

#dnsObject



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

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

#dns_continentsObject



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

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

#dns_country(continents) ⇒ Object



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

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

#dns_zone_listObject



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

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

#env_verify(appid, name) ⇒ Object



271
272
273
274
275
# File 'lib/pi/client.rb', line 271

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



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

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

#frameworks(runtime) ⇒ Object



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

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

#github(manifest = {}) ⇒ Object



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

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

#github_infoObject



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

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

#import_service(manifest = {}) ⇒ Object



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

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

#infoObject



66
67
68
# File 'lib/pi/client.rb', line 66

def info
  json_get(PI::INFO_PATH)
end

#login(user, password) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/pi/client.rb', line 58

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



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

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

#password(manifest = {}) ⇒ Object



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

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

#project_binary(projectid) ⇒ Object



155
156
157
158
# File 'lib/pi/client.rb', line 155

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

#project_binary_existed(projectid, versionname) ⇒ Object



160
161
162
163
164
# File 'lib/pi/client.rb', line 160

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



149
150
151
152
153
# File 'lib/pi/client.rb', line 149

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



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

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



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

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

#project_search(projectname = nil) ⇒ Object

projects



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

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

#project_sumObject



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

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

#project_tags(projectid) ⇒ Object



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

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

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



106
107
108
# File 'lib/pi/client.rb', line 106

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

#raw_infoObject



50
51
52
# File 'lib/pi/client.rb', line 50

def raw_info
  http_get(PI::INFO_PATH)
end

#regions(dnsid) ⇒ Object



426
427
428
429
# File 'lib/pi/client.rb', line 426

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

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



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

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

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



345
346
347
348
349
350
# File 'lib/pi/client.rb', line 345

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



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

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

#runtimesObject



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

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

#scale_app(appid, instance) ⇒ Object



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

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



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

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

#service_target(serviceid) ⇒ Object



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

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

#servicesObject



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

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

#start_app(appid) ⇒ Object



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

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

#status(appid) ⇒ Object



243
244
245
246
# File 'lib/pi/client.rb', line 243

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

#stop_app(appid, graceful) ⇒ Object



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

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

#target_memory(targetname) ⇒ Object



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

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

#target_valid?Boolean

Users

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/pi/client.rb', line 39

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



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

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

#unbind_service(appid, servicename) ⇒ Object



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

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



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

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



237
238
239
240
241
# File 'lib/pi/client.rb', line 237

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



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

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

#uri_encode(name) ⇒ Object



438
439
440
# File 'lib/pi/client.rb', line 438

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

#usable_dns(appid) ⇒ Object

dns



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

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

#usable_dns_target(dnsid) ⇒ Object



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

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

#usable_service_target(serviceid) ⇒ Object



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

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

#usable_services(appid) ⇒ Object

services



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

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

#user_infoObject



70
71
72
# File 'lib/pi/client.rb', line 70

def 
  json_get(PI::USER_PATH)
end

#verify_dns(dnsname, zoneid) ⇒ Object



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

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