F #3912: Add support for vCenter tags (#4531)

* Added REST client for vSphere
* Added support for vCenter tags
This commit is contained in:
Carlos J. Herrera authored and GitHub committed 2020-04-21 17:12:41 +02:00
1 parent 6d9c0d14d3
commit 231ef5f1ef
5 files changed
+228

No files matched your search

+1
View File
@@ -957,6 +957,7 @@ VMM_EXEC_LIB="src/vmm_mad/remotes/lib/command.rb"
VMM_EXEC_LIB_VCENTER_FILES="src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb \
src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb \
src/vmm_mad/remotes/lib/vcenter_driver/rest_client.rb \
src/vmm_mad/remotes/lib/vcenter_driver/vcenter_importer.rb \
src/vmm_mad/remotes/lib/vcenter_driver/file_helper.rb \
src/vmm_mad/remotes/lib/vcenter_driver/host.rb \
@@ -0,0 +1,201 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'openssl'
require 'yaml'
require 'vsphere-automation-vcenter'
require 'vsphere-automation-cis'
module VCenterDriver
########################################################################
# Class RESTClient
########################################################################
class RESTClient
attr_accessor :configuration
def initialize(opts)
@opts = {
:insecure => true,
:associable_types => [
ClusterComputeResource,
DistributedVirtualSwitch,
VmwareDistributedVirtualSwitch,
LibraryItem,
ResourcePool,
Folder,
HostNetwork,
DistributedVirtualPortgroup,
VirtualApp,
StoragePod,
Datastore,
Network,
Datacenter,
Library,
HostSystem,
OpaqueNetwork,
VirtualMachine
],
:category_name => 'OpenNebula',
:category_description => 'OpenNebula Category',
:cardinality =>
VSphereAutomation::CIS::CisTaggingCategoryModelCardinality
.const_get(
'multiple'.upcase
)
}.merge(opts)
@configuration = VSphereAutomation::Configuration.new.tap do |c|
c.host = @opts[:hostname]
c.username = @opts[:username]
c.password = @opts[:password]
c.scheme = 'https'
c.verify_ssl = !@opts[:insecure]
c.verify_ssl_host = !@opts[:insecure]
end
end
def self.new_from_host(host_id)
begin
client = OpenNebula::Client.new
host = OpenNebula::Host.new_with_id(host_id, client)
rc = host.info(true)
if OpenNebula.is_error?(rc)
raise "Could not get host info for ID: \
#{host_id} - #{rc.message}"
end
connection = {
:hostname => host['TEMPLATE/VCENTER_HOST'],
:username => host['TEMPLATE/VCENTER_USER'],
:password => host['TEMPLATE/VCENTER_PASSWORD']
}
new(connection)
rescue StandardError => e
raise e
end
end
def get_or_create_tag(
api_client,
category_id,
tag_name,
tag_description
)
tag_api = VSphereAutomation::CIS::TaggingTagApi.new(api_client)
tag = tag_api.list.value.find do |id|
c = tag_api.get(id).value
break c if c.name == tag_name
end
if tag.nil?
create_spec =
VSphereAutomation::CIS::CisTaggingTagCreateSpec
.new(
name => tag_name,
description => tag_description,
category_id => category_id
)
create_model = VSphereAutomation::CIS::CisTaggingTagCreate.new(
create_spec => create_spec
)
api_instance =
VSphereAutomation::CIS::TaggingTagApi.new(api_client)
api_instance.create(create_model).value
else
tag.id
end
end
def get_or_create_category(api_client, category_name)
category_api =
VSphereAutomation::CIS::TaggingCategoryApi.new(api_client)
category = category_api.list.value.find do |id|
c = category_api.get(id).value
break c if c.name == category_name
end
if category.nil?
create_spec =
VSphereAutomation::CIS::CisTaggingCategoryCreateSpec
.new(
name => category_name,
description => @opts[:category_description],
associable_types => @opts[:associable_types],
cardinality => @opts[:cardinality]
)
create_model =
VSphereAutomation::CIS::CisTaggingCategoryCreate
.new(create_spec => create_spec)
category = category_api.create(create_model)
category.value
else
category.id
end
end
def sync_tags(vm)
api_client = VSphereAutomation::ApiClient.new(@configuration)
VSphereAutomation::CIS::SessionApi.new(api_client).create('')
association_api =
VSphereAutomation::CIS::TaggingTagAssociationApi
.new(api_client)
vm.vcenter_tags.each do |tag|
category_name = @opts[:category_name]
unless tag['CATEGORY_NAME'].nil?
category_name = tag['CATEGORY_NAME']
end
category_id = get_or_create_category(api_client, category_name)
tag_name = tag['NAME']
tag_description = tag['DESCRIPTION']
tag_id =
get_or_create_tag(
api_client,
category_id,
tag_name,
tag_description
)
request_body =
VSphereAutomation::CIS::CisTaggingTagAssociationAttach.new
object_id = VSphereAutomation::CIS::VapiStdDynamicID.new
object_id.id = vm['_ref']
object_id.type = 'VirtualMachine'
request_body.object_id = object_id
begin
association_api.attach(tag_id, request_body)
rescue VSphereAutomation::ApiError => e
puts "Exception when calling \
TaggingTagAssociationApi->attach: #{e}"
end
end
end
end
end
@@ -365,6 +365,17 @@ module VCenterDriver
vm_prefix + one_item['NAME'] + vm_suffix
end
# @return vCenter Tags
def vcenter_tags
one_item.info if one_item.instance_of?(OpenNebula::VirtualMachine)
one_item.retrieve_xmlelements("USER_TEMPLATE/VCENTER_TAG")
end
# @return if has vCenter Tags
def vcenter_tags?
vcenter_tags.size > 0
end
############################################################################
# Create and reconfigure VM related methods
############################################################################
+9
View File
@@ -57,6 +57,10 @@ begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
if drv_action.retrieve_xmlelements("USER_TEMPLATE/VCENTER_TAG").size > 0
raise "vShepre automation gems are not installed" unless REST_CLIENT
end
if deploy_id_valid?(deploy_id)
# VM is not new, we just need to reconfigure it and to power it on
vm = VCenterDriver::VirtualMachine.new_one(vi_client, deploy_id, one_vm)
@@ -77,6 +81,11 @@ begin
vm_template.lock(1)
end
if vm.vcenter_tags?
rest_client = VCenterDriver::RESTClient.new_from_host(host_id)
rest_client.sync_tags(vm)
end
end
if vm.is_powered_off?
@@ -83,6 +83,12 @@ require 'resolv'
require 'vcenter_importer.rb'
require 'memoize'
require 'vi_client'
begin
require 'rest_client'
REST_CLIENT=true
rescue LoadError
REST_CLIENT=false
end
require 'vi_helper'
require 'datacenter'
require 'host'