cfoundry 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class Organization < Model
5
+ attribute :name
6
+ to_many :app_spaces
7
+ to_many :domains
8
+ to_many :users
9
+ to_many :managers, :as => :user
10
+ to_many :billing_managers, :as => :user
11
+ to_many :auditors, :as => :user
12
+
13
+ alias :spaces :app_spaces
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class Runtime < Model
5
+ attribute :name
6
+ attribute :description
7
+ to_many :apps
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class Service < Model
5
+ attribute :label
6
+ attribute :provider
7
+ attribute :url
8
+ attribute :description
9
+ attribute :version
10
+ attribute :info_url
11
+ attribute :acls
12
+ attribute :timeout
13
+ attribute :active
14
+ to_many :service_plans
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class ServiceInstance < Model
5
+ attribute :name
6
+ to_one :app_space
7
+ to_one :service_plan
8
+ to_many :service_bindings
9
+ attribute :credentials
10
+ attribute :vendor_data, :default => ""
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class ServicePlan < Model
5
+ attribute :name
6
+ attribute :description
7
+ to_one :service
8
+ to_many :service_instances
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class AppSpace < Model
5
+ attribute :name
6
+ to_one :organization
7
+ to_many :developers, :as => :user
8
+ to_many :managers, :as => :user
9
+ to_many :auditors, :as => :user
10
+ to_many :apps
11
+ to_many :domains
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require "cfoundry/v2/model"
2
+
3
+ module CFoundry::V2
4
+ class User < Model
5
+ to_many :app_spaces
6
+ to_many :organizations
7
+ to_many :managed_organizations, :as => :organization
8
+ to_many :billing_managed_organizations, :as => :organization
9
+ to_many :audited_organizations, :as => :organization
10
+ to_many :managed_app_spaces, :as => :app_space
11
+ to_many :audited_app_spaces, :as => :app_space
12
+ attribute :admin
13
+ to_one :default_app_space, :as => :app_space
14
+
15
+ attribute :guid # guid is explicitly set for users
16
+
17
+ alias :admin? :admin
18
+
19
+ alias :spaces :app_spaces
20
+ alias :managed_spaces :managed_app_spaces
21
+ alias :audited_spaces :audited_app_spaces
22
+ alias :default_space :default_app_space
23
+
24
+ # optional metadata from UAA
25
+ attr_accessor :emails, :name
26
+
27
+ def email
28
+ return unless @emails && @emails.first
29
+ @emails.first[:value]
30
+ end
31
+
32
+ def given_name
33
+ return unless @name && @name[:givenName] != email
34
+ @name[:givenName]
35
+ end
36
+
37
+ def family_name
38
+ return unless @name && @name[:familyName] != email
39
+ @name[:familyName]
40
+ end
41
+
42
+ def full_name
43
+ if @name && @name[:fullName]
44
+ @name[:fullName]
45
+ elsif given_name && family_name
46
+ "#{given_name} #{family_name}"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,4 +1,4 @@
1
1
  module CFoundry # :nodoc:
2
2
  # CFoundry library version number.
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-04 00:00:00 Z
18
+ date: 2012-07-13 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rest-client
@@ -105,12 +105,31 @@ extra_rdoc_files: []
105
105
  files:
106
106
  - LICENSE
107
107
  - Rakefile
108
- - lib/cfoundry/app.rb
108
+ - lib/cfoundry/baseclient.rb
109
109
  - lib/cfoundry/client.rb
110
110
  - lib/cfoundry/errors.rb
