jdc 0.2.1 → 0.2.2.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. data/LICENSE +1277 -24
  2. data/Rakefile +13 -0
  3. data/bin/jdc +12 -2
  4. data/lib/admin/README.md +15 -0
  5. data/lib/admin/curl.rb +60 -0
  6. data/lib/admin/guid.rb +89 -0
  7. data/lib/admin/plugin.rb +6 -0
  8. data/lib/admin/service_auth_token.rb +94 -0
  9. data/lib/admin/service_broker/add.rb +47 -0
  10. data/lib/admin/service_broker/service_brokers.rb +24 -0
  11. data/lib/admin/set_quota.rb +44 -0
  12. data/lib/console/README.md +8 -0
  13. data/lib/console/console.rb +187 -0
  14. data/lib/console/plugin.rb +33 -0
  15. data/lib/jdc.rb +15 -2
  16. data/lib/jdc/cli.rb +556 -0
  17. data/lib/jdc/cli/app/app.rb +43 -0
  18. data/lib/jdc/cli/app/apps.rb +87 -0
  19. data/lib/jdc/cli/app/base.rb +72 -0
  20. data/lib/jdc/cli/app/delete.rb +95 -0
  21. data/lib/jdc/cli/app/deprecated.rb +11 -0
  22. data/lib/jdc/cli/app/env.rb +78 -0
  23. data/lib/jdc/cli/app/events.rb +45 -0
  24. data/lib/jdc/cli/app/files.rb +137 -0
  25. data/lib/jdc/cli/app/health.rb +26 -0
  26. data/lib/jdc/cli/app/instances.rb +53 -0
  27. data/lib/jdc/cli/app/logs.rb +76 -0
  28. data/lib/jdc/cli/app/push.rb +103 -0
  29. data/lib/jdc/cli/app/push/create.rb +108 -0
  30. data/lib/jdc/cli/app/push/interactions.rb +86 -0
  31. data/lib/jdc/cli/app/push/sync.rb +57 -0
  32. data/lib/jdc/cli/app/rename.rb +35 -0
  33. data/lib/jdc/cli/app/restart.rb +31 -0
  34. data/lib/jdc/cli/app/scale.rb +63 -0
  35. data/lib/jdc/cli/app/start.rb +161 -0
  36. data/lib/jdc/cli/app/stats.rb +67 -0
  37. data/lib/jdc/cli/app/stop.rb +27 -0
  38. data/lib/jdc/cli/domain/base.rb +9 -0
  39. data/lib/jdc/cli/domain/domains.rb +40 -0
  40. data/lib/jdc/cli/domain/map.rb +55 -0
  41. data/lib/jdc/cli/domain/unmap.rb +56 -0
  42. data/lib/jdc/cli/help.rb +15 -0
  43. data/lib/jdc/cli/interactive.rb +105 -0
  44. data/lib/jdc/cli/login_requirements.rb +15 -0
  45. data/lib/jdc/cli/organization/base.rb +14 -0
  46. data/lib/jdc/cli/organization/create.rb +37 -0
  47. data/lib/jdc/cli/organization/delete.rb +63 -0
  48. data/lib/jdc/cli/organization/org.rb +45 -0
  49. data/lib/jdc/cli/organization/orgs.rb +30 -0
  50. data/lib/jdc/cli/organization/rename.rb +37 -0
  51. data/lib/jdc/cli/populators/base.rb +16 -0
  52. data/lib/jdc/cli/populators/organization.rb +32 -0
  53. data/lib/jdc/cli/populators/populator_methods.rb +64 -0
  54. data/lib/jdc/cli/populators/space.rb +33 -0
  55. data/lib/jdc/cli/populators/target.rb +13 -0
  56. data/lib/jdc/cli/route/base.rb +9 -0
  57. data/lib/jdc/cli/route/delete.rb +28 -0
  58. data/lib/jdc/cli/route/map.rb +68 -0
  59. data/lib/jdc/cli/route/routes.rb +26 -0
  60. data/lib/jdc/cli/route/unmap.rb +56 -0
  61. data/lib/jdc/cli/service/base.rb +9 -0
  62. data/lib/jdc/cli/service/bind.rb +44 -0
  63. data/lib/jdc/cli/service/create.rb +159 -0
  64. data/lib/jdc/cli/service/delete.rb +83 -0
  65. data/lib/jdc/cli/service/rename.rb +36 -0
  66. data/lib/jdc/cli/service/service.rb +42 -0
  67. data/lib/jdc/cli/service/service_instance_helper.rb +99 -0
  68. data/lib/jdc/cli/service/services.rb +111 -0
  69. data/lib/jdc/cli/service/unbind.rb +37 -0
  70. data/lib/jdc/cli/space/base.rb +29 -0
  71. data/lib/jdc/cli/space/create.rb +67 -0
  72. data/lib/jdc/cli/space/delete.rb +56 -0
  73. data/lib/jdc/cli/space/rename.rb +38 -0
  74. data/lib/jdc/cli/space/space.rb +66 -0
  75. data/lib/jdc/cli/space/spaces.rb +57 -0
  76. data/lib/jdc/cli/space/switch.rb +19 -0
  77. data/lib/jdc/cli/start/base.rb +41 -0
  78. data/lib/jdc/cli/start/colors.rb +13 -0
  79. data/lib/jdc/cli/start/target.rb +50 -0
  80. data/lib/jdc/cli/start/target_prettifier.rb +17 -0
  81. data/lib/jdc/cli/start/targets.rb +16 -0
  82. data/lib/jdc/cli/user/base.rb +30 -0
  83. data/lib/jdc/cli/user/create.rb +52 -0
  84. data/lib/jdc/cli/user/passwd.rb +37 -0
  85. data/lib/jdc/cli/user/register.rb +43 -0
  86. data/lib/jdc/cli/user/users.rb +32 -0
  87. data/lib/jdc/constants.rb +11 -0
  88. data/lib/jdc/errors.rb +19 -0
  89. data/lib/jdc/object_extensions.rb +15 -0
  90. data/lib/jdc/plugin.rb +56 -0
  91. data/lib/jdc/spacing.rb +89 -0
  92. data/lib/jdc/spec_helper.rb +1 -0
  93. data/lib/jdc/test_support.rb +6 -0
  94. data/lib/jdc/version.rb +3 -0
  95. data/lib/manifests/errors.rb +35 -0
  96. data/lib/manifests/loader.rb +31 -0
  97. data/lib/manifests/loader/builder.rb +39 -0
  98. data/lib/manifests/loader/normalizer.rb +145 -0
  99. data/lib/manifests/loader/resolver.rb +79 -0
  100. data/lib/manifests/manifests.rb +344 -0
  101. data/lib/manifests/plugin.rb +140 -0
  102. data/lib/micro/README.md +9 -0
  103. data/lib/micro/errors.rb +4 -0
  104. data/lib/{jdc → micro}/micro.rb +15 -15
  105. data/lib/micro/plugin.rb +197 -0
  106. data/lib/micro/switcher/base.rb +79 -0
  107. data/lib/{jdc/micro → micro}/switcher/darwin.rb +5 -3
  108. data/lib/{jdc/micro → micro}/switcher/dummy.rb +1 -1
  109. data/lib/micro/switcher/linux.rb +16 -0
  110. data/lib/{jdc/micro → micro}/switcher/windows.rb +5 -5
  111. data/lib/{jdc/micro → micro}/vmrun.rb +26 -19
  112. data/lib/tasks/gem_release.rake +42 -0
  113. data/lib/tunnel/README.md +29 -0
  114. data/{config → lib/tunnel/config}/clients.yml +2 -2
  115. data/lib/tunnel/helper-app/Gemfile +10 -0
  116. data/lib/tunnel/helper-app/Gemfile.lock +48 -0
  117. data/{caldecott_helper → lib/tunnel/helper-app}/server.rb +5 -5
  118. data/lib/tunnel/plugin.rb +183 -0
  119. data/lib/tunnel/tunnel.rb +295 -0
  120. metadata +371 -210
  121. data/README.md +0 -102
  122. data/config/micro/paths.yml +0 -22
  123. data/config/micro/refresh_ip.rb +0 -20
  124. data/lib/cli.rb +0 -53
  125. data/lib/cli/commands/admin.rb +0 -58
  126. data/lib/cli/commands/apps.rb +0 -1129
  127. data/lib/cli/commands/base.rb +0 -228
  128. data/lib/cli/commands/manifest.rb +0 -56
  129. data/lib/cli/commands/micro.rb +0 -115
  130. data/lib/cli/commands/misc.rb +0 -126
  131. data/lib/cli/commands/services.rb +0 -178
  132. data/lib/cli/commands/user.rb +0 -14
  133. data/lib/cli/config.rb +0 -173
  134. data/lib/cli/console_helper.rb +0 -170
  135. data/lib/cli/core_ext.rb +0 -122
  136. data/lib/cli/errors.rb +0 -19
  137. data/lib/cli/frameworks.rb +0 -265
  138. data/lib/cli/manifest_helper.rb +0 -300
  139. data/lib/cli/runner.rb +0 -505
  140. data/lib/cli/services_helper.rb +0 -84
  141. data/lib/cli/tunnel_helper.rb +0 -332
  142. data/lib/cli/usage.rb +0 -86
  143. data/lib/cli/version.rb +0 -7
  144. data/lib/cli/zip_util.rb +0 -77
  145. data/lib/jdc/client.rb +0 -457
  146. data/lib/jdc/const.rb +0 -25
  147. data/lib/jdc/micro/switcher/base.rb +0 -97
  148. data/lib/jdc/micro/switcher/linux.rb +0 -16
  149. data/lib/jdc/signature/version.rb +0 -27
  150. data/lib/jdc/signer.rb +0 -13
  151. data/lib/jdc/timer.rb +0 -12
