opennebula 5.12.12 → 5.13.80.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +5 -5
  2. data/lib/DriverExecHelper.rb +43 -27
  3. data/lib/OpenNebulaDriver.rb +7 -3
  4. data/lib/VirtualMachineDriver.rb +8 -1
  5. data/lib/cloud/CloudClient.rb +1 -1
  6. data/lib/datacenter.rb +1258 -0
  7. data/lib/datastore.rb +1025 -0
  8. data/lib/distributed_firewall.rb +280 -0
  9. data/lib/file_helper.rb +370 -0
  10. data/lib/host.rb +1517 -0
  11. data/lib/logical_port.rb +50 -0
  12. data/lib/logical_switch.rb +77 -0
  13. data/lib/memoize.rb +74 -0
  14. data/lib/models/role.rb +1126 -0
  15. data/lib/models/service.rb +709 -0
  16. data/lib/models.rb +32 -0
  17. data/lib/network.rb +635 -0
  18. data/lib/nsx_client.rb +144 -0
  19. data/lib/nsx_component.rb +28 -0
  20. data/lib/nsx_constants.rb +149 -0
  21. data/lib/nsx_driver.rb +78 -0
  22. data/lib/nsx_error.rb +77 -0
  23. data/lib/nsx_rule.rb +193 -0
  24. data/lib/nsxt_client.rb +176 -0
  25. data/lib/nsxt_dfw.rb +196 -0
  26. data/lib/nsxt_logical_port.rb +94 -0
  27. data/lib/nsxt_rule.rb +188 -0
  28. data/lib/nsxt_tz.rb +38 -0
  29. data/lib/nsxv_client.rb +176 -0
  30. data/lib/nsxv_dfw.rb +202 -0
  31. data/lib/nsxv_logical_port.rb +107 -0
  32. data/lib/nsxv_rule.rb +172 -0
  33. data/lib/nsxv_tz.rb +41 -0
  34. data/lib/opaque_network.rb +134 -0
  35. data/lib/opennebula/document.rb +7 -28
  36. data/lib/opennebula/document_json.rb +41 -11
  37. data/lib/opennebula/error.rb +3 -0
  38. data/lib/opennebula/flow/grammar.rb +1195 -0
  39. data/lib/opennebula/flow/service_pool.rb +190 -0
  40. data/lib/opennebula/flow/service_template.rb +572 -0
  41. data/lib/opennebula/flow/service_template_ext.rb +84 -0
  42. data/lib/opennebula/flow/service_template_pool.rb +32 -0
  43. data/lib/opennebula/flow/validator.rb +499 -0
  44. data/lib/opennebula/flow.rb +23 -0
  45. data/lib/opennebula/hook.rb +4 -11
  46. data/lib/opennebula/image.rb +16 -13
  47. data/lib/opennebula/lockable_ext.rb +163 -0
  48. data/lib/opennebula/marketplaceapp.rb +8 -118
  49. data/lib/opennebula/marketplaceapp_ext.rb +522 -0
  50. data/lib/opennebula/oneflow_client.rb +3 -2
  51. data/lib/opennebula/pool.rb +3 -2
  52. data/lib/opennebula/template.rb +3 -12
  53. data/lib/opennebula/template_ext.rb +325 -0
  54. data/lib/opennebula/user.rb +25 -1
  55. data/lib/opennebula/virtual_machine.rb +24 -206
  56. data/lib/opennebula/virtual_machine_ext.rb +469 -0
  57. data/lib/opennebula/virtual_machine_pool.rb +0 -4
  58. data/lib/opennebula/virtual_network.rb +3 -9
  59. data/lib/opennebula/virtual_router.rb +3 -11
  60. data/lib/opennebula/vm_group.rb +3 -10
  61. data/lib/opennebula/vntemplate.rb +3 -12
  62. data/lib/opennebula/wait_ext.rb +222 -0
  63. data/lib/opennebula.rb +4 -1
  64. data/lib/rest_client.rb +201 -0
  65. data/lib/scripts_common.rb +180 -0
  66. data/lib/transport_zone.rb +43 -0
  67. data/lib/vcenter_driver.rb +8 -21
  68. data/lib/vcenter_importer.rb +616 -0
  69. data/lib/vi_client.rb +281 -0
  70. data/lib/vi_helper.rb +312 -0
  71. data/lib/virtual_machine.rb +3477 -0
  72. data/lib/virtual_wire.rb +158 -0
  73. data/lib/vm_device.rb +80 -0
  74. data/lib/vm_disk.rb +202 -0
  75. data/lib/vm_folder.rb +69 -0
  76. data/lib/vm_helper.rb +30 -0
  77. data/lib/vm_monitor.rb +303 -0
  78. data/lib/vm_nic.rb +70 -0
  79. data/lib/vm_template.rb +1961 -0
  80. data/lib/vmm_importer.rb +121 -0
  81. metadata +140 -26
