cfoundry 0.4.8 → 0.4.9

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.
@@ -7,6 +7,8 @@ require "cfoundry/errors"
7
7
 
8
8
  module CFoundry::V1
9
9
  class Base < CFoundry::BaseClient
10
+ include BaseClientMethods
11
+
10
12
  attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
11
13
 
12
14
  def initialize(
@@ -46,45 +48,12 @@ module CFoundry::V1
46
48
  end
47
49
 
48
50
  # Users
49
- def users
50
- get("users", :accept => :json)
51
- end
52
-
53
- def create_user(payload)
54
- post(payload, "users", :content => :json)
55
- end
56
-
57
- def user(email)
58
- get("users", email, :accept => :json)
59
- end
60
-
61
- def delete_user(email)
62
- delete("users", email, :accept => :json)
63
- true
64
- end
65
-
66
- def update_user(email, payload)
67
- put(payload, "users", email, :content => :json)
68
- end
69
-
70
51
  def create_token(payload, email)
71
52
  post(payload, "users", email, "tokens",
72
53
  :content => :json, :accept => :json)
73
54
  end
74
55
 
75
56
  # Applications
76
- def apps
77
- get("apps", :accept => :json)
78
- end
79
-
80
- def create_app(payload)
81
- post(payload, "apps", :content => :json, :accept => :json)
82
- end
83
-
84
- def app(name)
85
- get("apps", name, :accept => :json)
86
- end
87
-
88
57
  def instances(name)
89
58
  get("apps", name, "instances", :accept => :json)[:instances]
90
59
  end
@@ -98,20 +67,11 @@ module CFoundry::V1
98
67
  end
99
68
  alias :file :files
100
69
 
101
- def update_app(name, payload)
102
- put(payload, "apps", name, :content => :json)
103
- end
104
-
105
- def delete_app(name)
106
- delete("apps", name)
107
- true
108
- end
109
-
110
70
  def stats(name)
111
71
  get("apps", name, "stats", :accept => :json)
112
72
  end
113
73
 
114
- def check_resources(fingerprints)
74
+ def resource_match(fingerprints)
115
75
  post(fingerprints, "resources", :content => :json, :accept => :json)
116
76
  end
117
77
 
@@ -134,24 +94,6 @@ module CFoundry::V1
134
94
  retry
135
95
  end
136
96
 
137
- # Services
138
- def services
139
- get("services", :accept => :json)
140
- end
141
-
142
- def create_service(manifest)
143
- post(manifest, "services", :content => :json, :accept => :json)
144
- end
145
-
146
- def service(name)
147
- get("services", name, :accept => :json)
148
- end
149
-
150
- def delete_service(name)
151
- delete("services", name, :accept => :json)
152
- true
153
- end
154
-
155
97
  private
156
98
 
157
99
  def handle_response(response, accept)
@@ -1,15 +1,10 @@
1
- require "cfoundry/v1/base"
2
- require "cfoundry/v1/app"
3
- require "cfoundry/v1/service"
4
- require "cfoundry/v1/service_instance"
5
- require "cfoundry/v1/user"
6
-
7
-
8
1
  module CFoundry::V1
9
2
  # The primary API entrypoint. Wraps a BaseClient to provide nicer return
10
3
  # values. Initialize with the target and, optionally, an auth token. These
11
4
  # are the only two internal states.
12
5
  class Client
6
+ include ClientMethods
7
+
13
8
  attr_reader :base
14
9
 
15
10
  # Create a new Client for interfacing with the given target.
@@ -93,7 +88,7 @@ module CFoundry::V1
93
88
  end
94
89
 
95
90
  # Retrieve available services.
96
- def services(depth = 0, query = {})
91
+ def services(options = {})
97
92
  services = []
98
93
 
99
94
  @base.system_services.each do |type, vendors|
@@ -105,7 +100,7 @@ module CFoundry::V1
105
100
 
106
101
  services <<
107
102
  Service.new(vendor.to_s, ver.to_s, meta[:description],
108
- type, provider.to_s, state && state.first)
103
+ type.to_s, provider.to_s, state && state.first)
109
104
  end
110
105
  end
111
106
  end
@@ -116,7 +111,7 @@ module CFoundry::V1
116
111
  end
117
112
 
118
113
  # Retrieve available runtimes.
119
- def runtimes(depth = 1, query = {})
114
+ def runtimes(options = {})
120
115
  runtimes = []
121
116
 
122
117
  @base.system_runtimes.each do |name, meta|
@@ -127,12 +122,16 @@ module CFoundry::V1
127
122
  runtimes
128
123
  end
129
124
 
125
+ def runtime(name)
126
+ Runtime.new(name)
127
+ end
128
+
130
129
  def runtime_by_name(name)
131
130
  runtimes.find { |r| r.name == name }
132
131
  end
133
132
 
134
133
  # Retrieve available frameworks.
