blob: f36c4ce43bb67c1af5b9ded2a819dc621953dc99 [file] [log] [blame]
Ales Komarekad46d2e2017-03-09 17:16:38 +01001#!/usr/bin/python
2# Copyright 2017 Mirantis, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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
Sergey Matov16896ac2018-02-15 15:46:31 +040016from vnc_api.vnc_api import *
17from vnc_api.gen.resource_xsd import *
18from cfgm_common.exceptions import *
19from cfgm_common.rbaclib import *
20import cfgm_common
21
Ales Komarekad46d2e2017-03-09 17:16:38 +010022from netaddr import IPNetwork
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020023from vnc_api.vnc_api import PhysicalRouter, PhysicalInterface, LogicalInterface
24from vnc_api.vnc_api import EncapsulationPrioritiesType
Vasyl Saienkob10b7202017-09-05 14:19:03 +030025from vnc_api.vnc_api import VirtualMachineInterface, MacAddressesType
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020026from vnc_api.vnc_api import ServiceApplianceSet, KeyValuePairs, KeyValuePair
Ales Komarekad46d2e2017-03-09 17:16:38 +010027
28try:
29 from vnc_api import vnc_api
Petr Jediný5f3efe32017-05-26 17:55:09 +020030 from vnc_api.vnc_api import LinklocalServiceEntryType, \
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +010031 LinklocalServicesTypes, GlobalVrouterConfig, GlobalSystemConfig
Ales Komarekad46d2e2017-03-09 17:16:38 +010032 from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \
Jan Cachb3092722018-01-31 12:46:16 +010033 ConfigNode, DatabaseNode, BgpRouter, VirtualNetwork, FloatingIpPool
Ales Komarekad46d2e2017-03-09 17:16:38 +010034 from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \
Marek Celoud3097e5b2018-01-09 13:52:14 +010035 BgpSession, BgpPeeringAttributes, BgpRouterParams, AuthenticationData, \
Jan Cacha859e6b2018-01-09 17:34:18 +010036 AuthenticationKeyItem, VirtualNetworkType, IpamSubnetType, SubnetType, \
Jan Cachb3092722018-01-31 12:46:16 +010037 VnSubnetsType, RouteTargetList, ShareType
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020038
Ales Komarekad46d2e2017-03-09 17:16:38 +010039 HAS_CONTRAIL = True
40except ImportError:
41 HAS_CONTRAIL = False
42
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +010043
44try:
45 from vnc_api.gen.resource_xsd import GracefulRestartParametersType
46 HAS_OLD = False
47except ImportError:
48 HAS_OLD = True
49
Ales Komarekad46d2e2017-03-09 17:16:38 +010050__opts__ = {}
51
52
53def __virtual__():
54 '''
55 Only load this module if vnc_api library is installed.
56 '''
57 if HAS_CONTRAIL:
58 return 'contrail'
59
60 return False
61
62
63def _auth(**kwargs):
64 '''
65 Set up Contrail API credentials.
66 '''
67 user = kwargs.get('user')
68 password = kwargs.get('password')
69 tenant_name = kwargs.get('project')
70 api_host = kwargs.get('api_server_ip')
71 api_port = kwargs.get('api_server_port')
72 api_base_url = kwargs.get('api_base_url')
73 use_ssl = False
74 auth_host = kwargs.get('auth_host_ip')
75 vnc_lib = vnc_api.VncApi(user, password, tenant_name,
Anton Samoylovffb5fdc2019-03-11 20:03:02 +040076 api_host, api_port, api_base_url, wait_for_connect=False,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020077 api_server_use_ssl=use_ssl, auth_host=auth_host)
Ales Komarekad46d2e2017-03-09 17:16:38 +010078
79 return vnc_lib
80
81
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020082def _get_config(vnc_client, global_system_config='default-global-system-config'):
Ales Komarekad46d2e2017-03-09 17:16:38 +010083 try:
84 gsc_obj = vnc_client.global_system_config_read(id=global_system_config)
85 except vnc_api.NoIdError:
86 gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config)
87 except:
88 gsc_obj = None
89
90 return gsc_obj
91
92
93def _get_rt_inst_obj(vnc_client):
Ales Komarekad46d2e2017-03-09 17:16:38 +010094 # TODO pick fqname hardcode from common
95 rt_inst_obj = vnc_client.routing_instance_read(
96 fq_name=['default-domain', 'default-project',
97 'ip-fabric', '__default__'])
98
99 return rt_inst_obj
100
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +0200101
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300102def _get_fq_name(vnc_client, resource_name, project_name, domain='default-domain'):
103 res = [domain]
104 if project_name:
105 res.append(project_name)
106 if resource_name:
107 res.append(resource_name)
108 return res
109
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +0200110
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300111def _get_project_obj(vnc_client, name, domain='default-domain'):
112 return vnc_client.project_read(fq_name=[domain, name])
113
Ales Komarekad46d2e2017-03-09 17:16:38 +0100114
115def _get_ip(ip_w_pfx):
116 return str(IPNetwork(ip_w_pfx).ip)
117
118
Ales Komarekad46d2e2017-03-09 17:16:38 +0100119def virtual_router_list(**kwargs):
120 '''
121 Return a list of all Contrail virtual routers
122
123 CLI Example:
124
125 .. code-block:: bash
126
127 salt '*' contrail.virtual_router_list
128 '''
129 ret = {}
130 vnc_client = _auth(**kwargs)
131 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
132 for vrouter_obj in vrouter_objs:
133 ret[vrouter_obj.name] = {
134 'ip_address': vrouter_obj.virtual_router_ip_address,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200135 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled,
136 'uuid': vrouter_obj.uuid
137
Ales Komarekad46d2e2017-03-09 17:16:38 +0100138 }
139 return ret
140
141
142def virtual_router_get(name, **kwargs):
143 '''
144 Return a specific Contrail virtual router
145
146 CLI Example:
147
148 .. code-block:: bash
149
150 salt '*' contrail.virtual_router_get cmp01
151 '''
152 ret = {}
153 vrouter_objs = virtual_router_list(**kwargs)
154 if name in vrouter_objs:
155 ret[name] = vrouter_objs.get(name)
156 if len(ret) == 0:
157 return {'Error': 'Error in retrieving virtual router.'}
158 return ret
159
160
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200161def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +0100162 '''
163 Create specific Contrail virtual router
164
165 CLI Example:
166
167 .. code-block:: bash
168
169 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200170 router_types:
171 - tor-agent
172 - tor-service-node
173 - embedded
Ales Komarekad46d2e2017-03-09 17:16:38 +0100174 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200175 ret = {'name': name,
176 'changes': {},
177 'result': True,
178 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100179 vnc_client = _auth(**kwargs)
180 gsc_obj = _get_config(vnc_client)
181 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200182 router_types = ['tor-agent', 'tor-service-node', 'embedded']
183 if router_type not in router_types:
184 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100185 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200186 vrouter = virtual_router_get(name)
187 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200188 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200189 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200190 changes['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200191 vrouter_obj.set_virtual_router_ip_address(ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200192 if vrouter_obj.get_virtual_router_type() != router_type:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200193 changes['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200194 vrouter_obj.set_virtual_router_type(router_type)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200195 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200196 changes['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200197 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200198 if len(changes) != 0:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200199 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200200 ret['result'] = None
201 ret['comment'] = "Virtual router " + name + " will be updated"
202 else:
203 ret['comment'] = "VirtualRouter " + name + " has been updated"
204 ret['changes'] = changes
205 vnc_client.virtual_router_update(vrouter_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200206 return ret
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200207 ret['comment'] = 'Virtual router ' + name + ' already exists and is updated'
208 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100209 else:
210 vrouter_obj = VirtualRouter(
211 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200212 virtual_router_ip_address=ip_address,
213 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100214 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200215 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200216 ret['result'] = None
217 ret['comment'] = "VirtualRouter " + name + " will be created"
218 else:
219 vnc_client.virtual_router_create(vrouter_obj)
220 ret['comment'] = "VirtualRouter " + name + " has been created"
221 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
222 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100223
224
225def virtual_router_delete(name, **kwargs):
226 '''
227 Delete specific Contrail virtual router
228
229 CLI Example:
230
231 .. code-block:: bash
232
233 salt '*' contrail.virtual_router_delete cmp01
234 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200235 ret = {'name': name,
236 'changes': {},
237 'result': True,
238 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100239 vnc_client = _auth(**kwargs)
240 gsc_obj = _get_config(vnc_client)
241 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200242 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200243 ret['result'] = None
244 ret['comment'] = "VirtualRouter " + name + " will be deleted"
245 else:
246 vnc_client.virtual_router_delete(fq_name=vrouter_obj.get_fq_name())
247 ret['comment'] = "VirtualRouter " + name + " has been deleted"
248 ret['changes'] = {'VirtualRouter': {'old': name, 'new': ''}}
249 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200250
251
252def physical_router_list(**kwargs):
253 '''
254 Return a list of all Contrail physical routers
255
256 CLI Example:
257
258 .. code-block:: bash
259
260 salt '*' contrail.physical_router_list
261 '''
262 ret = {}
263 vnc_client = _auth(**kwargs)
264 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
265 for prouter_obj in prouter_objs:
266 ret[prouter_obj.name] = {
267 'uuid': prouter_obj._uuid,
268 'management_ip': prouter_obj._physical_router_management_ip,
269 'product_name': prouter_obj._physical_router_product_name,
270 }
271
272 return ret
273
274
275def physical_router_get(name, **kwargs):
276 '''
277 Return a specific Contrail physical router
278
279 CLI Example:
280
281 .. code-block:: bash
282
283 salt '*' contrail.physical_router_get router_name
284 '''
285 ret = {}
286 vnc_client = _auth(**kwargs)
287 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
288 for prouter_obj in prouter_objs:
289 if name == prouter_obj.name:
290 ret[name] = prouter_obj.__dict__
291 if len(ret) == 0:
292 return {'Error': 'Error in retrieving physical router.'}
293 return ret
294
295
296def physical_router_create(name, parent_type=None,
297 management_ip=None,
298 dataplane_ip=None, # VTEP address in web GUI
299 vendor_name=None,
300 product_name=None,
301 vnc_managed=None,
302 junos_service_ports=None,
303 agents=None, **kwargs):
304 '''
305 Create specific Contrail physical router
306
307 CLI Example:
308
309 .. code-block:: bash
310
311 salt '*' contrail.physical_router_create OVSDB_router management_ip=10.167.4.202 dataplane_ip=172.16.20.15 vendor_name=MyVendor product_name=MyProduct agents="['tor01','tns01']"
312 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200313 ret = {'name': name,
314 'changes': {},
315 'result': True,
316 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200317 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200318 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200319 prouter_objs = physical_router_list(**kwargs)
320 if name in prouter_objs:
321 prouter = physical_router_get(name)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200322 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200323 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
324 if prouter_obj.physical_router_management_ip != management_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200325 changes['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200326 prouter_obj.set_physical_router_management_ip(management_ip)
327 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200328 changes['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200329 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
330 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200331 changes['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200332 prouter_obj.set_physical_router_vendor_name(vendor_name)
333 if prouter_obj.get_physical_router_product_name() != product_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200334 changes['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200335 prouter_obj.set_physical_router_product_name(product_name)
336 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200337 changes['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200338 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
339 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200340 changes['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
341 'to': junos_service_ports}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200342 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
343
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200344 if len(changes) != 0:
345 if __opts__['test']:
346 ret['result'] = None
347 ret['comment'] = "Physical router " + name + " will be updated"
348 else:
349 ret['comment'] = 'Physical router ' + name + ' already exists and is updated'
350 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200351
352 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
353 c_agents = [] # referenced vrouters
354 for c_agent in prouter_obj.get_virtual_router_refs():
355 c_agents.append(c_agent['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200356 # agent_objs = [] # required state of references
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200357 for vrouter_obj in vrouter_objs:
358 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
359 prouter_obj.add_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200360 changes['vrouter ' + vrouter_obj._display_name] = "Reference added"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200361 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
362 prouter_obj.del_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200363 changes['vrouter ' + vrouter_obj._display_name] = "Reference removed"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200364 vnc_client.physical_router_update(prouter_obj)
365
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200366 if __opts__['test']:
367 ret['result'] = None
368 ret['comment'] = "VirtualRouter " + name + " will be created"
369 else:
370 vnc_client.virtual_router_create(vrouter_obj)
371 ret['comment'] = "VirtualRouter " + name + " has been created"
372 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
373
374 if len(changes) == 0:
375 ret['comment'] = "Physical router exists and is updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200376 return ret
377 else:
378 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200379 ret['result'] = None
380 ret['comment'] = "Physical router " + name + " will be created"
381 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200382 prouter_obj = PhysicalRouter(
383 name=name,
384 parent_obj=None,
385 physical_router_management_ip=management_ip,
386 physical_router_dataplane_ip=dataplane_ip,
387 physical_router_vendor_name=vendor_name,
388 physical_router_product_name=product_name,
389 physical_router_vnc_managed=vnc_managed,
390 physical_router_junos_service_ports=junos_service_ports,
391 )
392 for agent in agents:
393 vrouter = virtual_router_get(agent)
394 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
395 prouter_obj.add_virtual_router(vrouter_obj)
396 vnc_client.physical_router_create(prouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200397 ret['comment'] = "Physical router " + name + " has been created"
398 ret['changes'] = {'PhysicalRouter': {'old': '', 'new': name}}
399 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200400
401
402def physical_router_delete(name, **kwargs):
403 '''
404 Delete specific Contrail physical router
405
406 CLI Example:
407
408 .. code-block:: bash
409
410 salt '*' contrail.physical_router_delete router_name
411 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200412 ret = {'name': name,
413 'changes': {},
414 'result': True,
415 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200416 vnc_client = _auth(**kwargs)
417 gsc_obj = _get_config(vnc_client)
418 prouter_obj = PhysicalRouter(name, gsc_obj)
419 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200420 ret['result'] = None
421 ret['comment'] = "Physical router " + name + " will be deleted"
422 else:
423 vnc_client.physical_router_delete(fq_name=prouter_obj.get_fq_name())
424 ret['comment'] = "Physical router " + name + " has been deleted"
425 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
426 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200427
428
429def physical_interface_list(**kwargs):
430 '''
431 Return a list of all Contrail physical interface
432
433 CLI Example:
434
435 .. code-block:: bash
436
437 salt '*' contrail.physical_interface_list
438 '''
439 ret = {}
440 vnc_client = _auth(**kwargs)
441 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
442 for pinterface_obj in pinterface_objs:
443 ret[pinterface_obj.name] = {
444 'uuid': pinterface_obj._uuid,
445 'fq_name': pinterface_obj.fq_name,
446 'parent_type': pinterface_obj.parent_type,
447 }
448
449 return ret
450
451
452def physical_interface_get(name, physical_router, **kwargs):
453 '''
454 Return a specific Contrail physical interface
455
456 CLI Example:
457
458 .. code-block:: bash
459
460 salt '*' contrail.physical_interface_get interface_name physical_router_name
461 '''
462 ret = {}
463 vnc_client = _auth(**kwargs)
464 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
465 for pinterf_obj in pinterf_objs:
466 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
467 ret[name] = pinterf_obj.__dict__
468 if len(ret) == 0:
469 return {'Error': 'Error in retrieving physical interface.'}
470 return ret
471
472
473def physical_interface_create(name, physical_router, **kwargs):
474 '''
475 Create specific Contrail physical interface
476
477 CLI Example:
478
479 .. code-block:: bash
480
481 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
482 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200483 ret = {'name': name,
484 'changes': {},
485 'result': True,
486 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200487 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200488 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200489 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
490 if 'Error' not in pinterf_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200491 ret['comment'] = 'Physical interface ' + name + ' on ' + physical_router + ' already exists'
492 return ret
493
494 if __opts__['test']:
495 ret['result'] = None
496 ret['comment'] = "Physical interface " + name + " will be created"
497 return ret
498
499 prouter = physical_router_get(physical_router)
500 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
501 pinterf_obj = PhysicalInterface(name, prouter_obj)
502 vnc_client.physical_interface_create(pinterf_obj)
503 ret['comment'] = "Physical interface " + name + " has been created"
504 ret['changes'] = {'Physical interface': {'old': '', 'new': name}}
505 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200506
507
508def physical_interface_delete(name, physical_router, **kwargs):
509 '''
510 Delete specific Contrail physical interface
511
512 CLI Example:
513 .. code-block:: bash
514
515 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
516 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200517 ret = {'name': name,
518 'changes': {},
519 'result': True,
520 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200521 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200522 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200523 piface = physical_interface_get(name, physical_router)
524 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200525 ret['result'] = None
526 ret['comment'] = "Physical interface " + name + " will be deleted"
527 else:
528 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
529 ret['comment'] = "Physical router " + name + " has been deleted"
530 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
531 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200532
533
534def logical_interface_list(**kwargs):
535 '''
536 Return a list of all Contrail logical interfaces
537
538 CLI Example:
539
540 .. code-block:: bash
541
542 salt '*' contrail.logical_interface_list
543 '''
544 ret = []
545 vnc_client = _auth(**kwargs)
546 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
547 for liface_obj in liface_objs:
548 ret.append({
549 'name': liface_obj.name,
550 'uuid': liface_obj._uuid,
551 'fq_name': liface_obj.fq_name,
552 'parent_type': liface_obj.parent_type,
553 })
554 return ret
555
556
557def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
558 '''
559 Return a specific Contrail logical interface
560
561 CLI Example:
562
563 .. code-block:: bash
564
565 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
566 or
567 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
568 or
569 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
570 '''
571 ret = {}
572 vnc_client = _auth(**kwargs)
573 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
574 count = 0
575 for liface_obj in liface_objs:
576 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
577 if parent_type and parent_type == liface_obj.parent_type:
578 count += 1
579 ret[liface_obj.name] = liface_obj.__dict__
580 if not parent_type:
581 count += 1
582 ret[liface_obj.name] = liface_obj.__dict__
583 if len(ret) == 0:
584 return {'Error': 'Error in retrieving logical interface.'}
585 if count > 1:
586 return {
587 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
588 return ret
589
590
591def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300592 vmis=None, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200593 '''
594 Create specific Contrail logical interface
595
596 CLI Example:
597
598 .. code-block:: bash
599
600 salt '*' contrail.logical_interface_create ge-0/0/10.11 parent_names="['ge-0/0/0','phr1']" parent_type=physical-interface vlan_tag=1025 interface_type=L2
601 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200602 ret = {'name': name,
603 'changes': {},
604 'result': True,
605 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200606 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200607 # gsc_obj = _get_config(vnc_client)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300608
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200609 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
610 if 'Error' not in liface_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200611 ret['comment'] = 'Logical interface ' + name + ' already exists'
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200612 else:
613 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200614 ret['result'] = None
615 ret['comment'] = "Logical interface " + name + " will be created"
616 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200617 parent_obj = None
618 for router in parent_names:
619 parent_router = physical_router_get(router)
620 if 'Error' not in parent_router:
621 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
622 break
623 if not parent_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200624 ret['result'] = False
625 ret['comment'] = 'Physical router have to be defined'
626 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200627 if parent_type == 'physical-interface':
628 for interface in parent_names:
629 parent_interface = physical_interface_get(interface, parent_obj.name)
630 if 'Error' not in parent_interface:
631 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
632 break
633 if interface_type.lower() == "l3":
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200634 ret['result'] = False
635 ret['comment'] = "Virtual Network have to be defined for L3 interface type"
636 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200637
638 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300639
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200640 if vmis:
641 for vmi_name, vmi in vmis.iteritems():
642 vmi = vnc_client.virtual_machine_interface_read(
643 fq_name=_get_fq_name(vnc_client, resource_name=vmi_name,
644 project_name=kwargs.get('tenant', 'admin')))
645 liface_obj.add_virtual_machine_interface(vmi)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200646 vnc_client.logical_interface_create(liface_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300647
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200648 ret['comment'] = "Logical interface " + name + " has been created"
649 ret['changes'] = {'Logical interface': {'old': '', 'new': name}}
650 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200651
652
653def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
654 '''
655 Delete specific Contrail logical interface
656
657 CLI Example:
658
659 .. code-block:: bash
660
661 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
662 or
663 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
664
665 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200666 ret = {'name': name,
667 'changes': {},
668 'result': True,
669 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200670 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200671 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200672 liface = logical_interface_get(name, parent_names, parent_type)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200673
674 if __opts__['test']:
675 ret['result'] = None
676 ret['comment'] = "Logical interface " + name + " will be deleted"
677 return ret
678 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
679 ret['comment'] = "Logical interface " + name + " has been deleted"
680 ret['changes'] = {'LogicalInterface ': {'old': name, 'new': ''}}
681 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200682
683
684def global_vrouter_config_list(**kwargs):
685 '''
686 Return a list of all Contrail global vrouter configs
687
688 CLI Example:
689
690 .. code-block:: bash"
691
692 salt '*' global_vrouter_config_list
693 '''
694 ret = {}
695 vnc_client = _auth(**kwargs)
696 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
697 for vrouter_conf_obj in vrouter_conf_objs:
Anton Samoylovce3d7772018-11-23 00:00:02 +0400698 ret[vrouter_conf_obj.name] = vrouter_conf_obj.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200699 return ret
700
701
702def global_vrouter_config_get(name, **kwargs):
703 '''
704 Return a specific Contrail global vrouter config
705
706 CLI Example:
707
708 .. code-block:: bash
709
710 salt '*' contrail.global_vrouter_get global-vrouter-config
711 '''
712 ret = {}
713 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
714 if name in vrouter_conf_objs:
715 ret[name] = vrouter_conf_objs.get(name)
716 if len(ret) == 0:
717 return {'Error': 'Error in retrieving global vrouter config.'}
718 return ret
719
720
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100721def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, flow_export_rate, *fq_names, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200722 '''
723 Create specific Contrail global vrouter config
724
725 CLI Example:
726
727 .. code-block:: bash
728
Anton Samoylovf1a5ef82019-04-15 12:42:54 +0400729 salt '*' contrail.global_vrouter_config_create \
730 name=global-vrouter-config parent_type=global-system-config \
731 encap_priority="MPLSoUDP,MPLSoGRE" vxlan_vn_id_mode="automatic" \
732 flow_export_rate=None \
733 fq_names="['default-global-system-config', 'default-global-vrouter-config']"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200734 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200735 ret = {'name': name,
736 'changes': {},
737 'result': True,
738 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200739 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200740 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200741 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
742 if name in vrouter_conf_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200743 ret['comment'] = 'Global vrouter config ' + name + ' already exists'
744 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200745 else:
746 vrouter_conf_obj = GlobalVrouterConfig(
747 name=name,
748 parent_obj=None,
749 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
750 fq_name=fq_names,
751 vxlan_network_identifier_mode=vxlan_vn_id_mode,
752 parent_type=parent_type,
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100753 flow_export_rate=flow_export_rate,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200754 )
755 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200756 ret['result'] = None
757 ret['comment'] = "Global vRouter config " + name + " will be created"
758 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200759 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200760 ret['comment'] = "Global vRouter config " + name + " has been created"
761 ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}}
762 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200763
764
765def global_vrouter_config_delete(name, **kwargs):
766 '''
767 Delete specific Contrail global vrouter config
768
769 CLI Example:
770
771 .. code-block:: bash
772
773 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
774 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200775 ret = {'name': name,
776 'changes': {},
777 'result': True,
778 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200779 vnc_client = _auth(**kwargs)
780 gsc_obj = _get_config(vnc_client)
781 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
782 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200783 ret['result'] = None
784 ret['comment'] = "Global vRouter config " + name + " will be deleted"
785 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200786 vnc_client.global_vrouter_config_delete(
787 fq_name=vrouter_conf_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200788 ret['comment'] = "Global vRouter config " + name + " has been deleted"
789 ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}}
790 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100791
792
793def analytics_node_list(**kwargs):
794 '''
795 Return a list of all Contrail analytics nodes
796
797 CLI Example:
798
799 .. code-block:: bash
800
801 salt '*' contrail.analytics_node_list
802 '''
803 ret = {}
804 vnc_client = _auth(**kwargs)
805 node_objs = vnc_client._objects_list('analytics-node', detail=True)
806 for node_obj in node_objs:
807 ret[node_obj.name] = node_obj.__dict__
808 return ret
809
810
811def analytics_node_get(name, **kwargs):
812 '''
813 Return a specific Contrail analytics node
814
815 CLI Example:
816
817 .. code-block:: bash
818
819 salt '*' contrail.analytics_node_get nal01
820 '''
821 ret = {}
822 vrouter_objs = analytics_node_list(**kwargs)
823 if name in vrouter_objs:
824 ret[name] = vrouter_objs.get(name)
825 if len(ret) == 0:
826 return {'Error': 'Error in retrieving analytics node.'}
827 return ret
828
829
830def analytics_node_create(name, ip_address, **kwargs):
831 '''
832 Create specific Contrail analytics node
833
834 CLI Example:
835
836 .. code-block:: bash
837
838 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
839 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200840
841 ret = {'name': name,
842 'changes': {},
843 'result': True,
844 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100845 vnc_client = _auth(**kwargs)
846 gsc_obj = _get_config(vnc_client)
847 analytics_node_objs = analytics_node_list(**kwargs)
848 if name in analytics_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200849 ret['comment'] = 'Analytics node %s already exists'
850 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100851 else:
852 analytics_node_obj = AnalyticsNode(
853 name, gsc_obj,
854 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200855 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200856 ret['result'] = None
857 ret['comment'] = "AnalyticsNode " + name + " will be created"
858 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100859 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200860 ret['comment'] = "AnalyticsNode " + name + " has been created"
861 ret['changes'] = {'Analytics Node': {'old': '', 'new': name}}
862 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100863
864
865def analytics_node_delete(name, **kwargs):
866 '''
867 Delete specific Contrail analytics node
868
869 CLI Example:
870
871 .. code-block:: bash
872
873 salt '*' contrail.analytics_node_delete cmp01
874 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200875 ret = {'name': name,
876 'changes': {},
877 'result': True,
878 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100879 vnc_client = _auth(**kwargs)
880 gsc_obj = _get_config(vnc_client)
881 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200882 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200883 ret['result'] = None
884 ret['comment'] = "AnalyticsNode " + name + " will be deleted"
885 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100886 vnc_client.analytics_node_delete(
887 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200888 ret['comment'] = "AnalyticsNode " + name + " has been deleted"
889 ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}}
890 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100891
892
893def config_node_list(**kwargs):
894 '''
895 Return a list of all Contrail config nodes
896
897 CLI Example:
898
899 .. code-block:: bash
900
901 salt '*' contrail.config_node_list
902 '''
903 ret = {}
904 vnc_client = _auth(**kwargs)
905 node_objs = vnc_client._objects_list('config-node', detail=True)
906 for node_obj in node_objs:
907 ret[node_obj.name] = node_obj.__dict__
908 return ret
909
910
911def config_node_get(name, **kwargs):
912 '''
913 Return a specific Contrail config node
914
915 CLI Example:
916
917 .. code-block:: bash
918
919 salt '*' contrail.config_node_get nal01
920 '''
921 ret = {}
922 vrouter_objs = config_node_list(**kwargs)
923 if name in vrouter_objs:
924 ret[name] = vrouter_objs.get(name)
925 if len(ret) == 0:
926 return {'Error': 'Error in retrieving config node.'}
927 return ret
928
929
930def config_node_create(name, ip_address, **kwargs):
931 '''
932 Create specific Contrail config node
933
934 CLI Example:
935
936 .. code-block:: bash
937
938 salt '*' contrail.config_node_create ntw03 10.10.10.103
939 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200940 ret = {'name': name,
941 'changes': {},
942 'result': True,
943 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100944 vnc_client = _auth(**kwargs)
945 gsc_obj = _get_config(vnc_client)
946 config_node_objs = config_node_list(**kwargs)
947 if name in config_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200948 ret['comment'] = 'Config node ' + name + ' already exists'
949 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100950 else:
951 config_node_obj = ConfigNode(
952 name, gsc_obj,
953 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200954 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200955 ret['comment'] = "ConfigNode " + name + " will be created"
956 ret['result'] = None
957 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100958 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200959 ret['comment'] = "ConfigNode " + name + " has been created"
960 ret['changes'] = {'ConfigNode': {'old': '', 'new': name}}
961 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100962
963
964def config_node_delete(name, **kwargs):
965 '''
966 Delete specific Contrail config node
967
968 CLI Example:
969
970 .. code-block:: bash
971
972 salt '*' contrail.config_node_delete cmp01
973 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200974 ret = {'name': name,
975 'changes': {},
976 'result': True,
977 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100978 vnc_client = _auth(**kwargs)
979 gsc_obj = _get_config(vnc_client)
980 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200981 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200982 ret['comment'] = "ConfigNode " + name + " will be deleted"
983 ret['result'] = None
984 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100985 vnc_client.config_node_delete(
986 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200987 ret['comment'] = "ConfigNode " + name + " has been deleted"
988 ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}}
989 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100990
991
992def bgp_router_list(**kwargs):
993 '''
994 Return a list of all Contrail BGP routers
995
996 CLI Example:
997
998 .. code-block:: bash
999
1000 salt '*' contrail.bgp_router_list
1001 '''
1002 ret = {}
1003 vnc_client = _auth(**kwargs)
1004 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
1005 for bgp_router_obj in bgp_router_objs:
1006 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
1007 return ret
1008
1009
1010def bgp_router_get(name, **kwargs):
1011 '''
1012 Return a specific Contrail BGP router
1013
1014 CLI Example:
1015
1016 .. code-block:: bash
1017
1018 salt '*' contrail.bgp_router_get nal01
1019 '''
1020 ret = {}
1021 bgp_router_objs = bgp_router_list(**kwargs)
1022 if name in bgp_router_objs:
1023 ret[name] = bgp_router_objs.get(name)
1024 if len(ret) == 0:
1025 return {'Error': 'Error in retrieving BGP router.'}
1026 return ret
1027
1028
Marek Celoud3097e5b2018-01-09 13:52:14 +01001029def bgp_router_create(name, type, ip_address, asn=64512, key_type=None, key=None, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +01001030 '''
1031 Create specific Contrail control node
1032
1033 CLI Example:
1034
1035 .. code-block:: bash
1036
1037 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
1038 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
1039 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001040 ret = {'name': name,
1041 'changes': {},
1042 'result': True,
1043 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001044 vnc_client = _auth(**kwargs)
1045
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001046 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
1047 'inet6-vpn']
1048 if type != 'control-node':
1049 address_families.remove('erm-vpn')
1050
Marek Celoud3097e5b2018-01-09 13:52:14 +01001051 key_type = None if key_type == 'None' else key_type
1052 key = None if key == 'None' else key
1053
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001054 bgp_addr_fams = AddressFamilies(address_families)
1055 bgp_sess_attrs = [
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001056 BgpSessionAttributes(address_families=bgp_addr_fams)]
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001057 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
1058 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
1059 rt_inst_obj = _get_rt_inst_obj(vnc_client)
1060
Marek Celoud3097e5b2018-01-09 13:52:14 +01001061 bgp_auth_data = None
1062
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001063 if type == 'control-node':
1064 vendor = 'contrail'
1065 elif type == 'router':
1066 vendor = 'mx'
Marek Celoud3097e5b2018-01-09 13:52:14 +01001067 if key_type == 'md5':
1068 key_id = 0
1069 key_items = AuthenticationKeyItem(key_id, key)
1070 bgp_auth_data = AuthenticationData(key_type, [key_items])
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001071 else:
1072 vendor = 'unknown'
1073
1074 router_params = BgpRouterParams(router_type=type,
1075 vendor=vendor, autonomous_system=int(asn),
1076 identifier=_get_ip(ip_address),
1077 address=_get_ip(ip_address),
Marek Celoud3097e5b2018-01-09 13:52:14 +01001078 port=179, address_families=bgp_addr_fams,
1079 auth_data=bgp_auth_data)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001080
Ales Komarekad46d2e2017-03-09 17:16:38 +01001081 bgp_router_objs = bgp_router_list(**kwargs)
1082 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001083 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001084
1085 if bgp_router_obj.bgp_router_parameters.autonomous_system != asn:
1086 ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}})
1087 if bgp_router_obj.bgp_router_parameters.vendor != vendor:
1088 ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}})
1089 if bgp_router_obj.bgp_router_parameters.address != ip_address:
1090 ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}})
Marek Celoud3097e5b2018-01-09 13:52:14 +01001091 try:
1092 if bgp_router_obj.bgp_router_parameters.auth_data.key_type != key_type:
1093 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_type, 'new': key_type}})
1094 except:
1095 if key_type != None:
1096 ret['changes'].update({"key_type": {'old': None, 'new': key_type}})
1097 if key_type == 'md5':
1098 try:
1099 if bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key != key:
1100 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key, 'new': key}})
1101 except:
1102 ret['changes'].update({"key_type": {'old': None, 'new': key}})
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001103
1104 if len(ret['changes']) == 0:
1105 return ret
1106
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001107 bgp_router_obj.set_bgp_router_parameters(router_params)
1108 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001109 ret['result'] = None
1110 ret['comment'] = "BGP router " + name + " will be updated"
1111 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001112 vnc_client.bgp_router_update(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001113 ret['comment'] = "BGP router " + name + " has been updated"
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001114 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001115 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001116 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
1117 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001118 ret['result'] = None
1119 ret['comment'] = "BGP router " + name + " will be created"
1120 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001121 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001122 ret['comment'] = "BGP router " + name + " has been created"
1123 ret['changes'] = {'BGP router': {'old': name, 'new': ''}}
1124 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001125
1126
1127def bgp_router_delete(name, **kwargs):
1128 '''
1129 Delete specific Contrail control node
1130
1131 CLI Example:
1132
1133 .. code-block:: bash
1134
1135 salt '*' contrail.bgp_router_delete mx01
1136 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001137 ret = {'name': name,
1138 'changes': {},
1139 'result': True,
1140 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001141 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001142
1143 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001144 ret['result'] = None
1145 ret['comment'] = "BGP router " + name + " will be deleted"
1146 return ret
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001147
1148 bgp_router = bgp_router_get(name)
1149 if name in bgp_router:
1150 vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name'])
1151 ret['comment'] = "BGP router " + name + " has been deleted"
1152 ret['changes'] = {'BGP router': {'old': '', 'new': name}}
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001153 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001154
Ales Komarekad46d2e2017-03-09 17:16:38 +01001155
1156def database_node_list(**kwargs):
1157 '''
1158 Return a list of all Contrail database nodes
1159
1160 CLI Example:
1161
1162 .. code-block:: bash
1163
1164 salt '*' contrail.database_node_list
1165 '''
1166 ret = {}
1167 vnc_client = _auth(**kwargs)
1168 node_objs = vnc_client._objects_list('database-node', detail=True)
1169 for node_obj in node_objs:
1170 ret[node_obj.name] = node_obj.__dict__
1171 return ret
1172
1173
1174def database_node_get(name, **kwargs):
1175 '''
1176 Return a specific Contrail database node
1177
1178 CLI Example:
1179
1180 .. code-block:: bash
1181
1182 salt '*' contrail.database_node_get nal01
1183 '''
1184 ret = {}
1185 vrouter_objs = database_node_list(**kwargs)
1186 if name in vrouter_objs:
1187 ret[name] = vrouter_objs.get(name)
1188 if len(ret) == 0:
1189 return {'Error': 'Error in retrieving database node.'}
1190 return ret
1191
1192
1193def database_node_create(name, ip_address, **kwargs):
1194 '''
1195 Create specific Contrail database node
1196
1197 CLI Example:
1198
1199 .. code-block:: bash
1200
1201 salt '*' contrail.database_node_create ntw03 10.10.10.103
1202 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001203 ret = {'name': name,
1204 'changes': {},
1205 'result': True,
1206 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001207 vnc_client = _auth(**kwargs)
1208 gsc_obj = _get_config(vnc_client)
1209 database_node_objs = database_node_list(**kwargs)
1210 if name in database_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001211 ret['comment'] = 'Database node ' + name + ' already exists'
1212 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001213 else:
1214 database_node_obj = DatabaseNode(
1215 name, gsc_obj,
1216 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001217 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001218 ret['result'] = None
1219 ret['comment'] = "DatabaseNode " + name + " will be created"
1220 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001221 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001222 ret['comment'] = "DatabaseNode " + name + " has been created"
1223 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1224 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001225
1226
1227def database_node_delete(name, **kwargs):
1228 '''
1229 Delete specific Contrail database node
1230
1231 CLI Example:
1232
1233 .. code-block:: bash
1234
1235 salt '*' contrail.database_node_delete cmp01
1236 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001237 ret = {'name': name,
1238 'changes': {},
1239 'result': True,
1240 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001241 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001242 gsc_obj = _get_config(vnc_client)
1243 database_node_obj = DatabaseNode(name, gsc_obj)
1244 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001245 ret['result'] = None
1246 ret['comment'] = "DatabaseNode " + name + " will be deleted"
1247 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001248 vnc_client.database_node_delete(
1249 fq_name=database_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001250 ret['comment'] = "DatabaseNode " + name + " has been deleted"
1251 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1252 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001253
1254
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001255def _get_vrouter_config(vnc_client, gvc_name=None):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001256 try:
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001257 if not gvc_name:
1258 gvc_list = global_vrouter_config_list()
1259 gvc_name = gvc_list.values()[0]['name']
1260
Petr Jediný5f3efe32017-05-26 17:55:09 +02001261 config = vnc_client.global_vrouter_config_read(
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001262 fq_name=['default-global-system-config', gvc_name])
Petr Jediný5f3efe32017-05-26 17:55:09 +02001263 except Exception:
1264 config = None
1265
1266 return config
1267
1268
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001269def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001270 '''
1271 Return a list of all Contrail link local services
1272
1273 CLI Example:
1274
1275 .. code-block:: bash
1276
1277 salt '*' contrail.linklocal_service_list
1278 '''
1279 ret = {}
1280 vnc_client = _auth(**kwargs)
1281
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001282 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001283 if current_config is None:
1284 return ret
1285
1286 service_list_res = current_config.get_linklocal_services()
1287 if service_list_res is None:
1288 service_list_obj = {'linklocal_service_entry': []}
1289 else:
1290 service_list_obj = service_list_res.__dict__
1291 for _, value in service_list_obj.iteritems():
1292 for entry in value:
1293 service = entry.__dict__
1294 if 'linklocal_service_name' in service:
1295 ret[service['linklocal_service_name']] = service
1296 return ret
1297
1298
1299def linklocal_service_get(name, **kwargs):
1300 '''
1301 Return a specific Contrail link local service
1302
1303 CLI Example:
1304
1305 .. code-block:: bash
1306
1307 salt '*' contrail.linklocal_service_get llservice
1308 '''
1309 ret = {}
1310 services = linklocal_service_list(**kwargs)
1311 if name in services:
1312 ret[name] = services.get(name)
1313 if len(ret) == 0:
1314 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1315 return ret
1316
1317
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001318def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001319 '''
1320 Create specific Contrail link local service
1321
1322 CLI Example:
1323
1324 .. code-block:: bash
1325
1326 salt '*' contrail.linklocal_service_create \
1327 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1328 salt '*' contrail.linklocal_service_create \
1329 llservice 10.10.10.103 22 link-local.service.dns-name 22
1330 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001331 ret = {'name': name,
1332 'changes': {},
1333 'result': True,
1334 'comment': ''}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001335 vnc_client = _auth(**kwargs)
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001336 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001337 service_entry = LinklocalServiceEntryType(
1338 linklocal_service_name=name,
1339 linklocal_service_ip=lls_ip,
1340 linklocal_service_port=lls_port,
1341 ip_fabric_service_port=ipf_port)
1342 if isinstance(ipf_dns_or_ip, basestring):
1343 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1344 elif isinstance(ipf_dns_or_ip, list):
1345 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1346 service_entry.ip_fabric_DNS_service_name = ''
1347
1348 if current_config is None:
1349 new_services = LinklocalServicesTypes([service_entry])
1350 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001351 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001352 ret['result'] = None
1353 ret['comment'] = "Link local service " + name + " will be created"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001354 else:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001355 ret['comment'] = "Link local service " + name + " has been created"
1356 ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001357 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001358 else:
1359 _current_service_list = current_config.get_linklocal_services()
1360 if _current_service_list is None:
1361 service_list = {'linklocal_service_entry': []}
1362 else:
1363 service_list = _current_service_list.__dict__
1364 new_services = [service_entry]
1365 for key, value in service_list.iteritems():
1366 if key != 'linklocal_service_entry':
1367 continue
1368 for _entry in value:
1369 entry = _entry.__dict__
1370 if 'linklocal_service_name' in entry:
1371 if entry['linklocal_service_name'] == name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001372 ret['comment'] = 'Link local service ' + name + ' already exists'
1373 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001374 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001375 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001376 ret['result'] = None
1377 ret['comment'] = "LinkLocalSevices " + name + " will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001378 service_list[key] = new_services
1379 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001380 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001381 ret['result'] = None
1382 ret['comment'] = "LinkLocalSevices " + name + " will be updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001383 else:
1384 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001385 ret['comment'] = "LinkLocalSevices " + name + " has been created"
1386 ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001387 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001388
1389
1390def linklocal_service_delete(name, **kwargs):
1391 '''
1392 Delete specific link local service entry
1393
1394 CLI Example:
1395
1396 .. code-block:: bash
1397
1398 salt '*' contrail.linklocal_service_delete llservice
1399 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001400 ret = {'name': name,
1401 'changes': {},
1402 'result': True,
1403 'comment': ''}
1404 lls = linklocal_service_get(name)
1405 print (lls)
1406 if name in lls:
1407 if __opts__['test']:
1408 print " ------------ Test only ------------"
1409 ret['result'] = None
1410 ret['comment'] = "Link local service " + name + " will be deleted"
1411 return ret
1412 else:
1413 return ret
1414
Petr Jediný5f3efe32017-05-26 17:55:09 +02001415 vnc_client = _auth(**kwargs)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001416 current_config = _get_vrouter_config(vnc_client)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001417 if current_config is not None:
1418 _current_service_list = current_config.get_linklocal_services()
1419 if _current_service_list is None:
1420 service_list = {'linklocal_service_entry': []}
1421 else:
1422 service_list = _current_service_list.__dict__
1423 new_services = []
1424 for key, value in service_list.iteritems():
1425 if key != 'linklocal_service_entry':
1426 continue
1427 for _entry in value:
1428 entry = _entry.__dict__
1429 if 'linklocal_service_name' in entry:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001430 if entry['linklocal_service_name'] != name:
Petr Jediný5f3efe32017-05-26 17:55:09 +02001431 new_services.append(_entry)
1432 service_list[key] = new_services
1433 new_config = GlobalVrouterConfig(linklocal_services=service_list)
1434 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001435 ret['comment'] = "Link local service " + name + " will be deleted"
1436 ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
1437 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001438
1439
1440def virtual_machine_interface_list(**kwargs):
1441 '''
1442 Return a list of all Contrail virtual machine interfaces
1443
1444 CLI Example:
1445
1446 .. code-block:: bash
1447
1448 salt '*' contrail.virtual_machine_interfaces
1449 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001450 ret = []
1451 vnc_client = _auth(**kwargs)
1452 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1453 project_uuid = project.get_uuid()
1454
1455 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1456 detail=True, parent_id=project_uuid)
1457
1458 for vm_iface in vm_ifaces:
1459 ret.append(vm_iface.__dict__)
1460
1461 return ret
1462
1463
1464def virtual_machine_interface_create(name,
1465 virtual_network,
1466 mac_address=None,
1467 ip_address=None,
1468 security_group=None,
1469 **kwargs):
1470 '''
1471 Create specific Contrail virtual machine interface (Port)
1472
1473 CLI Example:
1474
1475 .. code-block:: bash
1476
1477 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1478 router_types:
1479 - tor-agent
1480 - tor-service-node
1481 - embedded
1482 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001483 ret = {}
1484 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001485 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1486
1487 vm_int = VirtualMachineInterface(name, parent_obj=project)
1488
1489 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001490 mac_address_obj = MacAddressesType([mac_address])
1491 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001492
1493 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001494 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001495 vnc_client, security_group, kwargs.get('tenant', 'admin')))
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001496 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001497
1498 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1499 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1500 vm_int.set_virtual_network(vnet_obj)
1501
1502 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1503 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1504
1505 vm_int.set_port_security_enabled(False)
1506 vnc_client.virtual_machine_interface_update(vm_int)
1507
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001508 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001509 ip = vnc_api.InstanceIp(name + '.ip')
1510 ip.set_virtual_machine_interface(vmi)
1511 ip.set_virtual_network(vnet_obj)
1512
1513 ip_uuid = vnc_client.instance_ip_create(ip)
1514
1515 if ip_address:
1516 ip.set_instance_ip_address(ip_address)
1517 vnc_client.instance_ip_update(ip)
1518
1519 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001520
1521
1522def virtual_network_list(**kwargs):
1523 '''
1524 Return a list of all Contrail virtual network
1525
1526 CLI Example:
1527
1528 .. code-block:: bash
1529
1530 salt '*' contrail.virtual_network
1531 '''
1532
1533 ret = {}
1534 vnc_client = _auth(**kwargs)
1535 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1536 for virtual_network in virtual_networks:
1537 ret[virtual_network.name] = virtual_network.__dict__
1538 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001539
1540
1541def virtual_network_get(name, **kwargs):
1542 '''
1543 Return a specific Contrail virtual network
1544
1545 CLI Example:
1546
1547 .. code-block:: bash
1548
1549 salt '*' contrail.virtual_network_get net01
1550 '''
1551 ret = {}
1552 vnet_objs = virtual_network_list(**kwargs)
1553 if name in vnet_objs:
1554 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001555 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001556 return {'result': False,
1557 'Error': 'Error in retrieving virtual networks.'}
1558 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001559
1560
ibumarskov6ec99c42019-02-20 11:44:22 +04001561def virtual_network_create(name, domain='default-domain', project='admin',
1562 ipam_domain='default-domain',
1563 ipam_project='default-project',
1564 ipam_name='default-network-ipam',
1565 router_external=None, route_target_list=None,
1566 subnet_conf=None, vntype_conf=None, **kwargs):
Jan Cacha859e6b2018-01-09 17:34:18 +01001567 '''
1568 Create Contrail virtual network
1569 CLI Example:
1570 .. code-block:: bash
Jan Cacha859e6b2018-01-09 17:34:18 +01001571
ibumarskov6ec99c42019-02-20 11:44:22 +04001572 salt -C 'I@opencontrail:control:role:primary'
1573 contrail.virtual_network_create public router_external=True
1574 route_target_list=["target:64512:10000"]
1575 subnet_conf=["ip_prefix":"172.16.111.0","ip_prefix_len":24]
Jan Cacha859e6b2018-01-09 17:34:18 +01001576
1577 Parameters:
ibumarskov6ec99c42019-02-20 11:44:22 +04001578 name (str): name of the network (required)
1579 domain (str): name of domain (optional)
1580 project (str): name of project (optional)
1581 ipam_domain (str): domain for ipam (optional)
1582 ipam_project (str): project for ipam (optional)
1583 ipam_name (str): name of IP Address Management (optional)
1584 router_external (bool): When true, this virtual network is openstack router
1585 external network. (optional)
1586 route_target_list (list): route target list - format is
1587 ['target:<asn>:<target>']
1588 subnet_conf (dict):
Jan Cacha859e6b2018-01-09 17:34:18 +01001589 ip_prefix (string) optional - format is xxx.xxx.xxx.xxx
1590 ip_prefix_len (int) optional - format is xx
ibumarskov6ec99c42019-02-20 11:44:22 +04001591 vntype_conf (dict):
Jan Cacha859e6b2018-01-09 17:34:18 +01001592 allow_transit (boolean) optional - enable allow transit
ibumarskov6ec99c42019-02-20 11:44:22 +04001593 vxlan_network_identifier (int) - VxLAN VNI value for network
Jan Cacha859e6b2018-01-09 17:34:18 +01001594 forwarding_mode (any of ['l2_l3','l2','l3']) optional
1595 - packet forwarding mode for this virtual network
1596 rpf (any of ['enabled','disabled']) optional
1597 - Enable or disable Reverse Path Forwarding check
1598 for this network
1599 mirror_destination (boolean) optional
1600 - Mark the vn as mirror destination network
1601 '''
Jan Cacha859e6b2018-01-09 17:34:18 +01001602 ret = {'name': name,
1603 'changes': {},
1604 'result': True,
1605 'comment': ''}
1606
Jan Cacha859e6b2018-01-09 17:34:18 +01001607 vnc_client = _auth(**kwargs)
ibumarskov6ec99c42019-02-20 11:44:22 +04001608 fq_name = [domain, project, name]
1609 prj_obj = vnc_client.project_read(fq_name=[domain,
1610 project])
1611 ipam_obj = vnc_client.network_ipam_read(fq_name=[ipam_domain,
1612 ipam_project,
1613 ipam_name])
1614 if route_target_list:
1615 route_target_list = RouteTargetList(route_target_list)
1616 if subnet_conf:
1617 if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf:
1618 ipam_sn = IpamSubnetType(subnet=SubnetType(
1619 ip_prefix=subnet_conf['ip_prefix'],
1620 ip_prefix_len=subnet_conf['ip_prefix_len']))
Jan Cacha859e6b2018-01-09 17:34:18 +01001621
ibumarskov6ec99c42019-02-20 11:44:22 +04001622 # Check if the network exists
1623 vn_obj_list = vnc_client.virtual_networks_list(parent_id=prj_obj.uuid)
1624 if name in list(map(lambda x: x['fq_name'][2],
1625 vn_obj_list['virtual-networks'])):
1626 changes = {}
1627 vn_obj = vnc_client.virtual_network_read(fq_name=fq_name)
Jan Cacha859e6b2018-01-09 17:34:18 +01001628
ibumarskov6ec99c42019-02-20 11:44:22 +04001629 # Update IPAM properties
1630 ipam_refs = vn_obj.get_network_ipam_refs()
1631 subnet_detected = False
1632 if subnet_conf and ipam_refs:
1633 for ipam in ipam_refs:
1634 if ipam_obj.get_uuid() == ipam['uuid']:
1635 ipam_subnet_list = ipam['attr'].get_ipam_subnets()
1636 for ipam_subnet_type in ipam_subnet_list:
1637 subnet_obj = ipam_subnet_type.get_subnet()
1638 ip_prefix = subnet_obj.get_ip_prefix()
1639 ip_prefix_len = subnet_obj.get_ip_prefix_len()
1640 if subnet_conf['ip_prefix'] == ip_prefix and \
1641 subnet_conf['ip_prefix_len'] == ip_prefix_len:
1642 subnet_detected = True
1643 if subnet_conf and not subnet_detected:
1644 changes['ipam_subnet'] = \
1645 {'added': '{0}/{1}'.format(subnet_conf['ip_prefix'],
1646 subnet_conf['ip_prefix_len'])}
1647 vn_obj.add_network_ipam(ipam_obj,
1648 VnSubnetsType(ipam_subnets=[ipam_sn]))
Jan Cacha859e6b2018-01-09 17:34:18 +01001649
ibumarskov6ec99c42019-02-20 11:44:22 +04001650 # Update VirtualNetwork properties
1651 external = vn_obj.get_router_external()
1652 if router_external is not None and router_external != external:
1653 changes['router_external'] = {'from': external,
1654 'to': router_external}
1655 vn_obj.set_router_external(router_external)
1656 if route_target_list:
1657 changes['router_list'] = \
1658 {'from': str(vn_obj.get_route_target_list()),
1659 'to': str(route_target_list)}
1660 vn_obj.set_route_target_list(route_target_list)
Jan Cacha859e6b2018-01-09 17:34:18 +01001661
ibumarskov6ec99c42019-02-20 11:44:22 +04001662 # Update VirtualNetworkType properties
1663 if vntype_conf:
1664 vn_type_obj = vn_obj.get_virtual_network_properties()
1665 if vn_type_obj is None:
1666 vn_type_obj = VirtualNetworkType()
Jan Cacha859e6b2018-01-09 17:34:18 +01001667
ibumarskov6ec99c42019-02-20 11:44:22 +04001668 if 'allow_transit' in vntype_conf:
1669 allow_transit_attr = vn_type_obj.get_allow_transit()
1670 if vntype_conf['allow_transit'] != allow_transit_attr:
1671 changes['allow_transit'] = \
1672 {'from': allow_transit_attr,
1673 'to': vntype_conf['allow_transit']}
1674 vn_type_obj.set_allow_transit(vntype_conf['allow_transit'])
1675 if 'vxlan_network_identifier' in vntype_conf:
1676 vxlan_net_id = vn_type_obj.get_vxlan_network_identifier()
1677 if vntype_conf['vxlan_network_identifier'] != vxlan_net_id:
1678 vn_type_obj.set_vxlan_network_identifier(
1679 vntype_conf['vxlan_network_identifier'])
1680 changes['vxlan_network_identifier'] = \
1681 {'from': vxlan_net_id,
1682 'to': vntype_conf['vxlan_network_identifier']}
1683 if 'forwarding_mode' in vntype_conf:
1684 forwarding_mode_attr = vn_type_obj.get_forwarding_mode()
1685 if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3'] and \
1686 vntype_conf['forwarding_mode'] != forwarding_mode_attr:
1687 changes['forwarding_mode'] = \
1688 {'from': forwarding_mode_attr,
1689 'to': vntype_conf['forwarding_mode']}
1690 vn_type_obj.set_forwarding_mode(
1691 vntype_conf['forwarding_mode'])
1692 if 'mirror_destination' in vntype_conf:
1693 mirror_dst_attr = vn_type_obj.get_mirror_destination()
1694 if vntype_conf['mirror_destination'] != mirror_dst_attr:
1695 changes['mirror_destination'] = \
1696 {'from': mirror_dst_attr,
1697 'to': vntype_conf['mirror_destination']}
1698 vn_type_obj.set_mirror_destination(
1699 vntype_conf['mirror_destination'])
1700 if 'rpf' in vntype_conf:
1701 rpf_attr = vn_type_obj.get_rpf()
1702 if vntype_conf['rpf'] != rpf_attr:
1703 changes['rpf'] = \
1704 {'from': rpf_attr,
1705 'to': vntype_conf['rpf']}
1706 vn_type_obj.set_rpf(vntype_conf['rpf'])
1707 vn_obj.set_virtual_network_properties(vn_type_obj)
Jan Cacha859e6b2018-01-09 17:34:18 +01001708
ibumarskov6ec99c42019-02-20 11:44:22 +04001709 if changes:
1710 ret['changes'] = changes
1711 if __opts__['test']:
1712 ret['result'] = None
1713 ret['comment'] = "Virtual network " + name + " will be updated"
1714 else:
1715 ret['comment'] = "VirtualNetwork " + name + " has been updated"
1716 vnc_client.virtual_network_update(vn_obj)
1717 else:
1718 ret['comment'] = 'Virtual network ' + name + \
1719 ' already exists and is updated'
Jan Cacha859e6b2018-01-09 17:34:18 +01001720 else:
ibumarskov6ec99c42019-02-20 11:44:22 +04001721 vn_obj = VirtualNetwork(name, prj_obj)
Jan Cachb3092722018-01-31 12:46:16 +01001722
ibumarskov6ec99c42019-02-20 11:44:22 +04001723 # Configure IPAM properties
1724 if subnet_conf:
1725 if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf:
1726 vn_obj.add_network_ipam(ipam_obj,
1727 VnSubnetsType(ipam_subnets=[ipam_sn]))
1728
1729 # Configure VirtualNetwork properties
1730 if router_external is not None:
1731 vn_obj.set_router_external(router_external)
1732 if route_target_list is not None:
1733 vn_obj.set_route_target_list(route_target_list)
1734
1735 # Configure VirtualNetworkType properties
1736 if vntype_conf:
1737 vn_type_obj = VirtualNetworkType()
1738 if 'allow_transit' in vntype_conf:
1739 vn_type_obj.set_allow_transit(vntype_conf['allow_transit'])
1740 if 'vxlan_network_identifier' in vntype_conf:
1741 vn_type_obj.set_vxlan_network_identifier(
1742 vntype_conf['vxlan_network_identifier'])
1743 if 'forwarding_mode' in vntype_conf:
1744 if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3']:
1745 vn_type_obj.set_forwarding_mode(
1746 vntype_conf['forwarding_mode'])
1747 if 'mirror_destination' in vntype_conf:
1748 vn_type_obj.set_mirror_destination(vntype_conf['mirror_destination'])
1749 if 'rpf' in vntype_conf:
1750 vn_type_obj.set_rpf(vntype_conf['rpf'])
1751 vn_obj.set_virtual_network_properties(vn_type_obj)
1752
1753 if __opts__['test']:
1754 ret['result'] = None
1755 ret['comment'] = "VirtualNetwork " + name + " will be created"
1756 else:
1757 vnc_client.virtual_network_create(vn_obj)
1758 ret['comment'] = "VirtualNetwork " + name + " has been created"
1759 ret['changes'] = {'VirtualNetwork': {'created': name}}
Jan Cacha859e6b2018-01-09 17:34:18 +01001760 return ret
1761
1762
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001763def service_appliance_set_list(**kwargs):
1764 '''
1765 Return a list of Contrail service appliance set
1766
1767 CLI Example:
1768
1769 .. code-block:: bash
1770
1771 salt '*' contrail.service_appliance_set_list
1772 '''
1773 ret = {}
1774 vnc_client = _auth(**kwargs)
1775 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1776 for service_appliance_set in service_appliance_sets:
1777 ret[service_appliance_set.name] = service_appliance_set.__dict__
1778 return ret
1779
1780
1781def service_appliance_set_get(name, **kwargs):
1782 '''
1783 Return a specific Contrail service appliance set
1784
1785 CLI Example:
1786
1787 .. code-block:: bash
1788
1789 salt '*' contrail.service_appliance_set_get name
1790 '''
1791 ret = {}
1792 sas_objs = service_appliance_set_list(**kwargs)
1793 if name in sas_objs:
1794 ret[name] = sas_objs.get(name)
1795 if len(ret) != 1:
1796 return {'result': False,
1797 'Error': "Error in the retrieving service apliance set."}
1798 return ret
1799
1800
1801def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1802 '''
1803 Create Contrail service appliance set
1804
1805 CLI Example:
1806
1807 .. code-block:: bash
1808
1809 salt '*' contrail.service_appliance_set_create name
1810 '''
1811 ret = {'name': name,
1812 'changes': {},
1813 'result': True,
1814 'comment': ''}
1815 vnc_client = _auth(**kwargs)
1816 gsc_obj = _get_config(vnc_client)
1817 sas_objs = service_appliance_set_list(**kwargs)
1818 if name in sas_objs:
1819 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1820 else:
1821 service_appliance_set_obj = ServiceApplianceSet(
1822 name, gsc_obj)
1823 if properties:
1824 pairs = KeyValuePairs()
1825 for k, v in properties.items():
1826 pairs.add_key_value_pair(KeyValuePair(k, v))
1827 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1828 if driver:
1829 service_appliance_set_obj.set_service_appliance_driver(driver)
1830 if ha_mode:
1831 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1832 if __opts__['test']:
1833 ret['result'] = None
1834 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1835 else:
1836 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1837 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1838 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1839 return ret
1840
1841
1842def service_appliance_set_delete(name, **kwargs):
1843 '''
1844 Delete specific Contrail service appliance set
1845
1846 CLI Example:
1847
1848 .. code-block:: bash
1849
1850 salt '*' contrail.service_appliance_set_delete name
1851 '''
1852 ret = {'name': name,
1853 'changes': {},
1854 'result': True,
1855 'comment': ''}
1856 vnc_client = _auth(**kwargs)
1857 gsc_obj = _get_config(vnc_client)
1858 sas_obj = ServiceApplianceSet(name, gsc_obj)
1859 if __opts__['test']:
1860 ret['result'] = None
1861 ret['comment'] = "Service appliance set " + name + " will be deleted"
1862 else:
1863 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1864 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1865 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1866 return ret
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001867
1868def global_system_config_list(**kwargs):
1869 '''
1870 Return a list of all global system configs
1871
1872 CLI Example:
1873
1874 .. code-block:: bash
1875
1876 salt '*' contrail.global_system_config_list
1877 '''
1878
1879 ret = {}
1880 vnc_client = _auth(**kwargs)
1881 gsysconfs = vnc_client._objects_list('global-system-config', detail=True)
1882 for gsysconf in gsysconfs:
1883 ret[gsysconf.name] = gsysconf.__dict__
1884 return ret
1885
1886
1887def global_system_config_get(name, **kwargs):
1888 '''
1889 Return a specific Contrail global system config
1890
1891 CLI Example:
1892
1893 .. code-block:: bash
1894
1895 salt '*' contrail.global_system_config_get name
1896 '''
1897 ret = {}
1898 vnc_client = _auth(**kwargs)
1899 gsc_objs = vnc_client._objects_list('global-system-config', detail=True)
1900 for gsc_obj in gsc_objs:
1901 if name == gsc_obj.name:
1902 ret[name] = gsc_obj.__dict__
1903 if len(ret) == 0:
1904 return {'Error': 'Error in retrieving global system config.'}
1905 return ret
1906
1907
1908def global_system_config_create(name, ans=64512, grp=None, **kwargs):
1909 '''
1910 Create Contrail global system config
1911
1912 CLI Example:
1913
1914 .. code-block:: bash
1915
1916 salt '*' contrail.global_system_config_create name=default-global-system-config ans=64512
1917 '''
1918 ret = {'name': name,
1919 'changes': {},
1920 'result': True,
1921 'comment': ''}
1922 vnc_client = _auth(**kwargs)
1923
1924 gsc_objs = global_system_config_list(**kwargs)
1925 if name in gsc_objs:
1926 config_obj = vnc_client.global_system_config_read(fq_name=[name])
1927 if config_obj.graceful_restart_parameters and not HAS_OLD:
1928 curr_grp = str(config_obj.graceful_restart_parameters).replace(" ", "").split(",")
1929 curr_grpd = dict(item.split('=') for item in curr_grp)
psvimbersky3c84e272018-01-02 10:34:29 +01001930 else:
1931 curr_grpd = None
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001932
1933 if grp and 'enable' in grp and not HAS_OLD:
1934 grp_obj = GracefulRestartParametersType()
1935 if 'enable' in grp:
1936 grp_obj.enable = grp['enable']
1937 if curr_grpd and str(grp['enable']) != str(curr_grpd['enable']):
1938 ret['changes']['enable'] = {"from": str(curr_grpd['enable']), "to": str(grp['enable'])}
1939 elif not curr_grpd:
1940 ret['changes']['enable'] = {"from": None, "to": grp['enable']}
1941 if 'restart_time' in grp:
1942 grp_obj.restart_time = grp['restart_time']
1943 if curr_grpd and grp['restart_time'] != int(curr_grpd['restart_time']):
1944 ret['changes']['restart_time'] = {"from": int(curr_grpd['restart_time']), "to": grp['restart_time']}
1945 elif not curr_grpd:
1946 ret['changes']['restart_time'] = {"from": None, "to": grp['restart_time']}
1947 if 'end_of_rib_timeout' in grp:
1948 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1949 if curr_grpd and grp['end_of_rib_timeout'] != int(curr_grpd['end_of_rib_timeout']):
1950 ret['changes']['end_of_rib_timeout'] = {"from": int(curr_grpd['end_of_rib_timeout']), "to": grp['end_of_rib_timeout']}
1951 elif not curr_grpd:
1952 ret['changes']['end_of_rib_timeout'] = {"from": None, "to": grp['end_of_rib_timeout']}
1953 if 'bgp_helper_enable' in grp:
1954 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1955 if curr_grpd and str(grp['bgp_helper_enable']) != str(curr_grpd['bgp_helper_enable']):
1956 ret['changes']['bgp_helper_enable'] = {"from": str(curr_grpd['bgp_helper_enable']), "to": grp['bgp_helper_enable']}
1957 elif not curr_grpd:
1958 ret['changes']['bgp_helper_enable'] = {"from": None, "to": grp['bgp_helper_enable']}
1959 if 'xmpp_helper_enable' in grp:
1960 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1961 if curr_grpd and str(grp['xmpp_helper_enable']) != str(curr_grpd['xmpp_helper_enable']):
1962 ret['changes']['xmpp_helper_enable'] = {"from": str(curr_grpd['xmpp_helper_enable']), "to": grp['xmpp_helper_enable']}
1963 elif not curr_grpd:
1964 ret['changes']['xmpp_helper_enable'] = {"from": None, "to": grp['xmpp_helper_enable']}
1965 if 'long_lived_restart_time' in grp:
1966 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1967 if curr_grpd and grp['long_lived_restart_time'] != int(curr_grpd['long_lived_restart_time']):
1968 ret['changes']['long_lived_restart_time'] = {"from": int(curr_grpd['long_lived_restart_time']), "to": grp['long_lived_restart_time']}
1969 elif not curr_grpd:
1970 ret['changes']['long_lived_restart_time'] = {"from": None, "to": grp['long_lived_restart_time']}
1971 else:
1972 grp_obj = None
1973
1974 config_obj.graceful_restart_parameters = grp_obj
1975
1976 if ans:
1977 if config_obj.autonomous_system != ans:
1978 ret['changes']['autonomous_system'] = {"from": config_obj.autonomous_system, "to": ans}
1979 config_obj.autonomous_system = ans
1980
1981 vnc_client.global_system_config_update(config_obj)
1982 ret['comment'] = 'Global system config ' + name + ' has been updated'
1983 else:
1984 config_obj = GlobalSystemConfig(name=name)
1985 if grp and not HAS_OLD:
1986 grp_obj = GracefulRestartParametersType()
1987 if 'enable' in grp:
1988 grp_obj.enable = grp['enable']
1989 if 'restart_time' in grp:
1990 grp_obj.restart_time = grp['restart_time']
1991 if 'end_of_rib_timeout' in grp:
1992 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1993 if 'bgp_helper_enable' in grp:
1994 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1995 if 'xmpp_helper_enable' in grp:
1996 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1997 if 'long_lived_restart_time' in grp:
1998 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1999 config_obj.graceful_restart_parameters = grp_obj
2000 if ans:
2001 config_obj.autonomous_system = ans
2002
2003 vnc_client.global_system_config_create(config_obj)
2004 ret['changes'] = {"created": "new"}
2005 ret['comment'] = 'Global system config ' + name + ' has been created '
2006
2007 return ret
2008
2009
2010def global_system_config_delete(name, **kwargs):
2011 '''
2012 Delete specific Contrail global system config
2013
2014 CLI Example:
2015
2016 .. code-block:: bash
2017
2018 salt '*' contrail.global_system_config_delete name
2019 '''
2020 ret = {'name': name,
2021 'changes': {},
2022 'result': True,
2023 'comment': ''}
2024 vnc_client = _auth(**kwargs)
2025
2026 gsc_obj = GlobalSystemConfig(name)
2027 if __opts__['test']:
2028 ret['result'] = None
2029 ret['comment'] = "Global system config " + name + " will be deleted"
2030 else:
2031 vnc_client.global_system_config_delete(fq_name=gsc_obj.get_fq_name())
2032 ret['comment'] = "GlobalSystemConfig " + name + " has been deleted"
2033 ret['changes'] = {'GlobalSystemConfig': {'old': name, 'new': ''}}
2034 return ret
Jan Cachb3092722018-01-31 12:46:16 +01002035
2036
2037def list_floating_ip_pools(**kwargs):
2038 '''
2039 List all floating ip pools
2040
2041 CLI Example:
2042 .. code-block:: bash
2043 salt '*' contrail.list_floating_ip_pools
2044 '''
2045 vnc_client = _auth(**kwargs)
2046 pools = vnc_client.floating_ip_pools_list()
2047 # list of floating ip pools objects
2048 fp_list = []
2049
2050 for pool in vnc_client.floating_ip_pools_list()['floating-ip-pools']:
2051 fip_obj = vnc_client.floating_ip_pool_read(pool['fq_name'])
2052 fp_list.append(fip_obj)
2053 # print given pool
2054 fip_obj.dump()
2055
2056def update_floating_ip_pool(vn_name, vn_project, vn_domain=None,
2057 owner_access=None, global_access=None,
2058 projects=None, **kwargs):
2059 '''
2060 Update specific floating ip pool
2061
2062
2063 CLI Example
2064 .. code-block:: bash
2065 salt-call contrail.update_floating_ip_pool \
2066 'FLOATING-TEST' \
2067 'admin' \
2068 'default-domain' \
2069 7 7 \
2070 [['pepa',4],['karel',7]]
2071
2072
2073 params:
2074 vn_name - name of the virtual network, which to use
2075 vn-project - project which includes virtual network
2076 vn-domain - domain wchich includes vn_project and vn_name
2077 owner_access (int) - Permission rights for owner
2078 global_access (int) - Permission rights for others
2079 projects (list) - list of ShareType(tenant_name,tennat_permissions)
2080 '''
2081 ret = {'name': vn_name + "-default pool",
2082 'changes': {},
2083 'result': True,
2084 'comment': ''}
2085
2086 if vn_domain is None:
2087 vn_domain = 'default-domain'
2088 fip_obj = None
2089
2090 vnc_client = _auth(**kwargs)
2091 p_fq_name = [vn_domain, vn_project, vn_name, 'default']
2092 fip_obj = vnc_client.floating_ip_pool_read(fq_name=p_fq_name)
2093
2094 changes = {}
2095 # get perms from fip_obj (Floatin ip pool)
2096 perms2 = fip_obj.get_perms2()
2097 if owner_access is not None:
2098 if perms2.get_owner_access() != owner_access:
2099 changes['owner_access'] = {'old': str(perms2.get_owner_access()),
2100 'new': str(owner_access)}
2101 perms2.set_owner_access(owner_access)
2102
2103 if global_access is not None:
2104 if perms2.get_global_access() != global_access:
2105 changes['global_access'] = {'old': str(perms2.get_global_access()),
2106 'new': str(global_access)}
2107 perms2.set_global_access(global_access)
2108
2109 # list which represents the new state of perms
2110 final_list = []
2111 if projects:
2112 for item in perms2.get_share():
2113 for share in projects:
2114 if item.get_tenant() == share[0]:
2115 # project is in the new and old list
2116 # check is the permission number is same
2117 if item.get_tenant_access() == share[1]:
2118 # this project and permission is without change, keep it
2119 final_list.append(item)
2120 break
2121 else:
2122 # project exists but change the permission
2123 final_list.append(ShareType(tenant=share[0],
2124 tenant_access=share[1]))
2125 # show changes
2126 n = str('share-'+share[0])
2127 old_str = "permission for project {0} was {1}"
2128 new_str = "permission for project {0} will be {1}"
2129 old = old_str.format(share[0],
2130 str(item.get_tenant_access()))
2131
2132 new = new_str.format(share[0], str(share[1]))
2133 changes[n] = {'old': old, 'new': new}
2134 break
2135 else:
2136 rm_name = "share-" + item.get_tenant()
2137 changes[rm_name] = item.get_tenant() + " will be removed"
2138
2139 # check for the completly new projects
2140 for item in projects:
2141 for share in final_list:
2142 if item[0] == share.get_tenant():
2143 break
2144 else:
2145 final_list.append(ShareType(tenant=item[0],
2146 tenant_access=item[1]))
2147 name = 'share-' + str(item[0])
2148 c_str = '{0} will be added with permissions {1}'
2149 changes[name] = c_str.format(name, item[1])
2150 else:
2151 for item in perms2.get_share():
2152 rm_name = "share-" + item.get_tenant()
2153 changes[rm_name] = item.get_tenant() + " will be removed"
2154
2155 if __opts__['test']:
2156 ret['result'] = None
2157 ret['comment'] = changes
2158
2159 return ret
2160 else:
2161 ret['comment'] = changes
2162 perms2.set_share(final_list)
2163 fip_obj.set_perms2(perms2)
2164 vnc_client.floating_ip_pool_update(fip_obj)
2165
2166 return ret
Sergey Matov16896ac2018-02-15 15:46:31 +04002167
2168
2169def show_rbac_rules(api_access_list_entries):
2170 if api_access_list_entries is None:
2171 return 'Empty RBAC group!'
2172
2173 rule_list = api_access_list_entries.get_rbac_rule()
2174 response = 'Rules (%d):' % len(rule_list) + '----------\n'
2175 for idx, rule in enumerate(rule_list):
2176 o = rule.rule_object
2177 f = rule.rule_field
2178 ps = ', '.join([p.role_name+':'+p.role_crud for p in rule.rule_perms])
2179 o_f = "%s.%s" % (o, f) if f else o
2180 response += '%2d %-32s %s\n' % (idx, o_f, ps)
2181 return response
2182
2183
2184def vnc_read_obj(vnc, obj_type, fq_name):
2185 method_name = obj_type.replace('-', '_')
2186 method = getattr(vnc, "%s_read" % (method_name))
2187 try:
2188 return method(fq_name=fq_name)
2189 except NoIdError:
2190 print '%s %s not found!' % (obj_type, fq_name)
2191 return None
2192
2193
2194def rbac_show_group(name, uuid, **kwargs):
2195 '''
2196 Show specific RBAC group
2197
2198
2199 CLI Example
2200 .. code-block:: bash
2201 salt-call contrail.rbac_show_group name \
2202 'default-domain:default-project:default'
2203
2204 params:
2205 name - one of pair {name, uuid} addresing to access-list
2206 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2207 including domain and project
2208
2209 '''
2210 vnc = _auth(**kwargs)
2211 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2212 ret = {'name': fq_name,
2213 'changes': {},
2214 'result': True,
2215 'comment': ''}
2216 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2217 if not rg:
2218 ret['comment'] = 'No rules found'
2219 return ret
2220
2221 ret['comment'] = show_rbac_rules(rg.get_api_access_list_entries())
2222 return ret
2223
2224
2225def rbac_create_group(uuid, **kwargs):
2226 '''
2227 Create RBAC group
2228
2229 CLI Example
2230 .. code-block:: bash
2231 salt-call contrail.rbac_create_group name \
2232 'default-domain:default-project:default'
2233
2234 params:
2235 name - one of pair {name, uuid} addresing to access-list
2236 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2237 including domain and project
2238
2239 '''
2240 vnc = _auth(**kwargs)
2241 fq_name = uuid.split(':')
2242 ret = {'name': fq_name,
2243 'changes': {},
2244 'result': True,
2245 'comment': ''}
2246 if len(fq_name) != 2 and len(fq_name) != 3:
2247 ret['comment'] = 'Fully qualified name of rbac group expected'
2248 return ret
2249
2250 name = fq_name[-1]
2251
2252 if len(fq_name) == 2:
2253 if fq_name[0] == 'default-global-system-config':
2254 pobj = vnc.global_system_config_read(fq_name=fq_name[0:1])
2255 else:
2256 pobj = vnc.domain_read(fq_name=fq_name[0:1])
2257 else:
2258 pobj = vnc.project_read(fq_name=fq_name[0:2])
2259
2260 rentry = RbacRuleEntriesType([])
2261 rg = ApiAccessList(name, parent_obj=pobj, api_access_list_entries=rentry)
2262
2263 if __opts__['test']:
2264 ret['result'] = None
2265 ret['comment'] = "RBAC group " + uuid + " will be created"
2266
2267 return ret
2268 else:
2269 vnc.api_access_list_create(rg)
2270 rg2 = vnc.api_access_list_read(fq_name=fq_name)
2271 rge = rg.get_api_access_list_entries()
2272 show_rbac_rules(rge)
2273 ret['comment'] = "RBAC group " + uuid + " has been created"
2274
2275 return ret
2276
2277
2278def rbac_delete_group(name, uuid, **kwargs):
2279 '''
2280 Delete RBAC group
2281
2282 CLI Example
2283 .. code-block:: bash
2284 salt-call contrail.rbac_delete_group name \
2285 'default-domain:default-project:default'
2286
2287 params:
2288 name - one of pair {name, uuid} addresing to access-list
2289 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2290 including domain and project
2291
2292 '''
2293 vnc = _auth(**kwargs)
2294 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2295 ret = {'name': fq_name,
2296 'changes': {},
2297 'result': True,
2298 'comment': ''}
2299 if len(fq_name) != 2 and len(fq_name) != 3:
2300 ret['comment'] = 'Fully qualified name of rbac group expected'
2301 return ret
2302 name = fq_name[-1]
2303
2304 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2305 if not rg:
2306 ret['comment'] = 'No rules found'
2307 return ret
2308 rge = rg.get_api_access_list_entries()
2309 show_rbac_rules(rge)
2310
2311 if __opts__['test']:
2312 ret['result'] = None
2313 ret['comment'] = "RBAC group " + uuid + " will be deleted"
2314
2315 return ret
2316 else:
2317 vnc.api_access_list_delete(fq_name=fq_name)
2318 ret['comment'] = "RBAC group " + uuid + " has been deleted"
2319
2320 return ret
2321
2322
2323def rbac_add_rule(name, uuid, add_rule, **kwargs):
2324 '''
2325 Add rule to specific RBAC group
2326
2327 CLI Example
2328 .. code-block:: bash
2329 salt-call contrail.rbac_add_rule name \
2330 'default-domain:default-project:default' \
2331 '* admin:CRUD'
2332
2333 params:
2334 name - one of pair {name, uuid} addresing to access-list
2335 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2336 including domain and project
2337 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2338 list of <role:CRUD>' to be added
2339
2340 '''
2341 vnc = _auth(**kwargs)
2342 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2343 rule = build_rule(add_rule)
2344 ret = {'name': fq_name,
2345 'changes': {},
2346 'result': True,
2347 'comment': ''}
2348 if rule is None:
2349 ret['comment'] = 'A rule string must be specified for this operation'
2350 return ret
2351
2352 # rbac rule entry consists of one or more rules
2353 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2354 if not rg:
2355 ret['comment'] = 'No rules found'
2356 return ret
2357
2358 rge = rg.get_api_access_list_entries()
2359 if rge is None:
2360 rge = RbacRuleEntriesType([])
2361 show_rbac_rules(rge)
2362
2363 # avoid duplicates
2364 match = find_rule(rge, rule)
2365 if not match:
2366 rge.add_rbac_rule(rule)
2367 else:
2368 build_perms(rge.rbac_rule[match[0]-1], match[3])
2369
2370 show_rbac_rules(rge)
2371
2372 if __opts__['test']:
2373 ret['result'] = None
2374 ret['comment'] = "Rule " + add_rule
2375 ret['comment'] += " will be created for RBAC group " + uuid
2376
2377 return ret
2378 else:
2379 rg.set_api_access_list_entries(rge)
2380 vnc.api_access_list_update(rg)
2381 ret['comment'] = "Rule " + add_rule
2382 ret['comment'] += " has been added for RBAC group " + uuid
2383
2384 return ret
2385
2386
2387def rbac_delete_rule(name, uuid, del_rule, **kwargs):
2388 '''
2389 Delete rule to specific RBAC group
2390
2391 CLI Example
2392 .. code-block:: bash
2393 salt-call contrail.rbac_delete_rule name \
2394 'default-domain:default-project:default' \
2395 '* admin:CRUD'
2396
2397 params:
2398 name - one of pair {name, uuid} addresing to access-list
2399 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2400 including domain and project
2401 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2402 list of <role:CRUD>' to be deleted
2403
2404 '''
2405 vnc = _auth(**kwargs)
2406 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2407 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2408 ret = {'name': fq_name,
2409 'changes': {},
2410 'result': True,
2411 'comment': ''}
2412 if not rg:
2413 ret['comment'] = 'No rules found'
2414 return ret
2415 rge = rg.get_api_access_list_entries()
2416 show_rbac_rules(rge)
2417
2418 del_idx = re.match("^[0-9]+$", del_rule)
2419 if del_idx:
2420 del_idx = int(del_idx.group())
2421 rc = len(rge.rbac_rule)
2422 if del_idx > rc or del_idx < 1:
2423 ret['comment'] = 'Invalid rule index to delete.'
2424 ret['comment'] += 'Value must be 1-%d' % rc
2425 return ret
2426 match = (del_idx, True)
2427 else:
2428 rule = build_rule(del_rule)
2429 match = find_rule(rge, rule)
2430
2431 if not match:
2432 ret['comment'] = "Rule not found. Unchanged"
2433 return ret
2434 elif match[1]:
2435 rge.rbac_rule.pop(match[0]-1)
2436 else:
2437 build_perms(rge.rbac_rule[match[0]-1], match[2])
2438 show_rbac_rules(rge)
2439
2440 if __opts__['test']:
2441 ret['result'] = None
2442 ret['comment'] = "Rule " + del_rule
2443 ret['comment'] += " will be cleared from RBAC group " + uuid
2444
2445 return ret
2446 else:
2447 rg.set_api_access_list_entries(rge)
2448 vnc.api_access_list_update(rg)
2449 ret['comment'] = "Rule " + del_rule
2450 ret['comment'] += " has been cleared from RBAC group " + uuid
2451
2452 return ret