cfoundry 0.2.2 → 0.3.0

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.
@@ -0,0 +1,205 @@
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
+ module CFoundry::V1
9
+ # The primary API entrypoint. Wraps a BaseClient to provide nicer return
10
+ # values. Initialize with the target and, optionally, an auth token. These
11
+ # are the only two internal states.
12
+ class Client
13
+ attr_reader :base
14
+
15
+ # Create a new Client for interfacing with the given target.
16
+ #
17
+ # A token may also be provided to skip the login step.
18
+ def initialize(target = "http://api.cloudfoundry.com", token = nil)
19
+ @base = Base.new(target, token)
20
+ end
21
+
22
+ # The current target URL of the client.
23
+ def target
24
+ @base.target
25
+ end
26
+
27
+ # Current proxy user. Usually nil.
28
+ def proxy
29
+ @base.proxy
30
+ end
31
+
32
+ # Set the proxy user for the client. Must be authorized as an
33
+ # administrator for this to have any effect.
34
+ def proxy=(email)
35
+ @base.proxy = email
36
+ end
37
+
38
+ # Is the client tracing API requests?
39
+ def trace
40
+ @base.trace
41
+ end
42
+
43
+ # Set the tracing flag; if true, API requests and responses will be
44
+ # printed out.
45
+ def trace=(bool)
46
+ @base.trace = bool
47
+ end
48
+
49
+ # The currently authenticated user.
50
+ def current_user
51
+ if user = info[:user]
52
+ user(user)
53
+ end
54
+ end
55
+
56
+
57
+ # Retrieve target metadata.
58
+ def info
59
+ @base.info
60
+ end
61
+
62
+ # Retrieve available services.
63
+ def services
64
+ services = []
65
+
66
+ @base.system_services.each do |type, vendors|
67
+ vendors.each do |vendor, versions|
68
+ versions.each do |num, meta|
69
+ services << Service.new(vendor, meta[:description], num)
70
+ end
71
+ end
72
+ end
73
+
74
+ services
75
+ end
76
+
77
+ # Retrieve available runtimes.
78
+ def runtimes
79
+ runtimes = []
80
+
81
+ @base.system_runtimes.each do |name, meta|
82
+ runtimes << Runtime.new(name.to_s, meta[:version])
83
+ end
84
+
85
+ runtimes
86
+ end
87
+
88
+ # Retrieve available frameworks.
89
+ def frameworks
90
+ fs = info[:frameworks]
91
+ return unless fs
92
+
93
+ frameworks = []
94
+ fs.each do |name, meta|
95
+ runtimes = meta[:runtimes].collect do |r|
96
+ Runtime.new(r[:name], r[:description])
97
+ end
98
+
99
+ frameworks <<
100
+ Framework.new(name.to_s, nil, runtimes, meta[:detection])
101
+ end
102
+ frameworks
103
+ end
104
+
105
+
106
+ # Retrieve user list. Admin-only.
107
+ def users
108
+ @base.users.collect do |json|
109
+ User.new(
110
+ json[:email],
111
+ self,
112
+ { :email => json[:email],
113
+ :admin => json[:admin] })
114
+ end
115
+ end
116
+
117
+ # Construct a User object. The return value is lazy, and no requests are
118
+ # made from this alone.
119
+ #
120
+ # This should be used for both user creation (after calling User#create!)
121
+ # and retrieval.
122
+ def user(email)
123
+ User.new(email, self)
124
+ end
125
+
126
+ # Create a user on the target and return a User object representing them.
127
+ def register(email, password)
128
+ @base.create_user(:email => email, :password => password)
129
+ user(email)
130
+ end
131
+
132
+ # Login prompts
133
+ def login_prompts
134
+ if @base.uaa
135
+ @base.uaa.prompts
136
+ else
137
+ { :username => ["text", "Email"],
138
+ :password => ["password", "Password"]
139
+ }
140
+ end
141
+ end
142
+
143
+ # Authenticate with the target. Sets the client token.
144
+ #
145
+ # Credentials is a hash, typically containing :username and :password
146
+ # keys.
147
+ #
148
+ # The values in the hash should mirror the prompts given by
149
+ # `login_prompts`.
150
+ def login(credentials)
151
+ @base.token =
152
+ if @base.uaa
153
+ @base.uaa.authorize(credentials)
154
+ else
155
+ @base.create_token(
156
+ { :password => credentials[:password] },
157
+ credentials[:username])[:token]
158
+ end
159
+ end
160
+
161
+ # Clear client token. No requests are made for this.
162
+ def logout
163
+ @base.token = nil
164
+ end
165
+
166
+ # Is an authentication token set on the client?
167
+ def logged_in?
168
+ !!@base.token
169
+ end
170
+
171
+
172
+ # Retreive all of the current user's applications.
173
+ def apps
174
+ @base.apps.collect do |json|
175
+ App.new(json[:name], self, json)
176
+ end
177
+ end
178
+
179
+ # Construct an App object. The return value is lazy, and no requests are
180
+ # made from this method alone.
181
+ #
182
+ # This should be used for both app creation (after calling App#create!)
183
+ # and retrieval.
184
+ def app(name = nil)
185
+ App.new(name, self)
186
+ end
187
+
188
+
189
+ # Retrieve all of the current user's services.
190
+ def service_instances
191
+ @base.services.collect do |json|
192
+ Service.new(json[:name], self, json)
193
+ end
194
+ end
195
+
196
+ # Construct a Service object. The return value is lazy, and no requests are
197
+ # made from this method alone.
198
+ #
199
+ # This should be used for both service creation (after calling
200
+ # Service#create!) and retrieval.
201
+ def service_instance(name = nil)
202
+ ServiceInstance.new(name, self)
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,16 @@
1
+ module CFoundry::V1
2
+ class Framework
3
+ attr_accessor :name, :description, :runtimes, :detection
4
+
5
+ def initialize(name, description = nil, runtimes = [], detection = nil)
6
+ @name = name
7
+ @description = description
8
+ @runtimes = runtimes
9
+ @detection = detection
10
+ end
11
+
12
+ def apps
13
+ [] # not supported by v1
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module CFoundry::V1
2
+ class Runtime
3
+ attr_accessor :name, :description
4
+
5
+ def initialize(name, description = nil)
6
+ @name = name
7
+ @description = description
8
+ end
9
+
10
+ def apps
11
+ [] # not supported by v1
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module CFoundry::V1
2
+ class Service
3
+ attr_accessor :label, :description, :version
4
+
5
+ def initialize(label, description = nil, version = nil)
6
+ @label = label
7
+ @description = description
8
+ @version = version
9
+ end
10
+
11
+ def provider
12
+ "core"
13
+ end
14
+
15
+ def active
16
+ true
17
+ end
18
+ end
19
+ end
@@ -1,11 +1,11 @@
1
- module CFoundry
1
+ module CFoundry::V1
2
2
  # Class for representing a user's service on a given target (via Client).