135
- def frameworks(depth = 1, query = {})
134
+ def frameworks(options = {})
136
135
  fs = info[:frameworks]
137
136
  return unless fs
138
137
 
@@ -149,28 +148,12 @@ module CFoundry::V1
149
148
  frameworks
150
149
  end
151
150
 
152
- def framework_by_name(name)
153
- frameworks.find { |f| f.name == name }
151
+ def framework(name)
152
+ Framework.new(name)
154
153
  end
155
154
 
156
- # Retrieve user list. Admin-only.
157
- def users(depth = 1, query = {})
158
- @base.users.collect do |json|
159
- User.new(
160
- json[:email],
161
- self,
162
- { :email => json[:email],
163
- :admin => json[:admin] })
164
- end
165
- end
166
-
167
- # Construct a User object. The return value is lazy, and no requests are
168
- # made from this alone.
169
- #
170
- # This should be used for both user creation (after calling User#create!)
171
- # and retrieval.
172
- def user(email)
173
- User.new(email, self)
155
+ def framework_by_name(name)
156
+ frameworks.find { |f| f.name == name }
174
157
  end
175
158
 
176
159
  # Create a user on the target and return a User object representing them.
@@ -217,48 +200,5 @@ module CFoundry::V1
217
200
  def logged_in?
218
201
  !!@base.token
219
202
  end
220
-
221
-
222
- # Retreive all of the current user's applications.
223
- def apps(depth = 1, query = {})
224
- @base.apps.collect do |json|
225
- App.new(json[:name], self, json)
226
- end
227
- end
228
-
229
- # Construct an App object. The return value is lazy, and no requests are
230
- # made from this method alone.
231
- #
232
- # This should be used for both app creation (after calling App#create!)
233
- # and retrieval.
234
- def app(name = nil)
235
- App.new(name, self)
236
- end
237
-
238
- def app_by_name(name)
239
- app = app(name)
240
- app if app.exists?
241
- end
242
-
243
- # Retrieve all of the current user's services.
244
- def service_instances(depth = 1, query = {})
245
- @base.services.collect do |json|
246
- ServiceInstance.new(json[:name], self, json)
247
- end
248
- end
249
-
250
- # Construct a Service object. The return value is lazy, and no requests are
251
- # made from this method alone.
252
- #
253
- # This should be used for both service creation (after calling
254
- # Service#create!) and retrieval.
255
- def service_instance(name = nil)
256
- ServiceInstance.new(name, self)
257
- end
258
-
259
- def service_instance_by_name(name)
260
- service = service_instance(name)
261
- service if service.exists?
262
- end
263
203
  end
264
204
  end
