Class: CFoundry::V1::Client

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



13
14
15
# File 'lib/cfoundry/v1/client.rb', line 13

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.



8
9
10
# File 'lib/cfoundry/v1/client.rb', line 8

def base
  @base
end

Instance Method Details

#current_organizationObject



80
81
82
# File 'lib/cfoundry/v1/client.rb', line 80

def current_organization
  nil
end

#current_spaceObject



76
77
78
# File 'lib/cfoundry/v1/client.rb', line 76

def current_space
  nil
end

#current_userObject

The currently authenticated user.



70
71
72
73
74
# File 'lib/cfoundry/v1/client.rb', line 70

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

#framework(name) ⇒ Object



151
152
153
# File 'lib/cfoundry/v1/client.rb', line 151

def framework(name)
  Framework.new(name)
end

#framework_by_name(name) ⇒ Object



155
156
157
# File 'lib/cfoundry/v1/client.rb', line 155

def framework_by_name(name)
  frameworks.find { |f| f.name == name }
end

#frameworks(options = {}) ⇒ Object

Retrieve available frameworks.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cfoundry/v1/client.rb', line 134

def frameworks(options = {})
  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.



86
87
88
# File 'lib/cfoundry/v1/client.rb', line 86

def info
  @base.info
end

#logObject

The current log. See log=.



55
56
57
# File 'lib/cfoundry/v1/client.rb', line 55

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.



65
66
67
# File 'lib/cfoundry/v1/client.rb', line 65

def log=(mode)
  @base.log = mode
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


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

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



183
184
185
186
187
188
189
190
191
192
# File 'lib/cfoundry/v1/client.rb', line 183

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



166
167
168
169
170
171
172
173
174
# File 'lib/cfoundry/v1/client.rb', line 166

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.



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

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



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

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.



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

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

#register(email, password) ⇒ Object

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



160
161
162
163
# File 'lib/cfoundry/v1/client.rb', line 160

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

#runtime(name) ⇒ Object



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

def runtime(name)
  Runtime.new(name)
end

#runtime_by_name(name) ⇒ Object



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

def runtime_by_name(name)
  runtimes.find { |r| r.name == name }
end

#runtimes(options = {}) ⇒ Object

Retrieve available runtimes.



114
115
116
117
118
119
120
121
122
123
# File 'lib/cfoundry/v1/client.rb', line 114

def runtimes(options = {})
  runtimes = []

  @base.system_runtimes.each do |name, meta|
    runtimes <<
      Runtime.new(name.to_s, meta[:version], meta[:debug_modes])
  end

  runtimes
end

#services(options = {}) ⇒ Object

Retrieve available services.



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

def services(options = {})
  services = []

  @base.system_services.each do |type, vendors|
    vendors.each do |vendor, providers|
      providers.each do |provider, properties|
        properties.each do |_, meta|
          meta[:supported_versions].each do |ver|
            state = meta[:version_aliases].find { |k, v| v == ver }

            services <<
              Service.new(vendor.to_s, ver.to_s, meta[:description],
                          type.to_s, provider.to_s, state && state.first)
          end
        end
      end
    end
  end

  services
end

#targetObject

The current target URL of the client.



18
19
20
# File 'lib/cfoundry/v1/client.rb', line 18

def target
  @base.target
end

#tokenObject

Current authentication token.



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

def token
  @base.token
end

#token=(token) ⇒ Object

Set the authentication token.



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

def token=(token)
  @base.token = token
end

#traceObject

Is the client tracing API requests?



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

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

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



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

def trace=(bool)
  @base.trace = bool
end