3
3
  #
4
- # Goes not guarantee that the service exists; used for both service creation
4
+ # Does not guarantee that the service exists; used for both service creation
5
5
  # and retrieval, as the attributes are all lazily retrieved. Setting
6
6
  # attributes does not perform any requests; use #update! to commit your
7
7
  # changes.
8
- class Service
8
+ class ServiceInstance
9
9
  # Service name.
10
10
  attr_reader :name
11
11
 
@@ -43,19 +43,19 @@ module CFoundry
43
43
 
44
44
  # Delete the service from the target.
45
45
  def delete!
46
- @client.rest.delete_service(@name)
46
+ @client.base.delete_service(@name)
47
47
  end
48
48
 
49
49
  # Create the service on the target.
50
50
  #
51
51
  # Call this after setting the various attributes.
52
52
  def create!
53
- @client.rest.create_service(@manifest.merge("name" => @name))
53
+ @client.base.create_service(@manifest.merge(:name => @name))
54
54
  end
55
55
 
56
56
  # Check if the service exists on the target.
57
57
  def exists?
58
- @client.rest.service(@name)
58
+ @client.base.service(@name)
59
59
  true
60
60
  rescue CFoundry::NotFound
61
61
  false
@@ -63,20 +63,20 @@ module CFoundry
63
63
 
64
64
  # Timestamp of when the service was created.
65
65
  def created
66
- Time.at(meta["created"])
66
+ Time.at(meta[:created])
67
67
  end
68
68
 
69
69
  # Timestamp of when the service was last updated.
70
70
  def updated
71
- Time.at(meta["updated"])
71
+ Time.at(meta[:updated])
72
72
  end
73
73
 
