1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

F #3440: Improve NSX import process (#3796)

* M #-: Fix errors on commit 7b8438dd4

Change condition
Change definition function order

* F #3440: NSX parameters at import time

These attributes are added when importing a NSX network
NSX_ID
NSX_VNI
NSX_TZ_ID

Also now there is a different VCENTER_PORTGROUP_TYPE for
distributed port groups and NSX-V portgroups
This commit is contained in:
Angel Luis Moya Gonzalez 2019-10-03 13:58:40 +02:00 committed by Tino Vázquez
parent 39e3562624
commit 19b255e0e3
5 changed files with 132 additions and 42 deletions

View File

@ -43,6 +43,34 @@ require 'vcenter_driver'
require 'base64'
require 'nsx_driver'
# FUNCTIONS
def update_net(vnet, content)
vnet.unlock
rc = vnet.update(content, true)
vnet.lock(1)
raise 'Could not update the virtual network' if OpenNebula.is_error?(rc)
end
# waits for a vlan_id attribute to be generated
# only if automatic_vlan activated
def wait_vlanid(vnet)
retries = 5
i = 0
while vnet['VLAN_ID'].nil?
raise 'cannot get vlan_id' if i >= retries
sleep 1
i += 1
vnet.info
end
end
def err_and_exit(error_message)
STDERR.puts error_message
exit(-1)
end
# Constants
SUCCESS_XPATH = '//PARAMETER[TYPE="OUT" and POSITION="1"]/VALUE'
ERROR_XPATH = '//PARAMETER[TYPE="OUT" and POSITION="2"]/VALUE'
@ -71,11 +99,13 @@ err_and_exit(rc.message) if OpenNebula.is_error?(one_vnet.info)
managed = one_vnet['TEMPLATE/OPENNEBULA_MANAGED'] != 'NO'
imported = one_vnet['TEMPLATE/VCENTER_IMPORTED']
if one_vnet['VN_MAD'] == 'vcenter' && managed && imported.nil?
unless one_vnet['VN_MAD'] == 'vcenter' && managed && imported.nil?
STDOUT.puts 'Network present in vCenter, no actions taken. Exiting'
exit(0)
end
begin
esx_rollback = [] # Track hosts that require a rollback
@ -129,11 +159,14 @@ begin
<guestVlanAllowed>false</guestVlanAllowed>\
</virtualWireCreateSpec>"
logical_switch = NSXDriver::VirtualWire
.new(nsx_client, nil, tz_id, virtual_wire_spec)
.new(nsx_client, nil, tz_id, virtual_wire_spec)
# Get reference will have in vcenter and vni
vnet_ref = logical_switch.ls_vcenter_ref
ls_vni = logical_switch.ls_vni
net_info = "NSX_ID=\"#{logical_switch.ls_id}\"\nNSX_VNI=\"#{ls_vni}\"\n"
ls_name = logical_switch.ls_name
net_info = "NSX_ID=\"#{logical_switch.ls_id}\"\n"
net_info << "NSX_VNI=\"#{ls_vni}\"\n"
net_info << "BRIDGE=\"#{ls_name}\"\n"
end
if pg_type == VCenterDriver::Network::NETWORK_TYPE_NSXT
@ -148,12 +181,14 @@ begin
}
)
logical_switch = NSXDriver::OpaqueNetwork
.new(nsx_client, nil, nil, opaque_network_spec)
.new(nsx_client, nil, nil, opaque_network_spec)
# Get NSX_VNI
vnet_ref = dc.nsx_network(logical_switch.ls_id, pg_type)
ls_vni = logical_switch.ls_vni
net_info << "NSX_ID=\"#{logical_switch.ls_id}\"\n"
ls_name = logical_switch.ls_name
net_info = "NSX_ID=\"#{logical_switch.ls_id}\"\n"
net_info << "NSX_VNI=\"#{ls_vni}\"\n"
net_info << "BRIDGE=\"#{ls_name}\"\n"
end
# With DVS we have to work at datacenter level and then for each host
@ -350,30 +385,3 @@ ensure
one_vnet.unlock
vi_client.close_connection if vi_client
end
# waits for a vlan_id attribute to be generated
# only if automatic_vlan activated
def wait_vlanid(vnet)
retries = 5
i = 0
while vnet['VLAN_ID'].nil?
raise 'cannot get vlan_id' if i >= retries
sleep 1
i += 1
vnet.info
end
end
def update_net(vnet, content)
vnet.unlock
rc = vnet.update(content, true)
vnet.lock(1)
raise 'Could not update the virtual network' if OpenNebula.is_error?(rc)
end
def err_and_exit(error_message)
STDERR.puts error_message
exit(-1)
end

View File

