Class: CFoundry::V1::Client
- Inherits:
-
Object
- Object
- CFoundry::V1::Client
- Defined in:
- lib/cfoundry/v1/client.rb
Overview
The primary API entrypoint. Wraps a BaseClient to provide nicer return values. Initialize with the target and, optionally, an auth token. These are the only two internal states.
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
-
#app(name = nil) ⇒ Object
Construct an App object.
- #app_by_name(name) ⇒ Object
-
#apps(depth = 1, query = {}) ⇒ Object
Retreive all of the current user’s applications.
- #current_organization ⇒ Object
- #current_space ⇒ Object
-
#current_user ⇒ Object
The currently authenticated user.
- #framework_by_name(name) ⇒ Object
-
#frameworks ⇒ Object
Retrieve available frameworks.
-
#info ⇒ Object
Retrieve target metadata.
-
#initialize(target = "http://api.cloudfoundry.com", token = nil) ⇒ Client
constructor
Create a new Client for interfacing with the given target.
-
#log ⇒ Object
The current log.
-
#log=(mode) ⇒ Object
Set the logging mode.
-
#logged_in? ⇒ Boolean
Is an authentication token set on the client?.
-
#login(credentials) ⇒ Object
Authenticate with the target.
-
#login_prompts ⇒ Object
Login prompts.
-
#logout ⇒ Object
Clear client token.
-
#proxy ⇒ Object
Current proxy user.
-
#proxy=(email) ⇒ Object
Set the proxy user for the client.
-
#register(email, password) ⇒ Object
Create a user on the target and return a User object representing them.
- #runtime_by_name(name) ⇒ Object
-
#runtimes ⇒ Object
Retrieve available runtimes.
-
#service_instance(name = nil) ⇒ Object
Construct a Service object.
- #service_instance_by_name(name) ⇒ Object
-
#service_instances(depth = 1, query = {}) ⇒ Object
Retrieve all of the current user’s services.
-
#services ⇒ Object
Retrieve available services.
-
#target ⇒ Object
The current target URL of the client.
-
#token ⇒ Object
Current authentication token.
-
#token=(token) ⇒ Object
Set the authentication token.
-
#trace ⇒ Object
Is the client tracing API requests?.
-
#trace=(bool) ⇒ Object
Set the tracing flag; if true, API requests and responses will be printed out.
-
#user(email) ⇒ Object
Construct a User object.
-
#users ⇒ Object
Retrieve user list.
Constructor Details
#initialize(target = "http://api.cloudfoundry.com", token = nil) ⇒ Client
Create a new Client for interfacing with the given target.
A token may also be provided to skip the login step.
18 19 20 |
# File 'lib/cfoundry/v1/client.rb', line 18 def initialize(target = "http://api.cloudfoundry.com", token = nil) @base = Base.new(target, token) end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
13 14 15 |
# File 'lib/cfoundry/v1/client.rb', line 13 def base @base end |
Instance Method Details
#app(name = nil) ⇒ Object
Construct an App object. The return value is lazy, and no requests are made from this method alone.
This should be used for both app creation (after calling App#create!) and retrieval.
232 233 234 |
# File 'lib/cfoundry/v1/client.rb', line 232 def app(name = nil) App.new(name, self) end |
#app_by_name(name) ⇒ Object
236 237 238 239 |
# File 'lib/cfoundry/v1/client.rb', line 236 def app_by_name(name) app = app(name) app if app.exists? end |
#apps(depth = 1, query = {}) ⇒ Object
Retreive all of the current user’s applications.
221 222 223 224 225 |
# File 'lib/cfoundry/v1/client.rb', line 221 def apps(depth = 1, query = {}) @base.apps.collect do |json| App.new(json[:name], self, json) end end |
#current_organization ⇒ Object
85 86 87 |
# File 'lib/cfoundry/v1/client.rb', line 85 def current_organization nil end |
#current_space ⇒ Object
81 82 83 |
# File 'lib/cfoundry/v1/client.rb', line 81 def current_space nil end |
#current_user ⇒ Object
The currently authenticated user.
75 76 77 78 79 |
# File 'lib/cfoundry/v1/client.rb', line 75 def current_user if user = info[:user] user(user) end end |
#framework_by_name(name) ⇒ Object
150 151 152 |
# File 'lib/cfoundry/v1/client.rb', line 150 def framework_by_name(name) frameworks.find { |f| f.name == name } end |
#frameworks ⇒ Object
Retrieve available frameworks.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cfoundry/v1/client.rb', line 132 def frameworks fs = info[:frameworks] return unless fs frameworks = [] fs.each do |name, | runtimes = [:runtimes].collect do |r| Runtime.new(r[:name], r[:description], nil, [:version], [:status], [:series], [:category]) end frameworks << Framework.new(name.to_s, nil, runtimes, [:detection]) end frameworks end |
#info ⇒ Object
Retrieve target metadata.
91 92 93 |
# File 'lib/cfoundry/v1/client.rb', line 91 def info @base.info end |
#log ⇒ Object
The current log. See log=
.
60 61 62 |
# File 'lib/cfoundry/v1/client.rb', line 60 def log @base.log end |
#log=(mode) ⇒ Object
Set the logging mode. Mode can be one of:
String
-
Name of a file to log the last 10 requests to.
Array
-
Array to append with log data (a Hash).
IO
-
An IO object to write to.
false
-
No logging.
70 71 72 |
# File 'lib/cfoundry/v1/client.rb', line 70 def log=(mode) @base.log = mode end |
#logged_in? ⇒ Boolean
Is an authentication token set on the client?
215 216 217 |
# File 'lib/cfoundry/v1/client.rb', line 215 def logged_in? !!@base.token end |
#login(credentials) ⇒ Object
Authenticate with the target. Sets the client token.
Credentials is a hash, typically containing :username and :password keys.
The values in the hash should mirror the prompts given by ‘login_prompts`.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/cfoundry/v1/client.rb', line 198 def login(credentials) @base.token = if @base.uaa @base.uaa.(credentials) else @base.create_token( { :password => credentials[:password] }, credentials[:username])[:token] end end |
#login_prompts ⇒ Object
Login prompts
181 182 183 184 185 186 187 188 189 |
# File 'lib/cfoundry/v1/client.rb', line 181 def login_prompts if @base.uaa @base.uaa.prompts else { :username => ["text", "Email"], :password => ["password", "Password"] } end end |
#logout ⇒ Object
Clear client token. No requests are made for this.
210 211 212 |
# File 'lib/cfoundry/v1/client.rb', line 210 def logout @base.token = nil end |
#proxy ⇒ Object
Current proxy user. Usually nil.
38 39 40 |
# File 'lib/cfoundry/v1/client.rb', line 38 def proxy @base.proxy end |
#proxy=(email) ⇒ Object
Set the proxy user for the client. Must be authorized as an administrator for this to have any effect.
44 45 46 |
# File 'lib/cfoundry/v1/client.rb', line 44 def proxy=(email) @base.proxy = email end |
#register(email, password) ⇒ Object
Create a user on the target and return a User object representing them.
175 176 177 178 |
# File 'lib/cfoundry/v1/client.rb', line 175 def register(email, password) @base.create_user(:email => email, :password => password) user(email) end |
#runtime_by_name(name) ⇒ Object
127 128 129 |
# File 'lib/cfoundry/v1/client.rb', line 127 def runtime_by_name(name) runtimes.find { |r| r.name == name } end |
#runtimes ⇒ Object
Retrieve available runtimes.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cfoundry/v1/client.rb', line 115 def runtimes runtimes = [] @base.system_runtimes.each do |name, | runtimes << Runtime.new(name.to_s, [:description], [:debug_modes], [:version], [:status], [:series], [:category]) end runtimes end |
#service_instance(name = nil) ⇒ Object
Construct a Service object. The return value is lazy, and no requests are made from this method alone.
This should be used for both service creation (after calling Service#create!) and retrieval.
253 254 255 |
# File 'lib/cfoundry/v1/client.rb', line 253 def service_instance(name = nil) ServiceInstance.new(name, self) end |
#service_instance_by_name(name) ⇒ Object
257 258 259 260 |
# File 'lib/cfoundry/v1/client.rb', line 257 def service_instance_by_name(name) service = service_instance(name) service if service.exists? end |
#service_instances(depth = 1, query = {}) ⇒ Object
Retrieve all of the current user’s services.
242 243 244 245 246 |
# File 'lib/cfoundry/v1/client.rb', line 242 def service_instances(depth = 1, query = {}) @base.services.collect do |json| ServiceInstance.new(json[:name], self, json) end end |
#services ⇒ Object
Retrieve available services.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cfoundry/v1/client.rb', line 96 def services services = [] @base.system_services.each do |type, vendors| vendors.each do |vendor, providers| providers.each do |provider, versions| versions.each do |num, | services << Service.new(vendor.to_s, num.to_s, [:description], type, provider.to_s) end end end end services end |
#target ⇒ Object
The current target URL of the client.
23 24 25 |
# File 'lib/cfoundry/v1/client.rb', line 23 def target @base.target end |
#token ⇒ Object
Current authentication token.
28 29 30 |
# File 'lib/cfoundry/v1/client.rb', line 28 def token @base.token end |
#token=(token) ⇒ Object
Set the authentication token.
33 34 35 |
# File 'lib/cfoundry/v1/client.rb', line 33 def token=(token) @base.token = token end |
#trace ⇒ Object
Is the client tracing API requests?
49 50 51 |
# File 'lib/cfoundry/v1/client.rb', line 49 def trace @base.trace end |
#trace=(bool) ⇒ Object
Set the tracing flag; if true, API requests and responses will be printed out.
55 56 57 |
# File 'lib/cfoundry/v1/client.rb', line 55 def trace=(bool) @base.trace = bool end |
#user(email) ⇒ Object
Construct a User object. The return value is lazy, and no requests are made from this alone.
This should be used for both user creation (after calling User#create!) and retrieval.
170 171 172 |
# File 'lib/cfoundry/v1/client.rb', line 170 def user(email) User.new(email, self) end |