111
- - lib/cfoundry/restclient.rb
112
- - lib/cfoundry/service.rb
113
- - lib/cfoundry/user.rb
111
+ - lib/cfoundry/uaaclient.rb
112
+ - lib/cfoundry/v1/app.rb
113
+ - lib/cfoundry/v1/base.rb
114
+ - lib/cfoundry/v1/client.rb
115
+ - lib/cfoundry/v1/framework.rb
116
+ - lib/cfoundry/v1/runtime.rb
117
+ - lib/cfoundry/v1/service.rb
118
+ - lib/cfoundry/v1/service_instance.rb
119
+ - lib/cfoundry/v1/user.rb
120
+ - lib/cfoundry/v2/app.rb
121
+ - lib/cfoundry/v2/base.rb
122
+ - lib/cfoundry/v2/client.rb
123
+ - lib/cfoundry/v2/domain.rb
124
+ - lib/cfoundry/v2/framework.rb
125
+ - lib/cfoundry/v2/model.rb
126
+ - lib/cfoundry/v2/organization.rb
127
+ - lib/cfoundry/v2/runtime.rb
128
+ - lib/cfoundry/v2/service.rb
129
+ - lib/cfoundry/v2/service_instance.rb
130
+ - lib/cfoundry/v2/service_plan.rb
131
+ - lib/cfoundry/v2/space.rb
132
+ - lib/cfoundry/v2/user.rb
114
133
  - lib/cfoundry/version.rb
115
134
  - lib/cfoundry/zip.rb
116
135
  - lib/cfoundry.rb
@@ -154,4 +173,3 @@ test_files:
154
173
  - spec/client_spec.rb
155
174
  - spec/helpers.rb
156
175
  - spec/Rakefile
