Class: CFoundry::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cfoundry/v2/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.



32
33
34
# File 'lib/cfoundry/v2/client.rb', line 32

def initialize(target = "http://api.cloudfoundry.com", token = nil)
  @base = Base.new(target, token)
end

Instance Attribute Details

#baseObject (readonly)

Internal BaseClient instance. Normally won’t be touching this.



20
21
22
# File 'lib/cfoundry/v2/client.rb', line 20

def base
  @base
end

#current_organizationObject

Organization

Currently targeted organization.



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

def current_organization
  @current_organization
end

#current_spaceObject

Space

Currently targeted space.



26
27
28
# File 'lib/cfoundry/v2/client.rb', line 26

def current_space
  @current_space
end

Instance Method Details

#app_by_name(name) ⇒ Object

TODO: allow direct filtering



119
120
121
122
123
# File 'lib/cfoundry/v2/client.rb', line 119

def app_by_name(name)
  current_space.apps.find do |a|
    a.name == name
  end
end

#current_userObject

The currently authenticated user.



64
65
66
67
68
# File 'lib/cfoundry/v2/client.rb', line 64

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

#infoObject

Cloud metadata



71
72
73
# File 'lib/cfoundry/v2/client.rb', line 71

def info
  @base.info
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


113
114
115
# File 'lib/cfoundry/v2/client.rb', line 113

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



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

def (credentials)
  @current_organization = nil
  @current_space = nil

  @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



76
77
78
79
80
81
82
83
84
# File 'lib/cfoundry/v2/client.rb', line 76

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.



108
109
110
# File 'lib/cfoundry/v2/client.rb', line 108

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



42
43
44
# File 'lib/cfoundry/v2/client.rb', line 42

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.



48
49
50
# File 'lib/cfoundry/v2/client.rb', line 48

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

#targetObject

The current target URL of the client.



37
38
39
# File 'lib/cfoundry/v2/client.rb', line 37

def target
  @base.target
end

#traceObject

Is the client tracing API requests?



53
54
55
# File 'lib/cfoundry/v2/client.rb', line 53

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

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



59
60
61
# File 'lib/cfoundry/v2/client.rb', line 59

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