cfoundry 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,112 +1,32 @@
1
- module CFoundry::V1
2
- # Class for representing a user's service on a given target (via Client).
3
- #
4
- # Does not guarantee that the service exists; used for both service creation
5
- # and retrieval, as the attributes are all lazily retrieved. Setting
6
- # attributes does not perform any requests; use #update! to commit your
7
- # changes.
8
- class ServiceInstance
9
- # Service name.
10
- attr_accessor :name
11
-
12
- # Service type (e.g. key-value).
13
- attr_accessor :type
14
-
15
- # Service vendor (redis, mysql, etc.).
16
- attr_accessor :vendor
17
-
18
- # Service version.
19
- attr_accessor :version
20
-
21
- # Service properties.
22
- attr_accessor :properties
23
-
24
- # Service tier. Usually "free" for now.
25
- attr_accessor :tier
26
-
27
- # Service metadata.
28
- attr_accessor :meta
1
+ require "cfoundry/v1/model"
29
2
 
30
- # Create a Service object.
31
- #
32
- # You'll usually call Client#service instead.
33
- def initialize(name, client, manifest = nil)
34
- @name = name
35
- @client = client
36
- @manifest = manifest
37
- end
38
-
39
- # Show string representing the service.
40
- def inspect
41
- "#<ServiceInstance '#@name'>"
42
- end
43
-
44
- # Basic equality test by name.
45
- def eql?(other)
46
- other.is_a?(self.class) && other.name == @name
47
- end
48
- alias :== :eql?
3
+ module CFoundry::V1
4
+ class ServiceInstance < Model
5
+ self.base_object_name = :service
49
6
 
50
- # Delete the service from the target.
51
- def delete!
52
- @client.base.delete_service(@name)
53
- end
7
+ attribute :name, :string, :guid => true
8
+ attribute :created, :integer, :at => [:meta, :created]
9
+ attribute :updated, :integer, :at => [:meta, :updated]
10
+ attribute :tags, [:string], :at => [:meta, :tags]
11
+ attribute :type, :string
12
+ attribute :vendor, :string
13
+ attribute :version, :string
14
+ attribute :tier, :string
15
+ attribute :properties, :hash
54
16
 
55
- # Create the service on the target.
56
- #
57
- # Call this after setting the various attributes.
58
- def create!
59
- @client.base.create_service(@manifest.merge(:name => @name))
60
- end
17
+ define_client_methods
61
18
 
62
- # Check if the service exists on the target.
63
- def exists?
64
- @client.base.service(@name)
65
- true
66
- rescue CFoundry::ServiceNotFound
67
- false
68
- end
19
+ alias_method :created_unix, :created
20
+ alias_method :updated_unix, :updated
69
21
 
70
22
  # Timestamp of when the service was created.
71
23
  def created
72
- Time.at(meta[:created])
24
+ Time.at(created_unix)
73
25
  end
74
26
 
75
27
  # Timestamp of when the service was last updated.
76
28
  def updated
77
- Time.at(meta[:updated])
78
- end
79
-
80
- def invalidate!
81
- @manifest = nil
82
- end
83
-
84
- def eql?(other)
85
- other.is_a?(self.class) && @name == other.name
86
- end
87
- alias :== :eql?
88
-
89
- { :type => :type,
90
- :vendor => :vendor,
91
- :version => :version,
92
- :properties => :properties,
93
- :tier => :tier,
94
- :meta => :meta
95
- }.each do |meth, attr|
96
- define_method(meth) do
97
- manifest[attr]
98
- end
99
-
100
- define_method(:"#{meth}=") do |v|
101
- @manifest ||= {}
102
- @manifest[attr] = v
103
- end
104
- end
105
-
106
- private
107
-
108
- def manifest
109
- @manifest ||= @client.base.service(@name)
29
+ Time.at(updated_unix)
110
30
  end
111
31
  end
112
32
  end
@@ -1,79 +1,14 @@
1
- module CFoundry::V1
2
- # Class for representing a user on a given target (via Client).
3
- #
4
- # Does not guarantee that the user exists; used for both user creation and
5
- # retrieval, as the attributes are all lazily retrieved. Setting attributes
6
- # does not perform any requests; use #update! to commit your changes.
7
- class User
8
- # User email.
9
- attr_reader :email
10
-
11
-
12
- # Create a User object.
13
- #
14
- # You'll usually call Client#user instead
15
- def initialize(email, client, manifest = nil)
16
- @email = email
17
- @client = client
18
- @manifest = manifest
19
- end
1
+ require "cfoundry/v1/model"
20
2
 