74
- { :type => "type",
75
- :vendor => "vendor",
76
- :version => "version",
77
- :properties => "properties",
78
- :tier => "tier",
79
- :meta => "meta"
74
+ { :type => :type,
75
+ :vendor => :vendor,
76
+ :version => :version,
77
+ :properties => :properties,
78
+ :tier => :tier,
79
+ :meta => :meta
80
80
  }.each do |meth, attr|
81
81
  define_method(meth) do
82
82
  manifest[attr]
@@ -91,7 +91,7 @@ module CFoundry
91
91
  private
92
92
 
93
93
  def manifest
94
- @manifest ||= @client.rest.service(@name)
94
+ @manifest ||= @client.base.service(@name)
95
95
  end
96
96
  end
97
97
  end
@@ -1,7 +1,7 @@
1
- module CFoundry
1
+ module CFoundry::V1
2
2
  # Class for representing a user on a given target (via Client).
3
3
  #
4
- # Goes not guarantee that the user exists; used for both user creation and
4
+ # Does not guarantee that the user exists; used for both user creation and
5
5
  # retrieval, as the attributes are all lazily retrieved. Setting attributes
6
6
  # does not perform any requests; use #update! to commit your changes.
7
7
  class User
@@ -25,25 +25,25 @@ module CFoundry
25
25
 
26
26
  # Delete the user from the target.
27
27
  def delete!
28
- @client.rest.delete_user(@email)
28
+ @client.base.delete_user(@email)
29
29
  end
30
30
 
31
31
  # Create the user on the target.
32
32
  #
33
33
  # Call this after setting the various attributes.
34
34
  def create!
35
- @client.rest.create_user(@manifest.merge("email" => @email))
35
+ @client.base.create_user(@manifest.merge(:email => @email))
36
36
  end
37
37
 
38
38
  # Update user attributes.
39
39
  def update!(what = {})
40
- @client.rest.update_user(@email, manifest.merge(what))
40
+ @client.base.update_user(@email, manifest.merge(what))
41
41
  @manifest = nil
42
42
  end
43
43
 
44
44
  # Check if the user exists on the target.
45
45
  def exists?
46
- @client.rest.user(@email)
46
+ @client.base.user(@email)
47
47
  true
48
48
  rescue CFoundry::Denied
49
49
  false
@@ -51,20 +51,20 @@ module CFoundry
51
51
 
52
52
  # Check if the user is an administrator.
53
53
  def admin?
54
- manifest["admin"]
54
+ manifest[:admin]
55
55
  end
56
56
 
57
57
  # Set the user's password.
58
58
  #
59
59
  # Call #update! after using this.
60
60
  def password=(str)
61
- manifest["password"] = str
61
+ manifest[:password] = str
62
62
  end
63
63
 
64
64
  private
65
65
 
66
66
  def manifest
67
- @manifest ||= @client.rest.user(@email)
67
+ @manifest ||= @client.base.user(@email)
68
68
  end
69
69
  end
70
70
  end
@@ -0,0 +1,119 @@
1
+ require "fileutils"
2
+ require "digest/sha1"
3
+ require "pathname"
4
+ require "tmpdir"
5
+
6
+ require "cfoundry/zip"
7
+ require "cfoundry/v2/model"
8
+
9
+ module CFoundry::V2
10
+ # Class for representing a user's application on a given target (via
11
+ # Client).
12
+ #
13
+ # Does not guarantee that the app exists; used for both app creation and
14
+ # retrieval, as the attributes are all lazily retrieved. Setting attributes
15
+ # does not perform any requests; use #update! to commit your changes.
16
+ class App < Model
17
+ attribute :name
18
+ attribute :production
19
+ to_one :app_space
20
+ to_one :runtime
21
+ to_one :framework
22
+ attribute :environment_json, :default => {}
23
+ attribute :memory, :default => 256
24
+ attribute :instances, :default => 1
25
+ attribute :file_descriptors, :default => 256
26
+ attribute :disk_quota, :default => 256
27
+ attribute :state, :default => "STOPPED"
28
+ to_many :service_bindings
29
+
30
+ alias :total_instances :instances
31
+ alias :total_instances= :instances=
32
+
33
+ alias :services :service_bindings
34
+ alias :services= :service_bindings=
35
+
36
+ alias :space :app_space
37
+ alias :space= :app_space=
38
+
39
+ def debug_mode # TODO v2
40
+ nil
41
+ end
42
+ alias :console :debug_mode
43
+
44
+ def uris # TODO v2
45
+ []
46
+ end
47
+ alias :urls :uris
48
+
49
+ def uris=(x)
50
+ nil
51
+ end
52
+ alias :urls= :uris=
53
+
54
+ def uri
55
+ uris[0]
56
+ end
57
+ alias :url :uri
58
+
59
+ # Stop the application.
60
+ def stop!
61
+ update! :state => "STOPPED"
62
+ end
63
+
64
+ # Start the application.
65
+ def start!
66
+ update! :state => "STARTED"
67
+ end
68
+
69
+ # Restart the application.
70
+ def restart!
71
+ stop!
72
+ start!
73
+ end
74
+
75
+ # Determine application health.
76
+ #
77
+ # If all instances are running, returns "RUNNING". If only some are
78
+ # started, returns the precentage of them that are healthy.
79
+ #
80
+ # Otherwise, returns application's status.
81
+ def health
82
+ state
83
+ end
84
+
85
+ # Check that all application instances are running.
86
+ def healthy?
87
+ # invalidate cache so the check is fresh
88
+ @manifest = nil
89
+ health == "RUNNING"
90
+ end
91
+ alias_method :running?, :healthy?
92
+
93
+ # Is the application stopped?
94
+ def stopped?
95
+ state == "STOPPED"
96
+ end
97
+
98
+ # Is the application started?
99
+ #
100
+ # Note that this does not imply that all instances are running. See
101
+ # #healthy?
102
+ def started?
103
+ state == "STARTED"
104
+ end
105
+
106
+ # Bind services to application.
107
+ def bind(*service_names)
108
+ update!(:services => services + service_names)
109
+ end
110
+
111
+ # Unbind services from application.
112
+ def unbind(*service_names)
113
+ update!(:services =>
114
+ services.reject { |s|
115
+ service_names.include?(s)
116
+ })
117
+ end
118
+ end
119
+ end