Class: VMC::Client
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
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#trace ⇒ Object
Returns the value of attribute trace.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_user(user_email, password) ⇒ Object
def add_user(user_email, password, app) json_post(VMC::USERS_PATH, { :email => user_email, :password => password, :app => app}) end.
- #app_crashes(name) ⇒ Object
-
#app_files(name, path, instance = '0') ⇒ Object
List the directory or download the actual file indicated by the path.
- #app_info(name) ⇒ Object
- #app_instances(name) ⇒ Object
- #app_stats(name) ⇒ Object
- #app_update_info(name) ⇒ Object
-
#apps ⇒ Object
Apps.
- #bind_service(service, appname) ⇒ Object
-
#change_password(new_password) ⇒ Object
sets the password for the current logged user.
-
#change_quota(mem, apps, uris, services, user_info, email) ⇒ Object
sets the password for the current logged user.
- #check_quota(name) ⇒ Object
-
#check_resources(resources) ⇒ Object
Send in a resources manifest array to the system to have it check what is needed to actually send.
- #create_app(name, manifest = {}) ⇒ Object
-
#create_service(service, name, dbflag, dbjndi, dbusername, dbpasswd, dburl, dbsize) ⇒ Object
def create_service(service, name) check_login_status services = services_info services ||= [] service_hash = nil.
- #delete_app(name) ⇒ Object
- #delete_service(name) ⇒ Object
- #delete_user(user_email) ⇒ Object
- #get_user_info(email) ⇒ Object
-
#info ⇒ Object
Retrieves information on the target cloud, and optionally the logged in user.
-
#initialize(target_url = VMC::DEFAULT_TARGET, auth_token = nil) ⇒ Client
constructor
Initialize new client to the target_uri with optional auth_token.
-
#logged_in? ⇒ Boolean
Checks that the auth_token is valid.
-
#login(user, password) ⇒ Object
login and return an auth_token Auth token can be retained and used in creating new clients, avoiding login.
- #proxy_for(proxy) ⇒ Object
- #quota_info ⇒ Object
- #quota_service(name, dbsize, update_flag) ⇒ Object
- #raw_info ⇒ Object
- #runtimes_info ⇒ Object
-
#services ⇒ Object
listing of services that are available in the system.
-
#services_info ⇒ Object
Global listing of services that are available on the target system.
-
#target_valid? ⇒ Boolean
Checks that the target is valid.
- #unbind_service(service, appname) ⇒ Object
- #update_app(name, manifest) ⇒ Object
- #upload_app(name, zipfile, resource_manifest = nil) ⇒ Object
- #users ⇒ Object
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 |
# 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 end |
Instance Attribute Details
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
24 25 26 |
# File 'lib/vmc/client.rb', line 24 def auth_token @auth_token end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/vmc/client.rb', line 24 def host @host end |
#proxy ⇒ Object
Returns the value of attribute proxy.
24 25 26 |
# File 'lib/vmc/client.rb', line 24 def proxy @proxy end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
24 25 26 |
# File 'lib/vmc/client.rb', line 24 def target @target end |
#trace ⇒ Object
Returns the value of attribute trace.
25 26 27 |
# File 'lib/vmc/client.rb', line 25 def trace @trace end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
24 25 26 |
# File 'lib/vmc/client.rb', line 24 def user @user end |
Class Method Details
.path(*path) ⇒ Object
415 416 417 418 419 |
# File 'lib/vmc/client.rb', line 415 def self.path(*path) path.flatten.collect { |x| URI.encode x.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") }.join("/") end |
Instance Method Details
#add_user(user_email, password) ⇒ Object
def add_user(user_email, password, app)
json_post(VMC::USERS_PATH, { :email => user_email, :password => password, :app => app})
end
404 405 406 |
# File 'lib/vmc/client.rb', line 404 def add_user(user_email, password) json_post(VMC::USERS_PATH, { :email => user_email, :password => password }) end |
#app_crashes(name) ⇒ Object
150 151 152 153 |
# File 'lib/vmc/client.rb', line 150 def app_crashes(name) check_login_status 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.
157 158 159 160 161 162 163 |
# File 'lib/vmc/client.rb', line 157 def app_files(name, path, instance='0') check_login_status path = path.gsub('//', '/') url = path(VMC::APPS_PATH, name, "instances", instance, "files", path) _, body, headers = http_get(url) body end |
#app_info(name) ⇒ Object
121 122 123 124 |
# File 'lib/vmc/client.rb', line 121 def app_info(name) check_login_status json_get(path(VMC::APPS_PATH, name)) end |
#app_instances(name) ⇒ Object
145 146 147 148 |
# File 'lib/vmc/client.rb', line 145 def app_instances(name) check_login_status json_get(path(VMC::APPS_PATH, name, "instances")) end |
#app_stats(name) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/vmc/client.rb', line 131 def app_stats(name) check_login_status 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
126 127 128 129 |
# File 'lib/vmc/client.rb', line 126 def app_update_info(name) check_login_status json_get(path(VMC::APPS_PATH, name, "update")) end |
#apps ⇒ Object
Apps
79 80 81 82 |
# File 'lib/vmc/client.rb', line 79 def apps check_login_status json_get(VMC::APPS_PATH) end |
#bind_service(service, appname) ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/vmc/client.rb', line 275 def bind_service(service, appname) check_login_status 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
351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/vmc/client.rb', line 351 def change_password(new_password) check_login_status user_info = json_get(path(VMC::USERS_PATH, @user)) #puts "changepasswd" #puts user_info if user_info user_info[:password] = new_password # puts path(VMC::USERS_PATH, @user) json_put(path(VMC::USERS_PATH, @user), user_info) end end |
#change_quota(mem, apps, uris, services, user_info, email) ⇒ Object
sets the password for the current logged user
369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/vmc/client.rb', line 369 def change_quota(mem, apps, uris, services, user_info, email) check_login_status #user_info = json_get(path(VMC::USERS_PATH, @user)) #if user_info user_info[:mem] = mem user_info[:app] = apps user_info[:uri] = uris user_info[:service] = services user_info[:email] = email json_put(path(VMC::USERSQUOTA_PATH, user_info), user_info) #end end |
#check_quota(name) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/vmc/client.rb', line 249 def check_quota(name) check_login_status svcs = services || [] type = nil; max_db_size = 0 names = svcs.collect { |s| if name == s[:name] max_db_size = s[:max_db_size] type = s[:vendor] end s[:name] } raise TargetError, "Temporary does not support" unless "mysql" == type raise TargetError, "Service [#{name}] not a valid service" unless names.include? name max_db_size 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]
301 302 303 304 305 |
# File 'lib/vmc/client.rb', line 301 def check_resources(resources) check_login_status status, body, headers = json_post(VMC::RESOURCES_PATH, resources) json_parse(body) end |
#create_app(name, manifest = {}) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/vmc/client.rb', line 84 def create_app(name, manifest={}) check_login_status app = manifest.dup app[:name] = name app[:instances] ||= 1 json_post(VMC::APPS_PATH, app) end |
#create_service(service, name, dbflag, dbjndi, dbusername, dbpasswd, dburl, dbsize) ⇒ Object
def create_service(service, name)
check_login_status
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
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/vmc/client.rb', line 205 def create_service(service, name, dbflag , dbjndi, dbusername, dbpasswd, dburl, dbsize) check_login_status 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 service_hash[:db_flag] = dbflag service_hash[:db_jndi] = dbjndi service_hash[:db_uname] = dbusername service_hash[:db_passwd] = dbpasswd service_hash[:db_url] = dburl service_hash[:max_db_size] = dbsize #puts service_hash json_post(path(VMC::SERVICES_PATH), service_hash) end |
#delete_app(name) ⇒ Object
116 117 118 119 |
# File 'lib/vmc/client.rb', line 116 def delete_app(name) check_login_status http_delete(path(VMC::APPS_PATH, name)) end |
#delete_service(name) ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/vmc/client.rb', line 241 def delete_service(name) check_login_status 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
408 409 410 411 |
# File 'lib/vmc/client.rb', line 408 def delete_user(user_email) check_login_status http_delete(path(VMC::USERS_PATH, user_email)) end |
#get_user_info(email) ⇒ Object
363 364 365 366 |
# File 'lib/vmc/client.rb', line 363 def get_user_info(email) check_login_status user_info = json_get(path(VMC::USERSQUOTA_PATH, email)) end |
#info ⇒ Object
Retrieves information on the target cloud, and optionally the logged in user
51 52 53 54 |
# File 'lib/vmc/client.rb', line 51 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
324 325 326 327 328 329 330 331 332 |
# File 'lib/vmc/client.rb', line 324 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.
341 342 343 344 345 346 347 348 |
# File 'lib/vmc/client.rb', line 341 def login(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
391 392 393 |
# File 'lib/vmc/client.rb', line 391 def proxy_for(proxy) @proxy = proxy end |
#quota_info ⇒ Object
56 57 58 59 |
# File 'lib/vmc/client.rb', line 56 def quota_info # TODO: Should merge for new version IMO, general, services, user_account json_get(VMC::QUOTA_INFO_PATH) end |
#quota_service(name, dbsize, update_flag) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/vmc/client.rb', line 266 def quota_service(name, dbsize, update_flag) service_hash ={ :max_db_size => dbsize } service_hash[:update_flag] = update_flag #puts service_hash http_put(path(VMC::SERVICES_PATH, name), service_hash) end |
#raw_info ⇒ Object
61 62 63 |
# File 'lib/vmc/client.rb', line 61 def raw_info http_get(VMC::INFO_PATH) end |
#runtimes_info ⇒ Object
71 72 73 |
# File 'lib/vmc/client.rb', line 71 def runtimes_info json_get(path(VMC::GLOBAL_RUNTIMES_PATH)) end |
#services ⇒ Object
listing of services that are available in the system
170 171 172 173 |
# File 'lib/vmc/client.rb', line 170 def services check_login_status json_get(VMC::SERVICES_PATH) end |
#services_info ⇒ Object
Global listing of services that are available on the target system
66 67 68 69 |
# File 'lib/vmc/client.rb', line 66 def services_info check_login_status json_get(path(VMC::GLOBAL_SERVICES_PATH)) end |
#target_valid? ⇒ Boolean
Checks that the target is valid
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/vmc/client.rb', line 312 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 |
#unbind_service(service, appname) ⇒ Object
283 284 285 286 287 288 289 290 |
# File 'lib/vmc/client.rb', line 283 def unbind_service(service, appname) check_login_status app = app_info(appname) services = app[:services] || [] services.delete(service) app[:services] = services update_app(appname, app) end |
#update_app(name, manifest) ⇒ Object
92 93 94 95 |
# File 'lib/vmc/client.rb', line 92 def update_app(name, manifest) check_login_status json_put(path(VMC::APPS_PATH, name), manifest) end |
#upload_app(name, zipfile, resource_manifest = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/vmc/client.rb', line 97 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 ||= [] check_login_status 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 |
#users ⇒ Object
395 396 397 398 |
# File 'lib/vmc/client.rb', line 395 def users check_login_status json_get(VMC::USERS_PATH) end |