Module: ViprHost

Included in:
Vipr
Defined in:
lib/vipruby/objects/host.rb

Overview

The Following Host calls will get Host information for all tenants

Instance Method Summary collapse

Instance Method Details

#add_host(host_type = nil, ip_or_dns = nil, name = nil, user_name = nil, password = nil, port = nil, use_ssl = nil, discoverable = nil, auth = nil, cert = nil) ⇒ JSON

Add a host to ViPR

Examples:

vipr.add_host('Windows', 'windowshost.mydomain.org', 'WindowsHOST' 'DOMAIN\user', 'userpw')
vipr.add_host('Windows', 'windowshost.mydomain.org', 'WindowsHOST' 'DOMAIN\user', 'userpw', '453', 'true', 'true')
vipr.add_host('Linux', 'linuxhost.mydomain.org', 'LinuxHOST' 'DOMAIN\user', 'userpw')

Parameters:

  • host_type (String) (defaults to: nil)

    Type of Host. “Windows”, “Linux”, or, “HPUX”. Required Param

  • ip_or_dns (String) (defaults to: nil)

    IP Address or FQDN of host. Required Param

  • name (String) (defaults to: nil)

    Arbitrary Name only necesary and identifiable by ViPR. Required Param.

  • user_name (String) (defaults to: nil)

    User Name to connect to the host. Required Param

  • password (String) (defaults to: nil)

    Password for the User Name to connect to the host. Required Param

  • port (String) (defaults to: nil)

    Port to connect to the host. Optional Param. Defaults will be used if no param is passed

  • use_ssl (String) (defaults to: nil)

    Whether SSL is used. Trur or False. Optional Param

  • discoverable (String) (defaults to: nil)

    True or False. Initators and Nodes will be discovered after being added. By default this is true

Returns:

  • (JSON)

    returns host information



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vipruby/objects/host.rb', line 71

