Class: VMC::Client

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

Defined Under Namespace

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

Constant Summary collapse

VMC_HTTP_ERROR_CODES =

Error codes

[ 400, 500 ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize new client to the target_uri with optional auth_token



39
40
41
42
43
44
45
# File 'lib/vmc/client.rb', line 39

def initialize(target_url=VMC::DEFAULT_TARGET, auth_token=nil)
  target_url = "http://#{target_url}" unless /^https?/ =~ target_url
  target_url = target_url.gsub(/\/+$/, '')
  @target = target_url
  @auth_token = auth_token
  @via_uhuru_cloud = false
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



24
25
26
# File 'lib/vmc/client.rb', line 24

def auth_token
  @auth_token
end

#cloud_teamObject

Returns the value of attribute cloud_team.



24
25
26
# File 'lib/vmc/client.rb', line 24

def cloud_team
  @cloud_team
end

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/vmc/client.rb', line 24

def host
  @host
end

#proxyObject

Returns the value of attribute proxy.



24
25
26
# File 'lib/vmc/client.rb', line 24

def proxy
  @proxy
end

#proxy_realmObject

Returns the value of attribute proxy_realm.



24
25
26
# File 'lib/vmc/client.rb', line 24

def proxy_realm
  @proxy_realm
end

#targetObject (readonly)

Returns the value of attribute target.



24
25
26
# File 'lib/vmc/client.rb', line 24

def target
  @target
end

#traceObject

Returns the value of attribute trace.



25
26
27
# File 'lib/vmc/client.rb', line 25

def trace
  @trace
end

#userObject (readonly)

Returns the value of attribute user.



24
25
26
# File 'lib/vmc/client.rb', line 24

def user
  @user
end

#via_uhuru_cloudObject

Returns the value of attribute via_uhuru_cloud.



24
25
26
# File 'lib/vmc/client.rb', line 24

def via_uhuru_cloud
  @via_uhuru_cloud
end

Class Method Details

.path(*path) ⇒ Object



436
437
438
439
440
# File 'lib/vmc/client.rb', line 436

def self.path(*path)
  path.flatten.collect { |x|
    URI.encode x.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
  }.join("/")
end

.versionObject



20
21
22
# File 'lib/vmc/client.rb', line 20

def self.version
  VMC::VERSION
end

Instance Method Details

#add_user(user_email, password) ⇒ Object



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

def add_user(user_email, password)
  json_post(VMC::USERS_PATH, { :email => user_email, :password => password })
end

#app_crashes(name) ⇒ Object



146
147
148
149
# File 'lib/vmc/client.rb', line 146

def app_crashes(name)
  
  json_get(path(VMC::APPS_PATH, name, "crashes"))
end

#app_files(name, path, instance = '0') ⇒ Object

List the directory or download the actual file indicated by the path.



153
154
155
156
157
158
159
# File 'lib/vmc/client.rb', line 153

def app_files(name, path, instance='0')
  
  path = path.gsub('//', '/')
  url = path(VMC::APPS_PATH, name, "instances", instance, "files", path)
  _, body, headers = http_get(url)
  body
end

#app_info(name) ⇒ Object



117
118
119
120
# File 'lib/vmc/client.rb', line 117

def app_info(name)
  
  json_get(path(VMC::APPS_PATH, name))
end

#app_instances(name) ⇒ Object



141
142
143
144
# File 'lib/vmc/client.rb', line 141

def app_instances(name)
  
  json_get(path(VMC::APPS_PATH, name, "instances"))
end

#app_stats(name) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vmc/client.rb', line 127

def app_stats(name)
  
  stats_raw = json_get(path(VMC::APPS_PATH, name, "stats"))
  stats = []
  stats_raw.each_pair do |k, entry|
    # Skip entries with no stats
    next unless entry[:stats]
    entry[:instance] = k.to_s.to_i
    entry[:state] = entry[:state].to_sym if entry[:state]
    stats << entry
  end
  stats.sort { |a,b| a[:instance] - b[:instance] }
end

#app_update_info(name) ⇒ Object



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

def app_update_info(name)
  
  json_get(path(VMC::APPS_PATH, name, "update"))
end

#appsObject

Apps



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

def apps
  
  json_get(VMC::APPS_PATH)
end

#bind_service(service, appname) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/vmc/client.rb', line 207

def bind_service(service, appname)
  
  app = app_info(appname)
  services = app[:services] || []
  app[:services] = services << service
  update_app(appname, app)
end

#change_password(new_password) ⇒ Object

sets the password for the current logged user



313
314
315
316
317
318
319
320
# File 'lib/vmc/client.rb', line 313

def change_password(new_password)
  
   = json_get(path(VMC::USERS_PATH, @user))
  if 
    [:password] = new_password
    json_put(path(VMC::USERS_PATH, @user), )
  end
end

#check_resources(resources) ⇒ Object

Send in a resources manifest array to the system to have it check what is needed to actually send. Returns array indicating what is needed. This returned manifest should be sent in with the upload if resources were removed. E.g. [=> xxx, :size => xxx, :fn => filename]



233
234
235
236
237
# File 'lib/vmc/client.rb', line 233

def check_resources(resources)
  
  status, body, headers = json_post(VMC::RESOURCES_PATH, resources)
  json_parse(body)
end

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



80
81
82
83
84
85
86
# File 'lib/vmc/client.rb', line 80

def create_app(name, manifest={})
  
  app = manifest.dup
  app[:name] = name
  app[:instances] ||= 1
  json_post(VMC::APPS_PATH, app)
end

#create_service(service, name) ⇒ Object

Raises:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/vmc/client.rb', line 171

def create_service(service, name)
  
  services = services_info
  services ||= []
  service_hash = nil

  service = service.to_s

  # FIXME!
  services.each do |service_type, value|
    value.each do |vendor, version|
      version.each do |version_str, service_descr|
        if service == service_descr[:vendor]
          service_hash = {
            :type => service_descr[:type], :tier => 'free',
            :vendor => service, :version => version_str
          }
          break
        end
      end
    end
  end

  raise TargetError, "Service [#{service}] is not a valid service choice" unless service_hash
  service_hash[:name] = name
  json_post(path(VMC::SERVICES_PATH), service_hash)
end

#delete_app(name) ⇒ Object



112
113
114
115
# File 'lib/vmc/client.rb', line 112

def delete_app(name)
  
  http_delete(path(VMC::APPS_PATH, name))
end

#delete_service(name) ⇒ Object

Raises:



199
200
201
202
203
204
205
# File 'lib/vmc/client.rb', line 199

def delete_service(name)
  
  svcs = services || []
  names = svcs.collect { |s| s[:name] }
  raise TargetError, "Service [#{name}] not a valid service" unless names.include? name
  http_delete(path(VMC::SERVICES_PATH, name))
end

#delete_user(user_email) ⇒ Object



343
344
345
346
# File 'lib/vmc/client.rb', line 343

def delete_user(user_email)
  
  http_delete(path(VMC::USERS_PATH, user_email))
end

#determine_target_typeObject

checks if the target is a direct Cloud Foundry target or Uhuru target



276
277
278
279
280
281
282
283
# File 'lib/vmc/client.rb', line 276

def determine_target_type
  if uhuru_target_valid?
    @via_uhuru_cloud = true
  else
    @via_uhuru_cloud = false
  end

end

#generic_target_valid?Boolean

currently not used, kept just for reference

Returns:

  • (Boolean)


244
245
246
247
248
249
250
251
252
253
# File 'lib/vmc/client.rb', line 244

def generic_target_valid?
  @via_uhuru_cloud = false
  return true if target_valid?

  @via_uhuru_cloud = true
  return true if uhuru_target_valid?

  @via_uhuru_cloud = false
  false
end

#get_cloud_domainObject



388
389
390
391
392
# File 'lib/vmc/client.rb', line 388

def get_cloud_domain
  ct = get_cloud_team(@cloud_team)
  cloud_id = (get_cloud_team(@cloud_team)[:Cloud] || get_cloud_team(@cloud_team)[:CloudTeamCloud])[:Id]
  json_get("../clouds/#{cloud_id}")[:Domain]
end

#get_cloud_entity(cloud_id) ⇒ Object



384
385
386
# File 'lib/vmc/client.rb', line 384

def get_cloud_entity(cloud_id)
  @cloud_entity = json_get("../clouds/#{cloud_id}")
end

#get_cloud_team(id) ⇒ Object



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

def get_cloud_team(id)
  json_get("../cloud_teams/#{id}")
end

#get_detailed_cloud_teamsObject



376
377
378
379
380
381
382
# File 'lib/vmc/client.rb', line 376

def get_detailed_cloud_teams
  ret = []
  get_user_cloud_teams.each { |cloud_team_id|
     ret << get_cloud_team(cloud_team_id)
  }
  ret
end

#get_user_cloud_teamsObject



368
369
370
# File 'lib/vmc/client.rb', line 368

def get_user_cloud_teams
  cloud_ids = json_get("../usercloudteams/")
end

#infoObject

Retrieves information on the target cloud, and optionally the logged in user



52
53
54
55
# File 'lib/vmc/client.rb', line 52

def info
  # TODO: Should merge for new version IMO, general, services, user_account
  json_get(VMC::INFO_PATH)
end

#logged_in?Boolean

Checks that the auth_token is valid

Returns:

  • (Boolean)


286
287
288
289
290
291
292
293
294
# File 'lib/vmc/client.rb', line 286

def logged_in?
  descr = info
  if descr
    return false unless descr[:user]
    return false unless descr[:usage]
    @user = descr[:user]
    true
  end
end

#login(user, password) ⇒ Object

login and return an auth_token Auth token can be retained and used in creating new clients, avoiding login.



303
304
305
306
307
308
309
310
# File 'lib/vmc/client.rb', line 303

def (user, password)
  status, body, headers = json_post(path(VMC::USERS_PATH, user, "tokens"), {:password => password})
  response_info = json_parse(body)
  if response_info
    @user = user
    @auth_token = response_info[:token]
  end
end

#proxy_for(proxy) ⇒ Object



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

def proxy_for(proxy)
  @proxy = proxy
end

#raw_infoObject



57
58
59
# File 'lib/vmc/client.rb', line 57

def raw_info
  http_get(VMC::INFO_PATH)
end

#raw_uhuru_versionObject



403
404
405
406
407
408
409
# File 'lib/vmc/client.rb', line 403

def raw_uhuru_version
  if @via_uhuru_cloud
    http_get("../version")
  else
    http_get("version")
  end
end

#runtimes_infoObject



67
68
69
# File 'lib/vmc/client.rb', line 67

def runtimes_info
  json_get(path(VMC::GLOBAL_RUNTIMES_PATH))
end

#servicesObject

listing of services that are available in the system



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

def services
  
  json_get(VMC::SERVICES_PATH)
end

#services_infoObject

Global listing of services that are available on the target system



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

def services_info
  
  json_get(path(VMC::GLOBAL_SERVICES_PATH))
end

#target_valid?Boolean

Checks that the target is valid

Returns:

  • (Boolean)


256
257
258
259
260
261
262
263
264
265
# File 'lib/vmc/client.rb', line 256

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[:support]
  true
rescue
  false
end

#uhuru_login(token) ⇒ Object

login to Uhuru App Cloud with a one time token or a regular authentication token



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/vmc/client.rb', line 412

def (token)
  @via_uhuru_cloud = true;
  # check for one time token
  if token =~ /\A(\w+-)+\w+\z/
    response_target = json_get("../one_time_tokens/#{token}")
    if response_target
      @auth_token = response_target[:token]
      @cloud_team = response_target[:cloud_team]
      @proxy_realm = response_target[:realm]
      response_target[:cloud_domain] = get_cloud_domain if @cloud_team
      response_target
    end
  else
    @auth_token = token
    body = json_get("../usercloudteams/")
    if body
      {:token => token}
    end
  end

end

#uhuru_target_valid?Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
273
# File 'lib/vmc/client.rb', line 267

def uhuru_target_valid?
  # return false unless !via_uhuru_cloud || get_cloud_domain
  return false unless uhuru_version =~ /Uhuru Cloud API, version = \d+.\d+.\d+.\d+/
  true
rescue
  false
end

#uhuru_versionObject



394
395
396
397
398
399
400
401
# File 'lib/vmc/client.rb', line 394

def uhuru_version
  if @via_uhuru_cloud
    status, body, headers  = http_get("../version/")
  else
    status, body, headers  = http_get("version/")
  end
  body
end

#unbind_service(service, appname) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/vmc/client.rb', line 215

def unbind_service(service, appname)
  
  app = app_info(appname)
  services = app[:services] || []
  services.delete(service)
  app[:services] = services
  update_app(appname, app)
end

#update_app(name, manifest) ⇒ Object



88
89
90
91
# File 'lib/vmc/client.rb', line 88

def update_app(name, manifest)
  
  json_put(path(VMC::APPS_PATH, name), manifest)
end

#upload_app(name, zipfile, resource_manifest = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vmc/client.rb', line 93

def upload_app(name, zipfile, resource_manifest=nil)
  #FIXME, manifest should be allowed to be null, here for compatability with old cc's
  resource_manifest ||= []
  
  upload_data = {:_method => 'put'}
  if zipfile
    if zipfile.is_a? File
      file = zipfile
    else
      file = File.new(zipfile, 'rb')
    end
    upload_data[:application] = file
  end
  upload_data[:resources] = resource_manifest.to_json if resource_manifest
  http_post(path(VMC::APPS_PATH, name, "application"), upload_data)
rescue RestClient::ServerBrokeConnection
  retry
end

#usersObject



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

def users
  
  json_get(VMC::USERS_PATH)
end