157
- has_rdoc:
@@ -1,260 +0,0 @@
1
- require "restclient"
2
- require "json"
3
-
4
- require "cfoundry/errors"
5
-
6
-
7
- module CFoundry
8
- class RESTClient # :nodoc:
9
- attr_accessor :target, :token, :proxy, :trace
10
-
11
- def initialize(
12
- target = "http://api.cloudfoundry.com",
13
- token = nil)
14
- @target = target
15
- @token = token
16
- end
17
-
18
- # Cloud metadata
19
- def info
20
- json_get("info")
21
- end
22
-
23
- def system_services
24
- json_get("info", "services")
25
- end
26
-
27
- def system_runtimes
28
- json_get("info", "runtimes")
29
- end
30
-
31
- # Users
32
- def users
33
- json_get("users")
34
- end
35
-
36
- def create_user(payload)
37
- post(payload.to_json, "users")
38
- end
39
-
40
- def user(email)
41
- json_get("users", email)
42
- end
43
-
44
- def delete_user(email)
45
- delete("users", email)
46
- true
47
- end
48
-
49
- def update_user(email, payload)
50
- put(payload.to_json, "users", email)
51
- end
52
-
53
- def create_token(payload, email)
54
- json_post(payload.to_json, "users", email, "tokens")
55
- end
56
-
57
- # Applications
58
- def apps
59
- json_get("apps")
60
- end
61
-
62
- def create_app(payload)
63
- json_post(payload.to_json, "apps")
64
- end
65
-
66
- def app(name)
67
- json_get("apps", name)
68
- end
69
-
70
- def instances(name)
71
- json_get("apps", name, "instances")["instances"]
72
- end
73
-
74
- def files(name, instance, *path)
75
- get("apps", name, "instances", instance, "files", *path)
76
- end
77
- alias :file :files
78
-
79
- def update_app(name, payload)
80
- put(payload.to_json, "apps", name)
81
- end
82
-
83
- def delete_app(name)
84
- delete("apps", name)
85
- true
86
- end
87
-
88
- def stats(name)
89
- json_get("apps", name, "stats")
90
- end
91
-
92
- def check_resources(fingerprints)
93
- json_post(fingerprints.to_json, "resources")
94
- end
95
-
96
- def upload_app(name, zipfile, resources = [])
97
- payload = {
98
- :_method => "put",
99
- :resources => resources.to_json,
100
- :multipart => true,
101
- :application =>
102
- if zipfile.is_a? File
103
- zipfile
104
- elsif zipfile.is_a? String
105
- File.new(zipfile, "rb")
106
- end
107
- }
108
-
109
- post(payload, "apps", name, "application")
110
- rescue RestClient::ServerBrokeConnection
111
- retry
112
- end
113
-
114
- # Services
115
- def services
116
- json_get("services")
117
- end
118
-
119
- def create_service(manifest)
120
- json_post(manifest.to_json, "services")
121
- end
122
-
123
- def service(name)
124
- json_get("services", name)
125
- end
126
-
127
- def delete_service(name)
128
- delete("services", name)
129
- true
130
- end
131
-
132
- private
133
- def request(type, segments, options = {})
134
- headers = {}
135
- headers["AUTHORIZATION"] = @token if @token
136
- headers["PROXY-USER"] = @proxy if @proxy
137
- headers["Content-Type"] = "application/json" # TODO: probably not always
138
- # and set Accept
139
- headers["Content-Length"] =
140
- options[:payload] ? options[:payload].size : 0
141
-
142
- req = options.dup
143
- req[:method] = type
144
- req[:url] = url(segments)
145
- req[:headers] = headers.merge(req[:headers] || {})
146
-
147
- json = req.delete :json
148
-
149
- RestClient::Request.execute(req) do |response, request|
150
- if @trace
151
- puts '>>>'
152
- puts "PROXY: #{RestClient.proxy}" if RestClient.proxy
153
- puts "REQUEST: #{req[:method]} #{req[:url]}"
154
- puts "RESPONSE_HEADERS:"
155
- response.headers.each do |key, value|
156
- puts " #{key} : #{value}"
157
- end
158
- puts "REQUEST_HEADERS:"
159
- request.headers.each do |key, value|
160
- puts " #{key} : #{value}"
161
- end
162
- puts "REQUEST_BODY: #{req[:payload]}" if req[:payload]
163
- puts "RESPONSE: [#{response.code}]"
164
- begin
165
- puts JSON.pretty_generate(JSON.parse(response.body))
166
- rescue
167
- puts "#{response.body}"
168
- end
169
- puts '<<<'
170
- end
171
-
172
- case response.code
173
- when 200, 204, 302
174
- if json
175
- if response.code == 204
176
- raise "Expected JSON response, got 204 No Content"
177
- end
178
-
179
- JSON.parse response
180
- else
181
- response
182
- end
183
-
184
- # TODO: figure out how/when the CC distinguishes these
185
- when 400, 403
186
- info = JSON.parse response
187
- raise Denied.new(
188
- info["code"],
189
- info["description"])
190
-
191
- when 404
192
- raise NotFound
193
-
194
- when 411, 500, 504
195
- begin
196
- raise_error(JSON.parse(response))
197
- rescue JSON::ParserError
198
- raise BadResponse.new(response.code, response)
199
- end
200
-
201
- else
202
- raise BadResponse.new(response.code, response)
203
- end
204
- end
205
- rescue SocketError, Errno::ECONNREFUSED => e
206
- raise TargetRefused, e.message
207
- end
208
-
209
- def raise_error(info)
210
- case info["code"]
211
- when 402
212
- raise UploadFailed.new(info["description"])
213
- else
214
- raise APIError.new(info["code"], info["description"])
215
- end
216
- end
217
-
218
- def get(*path)
219
- request(:get, path)
220
- end
221
-
222
- def delete(*path)
223
- request(:delete, path)
224
- end
225
-
226
- def post(payload, *path)
227
- request(:post, path, :payload => payload)
228
- end
229
-
230
- def put(payload, *path)
231
- request(:put, path, :payload => payload)
232
- end
233
-
234
- def json_get(*path)
235
- request(:get, path, :json => true)
236
- end
237
-
238
- def json_delete(*path)
239
- request(:delete, path, :json => true)
240
- end
241
-
242
- def json_post(payload, *path)
243
- request(:post, path, :payload => payload, :json => true)
244
- end
245
-
246
- def json_put(payload, *path)
247
- request(:put, path, :payload => payload, :json => true)
248
- end
249
-
250
- def url(segments)
251
- "#@target/#{safe_path(segments)}"
252
- end
253
-
254
- def safe_path(*segments)
255
- segments.flatten.collect { |x|
256
- URI.encode x.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
257
- }.join("/")
258
- end
259
- end
260
- end