Attribut vcenter_tag

Can someone explain me why this attribute does not work for me, when I create a VM with this attribute it is cloned in Vcenter and then deleted because vcenter already knows the name.

VCENTER_TAG = [
CATEGORY_NAME = “Kunden”,
DESCRIPTION = “Tag description”,
NAME = “KundeA” ]

1 Like

Could you send us the virtual machine template you are trying to create?

If you use a new pair of NAME and CATEGORY_NAME, does it work for you? Or does this error only occur when both already exist?

Hello @cherrera,

here is the Template.

CONTEXT = [
  NETWORK = "YES",
  SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]" ]
CPU = "1"
DESCRIPTION = "vCenter Template imported by OpenNebula from Cluster Cluster1"
GRAPHICS = [
  LISTEN = "0.0.0.0",
  TYPE = "vnc" ]
VCENTER_TAG = [
  CATEGORY_NAME = "Kunden",
  DESCRIPTION = "Tag description",
  NAME = "KundeA" ]
HYPERVISOR = "vcenter"
MEMORY = "2048"
VCENTER_CCR_REF = "domain-c7"
VCENTER_INSTANCE_ID = "2c3d2743-2327-4128-9648-846a2b7f43c9"
VCENTER_TEMPLATE_NAME = "Debian-Temp"
VCENTER_TEMPLATE_REF = "vm-102"
VCENTER_VM_FOLDER = "Firmen/KundeL"
VCPU = "1"

Tried both, with tags exist and not exist.

Maybe you can work with this log:

Thu Feb 3 10:58:29 2022 [Z0][VMM][I]: Command execution failed (exit code: 255): /var/lib/one/remotes/vmm/vcenter/deploy '/var/lib/one/vms/39/deployment.0' 'Cluster1' 39 Cluster1
Thu Feb 3 10:58:29 2022 [Z0][VMM][E]: deploy: Deploy of VM 39 on vCenter cluster Cluster1 with /var/lib/one/vms/39/deployment.0 failed due to "uninitialized constant VCenterDriver::RESTClient::VmwareDistributedVirtualSwitch"on attempt #0.
Thu Feb 3 10:58:29 2022 [Z0][VMM][E]: deploy: Deploy of VM 39 on vCenter cluster Cluster1 with /var/lib/one/vms/39/deployment.0 failed due to "uninitialized constant VCenterDriver::RESTClient::VmwareDistributedVirtualSwitch"on attempt #1.
Thu Feb 3 10:58:29 2022 [Z0][VMM][E]: deploy: Deploy of VM 39 on vCenter cluster Cluster1 with /var/lib/one/vms/39/deployment.0 failed due to "uninitialized constant VCenterDriver::RESTClient::VmwareDistributedVirtualSwitch"on attempt #2.
Thu Feb 3 10:58:30 2022 [Z0][ReM][D]: Req:6032 UID:0 IP:127.0.0.1 one.vmpool.info invoked , -2, -1, -1, -1, ""
Thu Feb 3 10:58:30 2022 [Z0][ReM][D]: Req:6032 UID:0 one.vmpool.info result SUCCESS, "<VM_POOL><VM><ID>39<..."
Thu Feb 3 10:58:31 2022 [Z0][ReM][D]: Req:6432 UID:0 IP:127.0.0.1 one.hostpool.info invoked , -2, -1, -1
Thu Feb 3 10:58:31 2022 [Z0][ReM][D]: Req:6432 UID:0 one.hostpool.info result SUCCESS, "<HOST_POOL><HOST><ID..."
Thu Feb 3 10:58:31 2022 [Z0][ReM][D]: Req:4016 UID:0 IP:127.0.0.1 one.host.info invoked , 3, false
Thu Feb 3 10:58:31 2022 [Z0][ReM][D]: Req:4016 UID:0 one.host.info result SUCCESS, "<HOST><ID>3</ID><NAM..."
Thu Feb 3 10:58:31 2022 [Z0][VMM][I]: Successfully execute network driver operation: clean.
Thu Feb 3 10:58:31 2022 [Z0][VMM][I]: Failed to execute virtualization driver operation: deploy.
Thu Feb 3 10:58:31 2022 [Z0][IPM][D]: Message received: DEPLOY FAILURE 39 -

Thanks for the info.

In the logs you send us

failed due to "uninitialized constant VCenterDriver::RESTClient::VmwareDistributedVirtualSwitch"on attempt

Seems like this line is failing.

Could you confirm if you have this file?

/usr/lib/one/ruby/vcenter_driver/rest_client.rb

Yes, i have this file.

# -------------------------------------------------------------------------- #
# Copyright 2002-2021, 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

What ruby version are you using in your system ? And what Operative System?

Hi @cherrera,

Operative Sytsem = Ubuntu 20.04
Ruby = ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]