data/lib/cli/version.rb DELETED
@@ -1,7 +0,0 @@
1
- module JDC
2
- module Cli
3
- # This version number is used as the RubyGem release version.
4
- # The internal JDC version number is JDC::VERSION.
5
- VERSION = '0.1.2'
6
- end
7
- end
data/lib/cli/zip_util.rb DELETED
@@ -1,77 +0,0 @@
1
-
2
- require 'zip/zipfilesystem'
3
-
4
- module JDC::Cli
5
-
6
- class ZipUtil
7
-
8
- PACK_EXCLUSION_GLOBS = ['..', '.', '*~', '#*#', '*.log']
9
-
10
- class << self
11
-
12
- def to_dev_null
13
- if WINDOWS
14
- 'nul'
15
- else
16
- '/dev/null'
17
- end
18
- end
19
-
20
- def entry_lines(file)
21
- contents = nil
22
- unless JDC::Cli::Config.nozip
23
- contents = `unzip -l #{file} 2> #{to_dev_null}`
24
- contents = nil if $? != 0
25
- end
26
- # Do Ruby version if told to or native version failed
27
- unless contents
28
- entries = []
29
- Zip::ZipFile.foreach(file) { |zentry| entries << zentry }
30
- contents = entries.join("\n")
31
- end
32
- contents
33
- end
34
-
35
- def unpack(file, dest)
36
- unless JDC::Cli::Config.nozip
37
- FileUtils.mkdir(dest)
38
- `unzip -q #{file} -d #{dest} 2> #{to_dev_null}`
39
- return unless $? != 0
40
- end
41
- # Do Ruby version if told to or native version failed
42
- Zip::ZipFile.foreach(file) do |zentry|
43
- epath = "#{dest}/#{zentry}"
44
- dirname = File.dirname(epath)
45
- FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
46
- zentry.extract(epath) unless File.exists?(epath)
47
- end
48
- end
49
-
50
- def get_files_to_pack(dir)
51
- Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select do |f|
52
- process = true
53
- PACK_EXCLUSION_GLOBS.each { |e| process = false if File.fnmatch(e, File.basename(f)) }
54
- process && File.exists?(f)
55
- end
56
- end
57
-
58
- def pack(dir, zipfile)
59
- unless JDC::Cli::Config.nozip
60
- excludes = PACK_EXCLUSION_GLOBS.map { |e| "\\#{e}" }
61
- excludes = excludes.join(' ')
62
- Dir.chdir(dir) do
63
- `zip -y -q -r #{zipfile} . -x #{excludes} 2> #{to_dev_null}`
64
- return unless $? != 0
65
- end
66
- end
67
- # Do Ruby version if told to or native version failed
68
- Zip::ZipFile::open(zipfile, true) do |zf|
69
- get_files_to_pack(dir).each do |f|
70
- zf.add(f.sub("#{dir}/",''), f)
71
- end
72
- end
73
- end
74
-
75
- end
76
- end
77
- end
data/lib/jdc/client.rb DELETED
@@ -1,457 +0,0 @@
1
- # JDC client
2
- #
3
- # Example:
4
- #
5
- # require 'jdc'
6
- # client = JDC::Client.new('api.vcap.me')
7
- # client.create('myapplication', manifest)
8
- # client.create_service('redis', 'my_redis_service', opts);
9
- #
10
-
11
- require 'rubygems'
12
- require 'json/pure'
13
- require 'open-uri'
14
-
15
- require File.expand_path('../const', __FILE__)
16
- require File.expand_path('../timer', __FILE__)
17
- require File.expand_path('../signature/version.rb', __FILE__)
18
-
19
- class JDC::Client
20
-
21
- include JDC::Timer
22
- include JDC::Signature::Version
23
-
24
- def self.version
25
- JDC::VERSION
26
- end
27
-
28
- attr_reader :target, :host, :user, :proxy, :auth_token, :access_key_id, :secret_key
29
- attr_accessor :trace
30
-
31
- # Error codes
32
- JDC_HTTP_ERROR_CODES = [ 400, 500 ]
33
-
34
- # Errors
35
- class BadTarget < RuntimeError; end
36
- class AuthError < RuntimeError; end
37
- class TargetError < RuntimeError; end
38
- class NotFound < RuntimeError; end
39
- class BadResponse < RuntimeError; end
40
- class HTTPException < RuntimeError; end
41
-
42
- # Initialize new client to the target_uri with optional auth_token
43
- def initialize(target_url=JDC::DEFAULT_TARGET, auth_token=nil)
44
- target_url = "http://#{target_url}" unless /^https?/ =~ target_url
45
- target_url = target_url.gsub(/\/+$/, '')
46
- @target = target_url
47
- @auth_token = auth_token
48
- @access_key_id = ENV[JDC::ACCESS_KEY]
49
- raise TargetError, "Please set the enviroment value:ACCESS_KEY" unless @access_key_id
50
- @secret_key = ENV[JDC::SECRET_KEY]
51
- raise TargetError, "Please set the enviroment value:SECRET_KEY" unless @secret_key
52
- end
53
-
54
- ######################################################
55
- # Target info
56
- ######################################################
57
-
58
- # Retrieves information on the target cloud, and optionally the logged in user
59
- def info
60
- # TODO: Should merge for new version IMO, general, services, user_account
61
- json_get(JDC::INFO_PATH)
62
- end
63
-
64
- def raw_info
65
- http_get(JDC::INFO_PATH)
66
- end
67
-
68
- # Global listing of services that are available on the target system
69
- def services_info
70
- json_get(path(JDC::GLOBAL_SERVICES_PATH))
71
- end
72
-
73
- def runtimes_info
74
- json_get(path(JDC::GLOBAL_RUNTIMES_PATH))
75
- end
76
-
77
- ######################################################
78
- # Apps
79
- ######################################################
80
-
81
- def apps
82
- json_get(JDC::APPS_PATH)
83
- end
84
-
85
- def create_app(name, manifest={})
86
- app = manifest.dup
87
- app[:name] = name
88
- app[:instances] ||= 1
89
- json_post(JDC::APPS_PATH, app)
90
- end
91
-
92
- def update_app(name, manifest)
93
- json_put(path(JDC::APPS_PATH, name), manifest)
94
- end
95
-
96
- def upload_app(name, zipfile, resource_manifest=nil)
97
- #FIXME, manifest should be allowed to be null, here for compatability with old cc's
98
- resource_manifest ||= []
99
- upload_data = {:_method => 'put'}
100
- if zipfile
101
- if zipfile.is_a? File
102
- file = zipfile
103
- else
104
- file = File.new(zipfile, 'rb')
105
- end
106
- upload_data[:application] = file
107
- end
108
- upload_data[:resources] = resource_manifest.to_json if resource_manifest
109
- http_post(path(JDC::APPS_PATH, name, "application"), upload_data, 'multipart/form-data')
110
- rescue RestClient::ServerBrokeConnection
111
- retry
112
- end
113
-
114
- def delete_app(name)
115
- http_delete(path(JDC::APPS_PATH, name))
116
- end
117
-
118
- def app_info(name)
119
- json_get(path(JDC::APPS_PATH, name))
120
- end
121
-
122
- def app_update_info(name)
123
- json_get(path(JDC::APPS_PATH, name, "update"))
124
- end
125
-
126
- def app_stats(name)
127
- stats_raw = json_get(path(JDC::APPS_PATH, name, "stats"))
128
- stats = []
129
- stats_raw.each_pair do |k, entry|
130
- # Skip entries with no stats
131
- next unless entry[:stats]
132
- entry[:instance] = k.to_s.to_i
133
- entry[:state] = entry[:state].to_sym if entry[:state]
134
- stats << entry
135
- end
136
- stats.sort { |a,b| a[:instance] - b[:instance] }
137
- end
138
-
139
- def app_instances(name)
140
- json_get(path(JDC::APPS_PATH, name, "instances"))
141
- end
142
-
143
- def app_crashes(name)
144
- json_get(path(JDC::APPS_PATH, name, "crashes"))
145
- end
146
-
147
- # List the directory or download the actual file indicated by
148
- # the path.
149
- def app_files(name, path, instance='0')
150
- path = path.gsub('//', '/')
151
- url = path(JDC::APPS_PATH, name, "instances", instance, "files", path)
152
- _, body, headers = http_get(url)
153
- body
154
- end
155
-
156
- ######################################################
157
- # Services
158
- ######################################################
159
-
160
- # listing of services that are available in the system
161
- def services
162
- json_get(JDC::SERVICES_PATH)
163
- end
164
-
165
- def create_service(service, name)
166
- services = services_info
167
- services ||= []
168
- service_hash = nil
169
-
170
- service = service.to_s
171
-
172
- # FIXME!
173
- services.each do |service_type, value|
174
- value.each do |vendor, version|
175
- version.each do |version_str, service_descr|
176
- if service == service_descr[:vendor]
177
- service_hash = {
178
- :type => service_descr[:type], :tier => 'free',
179
- :vendor => service, :version => version_str
180
- }
181
- break
182
- end
183
- end
184
- end
185
- end
186
-
187
- raise TargetError, "Service [#{service}] is not a valid service choice" unless service_hash
188
- service_hash[:name] = name
189
- json_post(path(JDC::SERVICES_PATH), service_hash)
190
- end
191
-
192
- def delete_service(name)
193
- svcs = services || []
194
- names = svcs.collect { |s| s[:name] }
195
- raise TargetError, "Service [#{name}] not a valid service" unless names.include? name
196
- http_delete(path(JDC::SERVICES_PATH, name))
197
- end
198
-
199
- def bind_service(service, appname)
200
- app = app_info(appname)
201
- services = app[:services] || []
202
- app[:services] = services << service
203
- update_app(appname, app)
204
- end
205
-
206
- def unbind_service(service, appname)
207
- app = app_info(appname)
208
- services = app[:services] || []
209
- services.delete(service)
210
- app[:services] = services
211
- update_app(appname, app)
212
- end
213
-
214
- ######################################################
215
- # Resources
216
- ######################################################
217
-
218
- # Send in a resources manifest array to the system to have
219
- # it check what is needed to actually send. Returns array
220
- # indicating what is needed. This returned manifest should be
221
- # sent in with the upload if resources were removed.
222
- # E.g. [{:sha1 => xxx, :size => xxx, :fn => filename}]
223
- def check_resources(resources)
224
- status, body, headers = json_post(JDC::RESOURCES_PATH, resources)
225
- json_parse(body)
226
- end
227
-
228
- ######################################################
229
- # Validation Helpers
230
- ######################################################
231
-
232
- # Checks that the target is valid
233
- def target_valid?
234
- return false unless descr = info
235
- return false unless descr[:name]
236
- return false unless descr[:build]
237
- return false unless descr[:version]
238
- return false unless descr[:support]
239
- true
240
- rescue
241
- false
242
- end
243
-
244
- ######################################################
245
- # User login/password
246
- ######################################################
247
-
248
- # login and return an auth_token
249
- # Auth token can be retained and used in creating
250
- # new clients, avoiding login.
251
- def login(user, password)
252
- status, body, headers = json_post(path(JDC::USERS_PATH, user, "tokens"), {:password => password})
253
- response_info = json_parse(body)
254
- if response_info
255
- @user = user
256
- @auth_token = response_info[:token]
257
- end
258
- end
259
-
260
- # sets the password for the current logged user
261
- def change_password(new_password)
262
- user_info = json_get(path(JDC::USERS_PATH, @user))
263
- if user_info
264
- user_info[:password] = new_password
265
- json_put(path(JDC::USERS_PATH, @user), user_info)
266
- end
267
- end
268
-
269
- ######################################################
270
- # System administration
271
- ######################################################
272
-
273
- def proxy=(proxy)
274
- @proxy = proxy
275
- end
276
-
277
- def proxy_for(proxy)
278
- @proxy = proxy
279
- end
280
-
281
- def users
282
- json_get(JDC::USERS_PATH)
283
- end
284
-
285
- def add_user(user_email, password)
286
- json_post(JDC::USERS_PATH, { :email => user_email, :password => password })
287
- end
288
-
289
- def delete_user(user_email)
290
- http_delete(path(JDC::USERS_PATH, user_email))
291
- end
292
-
293
- ######################################################
294
-
295
- def self.path(*path)
296
- path.flatten.collect { |x|
297
- URI.encode x.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
298
- }.join("/")
299
- end
300
-
301
- private
302
-
303
- def path(*args, &blk)
304
- self.class.path(*args, &blk)
305
- end
306
-
307
- def json_get(url)
308
- status, body, headers = http_get(url, 'application/json')
309
- json_parse(body)
310
- rescue JSON::ParserError
311
- raise BadResponse, "Can't parse response into JSON", body
312
- end
313
-
314
- def json_post(url, payload)
315
- http_post(url, payload.to_json, 'application/json')
316
- end
317
-
318
- def json_put(url, payload)
319
- http_put(url, payload.to_json, 'application/json')
320
- end
321
-
322
- def json_parse(str)
323
- if str
324
- JSON.parse(str, :symbolize_names => true)
325
- end
326
- end
327
-
328
- require 'rest_client'
329
-
330
- # HTTP helpers
331
-
332
- def http_get(path, content_type=nil)
333
- request(:get, path, content_type)
334
- end
335
-
336
- def http_post(path, body, content_type=nil)
337
- request(:post, path, content_type, body)
338
- end
339
-
340
- def http_put(path, body, content_type=nil)
341
- request(:put, path, content_type, body)
342
- end
343
-
344
- def http_delete(path)
345
- request(:delete, path)
346
- end
347
-
348
- def request(method, path, content_type = nil, payload = nil, headers = {})
349
- headers = headers.dup
350
- headers['AUTHORIZATION'] = access_key_id if access_key_id
351
- headers['PROXY-USER'] = @proxy if @proxy
352
- headers['Version'] = JDC::VERSION
353
-
354
- #init date time -- tibelf
355
- headers['Date'] = get_time
356
- headers['ACCESS-KEY-ID'] = access_key_id if access_key_id
357
- headers['Path'] = path
358
-
359
- if content_type
360
- if content_type == 'application/json'
361
- headers['Content-Type'] = content_type
362
- headers['Accept'] = content_type
363
- else
364
- headers['Content-Type'] = content_type
365
- end
366
- end
367
-
368
- req = {
369
- :method => method, :url => "#{@target}/#{path}",
370
- :payload => payload, :headers => headers, :multipart => true
371
- }
372
-
373
- signature = generate_signature(secret_key, req)
374
- headers['signature'] = signature
375
-
376
- status, body, response_headers = perform_http_request(req)
377
-
378
- if request_failed?(status)
379
- # FIXME, old cc returned 400 on not found for file access
380
- err = (status == 404 || status == 400) ? NotFound : TargetError
381
- raise err, parse_error_message(status, body)
382
- else
383
- return status, body, response_headers
384
- end
385
- rescue URI::Error, SocketError, Errno::ECONNREFUSED => e
386
- raise BadTarget, "Cannot access target (%s)" % [ e.message ]
387
- end
388
-
389
- def request_failed?(status)
390
- JDC_HTTP_ERROR_CODES.detect{|error_code| status >= error_code}
391
- end
392
-
393
- def perform_http_request(req)
394
- proxy_uri = URI.parse(req[:url]).find_proxy()
395
- RestClient.proxy = proxy_uri.to_s if proxy_uri
396
-
397
- # Setup tracing if needed
398
- unless trace.nil?
399
- req[:headers]['X-VCAP-Trace'] = (trace == true ? '22' : trace)
400
- end
401
-
402
- result = nil
403
- RestClient::Request.execute(req) do |response, request|
404
- result = [ response.code, response.body, response.headers ]
405
- unless trace.nil?
406
- puts '>>>'
407
- puts "PROXY: #{RestClient.proxy}" if RestClient.proxy
408
- puts "REQUEST: #{req[:method]} #{req[:url]}"
409
- puts "RESPONSE_HEADERS:"
410
- response.headers.each do |key, value|
411
- puts " #{key} : #{value}"
412
- end
413
- puts "REQUEST_BODY: #{req[:payload]}" if req[:payload]
414
- puts "RESPONSE: [#{response.code}]"
415
- begin
416
- puts JSON.pretty_generate(JSON.parse(response.body))
417
- rescue
418
- puts "#{response.body}"
419
- end
420
- puts '<<<'
421
- end
422
- end
423
- result
424
- rescue Net::HTTPBadResponse => e
425
- raise BadTarget "Received bad HTTP response from target: #{e}"
426
- rescue SystemCallError, RestClient::Exception => e
427
- raise HTTPException, "HTTP exception: #{e.class}:#{e}"
428
- end
429
-
430
- def truncate(str, limit = 30)
431
- etc = '...'
432
- stripped = str.strip[0..limit]
433
- if stripped.length > limit
434
- stripped + etc
435
- else
436
- stripped
437
- end
438
- end
439
-
440
- def parse_error_message(status, body)
441
- parsed_body = json_parse(body.to_s)
442
- if parsed_body && parsed_body[:code] && parsed_body[:description]
443
- desc = parsed_body[:description].gsub("\"","'")
444
- "Error #{parsed_body[:code]}: #{desc}"
445
- else
446
- "Error (HTTP #{status}): #{body}"
447
- end
448
- rescue JSON::ParserError
449
- if body.nil? || body.empty?
450
- "Error (#{status}): No Response Received"
451
- else
452
- body_out = trace ? body : truncate(body)
453
- "Error (JSON #{status}): #{body_out}"
454
- end
455
- end
456
-
457
- end