@ -32,19 +32,32 @@ module NSXDriver
if ls_id
initialize_with_id(ls_id)
else
if ls_data
@ls_id = new_logical_switch(ls_data)
# Construct URL of the created logical switch
@url_ls = @base_url + SECTION_LS + @ls_id
@ls_vni = ls_vni
@ls_name = ls_name
@tz_id = ls_tz
@admin_display = 'UP'
if tz_id
if ls_data
@ls_id = new_logical_switch(ls_data)
# Construct URL of the created logical switch
@url_ls = @base_url + SECTION_LS + @ls_id
@ls_vni = ls_vni
@ls_name = ls_name
@tz_id = ls_tz
@admin_display = 'UP'
end
raise 'Missing logical switch data' unless ls_data
end
raise 'Missing logical switch data' unless ls_data
end
end
# Creates a NSXDriver::VirtualWire from its name
def self.new_from_name(nsx_client, ls_name)
lswitch = new(nsx_client)
ls_id = lswitch.ls_id_from_name(nsx_client, ls_name)
raise "Logical Switch with name: #{ls_name} not found" unless ls_id
# initialize_with_id(@ls_id)
lswitch.initialize_with_id(ls_id)
lswitch
end
# Creates a NSXDriver::OpaqueNetwork from its id
def initialize_with_id(ls_id)
@ls_id = ls_id
@ -53,11 +66,26 @@ module NSXDriver
if ls?
@ls_vni = ls_vni
@ls_name = ls_name
@tz_id = ls_tz
@admin_display = 'UP'
end
raise "Logical switch with id: #{ls_id} not found" unless ls?
end
# Get the logical switch id from its name
def ls_id_from_name(nsx_client, name)
url = @base_url + SECTION_LS
lswitches = nsx_client.get_json(url)['results']
lswitches.each do |lswitch|
lsname = lswitch['display_name']
lsid = lswitch['id']
if lsname == name
return lsid if lsid
end
end
nil
end
# METHODS
# Check if logical switch exists
def ls?

View File

@ -25,6 +25,7 @@ module NSXDriver
BACKING_XPATH = '//virtualWire/vdsContextWithBacking/backingValue'
OBJECTID_XPATH = '//virtualWire/vdsContextWithBacking/switch/objectId'
TZ_XPATH = '//virtualWire/vdnScopeId'
VW_XPATH = '//virtualWire'
SECTION_LS = '/vdn/virtualwires/'
SECTION_TZ = '/vdn/scopes/'
@ -53,6 +54,17 @@ module NSXDriver
end
end
# Creates a NSXDriver::VirtualWire from its name
def self.new_from_name(nsx_client, ls_name)
virtualwire = new(nsx_client)
ls_id = virtualwire.ls_id_from_name(nsx_client, ls_name)
raise "VirtualWire with name: #{ls_name} not found" unless ls_id
# initialize_with_id(@ls_id)
virtualwire.initialize_with_id(ls_id)
virtualwire
end
# Creates a NSXDriver::VirtualWire from its id
def initialize_with_id(ls_id)
@ls_id = ls_id
@ -68,6 +80,22 @@ module NSXDriver
raise "VirtualWire with id: #{ls_id} not found" unless ls?
end
# Get the logical switch id from its name
def ls_id_from_name(nsx_client, name)
url = @base_url + SECTION_LS
virtualwires = nsx_client.get_xml(url).xpath(VW_XPATH)
virtualwires.each do |virtualwire|
lsname_arr = name.split(/-sid-/)
lsname = lsname_arr[-1].split('-', 2)[-1]
lsid = lsname_arr[0].split(/vxw-dvs-\w.-/)[-1]
if virtualwire.xpath('name').text == lsname
return virtualwire.xpath('objectId').text \
if virtualwire.xpath('objectId').text == lsid
end
end
nil
end
# METHODS
# Check if logical switch exists

View File

@ -376,7 +376,12 @@ class DatacenterFolder
networks[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) || r.obj.is_a?(RbVmomi::VIM::Network) || r.obj.is_a?(RbVmomi::VIM::OpaqueNetwork)
if r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup)
networks[r.obj._ref][:network_type] = VCenterDriver::Network::NETWORK_TYPE_DPG
# Here can be NETWORK_TYPE_DPG or NETWORK_TYPE_NSXV
if r['name'].match(/^vxw-dvs-(.*)-virtualwire-(.*)-sid-(.*)/)
networks[r.obj._ref][:network_type] = VCenterDriver::Network::NETWORK_TYPE_NSXV
else
networks[r.obj._ref][:network_type] = VCenterDriver::Network::NETWORK_TYPE_DPG
end
elsif r.obj.is_a?(RbVmomi::VIM::OpaqueNetwork)
networks[r.obj._ref][:network_type] = VCenterDriver::Network::NETWORK_TYPE_NSXT
elsif r.obj.is_a?(RbVmomi::VIM::Network)

View File

@ -414,6 +414,27 @@ class NetImporter < VCenterDriver::VcImporter
net = VCenterDriver::Network.new_from_ref(selected[:ref], @vi_client)
vid = VCenterDriver::Network.retrieve_vlanid(net.item) if net
# 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)