@@ -0,0 +1,153 @@
1
+ require "multi_json"
2
+
3
+ require "cfoundry/v1/model_magic"
4
+
5
+
6
+ module CFoundry::V1
7
+ class Model
8
+ extend ModelMagic
9
+
10
+ class << self
11
+ attr_writer :object_name, :base_object_name
12
+
13
+ def object_name
14
+ @object_name ||=
15
+ name.split("::").last.gsub(
16
+ /([a-z])([A-Z])/,
17
+ '\1_\2').downcase.to_sym
18
+ end
19
+
20
+ def base_object_name
21
+ @base_object_name ||= object_name
22
+ end
23
+ end
24
+
25
+ attr_accessor :guid, :changes
26
+
27
+ def initialize(guid, client, manifest = nil)
28
+ @guid = guid
29
+ @client = client
30
+ @manifest = manifest
31
+ @changes = {}
32
+ end
33
+
34
+ def manifest
35
+ @manifest ||= @client.base.send(base_object_name, @guid)
36
+ end
37
+
38
+ def changed?
39
+ !@changes.empty?
40
+ end
41
+
42
+ def inspect
43
+ "\#<#{self.class.name} '#@guid'>"
44
+ end
45
+
46
+ def object_name
47
+ @object_name ||= self.class.object_name
48
+ end
49
+
50
+ def base_object_name
51
+ @base_object_name ||= self.class.base_object_name
52
+ end
53
+
54
+ def guid_name
55
+ self.class.guid_name
56
+ end
57
+
58
+ def invalidate!
59
+ @manifest = nil
60
+ @changes = {}
61
+ end
62
+
63
+ def create!
64
+ @manifest = @client.base.send(:"create_#{base_object_name}", write_manifest)
65
+
66
+ @guid = read_manifest[guid_name]
67
+
68
+ true
69
+ end
70
+
71
+ def update!
72
+ @client.base.send(:"update_#{base_object_name}", @guid, write_manifest)
73
+
74
+ invalidate!
75
+
76
+ true
77
+ end
78
+
79
+ def delete!
80
+ @client.base.send(:"delete_#{base_object_name}", @guid)
81
+
82
+ @guid = nil
83
+
84
+ true
85
+ end
86
+
87
+ def exists?
88
+ invalidate!
89
+ manifest
90
+ true
91
+ rescue CFoundry::NotFound
92
+ false
93
+ end
94
+
95
+ def read_manifest
96
+ read = {}
97
+
98
+ self.class.read_locations.each do |name, where|
99
+ found, val = find_path(manifest, where)
100
+ read[name] = val if found
101
+ end
102
+
103
+ read[guid_name] = @guid
104
+
105
+ read
106
+ end
107
+
108
+ def find_path(hash, path)
109
+ return [false, nil] unless hash
110
+
111
+ first, *rest = path
112
+ return [false, nil] unless hash.key?(first)
113
+
114
+ here = hash[first]
115
+
116
+ if rest.empty?
117
+ [true, here]
118
+ else
119
+ find_path(here, rest)
120
+ end
121
+ end
122
+
123
+ def write_manifest(body = read_manifest, onto = {})
124
+ onto[guid_name] = @guid
125
+
126
+ self.class.write_locations.each do |what, where|
127
+ put(body[what], onto, where) if body.key?(what)
128
+ end
129
+
130
+ onto
131
+ end
132
+
133
+ def put(what, where, path)
134
+ if path.size == 1
135
+ where[path.last] = what
136
+ elsif name = path.first
137
+ where[name] ||= {}
138
+ put(what, where[name], path[1..-1])
139
+ end
140
+
141
+ nil
142
+ end
143
+
144
+ def eql?(other)
145
+ other.is_a?(self.class) && @guid == other.guid
146
+ end
147
+ alias :== :eql?
148
+
149
+ def hash
150
+ @guid.hash
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,114 @@
1
+ require "cfoundry/validator"
2
+
3
+ module CFoundry::V1
4
+ module BaseClientMethods
5
+ end
6
+
7
+ module ClientMethods
8
+ end
9
+
10
+ module ModelMagic
11
+ attr_accessor :guid_name
12
+
13
+ def read_locations
14
+ @read_locations ||= {}
15
+ end
16
+
17
+ def write_locations
18
+ @write_locations ||= {}
19
+ end
20
+
21
+ def define_client_methods(klass = self)
22
+ singular = klass.object_name
23
+ plural = :"#{singular}s"
24
+
25
+ base_singular = klass.base_object_name
26
+ base_plural = :"#{base_singular}s"
27
+
28
+ BaseClientMethods.module_eval do
29
+ define_method(base_singular) do |guid|
30
+ get(base_plural, guid, :accept => :json)
31
+ end
32
+
33
+ define_method(:"create_#{base_singular}") do |payload|
34
+ post(payload, base_plural, :content => :json, :accept => :json)
35
+ end
36
+
37
+ define_method(:"delete_#{base_singular}") do |guid|
38
+ delete(base_plural, guid)
39
+ true
40
+ end
41
+
42
+ define_method(:"update_#{base_singular}") do |guid, payload|
43
+ put(payload, base_plural, guid, :content => :json)
44
+ end
45
+
46
+ define_method(base_plural) do |*args|
47
+ get(base_plural, :accept => :json)
48
+ end
49
+ end
50
+
51
+ ClientMethods.module_eval do
52
+ if klass.guid_name
53
+ define_method(:"#{singular}_by_#{klass.guid_name}") do |guid|
54
+ obj = send(singular, guid)
55
+ obj if obj.exists?
56
+ end
57
+ end
58
+
59
+ define_method(singular) do |*args|
60
+ guid, _ = args
61
+ klass.new(guid, self)
62
+ end
63
+
64
+ define_method(plural) do |*args|
65
+ options, _ = args
66
+ options ||= {}
67
+
68
+ @base.send(base_plural).collect do |json|
69
+ klass.new(json[klass.guid_name], self, json)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ def attribute(name, type, opts = {})
76
+ default = opts[:default]
77
+ is_guid = opts[:guid]
78
+ read_only = opts[:read_only]
79
+ write_only = opts[:write_only]
80
+ has_default = opts.key?(:default)
81
+
82
+ read_locations[name] = Array(opts[:read] || opts[:at] || name)
83
+ write_locations[name] = Array(opts[:write] || opts[:at] || name)
84
+
85
+ self.guid_name = name if is_guid
86
+
87
+ unless write_only
88
+ define_method(name) do
89
+ return @guid if @guid && is_guid
90
+
91
+ read = read_manifest
92
+ read.key?(name) ? read[name] : default
93
+ end
94
+ end
95
+
96
+ return if read_only
97
+
98
+ define_method(:"#{name}=") do |val|
99
+ unless has_default && val == default
100
+ CFoundry::Validator.validate_type(val, type)
101
+ end
102
+
103
+ @guid = val if is_guid
104
+
105
+ @manifest ||= {}
106
+
107
+ old = read_manifest[name]
108
+ @changes[name] = [old, val] if old != val
109
+
110
+ put(val, @manifest, self.class.write_locations[name])
111
+ end
112
+ end
113
+ end
114
+ end