Class: VCloud::Client
- Inherits:
-
BaseVCloudEntity
- Object
- BaseVCloudEntity
- VCloud::Client
- Defined in:
- lib/vcloud/client.rb
Overview
Handles creating and managing a session in vCloud Director.
Constant Summary collapse
- LOGIN =
'login'
- SESSION =
'sessions'
- LOGOUT_SESSION =
'session'
- LOGOUT_HTTP_RESPONSE =
204
- TOKEN =
'x_vcloud_authorization'.to_sym
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
Instance Method Summary collapse
-
#get_org_from_name(name) ⇒ VCloud::Org
Retrieves an Org, assuming the user has access to it.
-
#get_org_references ⇒ VCloud::OrgList
Returns an OrgList that contains all of the Orgs the user has access to.
-
#get_org_references_by_name ⇒ Hash{String => VCloud::Reference}
Returns a hash of of all Org refs keyed by the Org name.
-
#get_orglist_link ⇒ VCloud::Link
Returns the Link object to retrieve an OrgList.
-
#initialize(url, api_version, opts = {}) ⇒ Client
constructor
Create a new client.
-
#logged_in? ⇒ Boolean
Determins if the user is logged in.
-
#login(username, password) ⇒ Boolean
Create a new session and retrieves the session token.
-
#logout ⇒ Boolean
Destroy the current session.
Methods inherited from BaseVCloudEntity
attr_accessor, attr_reader, attr_writer, from_reference, inherited, type
Methods included from RestApi
#build_generic_http_opts, #create, #delete, #http_request, #parse_response, #post, #refresh, #update
Constructor Details
#initialize(url, api_version, opts = {}) ⇒ Client
Create a new client
26 27 28 29 30 31 32 33 34 |
# File 'lib/vcloud/client.rb', line 26 def initialize(url, api_version, opts={}) @links=[] @url = url.strip @api_version = api_version @token = {} @logged_in = false @verify_ssl = opts[:verify_ssl].nil? ? true : opts[:verify_ssl] end |
Instance Attribute Details
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
19 20 21 |
# File 'lib/vcloud/client.rb', line 19 def api_version @api_version end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
19 20 21 |
# File 'lib/vcloud/client.rb', line 19 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
19 20 21 |
# File 'lib/vcloud/client.rb', line 19 def url @url end |
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
20 21 22 |
# File 'lib/vcloud/client.rb', line 20 def verify_ssl @verify_ssl end |
Instance Method Details
#get_org_from_name(name) ⇒ VCloud::Org
Retrieves an Org, assuming the user has access to it
73 74 75 76 77 78 |
# File 'lib/vcloud/client.rb', line 73 def get_org_from_name(name) orgs = get_org_references_by_name ref = orgs[name] or return nil org = Org.from_reference(ref, self) return org end |
#get_org_references ⇒ VCloud::OrgList
Returns an OrgList that contains all of the Orgs the user has access to
65 66 67 |
# File 'lib/vcloud/client.rb', line 65 def get_org_references() OrgList.from_reference(get_orglist_link, self).org_references end |
#get_org_references_by_name ⇒ Hash{String => VCloud::Reference}
Returns a hash of of all Org refs keyed by the Org name
58 59 60 |
# File 'lib/vcloud/client.rb', line 58 def get_org_references_by_name() Hash[get_org_references.collect{ |i| [i.name, i] }] end |
#get_orglist_link ⇒ VCloud::Link
Returns the Link object to retrieve an OrgList
83 84 85 |
# File 'lib/vcloud/client.rb', line 83 def get_orglist_link @orglist ||= @links.select {|l| l.type == VCloud::Constants::ContentType::ORG_LIST}.first end |
#logged_in? ⇒ Boolean
Determins if the user is logged in
90 91 92 |
# File 'lib/vcloud/client.rb', line 90 def logged_in? @logged_in end |
#login(username, password) ⇒ Boolean
Create a new session and retrieves the session token
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vcloud/client.rb', line 41 def login(username, password) return true if @logged_in url = @api_version > VCloud::Constants::Version::V0_9 ? @url + SESSION : @url + LOGIN response = post(url, nil, nil, self, :user => username, :password => password) parse_xml(response) @token = { TOKEN => response.headers[TOKEN] } @logged_in = true return true end |
#logout ⇒ Boolean
Destroy the current session
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vcloud/client.rb', line 97 def logout return false if not logged_in? url = @api_version > VCloud::Constants::Version::V0_9 ? @url + LOGOUT_SESSION : @url + LOGIN begin delete(url, nil, nil, self) rescue => error if error.instance_of? VCloud::VCloudError raise error else return false end end @token = nil @logged_in = false return true end |