1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-29 18:50:08 +03:00

B : Fixes and improvements over NSX ()

This commit is contained in:
Angel Luis Moya Gonzalez 2019-11-19 17:35:38 +01:00 committed by Tino Vázquez
parent e1a6bf51cf
commit 43f159201b
10 changed files with 232 additions and 45 deletions

@ -864,7 +864,7 @@ VMM_EXEC_LIB_NSX_FILES="src/vmm_mad/remotes/lib/nsx_driver/logical_switch.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsxv_client.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsx_component.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsx_constants.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsx_exception.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsx_error.rb \
src/vmm_mad/remotes/lib/nsx_driver/opaque_network.rb \
src/vmm_mad/remotes/lib/nsx_driver/transport_zone.rb \
src/vmm_mad/remotes/lib/nsx_driver/nsxt_tz.rb \

@ -64,8 +64,9 @@ module NSXDriver
when NSXDriver::NSXConstants::NSXV
NSXDriver::NSXVClient.new(nsxmgr, nsx_user, nsx_password)
else
raise NSXDriver::NSXException::UnknownObject, \
'Unknown object type'
error_msg = "Unknown object type: #{type}"
error = NSXDriver::NSXError::UnknownObject.new(error_msg)
raise error
end
end
@ -90,8 +91,11 @@ module NSXDriver
# METHODS
def check_response(response, code)
response.code.to_i == code
def check_response(response, codes_array)
codes_array.each do |code|
return true if response.code.to_i == code
end
false
end
def self.nsx_pass(nsx_pass_enc)
@ -108,10 +112,13 @@ module NSXDriver
.decrypt(nsx_pass_enc, token)
end
# Return: respose.body
def get(url); end
# Return: id of the created object
def post(url, ls_data); end
def post(url, data); end
def put(url, data); end
def delete(url); end

@ -66,6 +66,14 @@ module NSXDriver
# OpaqueNetwork
NSXT_AUTH = NSXT_BASE + '/aaa/registration-token'
NSXT_LS_SECTION = NSXT_BASE + '/logical-switches/'
# DFW
ONE_SECTION_NAME = 'OpenNebula'
NSXT_DFW_BASE = NSXT_BASE + '/firewall'
NSXV_DFW_BASE = '/api/4.0/firewall/globalroot-0/config'
NSXT_DFW_SECTIONS = '/sections'
NSXV_DFW_SECTIONS = '/layer3sections'
NSXV_DFW_SECTION_XPATH = '//section'
NSXV_DFW_RULE_XPATH = '//rule'
# Messages
MSG_INCOMPLETE_REQ = 'Incomplete request, NSX_MANAGER, NSX_USER, \
NSX_PASSWORD and NSX_TYPE are needed'
@ -73,6 +81,24 @@ module NSXDriver
NSX_PASSWORD and NSX_TYPE are correct'
MSG_INVALID_NSXTYPE = 'Invalid NSX-TYPE: Only NSX-T and NSX-V are \
supported'
# Responses codes
# 2xx
CODE_OK = 200
CODE_CREATED = 201
CODE_ACCEPTED = 202
CODE_NO_CONTENT = 204
# 4xx
CODE_BAD_REQUEST = 400
CODE_UNAUTHORIZED = 401
CODE_FORBIDDEN = 403
CODE_NOT_FOUND = 404
CODE_METHOD_NOT_ALLOWED = 405
CODE_NOT_ACCEPTABLE = 406
# 5xx
CODE_INTERNAL_SERVER_ERROR = 500
CODE_BAD_GATEWAY = 502
CODE_SERVICE_UNAVAILABLE = 503
CODE_GATEWAY_TIMEOUT = 504
end

@ -15,13 +15,53 @@
#--------------------------------------------------------------------------- #
module NSXDriver
class NSXException < StandardError
class NSXError < StandardError
class IncorrectResponseCodeError < NSXException; end
# Class IncorrectResponseCodeError
class IncorrectResponseCodeError < NSXError
class LogicalSwitchNotFound < NSXException; end
def initialize(msg = 'Incorrect response code')
super(msg)
end
end
# Class ObjectNotFound
class ObjectNotFound < NSXError
def initialize(msg = 'Object not found')
super(msg)
end
end
# Class UnknownObject
class UnknownObject < NSXError
def initialize(msg = 'Unknown object type')
super(msg)
end
end
# Class CreateError
class CreateError < NSXError
def initialize(msg = 'Error creating NSX object')
super(msg)
end
end
# Class DeleteError
class DeleteError < NSXError
def initialize(msg = 'Error deleting NSX object')
super(msg)
end
end
class UnknownObject < NSXException; end
end