def add_host(host_type=nil, ip_or_dns=nil, name=nil, user_name=nil, password=nil, port=nil, use_ssl=nil, discoverable=nil, auth=nil, cert=nil)
    check_host_post(host_type, ip_or_dns, name, user_name, password)
    host_type = host_type.split('_').collect(&:capitalize).join

    if host_type == "Windows" 
        use_ssl.nil? ? use_ssl = false : use_ssl
        if use_ssl == true
            port.nil? ? port = '5986' : port
        else
            port.nil? ? port = '5985' : port
        end
        discoverable.nil? ? discoverable = true : discoverable
        user_name.nil? ? user_name = "admin" : user_name
        password.nil? ? password = "#1Password" : password
    elsif host_type == "Linux"
        use_ssl.nil? ? use_ssl = false : use_ssl
        port.nil? ? port = '22' : port = port
        discoverable.nil? ? discoverable = true : discoverable
        user_name.nil? ? user_name = "admin" : user_name
        password.nil? ? password = "#1Password" : password
    elsif host_type == "Hpux"
        host_type = "HPUX"
    end
    rest_post(generate_host_post_json(host_type, ip_or_dns, name, port, user_name, password, use_ssl, discoverable), "#{@base_url}/tenants/#{@tenant_uid}/hosts", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#add_host_initiator(host_id = nil, protocol = nil, initiator_port = nil, initiator_node = nil, auth = nil, cert = nil) ⇒ Object

Add an initiator to a host in ViPR

Examples:

x = vipr.get_all_hosts['id'][0]
vipr.add_host_initiator(x, 'FC', '10:13:27:65:60:38:68:BE', ['10:13:27:65:60:38:68:BD','10:13:27:65:60:38:68:BC'])

Parameters:

  • host_id (String) (defaults to: nil)

    The Host UID. Required Param.

  • protocol (String) (defaults to: nil)

    The protocol type. iSCSI or FC. Required

  • initiator_port (String) (defaults to: nil)

    Initator Port as a string. Required

  • initiator_node (Array) (defaults to: nil)

    Initator Nodes must be passed in as strings in an array. Required.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vipruby/objects/host.rb', line 107

def add_host_initiator(host_id=nil, protocol=nil, initiator_port=nil, initiator_node=nil, auth=nil, cert=nil)
    check_host_get(host_id)
    check_host_post_initiator(protocol, initiator_port)

    protocol = protocol.upcase
    if protocol == "ISCSI"
        protocol = "iSCSI"
    end
    initiator_payload_array = generate_initiators_json(protocol, initiator_node, initiator_port)
    initiator_payload_array.each do |i|
        rest_post(i, "#{@base_url}/compute/hosts/#{host_id}/initiators", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
    end
end

#check_host_get(host_id) ⇒ Boolean (private)

Error Handling method to check for Missing Host ID param. If the pass fails, an error exception is raised

Parameters:

  • host_id (String)

    Requires the string of the vcenter uid

Returns:

  • (Boolean)

    True if pass, false if it fails



215
216
217
218
219
# File 'lib/vipruby/objects/host.rb', line 215

def check_host_get(host_id)
  if host_id == nil
      raise "Missing the Required param (host_id). Find the host_id by using the get_all_hosts method."
  end
end

#check_host_post(host_type, ip_or_dns, name, user_name, password) ⇒ Boolean (private)

Error Handling method to check for Missing Host Post params. If the pass fails, an error exception is raised

Parameters:

  • host_type (String)

    Requires the string of the host type.

  • ip_or_dns (String)

    Requires the string of the IP Address or FQDN.

  • name (String)

    Requires the string of the Arbitrary name for ViPR.

  • user_name (String)

    Requires the string of the User Name for access.

  • password (String)

    Requires the string of the Password of the User Name for access

Returns:

  • (Boolean)

    True if pass, false if it fails



203
204
205
206
207
# File 'lib/vipruby/objects/host.rb', line 203

def check_host_post(host_type, ip_or_dns, name, user_name, password)
  if host_type == nil || ip_or_dns == nil || name == nil
      raise "Missing a Required param (host_type, ip_or_dns, name)"
  end
end

#check_host_post_initiator(protocol, initiator_port) ⇒ Boolean (private)

Error Handling method to check for Initiator params. If the pass fails, an error exception is raised

Parameters:

  • protocol (String)

    Requires the string of the Port

  • initiator_port (String)

    Requires the string of the initiator_port

Returns:

  • (Boolean)

    True if pass, false if it fails



228
229
230
231
232
# File 'lib/vipruby/objects/host.rb', line 228

def check_host_post_initiator(protocol, initiator_port)
  if protocol== nil || initiator_port == nil
      raise "Missing the Required param (protocol or initiator_port)."
  end
end

#deactivate_host(host_id = nil, auth = nil, cert = nil) ⇒ JSON

Deactive and Remove a Host from ViPR

Examples:

x = vipr.get_all_hosts['id'][0]
vipr.deactivate_host(x)

Parameters:

  • host_id (STRING) (defaults to: nil)

    ID of host to get information. Required Param

Returns:

  • (JSON)

    returns information from POST for removing Host object



154
155
156
157
# File 'lib/vipruby/objects/host.rb', line 154

def deactivate_host(host_id=nil, auth=nil, cert=nil)
    check_host_get(host_id)
    rest_post(nil, "#{@base_url}/compute/hosts/#{host_id}/deactivate", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#find_host_object(search_param, auth = nil, cert = nil) ⇒ JSON

Find and return query results for a host in ViPR

Examples:

vipr.find_host_object('host1')

Parameters:

  • search_param (STRING)

    Value to search host for. This will work with partials

Returns:

  • (JSON)

    returns search results



187
188
189
# File 'lib/vipruby/objects/host.rb', line 187

def find_host_object(search_param, auth=nil, cert=nil)
  rest_get("#{@base_url}/compute/hosts/search?name=#{search_param}", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#generate_host_post_json(host_type, ip_or_dns, name, port, user_name, password, use_ssl, discoverable) ⇒ JSON

generate JSON for Host POST

Parameters:

  • host_type (String)

    Type of Host. Required Param.

  • ip_or_dns (String)

    IP Address or FQDN of the host to add. Required Param.

  • name (String)

    Arbitrary name given to the host. Required Param

  • port (String)

    Port for connecting to the specfic host. Will be autogenerated if not specified by the #add_host method

  • user_name (String)

    User Name to connect to this host.

  • password (String)

    Password for the Username to connect to this host.

  • use_ssl (String)

    True or False

  • discoverable (String)

    True or False for searching initators after being added.

Returns:

  • (JSON)

    The JSON object for the POST operation



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vipruby/objects/host.rb', line 19

def generate_host_post_json(host_type, ip_or_dns, name, port, user_name, password, use_ssl, discoverable)
    payload = {
        type: host_type,
        host_name: ip_or_dns,
        name: name,
        port_number: port,
        user_name: user_name,
        password: password,
        use_ssl: use_ssl,
        discoverable: discoverable
    }.to_json

    return payload
end

#generate_initiators_json(protocol, initiator_node, initiator_port) ⇒ ARRAY

generate JSON for Initators POST

Parameters:

  • protocol (String)

    Type of protocol. Required Param

  • initiator_node (String)

    Node string. Required Param

  • initiator_port (Array)

    Ports should be passed as an array. Every port will be added into another array for the entire JSON object

Returns:

  • (ARRAY)

    JSON objects will be put into an Array



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vipruby/objects/host.rb', line 41

def generate_initiators_json(protocol, initiator_node, initiator_port)
    initiator_json = []
    initiator_port.each do |initiator|
        initiator_json << 
        {
            protocol: protocol,
            initiator_node: initiator_node,
            initiator_port: initiator
        }.to_json
    end
    return initiator_json
end

#get_all_hosts(auth = nil, cert = nil) ⇒ JSON

Get all Host objects in ViPR

Examples:

vipr.get_all_hosts

Returns:

  • (JSON)

    returns a JSON collection of all hosts in ViPR for a particular tenant



127
128
129
# File 'lib/vipruby/objects/host.rb', line 127

def get_all_hosts(auth=nil, cert=nil)
    rest_get("#{@base_url}/tenants/#{@tenant_uid}/hosts", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#get_host(host_id = nil, auth = nil, cert = nil) ⇒ Hash

Get an individual host’s details in ViPR

Examples:

x = vipr.get_all_hosts['id'][0]
vipr.get_host(x)

Parameters:

  • host_id (STRING) (defaults to: nil)

    ID of host to get information. Required Param

Returns:

  • (Hash)

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



140
141
142
143
# File 'lib/vipruby/objects/host.rb', line 140

def get_host(host_id=nil, auth=nil, cert=nil)
    check_host_get(host_id)
    rest_get("#{@base_url}/compute/hosts/#{host_id}", auth.nil? ? @auth_token : auth, cert.nil? ? @verify_cert : cert)
end

#host_exists?(hostname, auth = nil, cert = nil) ⇒ BOOLEAN

Determine if a host already exists in ViPR

Examples:

vipr.host_exists?('windowslab.mydomain.com')

Parameters:

  • hostname (STRING)

    The name of the host to search for. Requires full name and not partials

Returns:

  • (BOOLEAN)

    returns TRUE/FALSE



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vipruby/objects/host.rb', line 167

def host_exists?(hostname, auth=nil, cert=nil)
    hostname = hostname.downcase
    host_array = []
    hosts = get_all_hosts
    hosts.each do |key, value|
      value.each do |k|
        host_array << k['name'].to_s.downcase
      end
    end
   host_array.include?(hostname) 
end