Class: CFoundry::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cfoundry/client.rb

Overview

The primary API entrypoint. Wraps RESTClient 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.



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

def initialize(target = "http://api.cloudfoundry.com", token = nil)
  @rest = RESTClient.new(*args)
end

Instance Attribute Details

#restObject (readonly)

:nodoc:



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

def rest
  @rest
end

Instance Method Details

#app(name) ⇒ 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.



133
134
135
# File 'lib/cfoundry/client.rb', line 133

def app(name)
  CFoundry::App.new(name, self)
end

#appsObject

Retreive all of the current user’s applications.



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

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

#infoObject

Retrieve target metadata.



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

def info
  @rest.info
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


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

def logged_in?
  !!@rest.token
end

#login(email, password) ⇒ Object

Authenticate with the target. Sets the client token.



105
106
107
108
# File 'lib/cfoundry/client.rb', line 105

def (email, password)
  @rest.token =
    @rest.create_token({ :password => password }, email)["token"]
end

#logoutObject

Clear client token. No requests are made for this.



111
112
113
# File 'lib/cfoundry/client.rb', line 111

def logout
  @rest.token = nil
end

#proxyObject

Current proxy user. Usually nil.



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

def proxy
  @rest.proxy
end

#proxy=(email) ⇒ Object

Set the proxy user for the client. Must be authorized as an administrator for this to have any effect.



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

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

#register(email, password) ⇒ Object

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



99
100
101
102
# File 'lib/cfoundry/client.rb', line 99

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

#service(name) ⇒ 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.



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

def service(name)
  CFoundry::Service.new(name, self)
end

#servicesObject

Retrieve all of the current user’s services.



139
140
141
142
143
# File 'lib/cfoundry/client.rb', line 139

def services
  @rest.services.collect do |json|
    CFoundry::Service.new(json["name"], self, json)
  end
end

#system_runtimesObject

Retrieve available runtimes.



73
74
75
# File 'lib/cfoundry/client.rb', line 73

def system_runtimes
  @rest.system_runtimes
end

#system_servicesObject

Retrieve available services. Returned as a Hash from vendor => metadata.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cfoundry/client.rb', line 55

def system_services
  services = {}

  @rest.system_services.each do |type, vendors|
    vendors.each do |vendor, versions|
      services[vendor] =
        { :type => type,
          :versions => versions.keys,
          :description => versions.values[0]["description"],
          :vendor => vendor
        }
    end
  end

  services
end

#targetObject

The current target URL of the client.



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

def target
  @rest.target
end

#traceObject

Is the client tracing API requests?



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

def trace
  @rest.trace
end

#trace=(bool) ⇒ Object

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



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

def trace=(bool)
  @rest.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.



94
95
96
# File 'lib/cfoundry/client.rb', line 94

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

#usersObject

Retrieve user list. Admin-only.



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

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