Class: VCloud::Client

Inherits:
BaseVCloudEntity show all
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

Instance Method Summary collapse

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

Parameters:



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_versionObject (readonly)

Returns the value of attribute api_version.



19
20
21
# File 'lib/vcloud/client.rb', line 19

def api_version
  @api_version
end

#tokenObject (readonly)

Returns the value of attribute token.



19
20
21
# File 'lib/vcloud/client.rb', line 19

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/vcloud/client.rb', line 19

def url
  @url
end

#verify_sslObject

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

Parameters:

  • name (String)

    Org name

Returns:



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_referencesVCloud::OrgList

Returns an OrgList that contains all of the Orgs the user has access to

Returns:

  • (VCloud::OrgList)

    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_nameHash{String => VCloud::Reference}

Returns a hash of of all Org refs keyed by the Org name

Returns:

  • (Hash{String => VCloud::Reference})

    Reference to all Orgs the user has access to, keyed by 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

Returns the Link object to retrieve an OrgList

Returns:



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

Returns:

  • (Boolean)

    True if the user is logged in, otherwise false



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

Parameters:

  • username (String)

    Username to log in with

  • password (String)

    Password to log in with

Returns:

  • (Boolean)

    True if a session token has been generated



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vcloud/client.rb', line 41

def (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

#logoutBoolean

Destroy the current session

Returns:

  • (Boolean)

    True if the session was destroyed, false if it could not be destroyed or a session does not exist



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