forked from ManageIQ/manageiq-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.rb
82 lines (69 loc) · 2.48 KB
/
update.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# Description: Update ServiceNow CMDB Record
#
require 'rest-client'
require 'json'
require 'base64'
def log(level, message)
method = '----- Update ServiceNow CMDB Record -----'
$evm.log(level, "#{method} - #{message}")
end
def get_virtual_column_value(vm, virtual_column_name)
virtual_column_value = vm.send(virtual_column_name)
return virtual_column_value unless virtual_column_value.nil?
nil
end
def hostname(vm)
vm.hostnames.first.presence || vm.name
end
snow_server = $evm.object['snow_server']
snow_user = $evm.object['snow_user']
snow_password = $evm.object.decrypt('snow_password')
table_name = $evm.object['table_name']
uri = "https://#{snow_server}/api/now/table/#{table_name}"
headers = {
:content_type => 'application/json',
:accept => 'application/json',
:authorization => "Basic #{Base64.strict_encode64("#{snow_user}:#{snow_password}")}"
}
# Grab the VM object
log(:info, "vmdb_object_type => <#{$evm.root['vmdb_object_type']}>")
case $evm.root['vmdb_object_type']
when 'miq_provision'
prov = $evm.root['miq_provision']
vm = prov.vm unless prov.nil?
else
vm = $evm.root['vm']
end
log(:warn, 'VM object is empty') if vm.nil?
sys_id = $evm.object['sys_id'] || vm.custom_get(:servicenow_sys_id)
raise 'ServiceNow sys_id is empty' if sys_id.nil?
uri = "#{uri}/#{sys_id}"
log(:info, 'Getting item details')
request = RestClient::Request.new(
:method => :get,
:url => uri,
:headers => headers
)
rest_result = request.execute
log(:info, "Return code <#{rest_result.code}>")
json_parse = JSON.parse(rest_result)
result = json_parse['result']
# Extend payload attributes as required
result[:virtual] = $evm.object['virtual'] || true
result[:name] = $evm.object['name'] || vm.name
result[:short_description] = $evm.object['description'] || "CloudForms GUID <#{vm.guid}>"
result[:host_name] = $evm.object['host_name'] || hostname(vm)
result[:cpu_count] = $evm.object['cpu_count'] || get_virtual_column_value(vm, :num_cpu)
result[:ram] = $evm.object['memory'] || get_virtual_column_value(vm, :mem_cpu)
result[:vendor] = $evm.object['vendor'] || vm.vendor
log(:info, "payload => #{result}")
log(:info, 'Updating record details')
request = RestClient::Request.new(
:method => :put,
:url => uri,
:headers => (headers.merge!(:cookies => request.cookies)),
:payload => result.to_json
)
rest_result = request.execute
log(:info, "Return code <#{rest_result.code}>")