class VCenterDriver::VmImporter

Class VmImporter

Public Class Methods

new(one_client, vi_client) click to toggle source
Calls superclass method VCenterDriver::VcImporter.new
# File lib/vm_template.rb, line 1910
def initialize(one_client, vi_client)
    super(one_client, vi_client)
    @one_class = OpenNebula::Template

    @defaults = {
        :linked_clone => '0',
        :copy => '0',
        :name => '',
        :folder => '',
        :resourcepool => [],
        :type => ''
    }
end

Public Instance Methods

attr() click to toggle source
# File lib/vm_template.rb, line 2113
def attr
    'TEMPLATE/VCENTER_TEMPLATE_REF'
end
get_list(_args = {}) click to toggle source
# File lib/vm_template.rb, line 1924
def get_list(_args = {})
    dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)

    # Get OpenNebula's templates pool
    tpool =
        VCenterDriver::VIHelper
        .one_pool(
            OpenNebula::TemplatePool,
            false
        )
    if tpool.respond_to?(:message)
        raise "Could not get OpenNebula TemplatePool: #{tpool.message}"
    end

    @list = dc_folder.get_unimported_templates(@vi_client, tpool)
end
import(selected) click to toggle source
# File lib/vm_template.rb, line 1961
def import(selected)
    opts = @info[selected[:ref]][:opts]
    working_template = selected

    vcenter = selected[:vcenter]
    vc_uuid = selected[:vcenter_instance_uuid]
    dc      = selected[:dc_name]

    linked_clone     = opts[:linked_clone] == '1'
    copy             = opts[:copy] == '1'
    deploy_in_folder = !opts[:folder].empty?

    res = { :id => [], :name => selected[:name] }
    dpool, ipool, npool, hpool = create_pools

    template =
        VCenterDriver::Template
        .new_from_ref(
            selected[:vcenter_ref],
            @vi_client
        )
    # Linked clones and copy preparation
    if linked_clone
        # reached this point we need to delete
        # the template if something go wrong
        if copy
            error, template_copy_ref =
                selected[:template].save_as_linked_clones(opts[:name])

            unless template_copy_ref
                raise 'There is a problem creating ' \
                      "your copy: #{error}"
            end

            template =
                VCenterDriver::Template
                .new_from_ref(
                    template_copy_ref,
                    @vi_client
                )
            @rollback <<
                Raction
                .new(
                    template,
                    :delete_template
                )

            one_template =
                VCenterDriver::Template
                .get_xml_template(
                    template,
                    vc_uuid,
                    @vi_client,
                    dc
                )
            unless one_template
                raise 'There is a problem obtaining info '\
                      "from your template's copy"
            end

            working_template = one_template
        end

        lc_error, use_lc = template.create_delta_disks
        if lc_error
            raise 'Something was wront with create \
            delta disk operation'
        end

        if use_lc
            working_template[:one] <<
                "\nVCENTER_LINKED_CLONES=\"YES\"\n"
        end
    end

    if deploy_in_folder
        working_template[:one] <<
            "VCENTER_VM_FOLDER=\"#{opts[:folder]}\"\n"
    end

    working_template[:one] <<
        "VCENTER_TEMPLATE_NAME=\"#{selected[:name]}\"\n"

    unless copy
        create(working_template[:one]) do |one_object, id|
            res[:id] << id

            type = { :object => 'template', :id => id }
            error, template_disks, allocated_images =
                template
                .import_vcenter_disks(
                    vc_uuid,
                    dpool,
                    ipool,
                    type
                )

            if allocated_images
                # rollback stack
                allocated_images.reverse.each do |i|
                    @rollback.unshift(Raction.new(i, :delete))
                end
            end
            raise error unless error.empty?

            working_template[:one] << template_disks

            if template_copy_ref
                template_moref = template_copy_ref
            else
                template_moref = selected[:vcenter_ref]
            end

            opts_nics = {
                :vi_client => @vi_client,
                :vc_uuid => vc_uuid,
                :npool => npool,
                :hpool => hpool,
                :vcenter => vcenter,
                :template_moref => template_moref,
                :vm_object => nil
            }

            error, template_nics, _ar_ids, allocated_nets =
                template
                .import_vcenter_nics(
                    opts_nics,
                    id,
                    dc
                )

            if allocated_nets
                # rollback stack
                allocated_nets.reverse.each do |n|
                    @rollback.unshift(Raction.new(n, :delete))
                end
            end
            raise error unless error.empty?

            working_template[:one] << template_nics
            working_template[:one] << rp_opts(
                opts[:type],
                opts[:resourcepool]
            )

            one_object.update(working_template[:one])
        end
    end

    res
end
rp_opts(type, rps) click to toggle source
# File lib/vm_template.rb, line 1941
def rp_opts(type, rps)
    str = ''

    return str if (type == 'default') || rps.empty?

    if type == 'fixed'
        str << "VCENTER_RESOURCE_POOL=\"#{rps}\"\n"
    else
        default = rps.first
        rps_str = rps.join(',')

        str << 'USER_INPUTS=['
        str << "VCENTER_RESOURCE_POOL=\"M|list|resource \
        pool list|#{rps_str}|#{default}\""
        str << ']'
    end

    str
end