21
- # Show string representing the user.
22
- def inspect
23
- "#<User '#@email'>"
24
- end
25
-
26
- # Basic equality test by email.
27
- def eql?(other)
28
- other.is_a?(self.class) && other.email == @email
29
- end
30
- alias :== :eql?
31
-
32
- # Delete the user from the target.
33
- def delete!
34
- @client.base.delete_user(@email)
35
- end
36
-
37
- # Create the user on the target.
38
- #
39
- # Call this after setting the various attributes.
40
- def create!
41
- @client.base.create_user(@manifest.merge(:email => @email))
42
- end
43
-
44
- # Update user attributes.
45
- def update!(what = {})
46
- @client.base.update_user(@email, manifest.merge(what))
47
- @manifest = nil
48
- end
49
-
50
- # Check if the user exists on the target.
51
- def exists?
52
- @client.base.user(@email)
53
- true
54
- rescue CFoundry::Denied
55
- false
56
- end
57
-
58
- def invalidate!
59
- @manifest = nil
60
- end
61
-
62
- # Check if the user is an administrator.
63
- def admin?
64
- manifest[:admin]
65
- end
3
+ module CFoundry::V1
4
+ class User < Model
5
+ attribute :email, :string, :guid => true
6
+ attribute :password, :string, :write_only => true
7
+ attribute :admin, :boolean
66
8
 
67
- # Set the user's password.
68
- #
69
- # Call #update! after using this.
70
- def password=(str)
71
- manifest[:password] = str
72
- end
9
+ define_client_methods
73
10
 
74
- def guid
75
- @guid ||= @client.base.token_data[:user_id]
76
- end
11
+ alias_method :admin?, :admin
77
12
 
78
13
  def change_password!(new, old)
79
14
  if @client.base.uaa
@@ -83,11 +18,5 @@ module CFoundry::V1
83
18
  update!
84
19
  end
85
20
  end
86
-
87
- private
88
-
89
- def manifest
90
- @manifest ||= @client.base.user(@email)
91
- end
92
21
  end
93
22
  end
@@ -287,44 +287,6 @@ module CFoundry::V2
287
287
  }
288
288
  end
289
289
 
290
- # Upload application's code to target. Do this after #create! and before
291
- # #start!
292
- #
293
- # [path]
294
- # A path pointing to either a directory, or a .jar, .war, or .zip
295
- # file.
296
- #
297
- # If a .vmcignore file is detected under the given path, it will be used
298
- # to exclude paths from the payload, similar to a .gitignore.
299
- #
300
- # [check_resources]
301
- # If set to `false`, the entire payload will be uploaded
302
- # without checking the resource cache.
303
- #
304
- # Only do this if you know what you're doing.
305
- def upload(path, check_resources = true)
306
- unless File.exist? path
307
- raise CFoundry::Error, "Invalid application path '#{path}'"
308
- end
309
-
310
- zipfile = "#{Dir.tmpdir}/#{@guid}.zip"
311
- tmpdir = "#{Dir.tmpdir}/.vmc_#{@guid}_files"
312
-
313
- FileUtils.rm_f(zipfile)
314
- FileUtils.rm_rf(tmpdir)
315
-
316
- prepare_package(path, tmpdir)
317
-
318
- resources = determine_resources(tmpdir) if check_resources
319
-
320
- packed = CFoundry::Zip.pack(tmpdir, zipfile)
321
-
322
- @client.base.upload_app(@guid, packed && zipfile, resources || [])
323
- ensure
324
- FileUtils.rm_f(zipfile) if zipfile
325
- FileUtils.rm_rf(tmpdir) if tmpdir
326
- end
327
-
328
290
  def files(*path)
329
291
  Instance.new(self, "0", @client).files(*path)
330
292
  end
@@ -401,25 +363,5 @@ module CFoundry::V2
401
363
  @client.base.stream_file(@app.guid, @id, *path, &blk)
402
364
  end
403
365
  end
404
-
405
- private
406
-
407
- # Minimum size for an application payload to bother checking resources.
408
- RESOURCE_CHECK_LIMIT = 64 * 1024
409
-
410
- def determine_resources(path)
411
- fingerprints, total_size = make_fingerprints(path)
412
-
413
- return if total_size <= RESOURCE_CHECK_LIMIT
414
-
415
- resources = @client.base.resource_match(fingerprints)
416
-
417
- resources.each do |resource|
418
- FileUtils.rm_f resource[:fn]
419
- resource[:fn].sub!("#{path}/", "")
420
- end
421
-
422
- resources
423
- end
424
366
  end
425
367
  end
@@ -110,7 +110,8 @@ module CFoundry::V2
110
110
  end
111
111
 
112
112
  def exists?
113
- @client.base.send(object_name, @guid)
113
+ invalidate!
114
+ manifest
114
115
  true
115
116
  rescue CFoundry::NotFound
116
117
  false
@@ -1,3 +1,5 @@
1
+ require "cfoundry/validator"
2
+
1
3
  module CFoundry::V2
2
4
  # object name -> module containing query methods
3
5
  #
@@ -150,7 +152,7 @@ module CFoundry::V2
150
152
 
151
153
  define_method(:"#{name}=") do |val|
152
154
  unless has_default && val == default
