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.



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

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.



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

def base
  @base
end

#current_organizationObject

Organization

Currently targeted organization.



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

def current_organization
  @current_organization
end

#current_spaceObject

Space

Currently targeted space.



30
31
32
# File 'lib/cfoundry/v2/client.rb', line 30

def current_space
  @current_space
end

Instance Method Details

#current_userObject

The currently authenticated user.



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

def current_user
  if guid = token_data[:user_id]
    user = user(guid)
    user.emails = [{ :value => token_data[:email] }]
    user
  end
end

#infoObject

Cloud metadata



78
79
80
# File 'lib/cfoundry/v2/client.rb', line 78

def info
  @base.info
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cfoundry/v2/client.rb', line 100

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



83
84
85
86
87
88
89
90
91
# File 'lib/cfoundry/v2/client.rb', line 83

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.



115
116
117
# File 'lib/cfoundry/v2/client.rb', line 115

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



46
47
48
# File 'lib/cfoundry/v2/client.rb', line 46

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.



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

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

#targetObject

The current target URL of the client.



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

def target
  @base.target
end

#traceObject

Is the client tracing API requests?



57
58
59
# File 'lib/cfoundry/v2/client.rb', line 57

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

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



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

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