@ -69,15 +69,36 @@ module NSXDriver
raise e
end
return JSON.parse(response.body) \
if check_response(response, 200)
if check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
def get_full_response(url)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Get.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_JSON)
request.basic_auth(@nsx_user, @nsx_password)
begin
response = Net::HTTP
.start(uri.host,
uri.port,
:use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE)\
do |https|
https.request(request)
end
rescue StandardError => e
raise e
end
return response \
if check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
# Return: id of the created object
def post(url, ls_data)
def post(url, data)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Post.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_JSON)
request.body = ls_data
request.body = data
request.basic_auth(@nsx_user, @nsx_password)
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
@ -86,13 +107,42 @@ module NSXDriver
response_json = JSON.parse(response.body)
# If response is different as expected raise the message
unless check_response(response, 201)
nsx_error = "\nNSX error code: " \
unless check_response(response,
[NSXDriver::NSXConstants::CODE_CREATED])
error_msg = "\nNSX error code: " \
"#{response_json['errorCode']}, " \
"\nNSX error details: " \
"#{response_json['details']}"
raise NSXDriver::NSXException::IncorrectResponseCodeError,
nsx_error
error = NSXDriver::NSXError::IncorrectResponseCodeError
.new(error_msg)
raise error
end
response_json['id']
end
def put(url, data)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Put.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_JSON)
request.body = data
request.basic_auth(@nsx_user, @nsx_password)
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
https.request(request)
end
response_json = JSON.parse(response.body)
# If response is different as expected raise the message
unless check_response(response,
[NSXDriver::NSXConstants::CODE_CREATED])
error_msg = "\nNSX error code: " \
"#{response_json['errorCode']}, " \
"\nNSX error details: " \
"#{response_json['details']}"
error = NSXDriver::NSXError::IncorrectResponseCodeError
.new(error_msg)
raise error
end
response_json['id']
@ -108,7 +158,7 @@ module NSXDriver
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
https.request(request)
end
check_response(response, 200)
check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
def get_token(url)
@ -121,7 +171,8 @@ module NSXDriver
https.request(request)
end
return unless check_response(response, 200)
return unless check_response(response,
[NSXDriver::NSXConstants::CODE_OK])
response.body
end

@ -68,15 +68,37 @@ module NSXDriver
rescue StandardError => e
raise e
end
return Nokogiri::XML response.body if check_response(response, 200)
return Nokogiri::XML response.body \
if check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
def get_full_response(url)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Get.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_XML)
request.basic_auth(@nsx_user, @nsx_password)
begin
response = Net::HTTP
.start(uri.host,
uri.port,
:use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE)\
do |https|
https.request(request)
end
rescue StandardError => e
raise e
end
return response \
if check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
# Return: id of the created object
def post(url, ls_data)
def post(url, data)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Post.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_XML)
request.body = ls_data
request.body = data
request.basic_auth(@nsx_user, @nsx_password)
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
@ -84,13 +106,40 @@ module NSXDriver
end
# If response is different as expected raise the message
unless check_response(response, 201)
unless check_response(response,
[NSXDriver::NSXConstants::CODE_CREATED])
response_json = JSON.parse(response.body)
nsx_error = "\nNSX error code: " \
"#{response_json['errorCode']}, " \
"\nNSX error details: " \
"#{response_json['details']}"
raise NSXDriver::NSXException::IncorrectResponseCodeError, \
raise NSXDriver::NSXError::IncorrectResponseCodeError, \
nsx_error
end
response.body
end
def put(url, data)
uri = URI.parse(@nsxmgr + url)
request = Net::HTTP::Put.new(uri.request_uri,
NSXDriver::NSXConstants::HEADER_XML)
request.body = data
request.basic_auth(@nsx_user, @nsx_password)
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
https.request(request)
end
# If response is different as expected raise the message
unless check_response(response,
[NSXDriver::NSXConstants::CODE_CREATED])
response_json = JSON.parse(response.body)
nsx_error = "\nNSX error code: " \
"#{response_json['errorCode']}, " \
"\nNSX error details: " \
"#{response_json['details']}"
raise NSXDriver::NSXError::IncorrectResponseCodeError, \
nsx_error
end
@ -106,7 +155,7 @@ module NSXDriver
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
https.request(request)
end
check_response(response, 200)
check_response(response, [NSXDriver::NSXConstants::CODE_OK])
end
def get_token(url)
@ -119,7 +168,8 @@ module NSXDriver
https.request(request)
end
return unless check_response(response, 200)
return unless check_response(response,
[NSXDriver::NSXConstants::CODE_OK])
response_xml = Nokogiri::XML response.body
token = response_xml.xpath('//authToken/value').text