153
- ModelMagic.validate_type(val, type)
155
+ CFoundry::Validator.validate_type(val, type)
154
156
  end
155
157
 
156
158
  @cache[name] = val
@@ -209,7 +211,7 @@ module CFoundry::V2
209
211
  klass = CFoundry::V2.const_get(kls)
210
212
 
211
213
  unless has_default && val == default
212
- ModelMagic.validate_type(val, klass)
214
+ CFoundry::Validator.validate_type(val, klass)
213
215
  end
214
216
 
215
217
  @manifest ||= {}
@@ -284,7 +286,7 @@ module CFoundry::V2
284
286
  end
285
287
 
286
288
  define_method(:"add_#{singular}") do |x|
287
- ModelMagic.validate_type(x, CFoundry::V2.const_get(kls))
289
+ CFoundry::Validator.validate_type(x, CFoundry::V2.const_get(kls))
288
290
 
289
291
  if cache = @cache[plural]
290
292
  cache << x unless cache.include?(x)
@@ -297,7 +299,7 @@ module CFoundry::V2
297
299
  end
298
300
 
299
301
  define_method(:"remove_#{singular}") do |x|
300
- ModelMagic.validate_type(x, CFoundry::V2.const_get(kls))
302
+ CFoundry::Validator.validate_type(x, CFoundry::V2.const_get(kls))
301
303
 
302
304
  if cache = @cache[plural]
303
305
  cache.delete(x)
@@ -312,7 +314,7 @@ module CFoundry::V2
312
314
  define_method(:"#{plural}=") do |xs|
313
315
  klass = CFoundry::V2.const_get(kls)
314
316
 
315
- ModelMagic.validate_type(xs, [klass])
317
+ CFoundry::Validator.validate_type(xs, [klass])
316
318
 
317
319
  @manifest ||= {}
318
320
  @manifest[:entity] ||= {}
@@ -411,56 +413,22 @@ module CFoundry::V2
411
413
  end
412
414
  end
413
415
 
414
- class << self
415
- def value_matches?(val, type)
416
- case type
417
- when Class
418
- val.is_a?(type)
419
- when Regexp
420
- val.is_a?(String) && val =~ type
421
- when :url
422
- value_matches?(val, URI::regexp(%w(http https)))
423
- when :https_url
424
- value_matches?(val, URI::regexp("https"))
425
- when :boolean
426
- val.is_a?(TrueClass) || val.is_a?(FalseClass)
427
- when Array
428
- val.all? do |x|
429
- value_matches?(x, type.first)
430
- end
431
- when Hash
432
- val.is_a?(Hash) &&
433
- type.all? { |name, subtype|
434
- val.key?(name) && value_matches?(val[name], subtype)
435
- }
436
- else
437
- val.is_a?(Object.const_get(type.to_s.capitalize))
438
- end
439
- end
440
-
441
- def validate_type(val, type)
442
- unless value_matches?(val, type)
443
- raise CFoundry::Mismatch.new(type, val)
416
+ def self.params_from(args)
417
+ options, _ = args
418
+ options ||= {}
419
+ options[:depth] ||= 1
420
+
421
+ params = {}
422
+ options.each do |k, v|
423
+ case k
424
+ when :depth
425
+ params[:"inline-relations-depth"] = v
426
+ when :query
427
+ params[:q] = v.join(":")
444
428
  end
445
429
  end
446
430
 
447
- def params_from(args)
448
- options, _ = args
449
- options ||= {}
450
- options[:depth] ||= 1
451
-
452
- params = {}
453
- options.each do |k, v|
454
- case k
455
- when :depth
456
- params[:"inline-relations-depth"] = v
457
- when :query
458
- params[:q] = v.join(":")
459
- end
460
- end
461
-
462
- params
463
- end
431
+ params
464
432
  end
465
433
  end
466
434
  end
@@ -0,0 +1,37 @@
1
+ module CFoundry
2
+ module Validator
3
+ class << self
4
+ def value_matches?(val, type)
5
+ case type
6
+ when Class
7
+ val.is_a?(type)
8
+ when Regexp
9
+ val.is_a?(String) && val =~ type
10
+ when :url
11
+ value_matches?(val, URI::regexp(%w(http https)))
12
+ when :https_url
13
+ value_matches?(val, URI::regexp("https"))
14
+ when :boolean
15
+ val.is_a?(TrueClass) || val.is_a?(FalseClass)
16
+ when Array
17
+ val.all? do |x|
18
+ value_matches?(x, type.first)
19
+ end
20
+ when Hash
21
+ val.is_a?(Hash) &&
22
+ type.all? { |name, subtype|
23
+ val.key?(name) && value_matches?(val[name], subtype)
24
+ }
25
+ else
26
+ val.is_a?(Object.const_get(type.to_s.capitalize))
27
+ end
28
+ end
29
+
30
+ def validate_type(val, type)
31
+ unless value_matches?(val, type)
32
+ raise CFoundry::Mismatch.new(type, val)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end