data/lib/nsx_client.rb ADDED
@@ -0,0 +1,144 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+ module NSXDriver
17
+
18
+ ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
19
+
20
+ if !ONE_LOCATION
21
+ RUBY_LIB_LOCATION = '/usr/lib/one/ruby' \
22
+ unless defined?(RUBY_LIB_LOCATION)
23
+ GEMS_LOCATION = '/usr/share/one/gems' \
24
+ unless defined?(GEMS_LOCATION)
25
+ else
26
+ RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' \
27
+ unless defined?(RUBY_LIB_LOCATION)
28
+ GEMS_LOCATION = ONE_LOCATION + '/share/gems' \
29
+ unless defined?(GEMS_LOCATION)
30
+ end
31
+
32
+ if File.directory?(GEMS_LOCATION)
33
+ real_gems_path = File.realpath(GEMS_LOCATION)
34
+ if !defined?(Gem) || Gem.path != [real_gems_path]
35
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
36
+ require 'rubygems'
37
+ Gem.use_paths(real_gems_path)
38
+ end
39
+ end
40
+
41
+ $LOAD_PATH << RUBY_LIB_LOCATION
42
+
43
+ require 'net/http'
44
+ require 'json'
45
+ require 'nokogiri'
46
+ require 'opennebula'
47
+ require 'vcenter_driver'
48
+ require 'nsx_driver'
49
+
50
+ # Class NSXClient
51
+ class NSXClient
52
+
53
+ # ATTIBUTES
54
+ attr_accessor :nsxmgr
55
+ attr_accessor :nsx_user
56
+ attr_accessor :nsx_password
57
+
58
+ # CONSTRUCTORS
59
+ def initialize(nsxmgr, nsx_user, nsx_password)
60
+ @nsxmgr = nsxmgr
61
+ @nsx_user = nsx_user
62
+ @nsx_password = nsx_password
63
+ end
64
+
65
+ def self.new_child(nsxmgr, nsx_user, nsx_password, type)
66
+ [nsxmgr, nsx_user, nsx_password, type].each do |v|
67
+ next if !v.nil? && !v.empty?
68
+
69
+ return nil
70
+ end
71
+
72
+ case type.upcase
73
+ when NSXConstants::NSXT
74
+ NSXTClient.new(nsxmgr, nsx_user, nsx_password)
75
+ when NSXConstants::NSXV
76
+ NSXVClient.new(nsxmgr, nsx_user, nsx_password)
77
+ else
78
+ error_msg = "Unknown NSX type: #{type}"
79
+ error = NSXError::UnknownObject.new(error_msg)
80
+ raise error
81
+ end
82
+ end
83
+
84
+ def self.new_from_host(host)
85
+ nsxmgr = host['TEMPLATE/NSX_MANAGER']
86
+ nsx_user = host['TEMPLATE/NSX_USER']
87
+ nsx_password = host['TEMPLATE/NSX_PASSWORD']
88
+ nsx_type = host['TEMPLATE/NSX_TYPE']
89
+
90
+ new_child(nsxmgr, nsx_user, nsx_password, nsx_type)
91
+ end
92
+
93
+ def self.new_from_id(hid)
94
+ client = OpenNebula::Client.new
95
+ host = OpenNebula::Host.new_with_id(hid, client)
96
+
97
+ rc = host.info(true)
98
+
99
+ if OpenNebula.is_error?(rc)
100
+ raise "Could not get host info for ID: #{hid} - #{rc.message}"
101
+ end
102
+
103
+ new_from_host(host)
104
+ end
105
+
106
+ # METHODS
107
+
108
+ # Return response if match with responses codes, If response not match
109
+ # with expected responses codes then raise an IncorrectResponseCodeError
110
+ def check_response(response, codes_array)
111
+ unless response.nil?
112
+ return response if codes_array.include?(response.code.to_i)
113
+
114
+ response_json = JSON.parse(response.body)
115
+ nsx_error = "\nNSX error code: " \
116
+ "#{response_json['errorCode']}, " \
117
+ "\nNSX error details: " \
118
+ "#{response_json['details']}"
119
+ raise NSXError::IncorrectResponseCodeError, nsx_error
120
+ end
121
+ raise NSXError::IncorrectResponseCodeError, nsx_error
122
+ end
123
+
124
+ # Return: respose.body
125
+ def get(url, aditional_headers = []); end
126
+
127
+ # Return: response
128
+ def get_full_response(url, aditional_headers = []); end
129
+
130
+ # Return: id of the created object
131
+ def post(url, data, aditional_headers = []); end
132
+
133
+ def put(url, data, aditional_headers = []); end
134
+
135
+ def delete(url); end
136
+
137
+ def get_token(url); end
138
+
139
+ # Prepare headers
140
+ def add_headers(aditional_headers = []); end
141
+
142
+ end
143
+
144
+ end
@@ -0,0 +1,28 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+ module NSXDriver
17
+
18
+ # Class Transport Zone
19
+ class NSXComponent
20
+
21
+ # CONSTRUCTOR
22
+ def initialize(nsx_client)
23
+ @nsx_client = nsx_client
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,149 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+ module NSXDriver
17
+
18
+ ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
19
+
20
+ if !ONE_LOCATION
21
+ RUBY_LIB_LOCATION = '/usr/lib/one/ruby' \
22
+ unless defined?(RUBY_LIB_LOCATION)
23
+ GEMS_LOCATION = '/usr/share/one/gems' \
24
+ unless defined?(GEMS_LOCATION)
25
+ else
26
+ RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' \
27
+ unless defined?(RUBY_LIB_LOCATION)
28
+ GEMS_LOCATION = ONE_LOCATION + '/share/gems' \
29
+ unless defined?(GEMS_LOCATION)
30
+ end
31
+
32
+ if File.directory?(GEMS_LOCATION)
33
+ real_gems_path = File.realpath(GEMS_LOCATION)
34
+ if !defined?(Gem) || Gem.path != [real_gems_path]
35
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
36
+ require 'rubygems'
37
+ Gem.use_paths(real_gems_path)
38
+ end
39
+ end
40
+
41
+ $LOAD_PATH << RUBY_LIB_LOCATION
42
+
43
+ class NSXConstants
44
+
45
+ # CONSTANTS
46
+ NSXT = 'NSX-T'
47
+ NSXV = 'NSX-V'
48
+ HEADER_JSON = { 'Content-Type' => 'application/json' }
49
+ HEADER_XML = { 'Content-Type' => 'application/xml' }
50
+ # NSX Manager
51
+ NSXT_EXTENSION_LIST = 'com.vmware.nsx.management.nsxt'
52
+ NSXV_EXTENSION_LIST = 'com.vmware.vShieldManager'
53
+ NSXT_BASE = '/api/v1'
54
+ NSXV_BASE = '/api/2.0'
55
+ # Transport Zones
56
+ NSXV_TZS = NSXV_BASE + '/vdn/scopes'
57
+ NSXV_TZS_XPATH = '//vdnScope'
58
+ NSXT_TZS = NSXT_BASE + '/transport-zones'
59
+ # VirtualWire
60
+ NSXV_AUTH = NSXV_BASE + '/services/auth/token'
61
+ NSXV_LS_TYPE = 'NSX-V'
62
+ NSXV_LS_NAME_XPATH = '//virtualWire/name'
63
+ NSXV_LS_VNI_XPATH = '//virtualWire/vdnId'
64
+ NSXV_LS_BACKING_XPATH = '//virtualWire/vdsContextWithBacking' \
65
+ '/backingValue'
66
+ NSXV_LS_OBJECTID_XPATH = '//virtualWire/vdsContextWithBacking' \
67
+ '/switch/objectId'
68
+ NSXV_LS_XPATH = '//virtualWire'
69
+ NSXV_LS_SECTION = NSXV_BASE + '/vdn/virtualwires/'
70
+ NSXV_TZ_SECTION = NSXV_BASE + '/vdn/scopes/'
71
+ NSXV_TZ_XPATH = '//virtualWire/vdnScopeId'
72
+ # OpaqueNetwork
73
+ NSXT_AUTH = NSXT_BASE + '/aaa/registration-token'
74
+ NSXT_LS_TYPE = 'Opaque Network'
75
+ NSXT_LS_SECTION = NSXT_BASE + '/logical-switches/'
76
+ # DFW
77
+ ONE_SECTION_NAME = 'OpenNebula'
78
+ NSXT_DFW_BASE = NSXT_BASE + '/firewall'
79
+ NSXV_DFW_BASE = '/api/4.0/firewall/globalroot-0/config'
80
+ NSXT_DFW_SECTIONS = '/sections'
81
+ NSXV_DFW_SECTIONS = '/layer3sections'
82
+ NSXV_DFW_SECTION_XPATH = '//section'
83
+ NSXV_DFW_RULE_XPATH = '//rule'
84
+ # RULE
85
+ NSXT_RULE_BASE = NSXT_BASE + '/firewall/rules'
86
+ NSXT_RULE_PROTOCOL = {
87
+ 'TCP' => {
88
+ :service => {
89
+ :l4_protocol => 'TCP',
90
+ :source_ports => [],
91
+ :destination_ports => [],
92
+ :resource_type => 'L4PortSetNSService'
93
+ }
94
+ },
95
+ 'UDP' => {
96
+ :service => {
97
+ :l4_protocol => 'UDP',
98
+ :source_ports => [],
99
+ :destination_ports => [],
100
+ :resource_type => 'L4PortSetNSService'
101
+ }
102
+ },
103
+ 'ICMP' => {
104
+ :service => {
105
+ :protocol => 'ICMPv4',
106
+ :resource_type => 'ICMPTypeNSService'
107
+ }
108
+ },
109
+ 'ICMPv6' => {
110
+ :service => {
111
+ :protocol => 'ICMPv6',
112
+ :resource_type => 'ICMPTypeNSService'
113
+ }
114
+ }
115
+ }
116
+ NSXV_RULE_BASE = 'xxx'
117
+ NSX_RULE_IPSEC_PORTS = %w[500 4500]
118
+ # Logical Ports
119
+ NSXT_LP_BASE = NSXT_BASE + '/logical-ports/'
120
+ NSXV_LP_BASE = ''
121
+ # Messages
122
+ MSG_INCOMPLETE_REQ = 'Incomplete request, NSX_MANAGER, NSX_USER, \
123
+ NSX_PASSWORD and NSX_TYPE are needed'
124
+ MSG_INVALID_REQ = 'Invalid request, check that NSX_MANAGER, NSX_USER, \
125
+ NSX_PASSWORD and NSX_TYPE are correct'
126
+ MSG_INVALID_NSXTYPE = 'Invalid NSX-TYPE: Only NSX-T and NSX-V are \
127
+ supported'
128
+ # Responses codes
129
+ # 2xx
130
+ CODE_OK = 200
131
+ CODE_CREATED = 201
132
+ CODE_ACCEPTED = 202
133
+ CODE_NO_CONTENT = 204
134
+ # 4xx
135
+ CODE_BAD_REQUEST = 400
136
+ CODE_UNAUTHORIZED = 401
137
+ CODE_FORBIDDEN = 403
138
+ CODE_NOT_FOUND = 404
139
+ CODE_METHOD_NOT_ALLOWED = 405
140
+ CODE_NOT_ACCEPTABLE = 406
141
+ # 5xx
142
+ CODE_INTERNAL_SERVER_ERROR = 500
143
+ CODE_BAD_GATEWAY = 502
144
+ CODE_SERVICE_UNAVAILABLE = 503
145
+ CODE_GATEWAY_TIMEOUT = 504
146
+
147
+ end
148
+
149
+ end
data/lib/nsx_driver.rb ADDED
@@ -0,0 +1,78 @@
1
+ # ---------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ---------------------------------------------------------------------------- #
16
+
17
+ # ---------------------------------------------------------------------------- #
18
+ # Set up the environment for the driver #
19
+ # ---------------------------------------------------------------------------- #
20
+
21
+ ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
22
+
23
+ if !ONE_LOCATION
24
+ BIN_LOCATION = '/usr/bin' unless defined?(BIN_LOCATION)
25
+ LIB_LOCATION = '/usr/lib/one' unless defined?(LIB_LOCATION)
26
+ ETC_LOCATION = '/etc/one/' unless defined?(ETC_LOCATION)
27
+ VAR_LOCATION = '/var/lib/one' unless defined?(VAR_LOCATION)
28
+ GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION)
29
+ else
30
+ BIN_LOCATION = ONE_LOCATION + '/bin' unless defined?(BIN_LOCATION)
31
+ LIB_LOCATION = ONE_LOCATION + '/lib' unless defined?(LIB_LOCATION)
32
+ ETC_LOCATION = ONE_LOCATION + '/etc/' unless defined?(ETC_LOCATION)
33
+ VAR_LOCATION = ONE_LOCATION + '/var/' unless defined?(VAR_LOCATION)
34
+ GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION)
35
+ end
36
+
37
+ ENV['LANG'] = 'C'
38
+
39
+ if File.directory?(GEMS_LOCATION)
40
+ real_gems_path = File.realpath(GEMS_LOCATION)
41
+ if !defined?(Gem) || Gem.path != [real_gems_path]
42
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
43
+ require 'rubygems'
44
+ Gem.use_paths(real_gems_path)
45
+ end
46
+ end
47
+
48
+ $LOAD_PATH << LIB_LOCATION + '/ruby'
49
+ $LOAD_PATH << LIB_LOCATION + '/ruby/nsx_driver'
50
+
51
+ # ---------------------------------------------------------------------------- #
52
+ # NSX Library #
53
+ # ---------------------------------------------------------------------------- #
54
+ require 'nsx_constants'
55
+ require 'nsx_error'
56
+ require 'nsx_component'
57
+ require 'nsx_client'
58
+ require 'nsxt_client'
59
+ require 'nsxv_client'
60
+ require 'logical_switch'
61
+ require 'opaque_network'
62
+ require 'transport_zone'
63
+ require 'nsxt_tz'
64
+ require 'nsxv_tz'
65
+ require 'virtual_wire'
66
+ require 'distributed_firewall'
67
+ require 'nsxt_dfw'
68
+ require 'nsxv_dfw'
69
+ require 'logical_port'
70
+ require 'nsxt_logical_port'
71
+ require 'nsxv_logical_port'
72
+ require 'nsx_rule'
73
+ require 'nsxt_rule'
74
+ require 'nsxv_rule'
75
+
76
+ # NSX Driver module
77
+ module NSXDriver
78
+ end
data/lib/nsx_error.rb ADDED
@@ -0,0 +1,77 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+ module NSXDriver
17
+
18
+ class NSXError < StandardError
19
+
20
+ # Class IncorrectResponseCodeError
21
+ class IncorrectResponseCodeError < NSXError
22
+
23
+ def initialize(msg = 'Incorrect response code')
24
+ super(msg)
25
+ end
26
+
27
+ end
28
+
29
+ # Class ObjectNotFound
30
+ class ObjectNotFound < NSXError
31
+
32
+ def initialize(msg = 'Object not found')
33
+ super(msg)
34
+ end
35
+
36
+ end
37
+
38
+ # Class UnknownObject
39
+ class UnknownObject < NSXError
40
+
41
+ def initialize(msg = 'Unknown object type')
42
+ super(msg)
43
+ end
44
+
45
+ end
46
+
47
+ # Class CreateError
48
+ class CreateError < NSXError
49
+
50
+ def initialize(msg = 'Error creating NSX object')
51
+ super(msg)
52
+ end
53
+
54
+ end
55
+
56
+ # Class DeleteError
57
+ class DeleteError < NSXError
58
+
59
+ def initialize(msg = 'Error deleting NSX object')
60
+ super(msg)
61
+ end
62
+
63
+ end
64
+
65
+ # Class DeleteError
66
+ class MissingParameter < NSXError
67
+
68
+ def initialize(parameter)
69
+ msg = "Missing NSX parameter #{parameter}"
70
+ super(msg)
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
data/lib/nsx_rule.rb ADDED
@@ -0,0 +1,193 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ require 'ipaddr'
18
+
19
+ # Module NSXDriver
20
+ module NSXDriver
21
+
22
+ ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
23
+
24
+ if !ONE_LOCATION
25
+ RUBY_LIB_LOCATION = '/usr/lib/one/ruby' \
26
+ unless defined?(RUBY_LIB_LOCATION)
27
+ GEMS_LOCATION = '/usr/share/one/gems' \
28
+ unless defined?(GEMS_LOCATION)
29
+ else
30
+ RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' \
31
+ unless defined?(RUBY_LIB_LOCATION)
32
+ GEMS_LOCATION = ONE_LOCATION + '/share/gems' \
33
+ unless defined?(GEMS_LOCATION)
34
+ end
35
+
36
+ if File.directory?(GEMS_LOCATION)
37
+ real_gems_path = File.realpath(GEMS_LOCATION)
38
+ if !defined?(Gem) || Gem.path != [real_gems_path]
39
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
40
+ require 'rubygems'
41
+ Gem.use_paths(real_gems_path)
42
+ end
43
+ end
44
+
45
+ $LOAD_PATH << RUBY_LIB_LOCATION
46
+
47
+ require 'nsxt_rule'
48
+ require 'nsxv_rule'
49
+
50
+ # Class Logical Switch
51
+ module NSXRule
52
+
53
+ include NSXTRule
54
+ include NSXVRule
55
+
56
+ def to_nets(ip_start, size)
57
+ nets = []
58
+ ipaddr = IPAddr.new ip_start
59
+ ip_i = ipaddr.to_i
60
+
61
+ if ipaddr.ipv4?
62
+ ip_length = 32
63
+ elsif ipaddr.ipv6?
64
+ ip_length = 128
65
+ else
66
+ return
67
+ end
68
+
69
+ # Find the largest address block (look for the first 1-bit)
70
+ lblock = 0
71
+
72
+ lblock += 1 while ip_i[lblock] == 0 && lblock < ip_length
73
+
74
+ # Allocate whole blocks till the size fits
75
+ while size >= 2**lblock
76
+ nets << "#{IPAddr.new(ip_i, ipaddr.family)}" \
77
+ "/#{ip_length-lblock}"
78
+
79
+ ip_i += 2**lblock
80
+ size -= 2**lblock
81
+
82
+ lblock += 1 while ip_i[lblock] == 0 && lblock < ip_length
83
+ end
84
+
85
+ # Fit remaining address blocks
86
+ ip_length.downto(0) do |i|
87
+ next if size[i] == 0
88
+
89
+ nets << "#{IPAddr.new(ip_i, ipaddr.family)}/#{ip_length-i}"
90
+
91
+ ip_i += 2**i
92
+ end
93
+
94
+ nets
95
+ end
96
+
97
+ # Adapt port from ["22, 443"] to '22, 443'
98
+ # Adapt port from ["22", "443"] to '22, 443'
99
+ def parse_ports(rule_ports)
100
+ unless rule_ports.empty?
101
+ rule_ports = rule_ports.join(',')
102
+ end
103
+ rule_ports
104
+ end
105
+
106
+ def extract_vnet_data(vnet_id)
107
+ if vnet_id == ''
108
+ return {
109
+ :nsxid => '',
110
+ :name => ''
111
+ }
112
+ end
113
+ # Create client to communicate with OpenNebula
114
+ one_client = OpenNebula::Client.new
115
+ # Get the network XML from OpenNebula
116
+ # This is potentially different from the Netowrk Template
117
+ # provided as the API call argument
118
+ one_vnet = OpenNebula::VirtualNetwork.new_with_id(vnet_id,
119
+ one_client)
120
+ rc = one_vnet.info
121
+ if OpenNebula.is_error?(rc)
122
+ err_msg = rc.message
123
+ raise CreateNetworkError, err_msg
124
+ end
125
+ {
126
+ :nsxid => one_vnet['TEMPLATE/NSX_ID'],
127
+ :name => one_vnet['NAME']
128
+ }
129
+ end
130
+
131
+ def extract_rule_data(xml_rule)
132
+ sg_id = xml_rule.xpath('SECURITY_GROUP_ID').text
133
+ sg_name = xml_rule.xpath('SECURITY_GROUP_NAME').text
134
+ in_out = xml_rule.xpath('RULE_TYPE').text.upcase
135
+ in_out == 'INBOUND' ? sg_direction = 'IN' : sg_direction = 'OUT'
136
+ # Protocol: TCP, UDP, ICMP...
137
+ sg_protocol = xml_rule.xpath('PROTOCOL').text
138
+ if sg_protocol == 'ICMP'
139
+ sg_icmp_type = xml_rule.xpath('ICMP_TYPE').text
140
+ end
141
+ # OpenNebula network ID
142
+ sg_network_id = xml_rule.xpath('NETWORK_ID').text
143
+ vnet_data = extract_vnet_data(sg_network_id)
144
+
145
+ # ip / netmask
146
+ sg_ip = xml_rule.xpath('IP').text
147
+ sg_ipsize = xml_rule.xpath('SIZE').text
148
+ sg_subnets = []
149
+ if sg_ip != '' && sg_ipsize != ''
150
+ sg_subnets = to_nets(sg_ip, sg_ipsize.to_i)
151
+ end
152
+ # Ports
153
+ sg_ports = ''
154
+ sg_range_port = xml_rule.xpath('RANGE').text
155
+ if sg_range_port
156
+ if sg_range_port.index(':')
157
+ sg_port_from = sg_range_port[0..sg_range_port.index(':')-1]
158
+ sg_port_to = sg_range_port[sg_range_port.index(':')+1,
159
+ sg_range_port.length]
160
+ sg_ports = "#{sg_port_from}-#{sg_port_to}"
161
+ else
162
+ sg_ports = sg_range_port
163
+ end
164
+ end
165
+ # Create hash with data
166
+ {
167
+ :id => sg_id,
168
+ :name => sg_name,
169
+ :direction => sg_direction,
170
+ :protocol => sg_protocol,
171
+ :icmp_type => sg_icmp_type,
172
+ :network_id => sg_network_id,
173
+ :network_name => vnet_data[:name],
174
+ :network_nsxid => vnet_data[:nsxid],
175
+ :subnets => sg_subnets,
176
+ :ports => sg_ports.split(',')
177
+ }
178
+ end
179
+
180
+ def rule_spec(rule, vm_data, nic_data, nsx_client)
181
+ case nsx_client.nsx_type
182
+ when NSXDriver::NSXConstants::NSXT
183
+ nsxt_rule_spec(rule, vm_data, nic_data)
184
+ when NSXDriver::NSXConstants::NSXV
185
+ nsxv_rule_spec(rule, vm_data, nic_data)
186
+ else
187
+ raise "Unsupported NSX type: #{nsx_type}"
188
+ end
189
+ end
190
+
191
+ end
192
+
193
+ end