Class: CFoundry::V1::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#baseObject (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.



186
187
188
# File 'lib/cfoundry/v1/client.rb', line 186

def app(name = nil)
  App.new(name, self)
end

#app_by_name(name) ⇒ Object

TODO: remove once v2 allows filtering by name see V2::Client#app_by_name



192
193
194
195
196
197
# File 'lib/cfoundry/v1/client.rb', line 192

def app_by_name(name)
  app = app(name)
  if app.exists?
    app
  end
end

#appsObject

Retreive all of the current user’s applications.



175
176
177
178
179
# File 'lib/cfoundry/v1/client.rb', line 175

def apps
  @base.apps.collect do |json|
    App.new(json[:name], self, json)
  end
end

#current_userObject

The currently authenticated user.



50
51
52
53
54
# File 'lib/cfoundry/v1/client.rb', line 50

def current_user
  if user = info[:user]
    user(user)
  end
end

#frameworksObject

Retrieve available frameworks.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cfoundry/v1/client.rb', line 91

def frameworks
  fs = info[:frameworks]
  return unless fs

  frameworks = []
  fs.each do |name, meta|
    runtimes = meta[:runtimes].collect do |r|
      Runtime.new(r[:name], r[:description])
    end

    frameworks <<
      Framework.new(name.to_s, nil, runtimes, meta[:detection])
  end
  frameworks
end

#infoObject

Retrieve target metadata.



58
59
60
# File 'lib/cfoundry/v1/client.rb', line 58

def info
  @base.info
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


169
170
171
# File 'lib/cfoundry/v1/client.rb', line 169

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`.



152
153
154
155
156
157
158
159
160
161
# File 'lib/cfoundry/v1/client.rb', line 152

def (credentials)
  @base.token =
    if @base.uaa
      @base.uaa.authorize(credentials)
    else
      @base.create_token(
        { :password => credentials[:password] },
        credentials[:username])[:token]
    end
end

#login_promptsObject

Login prompts



135
136
137
138
139
140
141
142
143
# File 'lib/cfoundry/v1/client.rb', line 135

def 
  if @base.uaa
    @base.uaa.prompts
  else
    { :username => ["text", "Email"],
      :password => ["password", "Password"]
    }
  end
end

#logoutObject

Clear client token. No requests are made for this.



164
165
166
# File 'lib/cfoundry/v1/client.rb', line 164

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



28
29
30
# File 'lib/cfoundry/v1/client.rb', line 28

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.



34
35
36
# File 'lib/cfoundry/v1/client.rb', line 34

def proxy=(email)
  @base.proxy = email
end

#register(email, password) ⇒ Object

Create a user on the target and return a User object representing them.



129
130
131
132
# File 'lib/cfoundry/v1/client.rb', line 129

def register(email, password)
  @base.create_user(:email => email, :password => password)
  user(email)
end

#runtimesObject

Retrieve available runtimes.



79
80
81
82
83
84
85
86
87
88
# File 'lib/cfoundry/v1/client.rb', line 79

def runtimes
  runtimes = []

  @base.system_runtimes.each do |name, meta|
    runtimes <<
      Runtime.new(name.to_s, meta[:version], meta[:debug_modes])
  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.



211
212
213
# File 'lib/cfoundry/v1/client.rb', line 211

def service_instance(name = nil)
  ServiceInstance.new(name, self)
end

#service_instancesObject

Retrieve all of the current user’s services.



200
201
202
203
204
# File 'lib/cfoundry/v1/client.rb', line 200

def service_instances
  @base.services.collect do |json|
    ServiceInstance.new(json[:name], self, json)
  end
end

#servicesObject

Retrieve available services.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cfoundry/v1/client.rb', line 63

def services
  services = []

  @base.system_services.each do |type, vendors|
    vendors.each do |vendor, versions|
      versions.each do |num, meta|
        services <<
          Service.new(vendor.to_s, num, meta[:description], type)
      end
    end
  end

  services
end

#targetObject

The current target URL of the client.



23
24
25
# File 'lib/cfoundry/v1/client.rb', line 23

def target
  @base.target
end

#traceObject

Is the client tracing API requests?



39
40
41
# File 'lib/cfoundry/v1/client.rb', line 39

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

Set the tracing flag; if true, API requests and responses will be printed out.



45
46
47
# File 'lib/cfoundry/v1/client.rb', line 45

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.



124
125
126
# File 'lib/cfoundry/v1/client.rb', line 124

def user(email)
  User.new(email, self)
end

#usersObject

Retrieve user list. Admin-only.



109
110
111
112
113
114
115
116
117
# File 'lib/cfoundry/v1/client.rb', line 109

def users
  @base.users.collect do |json|
    User.new(
      json[:email],
      self,
      { :email => json[:email],
        :admin => json[:admin] })
  end
end