Class: VCenterDriver::NetImporter

Inherits:
VcImporter show all
Defined in:
lib/network.rb

Overview

Class NetImporter

Instance Attribute Summary

Attributes inherited from VcImporter

#list

Instance Method Summary collapse

Methods inherited from VcImporter

#get_indexes, import_clusters, new_child, #one_str, #output, #retrieve_resources, sanitize, #stdout

Constructor Details

#initialize(one_client, vi_client) ⇒ NetImporter

Returns a new instance of NetImporter.



483
484
485
486
487
# File 'lib/network.rb', line 483

def initialize(one_client, vi_client)
    super(one_client, vi_client)
    @one_class = OpenNebula::VirtualNetwork
    @defaults = { :size => '255', :type => 'ether' }
end

Instance Method Details

#add_cluster(cid, eid) ⇒ Object



593
594
595
596
597
598
# File 'lib/network.rb', line 593

def add_cluster(cid, eid)
    one_cluster = @info[:clusters][cid]
    raise 'no cluster defined' unless one_cluster

    one_cluster.addvnet(eid)
end

#build_ar(opts) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/network.rb', line 612

def build_ar(opts)
    str =  "\nAR=[TYPE=\""
    type = opts[:type].downcase

    case type
    when '4', 'ip4', 'ip'
        str << 'IP4"'
        opts[:ip] = '192.168.1.1' if opts[:ip].empty?
        str << ",IP=\"#{opts[:ip]}\""
    when 'ip6'
        str << 'IP6"'
        if opts[:global_prefix]
            str << ",GLOBAL_PREFIX=\"#{opts[:global_prefix]}\""
        end
        if opts[:ula_prefix]
            str << ",ULA_PREFIX=\"#{opts[:ula_prefix]}\""
        end
    when 'ether', 'e'
        str << 'ETHER"'
    when 'ip6_static'
        str << 'IP6_STATIC"'
        str << ",IP6=\"#{opts[:ip6]}\"" if opts[:ip6]
        if opts[:prefix_length]
            str << ",PREFIX_LENGTH=\"#{opts[:prefix_length]}\""
        end
    end

    str << ",MAC=\"#{opts[:mac]}\"" if opts[:mac]
    str << ",SIZE = \"#{opts[:size]}\"]"

    str
end

#get_list(args = {}) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/network.rb', line 559

def get_list(args = {})
    dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)

    # OpenNebula's VirtualNetworkPool
    npool = VCenterDriver::VIHelper
            .one_pool(
                OpenNebula::VirtualNetworkPool,
                false
            )
    if npool.respond_to?(:message)
        raise 'Could not get ' \
        "OpenNebula VirtualNetworkPool: #{npool.message}"
    end

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

    rs = dc_folder
         .get_unimported_networks(
             npool,
             @vi_client.vc_name,
             hpool,
             args
         )
    @list = rs
end

#import(selected, opts = nil) ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/network.rb', line 645

def import(selected, opts = nil)
    opts = @info[selected[:ref]][:opts] if opts.nil?

    net = VCenterDriver::Network
          .new_from_ref(selected[:ref], @vi_client)
    if net
        vid = VCenterDriver::Network
              .retrieve_vlanid(net.item)
    end

    # If type is NSX we need to update values
    if selected[:type] == VCenterDriver::Network::NETWORK_TYPE_NSXV
        host_id = @vi_client.instance_variable_get '@host_id'
        nsx_client = NSXDriver::NSXClient.new_from_id(host_id)
        nsx_net = NSXDriver::VirtualWire
                  .new_from_name(nsx_client, selected[:name])
        selected[:one] << "NSX_ID=\"#{nsx_net.ls_id}\"\n"
        selected[:one] << "NSX_VNI=\"#{nsx_net.ls_vni}\"\n"
        selected[:one] << "NSX_TZ_ID=\"#{nsx_net.tz_id}\"\n"
    end

    if selected[:type] == VCenterDriver::Network::NETWORK_TYPE_NSXT
        host_id = @vi_client.instance_variable_get '@host_id'
        nsx_client = NSXDriver::NSXClient.new_from_id(host_id)
        nsx_net = NSXDriver::OpaqueNetwork
                  .new_from_name(nsx_client, selected[:name])
        selected[:one] << "NSX_ID=\"#{nsx_net.ls_id}\"\n"
        selected[:one] << "NSX_VNI=\"#{nsx_net.ls_vni}\"\n"
        selected[:one] << "NSX_TZ_ID=\"#{nsx_net.tz_id}\"\n"
    end

    if vid
        vlanid = VCenterDriver::Network.vlanid(vid)

        # we have vlan id
        if /\A\d+\z/.match(vlanid)
            selected[:one] << "VCENTER_VLAN_ID=\"#{vlanid}\"\n"
        end
    end

    selected[:one] << build_ar(opts)
    if opts['selected_clusters']
        selected[:clusters][:one_ids] =
            opts['selected_clusters']
            .each
            .map(&:to_i)
    end

    res = { :id => [], :name => selected[:name] }
    create(selected[:one]) do |_one_object, id|
        res[:id] << id
        add_clusters(id, selected[:clusters][:one_ids])
    end

    res
end

#process_import(indexes, opts = {}, &_block) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/network.rb', line 489

def process_import(indexes, opts = {}, &_block)
    indexes = indexes.gsub(/\s*\,\s*/, ',').strip.split(',')

    dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)
    vcenter_instance_name = @vi_client.vc_name
    vcenter_uuid = @vi_client.vim.serviceContent.about.instanceUuid
    hpool = VCenterDriver::VIHelper.one_pool(
        OpenNebula::HostPool,
        false
    )

    one_client = OpenNebula::Client.new
    one_host = OpenNebula::Host.new_with_id(opts[:host], one_client)

    rc = one_host.info
    raise rc.message if OpenNebula.is_error? rc

    # Get all networks in vcenter cluster (one_host)
    vc_cluster_networks = dc_folder.cluster_networks(one_host)

    vc_cluster_networks_map_ref = {}

    # Iterate over vcenter networks
    vc_cluster_networks.each do |vc_cluster_network|
        vc_cluster_networks_map_ref[vc_cluster_network._ref] =
            vc_cluster_network
    end

    indexes.each do |index|
        begin
            @rollback = []
            @info[index] = {}

            vc_cluster_network = vc_cluster_networks_map_ref[index]

            if hpool.respond_to?(:message)
                raise 'Could not get OpenNebula HostPool: ' \
                      "#{hpool.message}"
            end

            params = {}
            params[:vc_network] = vc_cluster_network
            params[:vcenter_instance_name] = vcenter_instance_name
            params[:vcenter_uuid] = vcenter_uuid
            params[:_hpool] = hpool
            params[:one_host] = one_host
            params[:args] = {}

            selected, _networks_type, _hosts_list, _clusters_list =
                dc_folder.process_network(
                    params,
                    {},
                    {},
                    {}
                )

            selected = selected[index]

            # import the object
            opts[index] ||= @defaults
            @info[:success] << import(selected, opts[index])
        rescue StandardError => e
            @info[:error] << { index => e.message }
            @info[index][:e] = e

            apply_rollback
        end
    end
end

#remove_default(id) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
# File 'lib/network.rb', line 600

def remove_default(id)
    cid = 0
    @info[:clusters][cid] ||=
        VCenterDriver::VIHelper
        .one_item(
            OpenNebula::Cluster,
            cid.to_s,
            false
        )
    @info[:clusters][cid].delvnet(id.to_i)
end