forked from OpenNebula/one-swap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvsphere_client.rb
More file actions
141 lines (115 loc) · 3.83 KB
/
vsphere_client.rb
File metadata and controls
141 lines (115 loc) · 3.83 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require 'net/http'
require 'uri'
require 'json'
require 'logger'
#
# Client for the vCenter REST API
#
class VSphereClient
attr_reader :vcenter
def initialize(vcenter, user, password, logger)
@vcenter = vcenter
@logger = logger
@session_id = open_session(vcenter, user, password)
end
#
# Fetch a list of VMs matching the given name
#
# @param [String] name VM Name to filter from
#
# @return [Array] list of matching VMs
#
def get_vm(name)
uri = URI("https://#{vcenter}/rest/vcenter/vm?filter.names=#{name}")
request = Net::HTTP::Get.new(uri)
request['vmware-api-session-id'] = @session_id
@logger.info("Requesting information of VM: #{name}")
response = https_request(uri) {|http| http.request(request) }
vms = JSON.parse(response.body)['value'] # list of vms matching name
if vms.empty?
@logger.error("VM '#{name}' not found")
return
end
@logger.info("Found VMs matching name '#{name}'")
@logger.debug(vms)
return vms.first
end
#
# Looks up VM tags for a given VM ID
#
# @param [String] vm_id Virtual Machine Identifier
#
# @return [String] Tag Name
#
def get_vm_tags(vm_id)
uri = URI("https://#{vcenter}/rest/com/vmware/cis/tagging/tag-association?~action=list-attached-tags")
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = {
'object_id' => {
'id' => vm_id,
'type' => 'VirtualMachine'
}
}.to_json
@logger.info("Reading tags from VM ID: #{vm_id}")
tag_ids = session_https_request(request, uri)
tag_ids.map do |id|
tag = tag_info(id)
name = tag['name']
@logger.debug("Resolved tag ID #{id} to name '#{name}'")
name
end
end
#
# Returns the tag information of a given Tag ID
#
# @param [Int] id Tag ID
#
# @return [Hash] Tag information
#
def tag_info(id)
uri = URI("https://#{vcenter}/rest/com/vmware/cis/tagging/tag/id:#{id}")
request = Net::HTTP::Get.new(uri)
@logger.debug("Fetching tag info for tag ID: #{id}")
tag_info = session_https_request(request, uri)
@logger.debug(tag_info)
return tag_info
end
private
#
# Creates a new session ID in vCenter
#
# @param [String] vcenter endpoint where vCenter is reachable
# @param [String] vuser Username
# @param [String] vpass Password
#
# @return [String] Session ID
#
def open_session(vcenter, vuser, vpass)
uri = URI("https://#{vcenter}/rest/com/vmware/cis/session")
request = Net::HTTP::Post.new(uri)
request.basic_auth(vuser, vpass)
@logger.debug("Opening session for user: #{vuser}")
response = https_request(uri) {|http| http.request(request) }
session_id = JSON.parse(response.body)['value']
@logger.info("Established a connection to #{@vcenter}")
return session_id
end
def session_https_request(request, uri)
@logger.debug(request.body)
request['vmware-api-session-id'] = @session_id
response = https_request(uri) {|http| http.request(request) }
JSON.parse(response.body)['value']
end
def https_request(uri, &block)
response = Net::HTTP.start(uri.hostname, uri.port,
:use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE,
&block)
if response.code.to_i >= 400
@logger.error("HTTP #{response.code} from #{uri}: #{response.body}")
raise "HTTP error #{response.code}"
end
return response
end
end