Class: JDC::Client

Inherits:
Object show all
Includes:
Signature::Version, Timer
Defined in:
lib/jdc/client.rb

Defined Under Namespace

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

Constant Summary collapse

JDC_HTTP_ERROR_CODES =

Error codes

[ 400, 500 ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Signature::Version

#generate_signature, #string_to_sign

Methods included from Signer

#sign

Methods included from Timer

#get_time

Constructor Details

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

Initialize new client to the target_uri with optional auth_token

Raises:



43
44
45
46
47
48
49
50
51
52
# File 'lib/jdc/client.rb', line 43

def initialize(target_url=JDC::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
  @access_key_id = ENV[JDC::ACCESS_KEY_ID] 
  raise TargetError, "Please set the enviroment value:ACCESS_KEY_ID" unless @access_key_id 
  @secret_key = ENV[JDC::SECRET_KEY]
  raise TargetError, "Please set the enviroment value:SECRET_KEY" unless @secret_key 
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



28
29
30
# File 'lib/jdc/client.rb', line 28

def access_key_id
  @access_key_id
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



28
29
30
# File 'lib/jdc/client.rb', line 28

def auth_token
  @auth_token
end

#hostObject (readonly)

Returns the value of attribute host.



28
29
30
# File 'lib/jdc/client.rb', line 28

def host
  @host
end

#proxyObject

Returns the value of attribute proxy.



28
29
30
# File 'lib/jdc/client.rb', line 28

def proxy
  @proxy
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



28
29
30
# File 'lib/jdc/client.rb', line 28

def secret_key
  @secret_key
end

#targetObject (readonly)

Returns the value of attribute target.



28
29
30
# File 'lib/jdc/client.rb', line 28

def target
  @target
end

#traceObject

Returns the value of attribute trace.



29
30
31
# File 'lib/jdc/client.rb', line 29

def trace
  @trace
end

#userObject (readonly)

Returns the value of attribute user.



28
29
30
# File 'lib/jdc/client.rb', line 28

def user
  @user
end

Class Method Details

.path(*path) ⇒ Object



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

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

.versionObject



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

def self.version
  JDC::VERSION
end

Instance Method Details

#add_user(user_email, password) ⇒ Object



285
286
287
# File 'lib/jdc/client.rb', line 285

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

#app_crashes(name) ⇒ Object



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

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

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

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



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

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

#app_info(name) ⇒ Object



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

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

#app_instances(name) ⇒ Object



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

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

#app_stats(name) ⇒ Object



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

def app_stats(name)
  stats_raw = json_get(path(JDC::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
# File 'lib/jdc/client.rb', line 122

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

#appsObject

Apps



81
82
83
# File 'lib/jdc/client.rb', line 81

def apps
  json_get(JDC::APPS_PATH)
end

#bind_service(service, appname) ⇒ Object



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

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



261
262
263
264
265
266
267
# File 'lib/jdc/client.rb', line 261

def change_password(new_password)
   = json_get(path(JDC::USERS_PATH, @user))
  if 
    [:password] = new_password
    json_put(path(JDC::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]



223
224
225
226
# File 'lib/jdc/client.rb', line 223

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

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



85
86
87
88
89
90
# File 'lib/jdc/client.rb', line 85

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

#create_service(service, name) ⇒ Object

Raises:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/jdc/client.rb', line 165

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(JDC::SERVICES_PATH), service_hash)
end

#delete_app(name) ⇒ Object



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

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

#delete_service(name) ⇒ Object

Raises:



192
193
194
195
196
197
# File 'lib/jdc/client.rb', line 192

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(JDC::SERVICES_PATH, name))
end

#delete_user(user_email) ⇒ Object



289
290
291
# File 'lib/jdc/client.rb', line 289

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

#infoObject

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



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

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

#login(user, password) ⇒ Object

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



251
252
253
254
255
256
257
258
# File 'lib/jdc/client.rb', line 251

def (user, password)
  status, body, headers = json_post(path(JDC::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



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

def proxy_for(proxy)
  @proxy = proxy
end

#raw_infoObject



64
65
66
# File 'lib/jdc/client.rb', line 64

def raw_info
  http_get(JDC::INFO_PATH)
end

#runtimes_infoObject



73
74
75
# File 'lib/jdc/client.rb', line 73

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

#servicesObject

listing of services that are available in the system



161
162
163
# File 'lib/jdc/client.rb', line 161

def services
  json_get(JDC::SERVICES_PATH)
end

#services_infoObject

Global listing of services that are available on the target system



69
70
71
# File 'lib/jdc/client.rb', line 69

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

#target_valid?Boolean

Checks that the target is valid

Returns:

  • (Boolean)


233
234
235
236
237
238
239
240
241
242
# File 'lib/jdc/client.rb', line 233

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



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

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



92
93
94
# File 'lib/jdc/client.rb', line 92

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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jdc/client.rb', line 96

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(JDC::APPS_PATH, name, "application"), upload_data, 'multipart/form-data')
rescue RestClient::ServerBrokeConnection
  retry
end

#usersObject



281
282
283
# File 'lib/jdc/client.rb', line 281

def users
  json_get(JDC::USERS_PATH)
end