cfoundry 0.4.16 → 0.4.17
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.
- data/lib/cfoundry/uaaclient.rb +3 -5
- data/lib/cfoundry/v1/model.rb +8 -0
- data/lib/cfoundry/v1/model_magic.rb +2 -2
- data/lib/cfoundry/v2/model.rb +17 -0
- data/lib/cfoundry/v2/model_magic.rb +48 -25
- data/lib/cfoundry/validator.rb +2 -0
- data/lib/cfoundry/version.rb +1 -1
- data/spec/cfoundry/uaaclient_spec.rb +15 -4
- data/spec/cfoundry/v2/model_magic_spec.rb +115 -0
- data/spec/fakes/app_fake.rb +5 -0
- data/spec/fakes/domain_fake.rb +5 -0
- data/spec/fakes/framework_fake.rb +5 -0
- data/spec/fakes/organization_fake.rb +5 -0
- data/spec/fakes/route_fake.rb +5 -0
- data/spec/fakes/runtime_fake.rb +5 -0
- data/spec/fakes/service_fake.rb +5 -0
- data/spec/fakes/service_instance_fake.rb +5 -0
- data/spec/fakes/service_plan_fake.rb +5 -0
- data/spec/fakes/space_fake.rb +5 -0
- data/spec/fakes/user_fake.rb +5 -0
- data/spec/spec_helper.rb +6 -7
- data/spec/support/fake_helper.rb +226 -0
- metadata +30 -47
- data/spec/factories/app_factory.rb +0 -25
- data/spec/factories/client_factory.rb +0 -25
- data/spec/factories/domain_factory.rb +0 -10
- data/spec/factories/factory.rb +0 -4
- data/spec/factories/framework_factory.rb +0 -10
- data/spec/factories/organization_factory.rb +0 -23
- data/spec/factories/route_factory.rb +0 -11
- data/spec/factories/runtime_factory.rb +0 -10
- data/spec/factories/service_binding_factory.rb +0 -20
- data/spec/factories/service_factory.rb +0 -16
- data/spec/factories/service_instance_factory.rb +0 -20
- data/spec/factories/service_plan_factory.rb +0 -11
- data/spec/factories/space_factory.rb +0 -25
- data/spec/factories/user_factory.rb +0 -10
data/lib/cfoundry/uaaclient.rb
CHANGED
@@ -49,11 +49,9 @@ module CFoundry
|
|
49
49
|
|
50
50
|
def change_password(guid, new, old)
|
51
51
|
put(
|
52
|
-
{ :
|
53
|
-
|
54
|
-
|
55
|
-
},
|
56
|
-
"User", guid, "password",
|
52
|
+
{ :password => new, :oldPassword => old },
|
53
|
+
"Users", guid, "password",
|
54
|
+
:accept => :json,
|
57
55
|
:content => :json)
|
58
56
|
end
|
59
57
|
|
data/lib/cfoundry/v1/model.rb
CHANGED
@@ -20,6 +20,14 @@ module CFoundry::V1
|
|
20
20
|
def base_object_name
|
21
21
|
@base_object_name ||= object_name
|
22
22
|
end
|
23
|
+
|
24
|
+
def plural_object_name
|
25
|
+
"#{object_name}s"
|
26
|
+
end
|
27
|
+
|
28
|
+
def plural_base_object_name
|
29
|
+
"#{base_object_name}s"
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
attr_accessor :guid, :changes
|
@@ -20,10 +20,10 @@ module CFoundry::V1
|
|
20
20
|
|
21
21
|
def define_client_methods(klass = self)
|
22
22
|
singular = klass.object_name
|
23
|
-
plural =
|
23
|
+
plural = klass.plural_object_name
|
24
24
|
|
25
25
|
base_singular = klass.base_object_name
|
26
|
-
base_plural =
|
26
|
+
base_plural = klass.plural_base_object_name
|
27
27
|
|
28
28
|
BaseClientMethods.module_eval do
|
29
29
|
define_method(base_singular) do |guid|
|
data/lib/cfoundry/v2/model.rb
CHANGED
@@ -5,8 +5,21 @@ require "cfoundry/v2/model_magic"
|
|
5
5
|
|
6
6
|
module CFoundry::V2
|
7
7
|
class Model
|
8
|
+
@@objects = {}
|
9
|
+
|
8
10
|
extend ModelMagic
|
9
11
|
|
12
|
+
class << self
|
13
|
+
def objects
|
14
|
+
@@objects
|
15
|
+
end
|
16
|
+
|
17
|
+
def inherited(klass)
|
18
|
+
@@objects[klass.object_name] = klass
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
10
23
|
attr_accessor :guid, :cache, :changes
|
11
24
|
|
12
25
|
def initialize(guid, client, manifest = nil, partial = false)
|
@@ -39,6 +52,10 @@ module CFoundry::V2
|
|
39
52
|
@object_name ||= self.class.object_name
|
40
53
|
end
|
41
54
|
|
55
|
+
def plural_object_name
|
56
|
+
@plural_object_name ||= self.class.plural_object_name
|
57
|
+
end
|
58
|
+
|
42
59
|
def invalidate!
|
43
60
|
@manifest = nil
|
44
61
|
@partial = false
|
@@ -24,6 +24,18 @@ module CFoundry::V2
|
|
24
24
|
'\1_\2').downcase.to_sym
|
25
25
|
end
|
26
26
|
|
27
|
+
def plural_object_name
|
28
|
+
:"#{object_name}s"
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_client_methods(&blk)
|
32
|
+
ClientMethods.module_eval(&blk)
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_base_client_methods(&blk)
|
36
|
+
BaseClientMethods.module_eval(&blk)
|
37
|
+
end
|
38
|
+
|
27
39
|
def defaults
|
28
40
|
@defaults ||= {}
|
29
41
|
end
|
@@ -42,9 +54,9 @@ module CFoundry::V2
|
|
42
54
|
|
43
55
|
def inherited(klass)
|
44
56
|
singular = klass.object_name
|
45
|
-
plural =
|
57
|
+
plural = klass.plural_object_name
|
46
58
|
|
47
|
-
|
59
|
+
define_base_client_methods do
|
48
60
|
define_method(singular) do |guid, *args|
|
49
61
|
get("v2", plural, guid, :accept => :json,
|
50
62
|
:params => ModelMagic.params_from(args))
|
@@ -70,7 +82,7 @@ module CFoundry::V2
|
|
70
82
|
end
|
71
83
|
end
|
72
84
|
|
73
|
-
|
85
|
+
define_client_methods do
|
74
86
|
define_method(singular) do |*args|
|
75
87
|
guid, partial, _ = args
|
76
88
|
|
@@ -147,7 +159,12 @@ module CFoundry::V2
|
|
147
159
|
define_method(name) do
|
148
160
|
return @cache[name] if @cache.key?(name)
|
149
161
|
|
150
|
-
@cache[name] =
|
162
|
+
@cache[name] =
|
163
|
+
if manifest[:entity].key?(name)
|
164
|
+
manifest[:entity][name]
|
165
|
+
else
|
166
|
+
default
|
167
|
+
end
|
151
168
|
end
|
152
169
|
|
153
170
|
define_method(:"#{name}=") do |val|
|
@@ -208,7 +225,7 @@ module CFoundry::V2
|
|
208
225
|
end
|
209
226
|
|
210
227
|
define_method(:"#{name}=") do |val|
|
211
|
-
klass =
|
228
|
+
klass = self.class.objects[obj]
|
212
229
|
|
213
230
|
unless has_default && val == default
|
214
231
|
CFoundry::Validator.validate_type(val, klass)
|
@@ -218,15 +235,15 @@ module CFoundry::V2
|
|
218
235
|
@manifest[:entity] ||= {}
|
219
236
|
|
220
237
|
old = @manifest[:entity][:"#{name}_guid"]
|
221
|
-
if old != val.guid
|
222
|
-
old_obj =
|
238
|
+
if old != (val && val.guid)
|
239
|
+
old_obj =
|
240
|
+
@cache[name] || klass.new(@client, old, @manifest[:entity][name])
|
223
241
|
|
224
242
|
@changes[name] = [old_obj, val]
|
225
243
|
end
|
226
244
|
|
227
245
|
@cache[name] = val
|
228
246
|
|
229
|
-
@manifest[:entity][name] = val.manifest
|
230
247
|
@manifest[:entity][:"#{name}_guid"] =
|
231
248
|
@diff[:"#{name}_guid"] = val && val.guid
|
232
249
|
end
|
@@ -240,13 +257,14 @@ module CFoundry::V2
|
|
240
257
|
include QUERIES[singular]
|
241
258
|
|
242
259
|
object = opts[:as] || singular
|
243
|
-
plural_object = :"#{object}s"
|
244
260
|
|
245
261
|
kls = object.to_s.capitalize.gsub(/(.)_(.)/) do
|
246
262
|
$1 + $2.upcase
|
247
263
|
end
|
248
264
|
|
249
265
|
define_method(plural) do |*args|
|
266
|
+
klass = CFoundry::V2.const_get(kls)
|
267
|
+
|
250
268
|
opts, _ = args
|
251
269
|
opts ||= {}
|
252
270
|
|
@@ -269,8 +287,8 @@ module CFoundry::V2
|
|
269
287
|
else
|
270
288
|
res =
|
271
289
|
@client.send(
|
272
|
-
:"#{
|
273
|
-
"/v2/#{
|
290
|
+
:"#{klass.plural_object_name}_from",
|
291
|
+
"/v2/#{plural_object_name}/#@guid/#{plural}",
|
274
292
|
opts)
|
275
293
|
end
|
276
294
|
|
@@ -286,7 +304,9 @@ module CFoundry::V2
|
|
286
304
|
end
|
287
305
|
|
288
306
|
define_method(:"add_#{singular}") do |x|
|
289
|
-
|
307
|
+
klass = self.class.objects[object]
|
308
|
+
|
309
|
+
CFoundry::Validator.validate_type(x, klass)
|
290
310
|
|
291
311
|
if cache = @cache[plural]
|
292
312
|
cache << x unless cache.include?(x)
|
@@ -294,12 +314,14 @@ module CFoundry::V2
|
|
294
314
|
|
295
315
|
@client.base.request_path(
|
296
316
|
Net::HTTP::Put,
|
297
|
-
["v2",
|
317
|
+
["v2", plural_object_name, @guid, plural, x.guid],
|
298
318
|
:accept => :json)
|
299
319
|
end
|
300
320
|
|
301
321
|
define_method(:"remove_#{singular}") do |x|
|
302
|
-
|
322
|
+
klass = self.class.objects[object]
|
323
|
+
|
324
|
+
CFoundry::Validator.validate_type(x, klass)
|
303
325
|
|
304
326
|
if cache = @cache[plural]
|
305
327
|
cache.delete(x)
|
@@ -307,12 +329,12 @@ module CFoundry::V2
|
|
307
329
|
|
308
330
|
@client.base.request_path(
|
309
331
|
Net::HTTP::Delete,
|
310
|
-
["v2",
|
332
|
+
["v2", plural_object_name, @guid, plural, x.guid],
|
311
333
|
:accept => :json)
|
312
334
|
end
|
313
335
|
|
314
336
|
define_method(:"#{plural}=") do |xs|
|
315
|
-
klass =
|
337
|
+
klass = self.class.objects[object]
|
316
338
|
|
317
339
|
CFoundry::Validator.validate_type(xs, [klass])
|
318
340
|
|
@@ -322,15 +344,16 @@ module CFoundry::V2
|
|
322
344
|
old = @manifest[:entity][:"#{singular}_guids"]
|
323
345
|
if old != xs.collect(&:guid)
|
324
346
|
old_objs =
|
325
|
-
|
326
|
-
all
|
327
|
-
|
347
|
+
@cache[plural] ||
|
348
|
+
if all = @manifest[:entity][plural]
|
349
|
+
all.collect do |m|
|
350
|
+
klass.new(@client, m[:metadata][:guid], m)
|
351
|
+
end
|
352
|
+
elsif old
|
353
|
+
old.collect { |id| klass.new(@client, id) }
|
328
354
|
end
|
329
|
-
elsif old
|
330
|
-
old.collect { |id| klass.new(@client, id) }
|
331
|
-
end
|
332
355
|
|
333
|
-
@changes[
|
356
|
+
@changes[plural] = [old_objs, xs]
|
334
357
|
end
|
335
358
|
|
336
359
|
@cache[plural] = xs
|
@@ -344,7 +367,7 @@ module CFoundry::V2
|
|
344
367
|
define_method(:summary) do
|
345
368
|
@client.base.request_path(
|
346
369
|
Net::HTTP::Get,
|
347
|
-
["v2",
|
370
|
+
["v2", plural_object_name, @guid, "summary"],
|
348
371
|
:accept => :json)
|
349
372
|
end
|
350
373
|
|
@@ -386,7 +409,7 @@ module CFoundry::V2
|
|
386
409
|
def queryable_by(*names)
|
387
410
|
klass = self
|
388
411
|
singular = object_name
|
389
|
-
plural =
|
412
|
+
plural = plural_object_name
|
390
413
|
|
391
414
|
query = QUERIES[singular]
|
392
415
|
|
data/lib/cfoundry/validator.rb
CHANGED
data/lib/cfoundry/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe CFoundry::UAAClient do
|
4
4
|
let(:target) { "https://uaa.example.com" }
|
@@ -114,16 +114,27 @@ EOF
|
|
114
114
|
it 'sends a password change request' do
|
115
115
|
req = stub_request(
|
116
116
|
:put,
|
117
|
-
"#{target}/
|
117
|
+
"#{target}/Users/#{guid}/password"
|
118
118
|
).with(
|
119
119
|
:body => {
|
120
|
-
:schemas => ["urn:scim:schemas:core:1.0"],
|
121
120
|
:password => new,
|
122
121
|
:oldPassword => old
|
123
122
|
},
|
124
|
-
:headers => {
|
123
|
+
:headers => {
|
124
|
+
"Content-Type" => "application/json",
|
125
|
+
"Accept" => "application/json"
|
126
|
+
}
|
127
|
+
).to_return(
|
128
|
+
:status => 200,
|
129
|
+
:body => <<EOF
|
130
|
+
{
|
131
|
+
"status": "ok",
|
132
|
+
"message": "password_updated"
|
133
|
+
}
|
134
|
+
EOF
|
125
135
|
)
|
126
136
|
|
137
|
+
|
127
138
|
subject
|
128
139
|
|
129
140
|
expect(req).to have_been_requested
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CFoundry::V2::ModelMagic do
|
4
|
+
let(:client) { fake_client }
|
5
|
+
let(:mymodel) { fake_model }
|
6
|
+
let(:guid) { random_string("my-object-guid") }
|
7
|
+
let(:myobject) { mymodel.new(guid, client) }
|
8
|
+
|
9
|
+
describe 'attributes' do
|
10
|
+
describe 'reading' do
|
11
|
+
let(:mymodel) { fake_model { attribute :foo, :object } }
|
12
|
+
|
13
|
+
context 'when it exists in the manifest' do
|
14
|
+
subject { myobject.fake(:foo => "bar") }
|
15
|
+
|
16
|
+
it 'returns the value from the manifest' do
|
17
|
+
expect(subject.foo).to eq "bar"
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'and the default is nil but the value is false' do
|
21
|
+
let(:mymodel) {
|
22
|
+
fake_model { attribute :foo, :object, :default => nil }
|
23
|
+
}
|
24
|
+
|
25
|
+
subject { myobject.fake(:foo => false) }
|
26
|
+
|
27
|
+
it 'returns false' do
|
28
|
+
expect(subject.foo).to eq false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'and the default is false but the value is nil' do
|
33
|
+
let(:mymodel) {
|
34
|
+
fake_model { attribute :foo, :object, :default => false }
|
35
|
+
}
|
36
|
+
|
37
|
+
subject { myobject.fake(:foo => nil) }
|
38
|
+
|
39
|
+
it 'returns nil' do
|
40
|
+
expect(subject.foo).to eq nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when the manifest has not been acquired' do
|
46
|
+
it 'retrieves the manifest the first time' do
|
47
|
+
mock(client.base).my_fake_model(guid) {
|
48
|
+
{ :entity => { :foo => "fizz" } }
|
49
|
+
}.ordered
|
50
|
+
|
51
|
+
expect(myobject.foo).to eq "fizz"
|
52
|
+
|
53
|
+
dont_allow(client.base).my_fake_model.ordered
|
54
|
+
|
55
|
+
expect(myobject.foo).to eq "fizz"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when it does not exist in the manifest' do
|
60
|
+
let(:mymodel) {
|
61
|
+
fake_model { attribute :foo, :object, :default => "foo" }
|
62
|
+
}
|
63
|
+
|
64
|
+
subject { myobject.fake }
|
65
|
+
|
66
|
+
it 'returns the default value' do
|
67
|
+
expect(subject.foo).to eq "foo"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'to_one relationships' do
|
74
|
+
describe 'writing' do
|
75
|
+
let!(:mymodel) { fake_model { to_one :foo } }
|
76
|
+
let!(:othermodel) { fake_model :foo }
|
77
|
+
|
78
|
+
let(:myobject) { mymodel.new(nil, client).fake }
|
79
|
+
let(:otherobject) { othermodel.new(nil, client).fake }
|
80
|
+
|
81
|
+
subject { myobject.foo = otherobject }
|
82
|
+
|
83
|
+
it "sets the GUID in the manifest to the object's GUID" do
|
84
|
+
expect { subject }.to change {
|
85
|
+
myobject.manifest[:entity][:foo_guid]
|
86
|
+
}.to(otherobject.guid)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "tracks internal changes in the diff" do
|
90
|
+
expect { subject }.to change { myobject.diff }.to(
|
91
|
+
:foo_guid => otherobject.guid)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "tracks high-level changes in .changes" do
|
95
|
+
before = myobject.foo
|
96
|
+
expect { subject }.to change { myobject.changes }.to(
|
97
|
+
:foo => [before, otherobject])
|
98
|
+
end
|
99
|
+
|
100
|
+
context "when there is a default" do
|
101
|
+
let(:mymodel) { fake_model { to_one :foo, :default => nil } }
|
102
|
+
|
103
|
+
subject { myobject.foo = nil }
|
104
|
+
|
105
|
+
it "allows setting to the default" do
|
106
|
+
myobject.foo = otherobject
|
107
|
+
|
108
|
+
expect { subject }.to change {
|
109
|
+
myobject.manifest[:entity][:foo_guid]
|
110
|
+
}.from(otherobject.guid).to(nil)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
require "rspec"
|
2
2
|
require "cfoundry"
|
3
|
-
require "factory_girl"
|
4
3
|
require "webmock/rspec"
|
5
4
|
|
6
|
-
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |file|
|
5
|
+
Dir[File.expand_path('../{support,fakes}/**/*.rb', __FILE__)].each do |file|
|
7
6
|
require file
|
8
7
|
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
FactoryGirl.find_definitions
|
9
|
+
def random_string(tag = "random")
|
10
|
+
sprintf("%s-%x", tag, rand(10 ** 6))
|
11
|
+
end
|
14
12
|
|
15
13
|
RSpec.configure do |c|
|
14
|
+
c.include Fake::FakeMethods
|
16
15
|
c.mock_with :rr
|
17
|
-
end
|
16
|
+
end
|
@@ -0,0 +1,226 @@
|
|
1
|
+
module Fake
|
2
|
+
module FakeMethods
|
3
|
+
def fake_client(attributes = {})
|
4
|
+
CFoundry::V2::FakeClient.new.fake(attributes)
|
5
|
+
end
|
6
|
+
|
7
|
+
def fake(what, attributes = {})
|
8
|
+
fake_client.send(what).fake(attributes)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fake_list(what, count, attributes = {})
|
12
|
+
objs = []
|
13
|
+
|
14
|
+
count.times do
|
15
|
+
objs << fake(what, attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
objs
|
19
|
+
end
|
20
|
+
|
21
|
+
def fake_model(name = :my_fake_model, &init)
|
22
|
+
# There is a difference between ruby 1.8.7 and 1.8.8 in the order that
|
23
|
+
# the inherited callback gets called. In 1.8.7 the inherited callback
|
24
|
+
# is called after the block; in 1.8.8 and later it's called before.
|
25
|
+
# The upshot for us is we need a failproof way of getting the name
|
26
|
+
# to klass. So we're using a global variable to hand off the value.
|
27
|
+
# Please don't shoot us. - ESH & MMB
|
28
|
+
$object_name = name
|
29
|
+
klass = Class.new(CFoundry::V2::FakeModel) do
|
30
|
+
self.object_name = name
|
31
|
+
end
|
32
|
+
|
33
|
+
klass.class_eval(&init) if init
|
34
|
+
|
35
|
+
klass
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def fake(attributes = {})
|
40
|
+
fake_attributes(attributes).each do |k, v|
|
41
|
+
send(:"#{k}=", v)
|
42
|
+
setup_reverse_relationship(v)
|
43
|
+
end
|
44
|
+
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.define_many_association(target, plural)
|
49
|
+
target.class_eval do
|
50
|
+
define_method(plural) do |*args|
|
51
|
+
options, _ = args
|
52
|
+
options ||= {}
|
53
|
+
|
54
|
+
vals = get_many(plural) || []
|
55
|
+
|
56
|
+
if options[:query]
|
57
|
+
by, val = options[:query]
|
58
|
+
vals.select do |v|
|
59
|
+
v.send(by) == val
|
60
|
+
end
|
61
|
+
else
|
62
|
+
vals
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module CFoundry::V2
|
70
|
+
class Model
|
71
|
+
include Fake
|
72
|
+
|
73
|
+
attr_writer :client
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def get_many(plural)
|
78
|
+
@cache[plural]
|
79
|
+
end
|
80
|
+
|
81
|
+
def fake_attributes(attributes)
|
82
|
+
fakes = default_fakes.merge(attributes)
|
83
|
+
|
84
|
+
# default relationships to other fake objects
|
85
|
+
self.class.to_one_relations.each do |name, opts|
|
86
|
+
# remove _guid (not an actual attribute)
|
87
|
+
fakes.delete :"#{name}_guid"
|
88
|
+
next if fakes.key?(name)
|
89
|
+
|
90
|
+
fakes[name] =
|
91
|
+
if opts.key?(:default)
|
92
|
+
opts[:default]
|
93
|
+
else
|
94
|
+
@client.send(opts[:as] || name).fake
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
fakes
|
99
|
+
end
|
100
|
+
|
101
|
+
# override this to provide basic attributes (like name) dynamically
|
102
|
+
def default_fakes
|
103
|
+
self.class.defaults.merge(
|
104
|
+
:guid => random_string("fake-#{object_name}-guid"))
|
105
|
+
end
|
106
|
+
|
107
|
+
def setup_reverse_relationship(v)
|
108
|
+
if v.is_a?(Array)
|
109
|
+
v.each do |x|
|
110
|
+
setup_reverse_relationship(x)
|
111
|
+
end
|
112
|
+
|
113
|
+
return
|
114
|
+
end
|
115
|
+
|
116
|
+
return unless v.is_a?(Model)
|
117
|
+
|
118
|
+
relation, type = find_reverse_relationship(v)
|
119
|
+
|
120
|
+
v.client = @client
|
121
|
+
|
122
|
+
if type == :one
|
123
|
+
v.send(:"#{relation}=", self)
|
124
|
+
elsif type == :many
|
125
|
+
v.send(:"#{relation}=", v.send(relation) + [self])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def find_reverse_relationship(v)
|
130
|
+
singular = object_name
|
131
|
+
plural = plural_object_name
|
132
|
+
|
133
|
+
v.class.to_one_relations.each do |attr, opts|
|
134
|
+
return [attr, :one] if attr == singular
|
135
|
+
return [attr, :one] if opts[:as] == singular
|
136
|
+
end
|
137
|
+
|
138
|
+
v.class.to_many_relations.each do |attr, opts|
|
139
|
+
return [attr, :many] if attr == plural
|
140
|
+
return [attr, :many] if opts[:as] == singular
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
class FakeBase < Base
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
class FakeClient < Client
|
151
|
+
include Fake
|
152
|
+
|
153
|
+
def initialize(target = "http://example.com", token = nil)
|
154
|
+
@base = FakeBase.new(target, token)
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def get_many(plural)
|
160
|
+
instance_variable_get(:"@#{plural}")
|
161
|
+
end
|
162
|
+
|
163
|
+
def fake_attributes(attributes)
|
164
|
+
attributes
|
165
|
+
end
|
166
|
+
|
167
|
+
def setup_reverse_relationship(v)
|
168
|
+
if v.is_a?(Model)
|
169
|
+
v.client = self
|
170
|
+
elsif v.is_a?(Array)
|
171
|
+
v.each do |x|
|
172
|
+
setup_reverse_relationship(x)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
class FakeModel < CFoundry::V2::Model
|
180
|
+
attr_reader :diff
|
181
|
+
|
182
|
+
def self.inherited(klass)
|
183
|
+
class << klass
|
184
|
+
attr_writer :object_name
|
185
|
+
end
|
186
|
+
|
187
|
+
# There is a difference between ruby 1.8.7 and 1.8.8 in the order that
|
188
|
+
# the inherited callback gets called. In 1.8.7 the inherited callback
|
189
|
+
# is called after the block; in 1.8.8 and later it's called before.
|
190
|
+
# The upshot for us is we need a failproof way of getting the name
|
191
|
+
# to klass. So we're using a global variable to hand off the value.
|
192
|
+
# Please don't shoot us. - ESH & MMB
|
193
|
+
klass.object_name = $object_name
|
194
|
+
super
|
195
|
+
end
|
196
|
+
|
197
|
+
class << self
|
198
|
+
attr_writer :object_name
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
|
203
|
+
module ModelMagic
|
204
|
+
def self.define_client_methods(&blk)
|
205
|
+
FakeClient.module_eval(&blk)
|
206
|
+
end
|
207
|
+
|
208
|
+
def self.define_base_client_methods(&blk)
|
209
|
+
FakeBase.module_eval(&blk)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
Model.objects.each_value do |klass|
|
215
|
+
klass.to_many_relations.each do |plural, _|
|
216
|
+
Fake.define_many_association(klass, plural)
|
217
|
+
end
|
218
|
+
|
219
|
+
FakeClient.class_eval do
|
220
|
+
plural = klass.plural_object_name
|
221
|
+
|
222
|
+
attr_writer plural
|
223
|
+
Fake.define_many_association(self, plural)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cfoundry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 45
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 17
|
10
|
+
version: 0.4.17
|
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:
|
18
|
+
date: 2013-01-03 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: multipart-post
|
@@ -122,21 +122,6 @@ dependencies:
|
|
122
122
|
version: "1.0"
|
123
123
|
type: :development
|
124
124
|
version_requirements: *id007
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: factory_girl
|
127
|
-
prerelease: false
|
128
|
-
requirement: &id008 !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
|
-
requirements:
|
131
|
-
- - ~>
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
hash: 15
|
134
|
-
segments:
|
135
|
-
- 2
|
136
|
-
- 6
|
137
|
-
version: "2.6"
|
138
|
-
type: :development
|
139
|
-
version_requirements: *id008
|
140
125
|
description:
|
141
126
|
email:
|
142
127
|
- asuraci@vmware.com
|
@@ -190,21 +175,20 @@ files:
|
|
190
175
|
- spec/cfoundry/baseclient_spec.rb
|
191
176
|
- spec/cfoundry/errors_spec.rb
|
192
177
|
- spec/cfoundry/uaaclient_spec.rb
|
193
|
-
- spec/
|
194
|
-
- spec/
|
195
|
-
- spec/
|
196
|
-
- spec/
|
197
|
-
- spec/
|
198
|
-
- spec/
|
199
|
-
- spec/
|
200
|
-
- spec/
|
201
|
-
- spec/
|
202
|
-
- spec/
|
203
|
-
- spec/
|
204
|
-
- spec/
|
205
|
-
- spec/factories/space_factory.rb
|
206
|
-
- spec/factories/user_factory.rb
|
178
|
+
- spec/cfoundry/v2/model_magic_spec.rb
|
179
|
+
- spec/fakes/app_fake.rb
|
180
|
+
- spec/fakes/domain_fake.rb
|
181
|
+
- spec/fakes/framework_fake.rb
|
182
|
+
- spec/fakes/organization_fake.rb
|
183
|
+
- spec/fakes/route_fake.rb
|
184
|
+
- spec/fakes/runtime_fake.rb
|
185
|
+
- spec/fakes/service_fake.rb
|
186
|
+
- spec/fakes/service_instance_fake.rb
|
187
|
+
- spec/fakes/service_plan_fake.rb
|
188
|
+
- spec/fakes/space_fake.rb
|
189
|
+
- spec/fakes/user_fake.rb
|
207
190
|
- spec/spec_helper.rb
|
191
|
+
- spec/support/fake_helper.rb
|
208
192
|
homepage: http://cloudfoundry.com/
|
209
193
|
licenses: []
|
210
194
|
|
@@ -242,18 +226,17 @@ test_files:
|
|
242
226
|
- spec/cfoundry/baseclient_spec.rb
|
243
227
|
- spec/cfoundry/errors_spec.rb
|
244
228
|
- spec/cfoundry/uaaclient_spec.rb
|
245
|
-
- spec/
|
246
|
-
- spec/
|
247
|
-
- spec/
|
248
|
-
- spec/
|
249
|
-
- spec/
|
250
|
-
- spec/
|
251
|
-
- spec/
|
252
|
-
- spec/
|
253
|
-
- spec/
|
254
|
-
- spec/
|
255
|
-
- spec/
|
256
|
-
- spec/
|
257
|
-
- spec/factories/space_factory.rb
|
258
|
-
- spec/factories/user_factory.rb
|
229
|
+
- spec/cfoundry/v2/model_magic_spec.rb
|
230
|
+
- spec/fakes/app_fake.rb
|
231
|
+
- spec/fakes/domain_fake.rb
|
232
|
+
- spec/fakes/framework_fake.rb
|
233
|
+
- spec/fakes/organization_fake.rb
|
234
|
+
- spec/fakes/route_fake.rb
|
235
|
+
- spec/fakes/runtime_fake.rb
|
236
|
+
- spec/fakes/service_fake.rb
|
237
|
+
- spec/fakes/service_instance_fake.rb
|
238
|
+
- spec/fakes/service_plan_fake.rb
|
239
|
+
- spec/fakes/space_fake.rb
|
240
|
+
- spec/fakes/user_fake.rb
|
259
241
|
- spec/spec_helper.rb
|
242
|
+
- spec/support/fake_helper.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :app, :class => CFoundry::V2::App do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
name { FactoryGirl.generate(:random_string) }
|
5
|
-
memory 128
|
6
|
-
total_instances 0
|
7
|
-
production false
|
8
|
-
state "STOPPED"
|
9
|
-
|
10
|
-
ignore do
|
11
|
-
routes []
|
12
|
-
service_bindings []
|
13
|
-
end
|
14
|
-
|
15
|
-
initialize_with do
|
16
|
-
CFoundry::V2::App.new(nil, nil)
|
17
|
-
end
|
18
|
-
|
19
|
-
after_build do |app, evaluator|
|
20
|
-
%w{name routes service_bindings}.each do |attr|
|
21
|
-
RR.stub(app).__send__(attr) { evaluator.send(attr) }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :client, :class => CFoundry::V2::Client do
|
3
|
-
ignore do
|
4
|
-
routes []
|
5
|
-
apps []
|
6
|
-
frameworks []
|
7
|
-
runtimes []
|
8
|
-
service_instances []
|
9
|
-
spaces []
|
10
|
-
organizations []
|
11
|
-
logged_in true
|
12
|
-
end
|
13
|
-
|
14
|
-
after_build do |client, evaluator|
|
15
|
-
RR.stub(client).logged_in? { evaluator.logged_in }
|
16
|
-
RR.stub(client).routes { evaluator.routes }
|
17
|
-
RR.stub(client).apps { evaluator.apps }
|
18
|
-
RR.stub(client).frameworks { evaluator.frameworks }
|
19
|
-
RR.stub(client).runtimes { evaluator.runtimes }
|
20
|
-
RR.stub(client).service_instances { evaluator.service_instances }
|
21
|
-
RR.stub(client).spaces { evaluator.spaces }
|
22
|
-
RR.stub(client).organizations { evaluator.organizations }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/spec/factories/factory.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :organization, :class => CFoundry::V2::Organization do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
name { FactoryGirl.generate(:random_string) }
|
5
|
-
|
6
|
-
ignore do
|
7
|
-
spaces []
|
8
|
-
domains []
|
9
|
-
end
|
10
|
-
|
11
|
-
initialize_with do
|
12
|
-
CFoundry::V2::Organization.new(nil, nil)
|
13
|
-
end
|
14
|
-
|
15
|
-
after_build do |org, evaluator|
|
16
|
-
evaluator.spaces.each { |s| s.organization = org }
|
17
|
-
evaluator.domains.each { |s| s.owning_organization = org }
|
18
|
-
|
19
|
-
RR.stub(org).spaces { evaluator.spaces }
|
20
|
-
RR.stub(org).domains { evaluator.domains }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :route, :class => CFoundry::V2::Route do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
host { FactoryGirl.generate(:random_string) }
|
5
|
-
association :domain, :factory => :domain, :strategy => :build
|
6
|
-
|
7
|
-
initialize_with do
|
8
|
-
CFoundry::V2::Route.new(nil, nil)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :service_binding, :class => CFoundry::V2::ServiceBinding do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
|
5
|
-
ignore do
|
6
|
-
app nil
|
7
|
-
service_instance nil
|
8
|
-
end
|
9
|
-
|
10
|
-
initialize_with do
|
11
|
-
CFoundry::V2::ServiceBinding.new(nil, nil)
|
12
|
-
end
|
13
|
-
|
14
|
-
after_build do |app, evaluator|
|
15
|
-
%w{app service_instance}.each do |attr|
|
16
|
-
RR.stub(app).__send__(attr) { evaluator.send(attr) }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :service, :class => CFoundry::V2::Service do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
label "redis"
|
5
|
-
provider "core"
|
6
|
-
url "http://example.com"
|
7
|
-
description "small key-value store"
|
8
|
-
version "2.8"
|
9
|
-
info_url "http://cloudfoundry.com/redis"
|
10
|
-
active true
|
11
|
-
|
12
|
-
initialize_with do
|
13
|
-
CFoundry::V2::Service.new(nil, nil)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :service_instance, :class => CFoundry::V2::ServiceInstance do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
name { FactoryGirl.generate(:random_string) }
|
5
|
-
|
6
|
-
ignore do
|
7
|
-
service_bindings []
|
8
|
-
end
|
9
|
-
|
10
|
-
initialize_with do
|
11
|
-
CFoundry::V2::ServiceInstance.new(nil, nil)
|
12
|
-
end
|
13
|
-
|
14
|
-
after_build do |svc, evaluator|
|
15
|
-
%w{name service_bindings}.each do |attr|
|
16
|
-
RR.stub(svc).__send__(attr) { evaluator.send(attr) }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :space, :class => CFoundry::V2::Space do
|
3
|
-
guid { FactoryGirl.generate(:guid) }
|
4
|
-
name { FactoryGirl.generate(:random_string) }
|
5
|
-
|
6
|
-
ignore do
|
7
|
-
apps []
|
8
|
-
service_instances []
|
9
|
-
domains []
|
10
|
-
end
|
11
|
-
|
12
|
-
initialize_with do
|
13
|
-
CFoundry::V2::Space.new(nil, nil)
|
14
|
-
end
|
15
|
-
|
16
|
-
after_build do |org, evaluator|
|
17
|
-
evaluator.apps.each { |s| s.space = org }
|
18
|
-
evaluator.service_instances.each { |s| s.space = org }
|
19
|
-
|
20
|
-
RR.stub(org).apps { evaluator.apps }
|
21
|
-
RR.stub(org).service_instances { evaluator.service_instances }
|
22
|
-
RR.stub(org).domains { evaluator.domains }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|