@ -31,7 +31,7 @@ module NSXDriver
if ls_data
begin
@ls_id = new_logical_switch(ls_data)
rescue NSXDriver::NSXException::
rescue NSXDriver::NSXError::
IncorrectResponseCodeError => e
raise 'Opaque Network not created in ' \
"NSX Manager: #{e.message}"
@ -53,13 +53,15 @@ module NSXDriver
end
end
# Creates a NSXDriver::VirtualWire from its name
# Creates a NSXDriver::OpaqueNetwork 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)
unless ls_id
raise NSXDriver::NSXException::LogicalSwitchNotFound, \
"Logical Switch with name: #{ls_name} not found"
error_msg = "Opaque Network with name: #{ls_name} not found"
error = NSXDriver::NSXError::ObjectNotFound
.new(error_msg)
raise error
end
# initialize_with_id(@ls_id)
@ -79,8 +81,10 @@ module NSXDriver
@tz_id = ls_tz
@admin_display = 'UP'
else
raise NSXDriver::NSXException::LogicalSwitchNotFound, \
"Logical switch with id: #{ls_id} not found"
error_msg = "Opaque Network with id: #{ls_id} not found"
error = NSXDriver::NSXError::ObjectNotFound
.new(error_msg)
raise error
end
end

@ -33,8 +33,9 @@ module NSXDriver
when NSXDriver::NSXConstants::NSXV
NSXDriver::NSXVtz.new(nsx_client)
else
raise NSXDriver::NSXException::UnknownObject, \
'Unknown object type'
error_msg = "Unknown object type: #{nsx_client.nsx_type}"
error = NSXDriver::NSXError::UnknownObject.new(error_msg)
raise error
end
end

@ -28,7 +28,7 @@ module NSXDriver
if ls_data
begin
@ls_id = new_logical_switch(ls_data, tz_id)
rescue NSXDriver::NSXException::
rescue NSXDriver::NSXError::
IncorrectResponseCodeError => e
raise 'VirtualWire not created in NSX Manager: ' \
"#{e.message}"
@ -56,8 +56,10 @@ module NSXDriver
virtualwire = new(nsx_client)
ls_id = virtualwire.ls_id_from_name(nsx_client, ls_name)
unless ls_id
raise NSXDriver::NSXException::LogicalSwitchNotFound, \
"VirtualWire with name: #{ls_name} not found"
error_msg = "VirtualWire with name: #{ls_name} not found"
error = NSXDriver::NSXError::ObjectNotFound
.new(error_msg)
raise error
end
# initialize_with_id(@ls_id)
@ -71,14 +73,20 @@ module NSXDriver
# Construct URL of the created logical switch
@url_ls = NSXDriver::NSXConstants::NSXV_LS_SECTION + \
@ls_id
if ls?
@ls_vni = ls_vni
@ls_name = ls_name
@tz_id = ls_tz
@tenant_id = 'virtual wire tenant'
@guest_vlan_allowed = false
# Raise an error if VirtualWire id doesn't exists
unless ls?
error_msg = "VirtualWire with id: #{ls_id} not found"
error = NSXDriver::NSXError::ObjectNotFound
.new(error_msg)
raise error
end
raise "VirtualWire with id: #{ls_id} not found" unless ls?
@ls_vni = ls_vni
@ls_name = ls_name
@tz_id = ls_tz
@tenant_id = 'virtual wire tenant'
@guest_vlan_allowed = false
end
# Get the logical switch id from its name

@ -47,7 +47,7 @@ $LOAD_PATH << LIB_LOCATION + '/ruby/nsx_driver'
# NSX Library #
# ---------------------------------------------------------------------------- #
require 'nsx_constants'
require 'nsx_exception'
require 'nsx_error'
require 'nsx_component'
require 'nsx_client'
require 'nsxt_client'