Module: ViprBase

Included in:
Vipr
Defined in:
lib/vipruby/viprbase.rb

Overview

Base methods used for all operations such as logging in and performing API calls

Instance Method Summary collapse

Instance Method Details

#generate_base_url(ip_or_fqdn) ⇒ strong

Generate the URL for standard ViPR instances using https: and port :443

Parameters:

  • ip_or_fqdn (string)

    String representation of the IP Address or Fully Qualified Domain Name of the ViPR instance

Returns:

  • (strong)

    returns the full URL for API called



63
64
65
# File 'lib/vipruby/viprbase.rb', line 63

def generate_base_url(ip_or_fqdn)
  return "https://#{ip_or_fqdn}:4443"
end

#get_auth_token(user_name, password, cert = nil, base = nil) ⇒ String

Get User’s Authentication Token

Parameters:

  • user_name (string)

    the username used to login

  • password (string)

    the password for the username

Returns:

  • (String)

    returns authentication token for the user



72
73
74
# File 'lib/vipruby/viprbase.rb', line 72

def get_auth_token(user_name,password, cert=nil, base=nil)
  (user_name, password, cert.nil? ? @verify_cert : cert, base.nil? ? @base_url : base).headers[:x_sds_auth_token]
end

#get_tenant_uid(base = nil, auth = nil, cert = nil) ⇒ Hash

Get the current users Tenant UID

Returns:

  • (Hash)

    Hash return with the Tenant UID [‘id’].



41
42
43
# File 'lib/vipruby/viprbase.rb', line 41

def get_tenant_uid(base=nil, auth=nil, cert=nil)
  rest_get(base.nil? ? @base_url + "/tenant" : base + "/tenant", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#login(user_name, password, cert = nil, base = nil) ⇒ HTML

Login to ViPR

Parameters:

  • user_name (string)

    the username used to login

  • password (string)

    the password for the username

Returns:

  • (HTML)

    returns token information in headers



50
51
52
53
54
55
56
57
# File 'lib/vipruby/viprbase.rb', line 50

def (user_name, password, cert=nil, base=nil)
  RestClient::Request.execute(method: :get,
    url: base.nil? ? @base_url : base + "/login",
    user: user_name,
    password: password,
    verify_ssl: cert.nil? ? @verify_cert : cert
  )
end

#rest_get(api_url, auth = nil, cert = nil) ⇒ Hash

Generic API get call

Parameters:

  • api_url (string)

    the full API URL path

Returns:

  • (Hash)

    the object converted into Hash format and can be parsed with object or object notation



28
29
30
31
32
33
34
35
36
# File 'lib/vipruby/viprbase.rb', line 28

def rest_get(api_url, auth=nil, cert=nil)
  JSON.parse(RestClient::Request.execute(method: :get,
    url: api_url,
    verify_ssl: cert.nil? ? @verify_cert : cert,
    headers: {
      :'X-SDS-AUTH-TOKEN' => auth.nil? ? @auth_token : auth,
      accept: :json
    }))
end

#rest_post(payload, api_url, auth = nil, cert = nil) ⇒ Hash

Generic API post call

Parameters:

  • payload (JSON)

    the JSON payload to be posted

  • api_url (string)

    the full API URL path

Returns:

  • (Hash)

    the object converted into Hash format and can be parsed with object or object notation



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vipruby/viprbase.rb', line 12

def rest_post(payload, api_url, auth=nil, cert=nil)
  JSON.parse(RestClient::Request.execute(method: :post,
       url: api_url,
       verify_ssl: cert.nil? ? @verify_cert : cert,
       payload: payload,
       headers: {
         :'X-SDS-AUTH-TOKEN' => auth.nil? ? @auth_token : auth,
         content_type: 'application/json',
         accept: :json
       }))
end

#to_boolean(str) ⇒ bool

Ensure value is a boolean

Parameters:

  • str (string, bool)

    the value to convert or verify

Returns:

  • (bool)

    returns True or False



80
81
82
# File 'lib/vipruby/viprbase.rb', line 80

def to_boolean(str)
  str.to_s.downcase == "true"
end