blob: d8a5114a21a5a786836f5027d7b1e8091f8fddfe [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
729 salt '*' contrail.global_vrouter_config_create name=global-vrouter-config parent_type=global-system-config encap_priority="MPLSoUDP,MPLSoGRE" vxlan_vn_id_mode="automatic" fq_names="['default-global-system-config', 'default-global-vrouter-config']"
730 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200731 ret = {'name': name,
732 'changes': {},
733 'result': True,
734 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200735 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200736 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200737 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
738 if name in vrouter_conf_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200739 ret['comment'] = 'Global vrouter config ' + name + ' already exists'
740 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200741 else:
742 vrouter_conf_obj = GlobalVrouterConfig(
743 name=name,
744 parent_obj=None,
745 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
746 fq_name=fq_names,
747 vxlan_network_identifier_mode=vxlan_vn_id_mode,
748 parent_type=parent_type,
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100749 flow_export_rate=flow_export_rate,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200750 )
751 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200752 ret['result'] = None
753 ret['comment'] = "Global vRouter config " + name + " will be created"
754 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200755 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200756 ret['comment'] = "Global vRouter config " + name + " has been created"
757 ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}}
758 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200759
760
761def global_vrouter_config_delete(name, **kwargs):
762 '''
763 Delete specific Contrail global vrouter config
764
765 CLI Example:
766
767 .. code-block:: bash
768
769 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
770 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200771 ret = {'name': name,
772 'changes': {},
773 'result': True,
774 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200775 vnc_client = _auth(**kwargs)
776 gsc_obj = _get_config(vnc_client)
777 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
778 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200779 ret['result'] = None
780 ret['comment'] = "Global vRouter config " + name + " will be deleted"
781 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200782 vnc_client.global_vrouter_config_delete(
783 fq_name=vrouter_conf_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200784 ret['comment'] = "Global vRouter config " + name + " has been deleted"
785 ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}}
786 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100787
788
789def analytics_node_list(**kwargs):
790 '''
791 Return a list of all Contrail analytics nodes
792
793 CLI Example:
794
795 .. code-block:: bash
796
797 salt '*' contrail.analytics_node_list
798 '''
799 ret = {}
800 vnc_client = _auth(**kwargs)
801 node_objs = vnc_client._objects_list('analytics-node', detail=True)
802 for node_obj in node_objs:
803 ret[node_obj.name] = node_obj.__dict__
804 return ret
805
806
807def analytics_node_get(name, **kwargs):
808 '''
809 Return a specific Contrail analytics node
810
811 CLI Example:
812
813 .. code-block:: bash
814
815 salt '*' contrail.analytics_node_get nal01
816 '''
817 ret = {}
818 vrouter_objs = analytics_node_list(**kwargs)
819 if name in vrouter_objs:
820 ret[name] = vrouter_objs.get(name)
821 if len(ret) == 0:
822 return {'Error': 'Error in retrieving analytics node.'}
823 return ret
824
825
826def analytics_node_create(name, ip_address, **kwargs):
827 '''
828 Create specific Contrail analytics node
829
830 CLI Example:
831
832 .. code-block:: bash
833
834 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
835 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200836
837 ret = {'name': name,
838 'changes': {},
839 'result': True,
840 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100841 vnc_client = _auth(**kwargs)
842 gsc_obj = _get_config(vnc_client)
843 analytics_node_objs = analytics_node_list(**kwargs)
844 if name in analytics_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200845 ret['comment'] = 'Analytics node %s already exists'
846 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100847 else:
848 analytics_node_obj = AnalyticsNode(
849 name, gsc_obj,
850 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200851 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200852 ret['result'] = None
853 ret['comment'] = "AnalyticsNode " + name + " will be created"
854 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100855 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200856 ret['comment'] = "AnalyticsNode " + name + " has been created"
857 ret['changes'] = {'Analytics Node': {'old': '', 'new': name}}
858 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100859
860
861def analytics_node_delete(name, **kwargs):
862 '''
863 Delete specific Contrail analytics node
864
865 CLI Example:
866
867 .. code-block:: bash
868
869 salt '*' contrail.analytics_node_delete cmp01
870 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200871 ret = {'name': name,
872 'changes': {},
873 'result': True,
874 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100875 vnc_client = _auth(**kwargs)
876 gsc_obj = _get_config(vnc_client)
877 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200878 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200879 ret['result'] = None
880 ret['comment'] = "AnalyticsNode " + name + " will be deleted"
881 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100882 vnc_client.analytics_node_delete(
883 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200884 ret['comment'] = "AnalyticsNode " + name + " has been deleted"
885 ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}}
886 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100887
888
889def config_node_list(**kwargs):
890 '''
891 Return a list of all Contrail config nodes
892
893 CLI Example:
894
895 .. code-block:: bash
896
897 salt '*' contrail.config_node_list
898 '''
899 ret = {}
900 vnc_client = _auth(**kwargs)
901 node_objs = vnc_client._objects_list('config-node', detail=True)
902 for node_obj in node_objs:
903 ret[node_obj.name] = node_obj.__dict__
904 return ret
905
906
907def config_node_get(name, **kwargs):
908 '''
909 Return a specific Contrail config node
910
911 CLI Example:
912
913 .. code-block:: bash
914
915 salt '*' contrail.config_node_get nal01
916 '''
917 ret = {}
918 vrouter_objs = config_node_list(**kwargs)
919 if name in vrouter_objs:
920 ret[name] = vrouter_objs.get(name)
921 if len(ret) == 0:
922 return {'Error': 'Error in retrieving config node.'}
923 return ret
924
925
926def config_node_create(name, ip_address, **kwargs):
927 '''
928 Create specific Contrail config node
929
930 CLI Example:
931
932 .. code-block:: bash
933
934 salt '*' contrail.config_node_create ntw03 10.10.10.103
935 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200936 ret = {'name': name,
937 'changes': {},
938 'result': True,
939 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100940 vnc_client = _auth(**kwargs)
941 gsc_obj = _get_config(vnc_client)
942 config_node_objs = config_node_list(**kwargs)
943 if name in config_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200944 ret['comment'] = 'Config node ' + name + ' already exists'
945 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100946 else:
947 config_node_obj = ConfigNode(
948 name, gsc_obj,
949 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200950 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200951 ret['comment'] = "ConfigNode " + name + " will be created"
952 ret['result'] = None
953 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100954 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200955 ret['comment'] = "ConfigNode " + name + " has been created"
956 ret['changes'] = {'ConfigNode': {'old': '', 'new': name}}
957 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100958
959
960def config_node_delete(name, **kwargs):
961 '''
962 Delete specific Contrail config node
963
964 CLI Example:
965
966 .. code-block:: bash
967
968 salt '*' contrail.config_node_delete cmp01
969 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200970 ret = {'name': name,
971 'changes': {},
972 'result': True,
973 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100974 vnc_client = _auth(**kwargs)
975 gsc_obj = _get_config(vnc_client)
976 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200977 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200978 ret['comment'] = "ConfigNode " + name + " will be deleted"
979 ret['result'] = None
980 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100981 vnc_client.config_node_delete(
982 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200983 ret['comment'] = "ConfigNode " + name + " has been deleted"
984 ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}}
985 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100986
987
988def bgp_router_list(**kwargs):
989 '''
990 Return a list of all Contrail BGP routers
991
992 CLI Example:
993
994 .. code-block:: bash
995
996 salt '*' contrail.bgp_router_list
997 '''
998 ret = {}
999 vnc_client = _auth(**kwargs)
1000 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
1001 for bgp_router_obj in bgp_router_objs:
1002 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
1003 return ret
1004
1005
1006def bgp_router_get(name, **kwargs):
1007 '''
1008 Return a specific Contrail BGP router
1009
1010 CLI Example:
1011
1012 .. code-block:: bash
1013
1014 salt '*' contrail.bgp_router_get nal01
1015 '''
1016 ret = {}
1017 bgp_router_objs = bgp_router_list(**kwargs)
1018 if name in bgp_router_objs:
1019 ret[name] = bgp_router_objs.get(name)
1020 if len(ret) == 0:
1021 return {'Error': 'Error in retrieving BGP router.'}
1022 return ret
1023
1024
Marek Celoud3097e5b2018-01-09 13:52:14 +01001025def bgp_router_create(name, type, ip_address, asn=64512, key_type=None, key=None, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +01001026 '''
1027 Create specific Contrail control node
1028
1029 CLI Example:
1030
1031 .. code-block:: bash
1032
1033 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
1034 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
1035 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001036 ret = {'name': name,
1037 'changes': {},
1038 'result': True,
1039 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001040 vnc_client = _auth(**kwargs)
1041
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001042 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
1043 'inet6-vpn']
1044 if type != 'control-node':
1045 address_families.remove('erm-vpn')
1046
Marek Celoud3097e5b2018-01-09 13:52:14 +01001047 key_type = None if key_type == 'None' else key_type
1048 key = None if key == 'None' else key
1049
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001050 bgp_addr_fams = AddressFamilies(address_families)
1051 bgp_sess_attrs = [
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001052 BgpSessionAttributes(address_families=bgp_addr_fams)]
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001053 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
1054 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
1055 rt_inst_obj = _get_rt_inst_obj(vnc_client)
1056
Marek Celoud3097e5b2018-01-09 13:52:14 +01001057 bgp_auth_data = None
1058
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001059 if type == 'control-node':
1060 vendor = 'contrail'
1061 elif type == 'router':
1062 vendor = 'mx'
Marek Celoud3097e5b2018-01-09 13:52:14 +01001063 if key_type == 'md5':
1064 key_id = 0
1065 key_items = AuthenticationKeyItem(key_id, key)
1066 bgp_auth_data = AuthenticationData(key_type, [key_items])
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001067 else:
1068 vendor = 'unknown'
1069
1070 router_params = BgpRouterParams(router_type=type,
1071 vendor=vendor, autonomous_system=int(asn),
1072 identifier=_get_ip(ip_address),
1073 address=_get_ip(ip_address),
Marek Celoud3097e5b2018-01-09 13:52:14 +01001074 port=179, address_families=bgp_addr_fams,
1075 auth_data=bgp_auth_data)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001076
Ales Komarekad46d2e2017-03-09 17:16:38 +01001077 bgp_router_objs = bgp_router_list(**kwargs)
1078 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001079 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001080
1081 if bgp_router_obj.bgp_router_parameters.autonomous_system != asn:
1082 ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}})
1083 if bgp_router_obj.bgp_router_parameters.vendor != vendor:
1084 ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}})
1085 if bgp_router_obj.bgp_router_parameters.address != ip_address:
1086 ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}})
Marek Celoud3097e5b2018-01-09 13:52:14 +01001087 try:
1088 if bgp_router_obj.bgp_router_parameters.auth_data.key_type != key_type:
1089 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_type, 'new': key_type}})
1090 except:
1091 if key_type != None:
1092 ret['changes'].update({"key_type": {'old': None, 'new': key_type}})
1093 if key_type == 'md5':
1094 try:
1095 if bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key != key:
1096 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key, 'new': key}})
1097 except:
1098 ret['changes'].update({"key_type": {'old': None, 'new': key}})
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001099
1100 if len(ret['changes']) == 0:
1101 return ret
1102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001103 bgp_router_obj.set_bgp_router_parameters(router_params)
1104 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001105 ret['result'] = None
1106 ret['comment'] = "BGP router " + name + " will be updated"
1107 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001108 vnc_client.bgp_router_update(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001109 ret['comment'] = "BGP router " + name + " has been updated"
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001110 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001111 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001112 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
1113 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001114 ret['result'] = None
1115 ret['comment'] = "BGP router " + name + " will be created"
1116 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001117 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001118 ret['comment'] = "BGP router " + name + " has been created"
1119 ret['changes'] = {'BGP router': {'old': name, 'new': ''}}
1120 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001121
1122
1123def bgp_router_delete(name, **kwargs):
1124 '''
1125 Delete specific Contrail control node
1126
1127 CLI Example:
1128
1129 .. code-block:: bash
1130
1131 salt '*' contrail.bgp_router_delete mx01
1132 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001133 ret = {'name': name,
1134 'changes': {},
1135 'result': True,
1136 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001137 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001138
1139 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001140 ret['result'] = None
1141 ret['comment'] = "BGP router " + name + " will be deleted"
1142 return ret
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001143
1144 bgp_router = bgp_router_get(name)
1145 if name in bgp_router:
1146 vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name'])
1147 ret['comment'] = "BGP router " + name + " has been deleted"
1148 ret['changes'] = {'BGP router': {'old': '', 'new': name}}
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001149 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001150
Ales Komarekad46d2e2017-03-09 17:16:38 +01001151
1152def database_node_list(**kwargs):
1153 '''
1154 Return a list of all Contrail database nodes
1155
1156 CLI Example:
1157
1158 .. code-block:: bash
1159
1160 salt '*' contrail.database_node_list
1161 '''
1162 ret = {}
1163 vnc_client = _auth(**kwargs)
1164 node_objs = vnc_client._objects_list('database-node', detail=True)
1165 for node_obj in node_objs:
1166 ret[node_obj.name] = node_obj.__dict__
1167 return ret
1168
1169
1170def database_node_get(name, **kwargs):
1171 '''
1172 Return a specific Contrail database node
1173
1174 CLI Example:
1175
1176 .. code-block:: bash
1177
1178 salt '*' contrail.database_node_get nal01
1179 '''
1180 ret = {}
1181 vrouter_objs = database_node_list(**kwargs)
1182 if name in vrouter_objs:
1183 ret[name] = vrouter_objs.get(name)
1184 if len(ret) == 0:
1185 return {'Error': 'Error in retrieving database node.'}
1186 return ret
1187
1188
1189def database_node_create(name, ip_address, **kwargs):
1190 '''
1191 Create specific Contrail database node
1192
1193 CLI Example:
1194
1195 .. code-block:: bash
1196
1197 salt '*' contrail.database_node_create ntw03 10.10.10.103
1198 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001199 ret = {'name': name,
1200 'changes': {},
1201 'result': True,
1202 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001203 vnc_client = _auth(**kwargs)
1204 gsc_obj = _get_config(vnc_client)
1205 database_node_objs = database_node_list(**kwargs)
1206 if name in database_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001207 ret['comment'] = 'Database node ' + name + ' already exists'
1208 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001209 else:
1210 database_node_obj = DatabaseNode(
1211 name, gsc_obj,
1212 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001213 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001214 ret['result'] = None
1215 ret['comment'] = "DatabaseNode " + name + " will be created"
1216 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001217 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001218 ret['comment'] = "DatabaseNode " + name + " has been created"
1219 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1220 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001221
1222
1223def database_node_delete(name, **kwargs):
1224 '''
1225 Delete specific Contrail database node
1226
1227 CLI Example:
1228
1229 .. code-block:: bash
1230
1231 salt '*' contrail.database_node_delete cmp01
1232 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001233 ret = {'name': name,
1234 'changes': {},
1235 'result': True,
1236 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001237 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001238 gsc_obj = _get_config(vnc_client)
1239 database_node_obj = DatabaseNode(name, gsc_obj)
1240 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001241 ret['result'] = None
1242 ret['comment'] = "DatabaseNode " + name + " will be deleted"
1243 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001244 vnc_client.database_node_delete(
1245 fq_name=database_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001246 ret['comment'] = "DatabaseNode " + name + " has been deleted"
1247 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1248 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001249
1250
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001251def _get_vrouter_config(vnc_client, gvc_name=None):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001252 try:
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001253 if not gvc_name:
1254 gvc_list = global_vrouter_config_list()
1255 gvc_name = gvc_list.values()[0]['name']
1256
Petr Jediný5f3efe32017-05-26 17:55:09 +02001257 config = vnc_client.global_vrouter_config_read(
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001258 fq_name=['default-global-system-config', gvc_name])
Petr Jediný5f3efe32017-05-26 17:55:09 +02001259 except Exception:
1260 config = None
1261
1262 return config
1263
1264
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001265def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001266 '''
1267 Return a list of all Contrail link local services
1268
1269 CLI Example:
1270
1271 .. code-block:: bash
1272
1273 salt '*' contrail.linklocal_service_list
1274 '''
1275 ret = {}
1276 vnc_client = _auth(**kwargs)
1277
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001278 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001279 if current_config is None:
1280 return ret
1281
1282 service_list_res = current_config.get_linklocal_services()
1283 if service_list_res is None:
1284 service_list_obj = {'linklocal_service_entry': []}
1285 else:
1286 service_list_obj = service_list_res.__dict__
1287 for _, value in service_list_obj.iteritems():
1288 for entry in value:
1289 service = entry.__dict__
1290 if 'linklocal_service_name' in service:
1291 ret[service['linklocal_service_name']] = service
1292 return ret
1293
1294
1295def linklocal_service_get(name, **kwargs):
1296 '''
1297 Return a specific Contrail link local service
1298
1299 CLI Example:
1300
1301 .. code-block:: bash
1302
1303 salt '*' contrail.linklocal_service_get llservice
1304 '''
1305 ret = {}
1306 services = linklocal_service_list(**kwargs)
1307 if name in services:
1308 ret[name] = services.get(name)
1309 if len(ret) == 0:
1310 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1311 return ret
1312
1313
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001314def 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 +02001315 '''
1316 Create specific Contrail link local service
1317
1318 CLI Example:
1319
1320 .. code-block:: bash
1321
1322 salt '*' contrail.linklocal_service_create \
1323 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1324 salt '*' contrail.linklocal_service_create \
1325 llservice 10.10.10.103 22 link-local.service.dns-name 22
1326 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001327 ret = {'name': name,
1328 'changes': {},
1329 'result': True,
1330 'comment': ''}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001331 vnc_client = _auth(**kwargs)
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001332 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001333 service_entry = LinklocalServiceEntryType(
1334 linklocal_service_name=name,
1335 linklocal_service_ip=lls_ip,
1336 linklocal_service_port=lls_port,
1337 ip_fabric_service_port=ipf_port)
1338 if isinstance(ipf_dns_or_ip, basestring):
1339 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1340 elif isinstance(ipf_dns_or_ip, list):
1341 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1342 service_entry.ip_fabric_DNS_service_name = ''
1343
1344 if current_config is None:
1345 new_services = LinklocalServicesTypes([service_entry])
1346 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001347 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001348 ret['result'] = None
1349 ret['comment'] = "Link local service " + name + " will be created"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001350 else:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001351 ret['comment'] = "Link local service " + name + " has been created"
1352 ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001353 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001354 else:
1355 _current_service_list = current_config.get_linklocal_services()
1356 if _current_service_list is None:
1357 service_list = {'linklocal_service_entry': []}
1358 else:
1359 service_list = _current_service_list.__dict__
1360 new_services = [service_entry]
1361 for key, value in service_list.iteritems():
1362 if key != 'linklocal_service_entry':
1363 continue
1364 for _entry in value:
1365 entry = _entry.__dict__
1366 if 'linklocal_service_name' in entry:
1367 if entry['linklocal_service_name'] == name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001368 ret['comment'] = 'Link local service ' + name + ' already exists'
1369 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001370 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001371 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001372 ret['result'] = None
1373 ret['comment'] = "LinkLocalSevices " + name + " will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001374 service_list[key] = new_services
1375 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001376 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001377 ret['result'] = None
1378 ret['comment'] = "LinkLocalSevices " + name + " will be updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001379 else:
1380 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001381 ret['comment'] = "LinkLocalSevices " + name + " has been created"
1382 ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001383 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001384
1385
1386def linklocal_service_delete(name, **kwargs):
1387 '''
1388 Delete specific link local service entry
1389
1390 CLI Example:
1391
1392 .. code-block:: bash
1393
1394 salt '*' contrail.linklocal_service_delete llservice
1395 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001396 ret = {'name': name,
1397 'changes': {},
1398 'result': True,
1399 'comment': ''}
1400 lls = linklocal_service_get(name)
1401 print (lls)
1402 if name in lls:
1403 if __opts__['test']:
1404 print " ------------ Test only ------------"
1405 ret['result'] = None
1406 ret['comment'] = "Link local service " + name + " will be deleted"
1407 return ret
1408 else:
1409 return ret
1410
Petr Jediný5f3efe32017-05-26 17:55:09 +02001411 vnc_client = _auth(**kwargs)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001412 current_config = _get_vrouter_config(vnc_client)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001413 if current_config is not None:
1414 _current_service_list = current_config.get_linklocal_services()
1415 if _current_service_list is None:
1416 service_list = {'linklocal_service_entry': []}
1417 else:
1418 service_list = _current_service_list.__dict__
1419 new_services = []
1420 for key, value in service_list.iteritems():
1421 if key != 'linklocal_service_entry':
1422 continue
1423 for _entry in value:
1424 entry = _entry.__dict__
1425 if 'linklocal_service_name' in entry:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001426 if entry['linklocal_service_name'] != name:
Petr Jediný5f3efe32017-05-26 17:55:09 +02001427 new_services.append(_entry)
1428 service_list[key] = new_services
1429 new_config = GlobalVrouterConfig(linklocal_services=service_list)
1430 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001431 ret['comment'] = "Link local service " + name + " will be deleted"
1432 ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
1433 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001434
1435
1436def virtual_machine_interface_list(**kwargs):
1437 '''
1438 Return a list of all Contrail virtual machine interfaces
1439
1440 CLI Example:
1441
1442 .. code-block:: bash
1443
1444 salt '*' contrail.virtual_machine_interfaces
1445 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001446 ret = []
1447 vnc_client = _auth(**kwargs)
1448 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1449 project_uuid = project.get_uuid()
1450
1451 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1452 detail=True, parent_id=project_uuid)
1453
1454 for vm_iface in vm_ifaces:
1455 ret.append(vm_iface.__dict__)
1456
1457 return ret
1458
1459
1460def virtual_machine_interface_create(name,
1461 virtual_network,
1462 mac_address=None,
1463 ip_address=None,
1464 security_group=None,
1465 **kwargs):
1466 '''
1467 Create specific Contrail virtual machine interface (Port)
1468
1469 CLI Example:
1470
1471 .. code-block:: bash
1472
1473 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1474 router_types:
1475 - tor-agent
1476 - tor-service-node
1477 - embedded
1478 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001479 ret = {}
1480 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001481 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1482
1483 vm_int = VirtualMachineInterface(name, parent_obj=project)
1484
1485 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001486 mac_address_obj = MacAddressesType([mac_address])
1487 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001488
1489 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001490 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001491 vnc_client, security_group, kwargs.get('tenant', 'admin')))
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001492 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001493
1494 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1495 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1496 vm_int.set_virtual_network(vnet_obj)
1497
1498 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1499 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1500
1501 vm_int.set_port_security_enabled(False)
1502 vnc_client.virtual_machine_interface_update(vm_int)
1503
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001504 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001505 ip = vnc_api.InstanceIp(name + '.ip')
1506 ip.set_virtual_machine_interface(vmi)
1507 ip.set_virtual_network(vnet_obj)
1508
1509 ip_uuid = vnc_client.instance_ip_create(ip)
1510
1511 if ip_address:
1512 ip.set_instance_ip_address(ip_address)
1513 vnc_client.instance_ip_update(ip)
1514
1515 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001516
1517
1518def virtual_network_list(**kwargs):
1519 '''
1520 Return a list of all Contrail virtual network
1521
1522 CLI Example:
1523
1524 .. code-block:: bash
1525
1526 salt '*' contrail.virtual_network
1527 '''
1528
1529 ret = {}
1530 vnc_client = _auth(**kwargs)
1531 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1532 for virtual_network in virtual_networks:
1533 ret[virtual_network.name] = virtual_network.__dict__
1534 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001535
1536
1537def virtual_network_get(name, **kwargs):
1538 '''
1539 Return a specific Contrail virtual network
1540
1541 CLI Example:
1542
1543 .. code-block:: bash
1544
1545 salt '*' contrail.virtual_network_get net01
1546 '''
1547 ret = {}
1548 vnet_objs = virtual_network_list(**kwargs)
1549 if name in vnet_objs:
1550 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001551 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001552 return {'result': False,
1553 'Error': 'Error in retrieving virtual networks.'}
1554 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001555
1556
ibumarskov6ec99c42019-02-20 11:44:22 +04001557def virtual_network_create(name, domain='default-domain', project='admin',
1558 ipam_domain='default-domain',
1559 ipam_project='default-project',
1560 ipam_name='default-network-ipam',
1561 router_external=None, route_target_list=None,
1562 subnet_conf=None, vntype_conf=None, **kwargs):
Jan Cacha859e6b2018-01-09 17:34:18 +01001563 '''
1564 Create Contrail virtual network
1565 CLI Example:
1566 .. code-block:: bash
Jan Cacha859e6b2018-01-09 17:34:18 +01001567
ibumarskov6ec99c42019-02-20 11:44:22 +04001568 salt -C 'I@opencontrail:control:role:primary'
1569 contrail.virtual_network_create public router_external=True
1570 route_target_list=["target:64512:10000"]
1571 subnet_conf=["ip_prefix":"172.16.111.0","ip_prefix_len":24]
Jan Cacha859e6b2018-01-09 17:34:18 +01001572
1573 Parameters:
ibumarskov6ec99c42019-02-20 11:44:22 +04001574 name (str): name of the network (required)
1575 domain (str): name of domain (optional)
1576 project (str): name of project (optional)
1577 ipam_domain (str): domain for ipam (optional)
1578 ipam_project (str): project for ipam (optional)
1579 ipam_name (str): name of IP Address Management (optional)
1580 router_external (bool): When true, this virtual network is openstack router
1581 external network. (optional)
1582 route_target_list (list): route target list - format is
1583 ['target:<asn>:<target>']
1584 subnet_conf (dict):
Jan Cacha859e6b2018-01-09 17:34:18 +01001585 ip_prefix (string) optional - format is xxx.xxx.xxx.xxx
1586 ip_prefix_len (int) optional - format is xx
ibumarskov6ec99c42019-02-20 11:44:22 +04001587 vntype_conf (dict):
Jan Cacha859e6b2018-01-09 17:34:18 +01001588 allow_transit (boolean) optional - enable allow transit
ibumarskov6ec99c42019-02-20 11:44:22 +04001589 vxlan_network_identifier (int) - VxLAN VNI value for network
Jan Cacha859e6b2018-01-09 17:34:18 +01001590 forwarding_mode (any of ['l2_l3','l2','l3']) optional
1591 - packet forwarding mode for this virtual network
1592 rpf (any of ['enabled','disabled']) optional
1593 - Enable or disable Reverse Path Forwarding check
1594 for this network
1595 mirror_destination (boolean) optional
1596 - Mark the vn as mirror destination network
1597 '''
Jan Cacha859e6b2018-01-09 17:34:18 +01001598 ret = {'name': name,
1599 'changes': {},
1600 'result': True,
1601 'comment': ''}
1602
Jan Cacha859e6b2018-01-09 17:34:18 +01001603 vnc_client = _auth(**kwargs)
ibumarskov6ec99c42019-02-20 11:44:22 +04001604 fq_name = [domain, project, name]
1605 prj_obj = vnc_client.project_read(fq_name=[domain,
1606 project])
1607 ipam_obj = vnc_client.network_ipam_read(fq_name=[ipam_domain,
1608 ipam_project,
1609 ipam_name])
1610 if route_target_list:
1611 route_target_list = RouteTargetList(route_target_list)
1612 if subnet_conf:
1613 if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf:
1614 ipam_sn = IpamSubnetType(subnet=SubnetType(
1615 ip_prefix=subnet_conf['ip_prefix'],
1616 ip_prefix_len=subnet_conf['ip_prefix_len']))
Jan Cacha859e6b2018-01-09 17:34:18 +01001617
ibumarskov6ec99c42019-02-20 11:44:22 +04001618 # Check if the network exists
1619 vn_obj_list = vnc_client.virtual_networks_list(parent_id=prj_obj.uuid)
1620 if name in list(map(lambda x: x['fq_name'][2],
1621 vn_obj_list['virtual-networks'])):
1622 changes = {}
1623 vn_obj = vnc_client.virtual_network_read(fq_name=fq_name)
Jan Cacha859e6b2018-01-09 17:34:18 +01001624
ibumarskov6ec99c42019-02-20 11:44:22 +04001625 # Update IPAM properties
1626 ipam_refs = vn_obj.get_network_ipam_refs()
1627 subnet_detected = False
1628 if subnet_conf and ipam_refs:
1629 for ipam in ipam_refs:
1630 if ipam_obj.get_uuid() == ipam['uuid']:
1631 ipam_subnet_list = ipam['attr'].get_ipam_subnets()
1632 for ipam_subnet_type in ipam_subnet_list:
1633 subnet_obj = ipam_subnet_type.get_subnet()
1634 ip_prefix = subnet_obj.get_ip_prefix()
1635 ip_prefix_len = subnet_obj.get_ip_prefix_len()
1636 if subnet_conf['ip_prefix'] == ip_prefix and \
1637 subnet_conf['ip_prefix_len'] == ip_prefix_len:
1638 subnet_detected = True
1639 if subnet_conf and not subnet_detected:
1640 changes['ipam_subnet'] = \
1641 {'added': '{0}/{1}'.format(subnet_conf['ip_prefix'],
1642 subnet_conf['ip_prefix_len'])}
1643 vn_obj.add_network_ipam(ipam_obj,
1644 VnSubnetsType(ipam_subnets=[ipam_sn]))
Jan Cacha859e6b2018-01-09 17:34:18 +01001645
ibumarskov6ec99c42019-02-20 11:44:22 +04001646 # Update VirtualNetwork properties
1647 external = vn_obj.get_router_external()
1648 if router_external is not None and router_external != external:
1649 changes['router_external'] = {'from': external,
1650 'to': router_external}
1651 vn_obj.set_router_external(router_external)
1652 if route_target_list:
1653 changes['router_list'] = \
1654 {'from': str(vn_obj.get_route_target_list()),
1655 'to': str(route_target_list)}
1656 vn_obj.set_route_target_list(route_target_list)
Jan Cacha859e6b2018-01-09 17:34:18 +01001657
ibumarskov6ec99c42019-02-20 11:44:22 +04001658 # Update VirtualNetworkType properties
1659 if vntype_conf:
1660 vn_type_obj = vn_obj.get_virtual_network_properties()
1661 if vn_type_obj is None:
1662 vn_type_obj = VirtualNetworkType()
Jan Cacha859e6b2018-01-09 17:34:18 +01001663
ibumarskov6ec99c42019-02-20 11:44:22 +04001664 if 'allow_transit' in vntype_conf:
1665 allow_transit_attr = vn_type_obj.get_allow_transit()
1666 if vntype_conf['allow_transit'] != allow_transit_attr:
1667 changes['allow_transit'] = \
1668 {'from': allow_transit_attr,
1669 'to': vntype_conf['allow_transit']}
1670 vn_type_obj.set_allow_transit(vntype_conf['allow_transit'])
1671 if 'vxlan_network_identifier' in vntype_conf:
1672 vxlan_net_id = vn_type_obj.get_vxlan_network_identifier()
1673 if vntype_conf['vxlan_network_identifier'] != vxlan_net_id:
1674 vn_type_obj.set_vxlan_network_identifier(
1675 vntype_conf['vxlan_network_identifier'])
1676 changes['vxlan_network_identifier'] = \
1677 {'from': vxlan_net_id,
1678 'to': vntype_conf['vxlan_network_identifier']}
1679 if 'forwarding_mode' in vntype_conf:
1680 forwarding_mode_attr = vn_type_obj.get_forwarding_mode()
1681 if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3'] and \
1682 vntype_conf['forwarding_mode'] != forwarding_mode_attr:
1683 changes['forwarding_mode'] = \
1684 {'from': forwarding_mode_attr,
1685 'to': vntype_conf['forwarding_mode']}
1686 vn_type_obj.set_forwarding_mode(
1687 vntype_conf['forwarding_mode'])
1688 if 'mirror_destination' in vntype_conf:
1689 mirror_dst_attr = vn_type_obj.get_mirror_destination()
1690 if vntype_conf['mirror_destination'] != mirror_dst_attr:
1691 changes['mirror_destination'] = \
1692 {'from': mirror_dst_attr,
1693 'to': vntype_conf['mirror_destination']}
1694 vn_type_obj.set_mirror_destination(
1695 vntype_conf['mirror_destination'])
1696 if 'rpf' in vntype_conf:
1697 rpf_attr = vn_type_obj.get_rpf()
1698 if vntype_conf['rpf'] != rpf_attr:
1699 changes['rpf'] = \
1700 {'from': rpf_attr,
1701 'to': vntype_conf['rpf']}
1702 vn_type_obj.set_rpf(vntype_conf['rpf'])
1703 vn_obj.set_virtual_network_properties(vn_type_obj)
Jan Cacha859e6b2018-01-09 17:34:18 +01001704
ibumarskov6ec99c42019-02-20 11:44:22 +04001705 if changes:
1706 ret['changes'] = changes
1707 if __opts__['test']:
1708 ret['result'] = None
1709 ret['comment'] = "Virtual network " + name + " will be updated"
1710 else:
1711 ret['comment'] = "VirtualNetwork " + name + " has been updated"
1712 vnc_client.virtual_network_update(vn_obj)
1713 else:
1714 ret['comment'] = 'Virtual network ' + name + \
1715 ' already exists and is updated'
Jan Cacha859e6b2018-01-09 17:34:18 +01001716 else:
ibumarskov6ec99c42019-02-20 11:44:22 +04001717 vn_obj = VirtualNetwork(name, prj_obj)
Jan Cachb3092722018-01-31 12:46:16 +01001718
ibumarskov6ec99c42019-02-20 11:44:22 +04001719 # Configure IPAM properties
1720 if subnet_conf:
1721 if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf:
1722 vn_obj.add_network_ipam(ipam_obj,
1723 VnSubnetsType(ipam_subnets=[ipam_sn]))
1724
1725 # Configure VirtualNetwork properties
1726 if router_external is not None:
1727 vn_obj.set_router_external(router_external)
1728 if route_target_list is not None:
1729 vn_obj.set_route_target_list(route_target_list)
1730
1731 # Configure VirtualNetworkType properties
1732 if vntype_conf:
1733 vn_type_obj = VirtualNetworkType()
1734 if 'allow_transit' in vntype_conf:
1735 vn_type_obj.set_allow_transit(vntype_conf['allow_transit'])
1736 if 'vxlan_network_identifier' in vntype_conf:
1737 vn_type_obj.set_vxlan_network_identifier(
1738 vntype_conf['vxlan_network_identifier'])
1739 if 'forwarding_mode' in vntype_conf:
1740 if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3']:
1741 vn_type_obj.set_forwarding_mode(
1742 vntype_conf['forwarding_mode'])
1743 if 'mirror_destination' in vntype_conf:
1744 vn_type_obj.set_mirror_destination(vntype_conf['mirror_destination'])
1745 if 'rpf' in vntype_conf:
1746 vn_type_obj.set_rpf(vntype_conf['rpf'])
1747 vn_obj.set_virtual_network_properties(vn_type_obj)
1748
1749 if __opts__['test']:
1750 ret['result'] = None
1751 ret['comment'] = "VirtualNetwork " + name + " will be created"
1752 else:
1753 vnc_client.virtual_network_create(vn_obj)
1754 ret['comment'] = "VirtualNetwork " + name + " has been created"
1755 ret['changes'] = {'VirtualNetwork': {'created': name}}
Jan Cacha859e6b2018-01-09 17:34:18 +01001756 return ret
1757
1758
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001759def service_appliance_set_list(**kwargs):
1760 '''
1761 Return a list of Contrail service appliance set
1762
1763 CLI Example:
1764
1765 .. code-block:: bash
1766
1767 salt '*' contrail.service_appliance_set_list
1768 '''
1769 ret = {}
1770 vnc_client = _auth(**kwargs)
1771 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1772 for service_appliance_set in service_appliance_sets:
1773 ret[service_appliance_set.name] = service_appliance_set.__dict__
1774 return ret
1775
1776
1777def service_appliance_set_get(name, **kwargs):
1778 '''
1779 Return a specific Contrail service appliance set
1780
1781 CLI Example:
1782
1783 .. code-block:: bash
1784
1785 salt '*' contrail.service_appliance_set_get name
1786 '''
1787 ret = {}
1788 sas_objs = service_appliance_set_list(**kwargs)
1789 if name in sas_objs:
1790 ret[name] = sas_objs.get(name)
1791 if len(ret) != 1:
1792 return {'result': False,
1793 'Error': "Error in the retrieving service apliance set."}
1794 return ret
1795
1796
1797def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1798 '''
1799 Create Contrail service appliance set
1800
1801 CLI Example:
1802
1803 .. code-block:: bash
1804
1805 salt '*' contrail.service_appliance_set_create name
1806 '''
1807 ret = {'name': name,
1808 'changes': {},
1809 'result': True,
1810 'comment': ''}
1811 vnc_client = _auth(**kwargs)
1812 gsc_obj = _get_config(vnc_client)
1813 sas_objs = service_appliance_set_list(**kwargs)
1814 if name in sas_objs:
1815 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1816 else:
1817 service_appliance_set_obj = ServiceApplianceSet(
1818 name, gsc_obj)
1819 if properties:
1820 pairs = KeyValuePairs()
1821 for k, v in properties.items():
1822 pairs.add_key_value_pair(KeyValuePair(k, v))
1823 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1824 if driver:
1825 service_appliance_set_obj.set_service_appliance_driver(driver)
1826 if ha_mode:
1827 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1828 if __opts__['test']:
1829 ret['result'] = None
1830 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1831 else:
1832 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1833 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1834 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1835 return ret
1836
1837
1838def service_appliance_set_delete(name, **kwargs):
1839 '''
1840 Delete specific Contrail service appliance set
1841
1842 CLI Example:
1843
1844 .. code-block:: bash
1845
1846 salt '*' contrail.service_appliance_set_delete name
1847 '''
1848 ret = {'name': name,
1849 'changes': {},
1850 'result': True,
1851 'comment': ''}
1852 vnc_client = _auth(**kwargs)
1853 gsc_obj = _get_config(vnc_client)
1854 sas_obj = ServiceApplianceSet(name, gsc_obj)
1855 if __opts__['test']:
1856 ret['result'] = None
1857 ret['comment'] = "Service appliance set " + name + " will be deleted"
1858 else:
1859 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1860 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1861 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1862 return ret
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001863
1864def global_system_config_list(**kwargs):
1865 '''
1866 Return a list of all global system configs
1867
1868 CLI Example:
1869
1870 .. code-block:: bash
1871
1872 salt '*' contrail.global_system_config_list
1873 '''
1874
1875 ret = {}
1876 vnc_client = _auth(**kwargs)
1877 gsysconfs = vnc_client._objects_list('global-system-config', detail=True)
1878 for gsysconf in gsysconfs:
1879 ret[gsysconf.name] = gsysconf.__dict__
1880 return ret
1881
1882
1883def global_system_config_get(name, **kwargs):
1884 '''
1885 Return a specific Contrail global system config
1886
1887 CLI Example:
1888
1889 .. code-block:: bash
1890
1891 salt '*' contrail.global_system_config_get name
1892 '''
1893 ret = {}
1894 vnc_client = _auth(**kwargs)
1895 gsc_objs = vnc_client._objects_list('global-system-config', detail=True)
1896 for gsc_obj in gsc_objs:
1897 if name == gsc_obj.name:
1898 ret[name] = gsc_obj.__dict__
1899 if len(ret) == 0:
1900 return {'Error': 'Error in retrieving global system config.'}
1901 return ret
1902
1903
1904def global_system_config_create(name, ans=64512, grp=None, **kwargs):
1905 '''
1906 Create Contrail global system config
1907
1908 CLI Example:
1909
1910 .. code-block:: bash
1911
1912 salt '*' contrail.global_system_config_create name=default-global-system-config ans=64512
1913 '''
1914 ret = {'name': name,
1915 'changes': {},
1916 'result': True,
1917 'comment': ''}
1918 vnc_client = _auth(**kwargs)
1919
1920 gsc_objs = global_system_config_list(**kwargs)
1921 if name in gsc_objs:
1922 config_obj = vnc_client.global_system_config_read(fq_name=[name])
1923 if config_obj.graceful_restart_parameters and not HAS_OLD:
1924 curr_grp = str(config_obj.graceful_restart_parameters).replace(" ", "").split(",")
1925 curr_grpd = dict(item.split('=') for item in curr_grp)
psvimbersky3c84e272018-01-02 10:34:29 +01001926 else:
1927 curr_grpd = None
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001928
1929 if grp and 'enable' in grp and not HAS_OLD:
1930 grp_obj = GracefulRestartParametersType()
1931 if 'enable' in grp:
1932 grp_obj.enable = grp['enable']
1933 if curr_grpd and str(grp['enable']) != str(curr_grpd['enable']):
1934 ret['changes']['enable'] = {"from": str(curr_grpd['enable']), "to": str(grp['enable'])}
1935 elif not curr_grpd:
1936 ret['changes']['enable'] = {"from": None, "to": grp['enable']}
1937 if 'restart_time' in grp:
1938 grp_obj.restart_time = grp['restart_time']
1939 if curr_grpd and grp['restart_time'] != int(curr_grpd['restart_time']):
1940 ret['changes']['restart_time'] = {"from": int(curr_grpd['restart_time']), "to": grp['restart_time']}
1941 elif not curr_grpd:
1942 ret['changes']['restart_time'] = {"from": None, "to": grp['restart_time']}
1943 if 'end_of_rib_timeout' in grp:
1944 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1945 if curr_grpd and grp['end_of_rib_timeout'] != int(curr_grpd['end_of_rib_timeout']):
1946 ret['changes']['end_of_rib_timeout'] = {"from": int(curr_grpd['end_of_rib_timeout']), "to": grp['end_of_rib_timeout']}
1947 elif not curr_grpd:
1948 ret['changes']['end_of_rib_timeout'] = {"from": None, "to": grp['end_of_rib_timeout']}
1949 if 'bgp_helper_enable' in grp:
1950 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1951 if curr_grpd and str(grp['bgp_helper_enable']) != str(curr_grpd['bgp_helper_enable']):
1952 ret['changes']['bgp_helper_enable'] = {"from": str(curr_grpd['bgp_helper_enable']), "to": grp['bgp_helper_enable']}
1953 elif not curr_grpd:
1954 ret['changes']['bgp_helper_enable'] = {"from": None, "to": grp['bgp_helper_enable']}
1955 if 'xmpp_helper_enable' in grp:
1956 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1957 if curr_grpd and str(grp['xmpp_helper_enable']) != str(curr_grpd['xmpp_helper_enable']):
1958 ret['changes']['xmpp_helper_enable'] = {"from": str(curr_grpd['xmpp_helper_enable']), "to": grp['xmpp_helper_enable']}
1959 elif not curr_grpd:
1960 ret['changes']['xmpp_helper_enable'] = {"from": None, "to": grp['xmpp_helper_enable']}
1961 if 'long_lived_restart_time' in grp:
1962 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1963 if curr_grpd and grp['long_lived_restart_time'] != int(curr_grpd['long_lived_restart_time']):
1964 ret['changes']['long_lived_restart_time'] = {"from": int(curr_grpd['long_lived_restart_time']), "to": grp['long_lived_restart_time']}
1965 elif not curr_grpd:
1966 ret['changes']['long_lived_restart_time'] = {"from": None, "to": grp['long_lived_restart_time']}
1967 else:
1968 grp_obj = None
1969
1970 config_obj.graceful_restart_parameters = grp_obj
1971
1972 if ans:
1973 if config_obj.autonomous_system != ans:
1974 ret['changes']['autonomous_system'] = {"from": config_obj.autonomous_system, "to": ans}
1975 config_obj.autonomous_system = ans
1976
1977 vnc_client.global_system_config_update(config_obj)
1978 ret['comment'] = 'Global system config ' + name + ' has been updated'
1979 else:
1980 config_obj = GlobalSystemConfig(name=name)
1981 if grp and not HAS_OLD:
1982 grp_obj = GracefulRestartParametersType()
1983 if 'enable' in grp:
1984 grp_obj.enable = grp['enable']
1985 if 'restart_time' in grp:
1986 grp_obj.restart_time = grp['restart_time']
1987 if 'end_of_rib_timeout' in grp:
1988 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1989 if 'bgp_helper_enable' in grp:
1990 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1991 if 'xmpp_helper_enable' in grp:
1992 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1993 if 'long_lived_restart_time' in grp:
1994 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1995 config_obj.graceful_restart_parameters = grp_obj
1996 if ans:
1997 config_obj.autonomous_system = ans
1998
1999 vnc_client.global_system_config_create(config_obj)
2000 ret['changes'] = {"created": "new"}
2001 ret['comment'] = 'Global system config ' + name + ' has been created '
2002
2003 return ret
2004
2005
2006def global_system_config_delete(name, **kwargs):
2007 '''
2008 Delete specific Contrail global system config
2009
2010 CLI Example:
2011
2012 .. code-block:: bash
2013
2014 salt '*' contrail.global_system_config_delete name
2015 '''
2016 ret = {'name': name,
2017 'changes': {},
2018 'result': True,
2019 'comment': ''}
2020 vnc_client = _auth(**kwargs)
2021
2022 gsc_obj = GlobalSystemConfig(name)
2023 if __opts__['test']:
2024 ret['result'] = None
2025 ret['comment'] = "Global system config " + name + " will be deleted"
2026 else:
2027 vnc_client.global_system_config_delete(fq_name=gsc_obj.get_fq_name())
2028 ret['comment'] = "GlobalSystemConfig " + name + " has been deleted"
2029 ret['changes'] = {'GlobalSystemConfig': {'old': name, 'new': ''}}
2030 return ret
Jan Cachb3092722018-01-31 12:46:16 +01002031
2032
2033def list_floating_ip_pools(**kwargs):
2034 '''
2035 List all floating ip pools
2036
2037 CLI Example:
2038 .. code-block:: bash
2039 salt '*' contrail.list_floating_ip_pools
2040 '''
2041 vnc_client = _auth(**kwargs)
2042 pools = vnc_client.floating_ip_pools_list()
2043 # list of floating ip pools objects
2044 fp_list = []
2045
2046 for pool in vnc_client.floating_ip_pools_list()['floating-ip-pools']:
2047 fip_obj = vnc_client.floating_ip_pool_read(pool['fq_name'])
2048 fp_list.append(fip_obj)
2049 # print given pool
2050 fip_obj.dump()
2051
2052def update_floating_ip_pool(vn_name, vn_project, vn_domain=None,
2053 owner_access=None, global_access=None,
2054 projects=None, **kwargs):
2055 '''
2056 Update specific floating ip pool
2057
2058
2059 CLI Example
2060 .. code-block:: bash
2061 salt-call contrail.update_floating_ip_pool \
2062 'FLOATING-TEST' \
2063 'admin' \
2064 'default-domain' \
2065 7 7 \
2066 [['pepa',4],['karel',7]]
2067
2068
2069 params:
2070 vn_name - name of the virtual network, which to use
2071 vn-project - project which includes virtual network
2072 vn-domain - domain wchich includes vn_project and vn_name
2073 owner_access (int) - Permission rights for owner
2074 global_access (int) - Permission rights for others
2075 projects (list) - list of ShareType(tenant_name,tennat_permissions)
2076 '''
2077 ret = {'name': vn_name + "-default pool",
2078 'changes': {},
2079 'result': True,
2080 'comment': ''}
2081
2082 if vn_domain is None:
2083 vn_domain = 'default-domain'
2084 fip_obj = None
2085
2086 vnc_client = _auth(**kwargs)
2087 p_fq_name = [vn_domain, vn_project, vn_name, 'default']
2088 fip_obj = vnc_client.floating_ip_pool_read(fq_name=p_fq_name)
2089
2090 changes = {}
2091 # get perms from fip_obj (Floatin ip pool)
2092 perms2 = fip_obj.get_perms2()
2093 if owner_access is not None:
2094 if perms2.get_owner_access() != owner_access:
2095 changes['owner_access'] = {'old': str(perms2.get_owner_access()),
2096 'new': str(owner_access)}
2097 perms2.set_owner_access(owner_access)
2098
2099 if global_access is not None:
2100 if perms2.get_global_access() != global_access:
2101 changes['global_access'] = {'old': str(perms2.get_global_access()),
2102 'new': str(global_access)}
2103 perms2.set_global_access(global_access)
2104
2105 # list which represents the new state of perms
2106 final_list = []
2107 if projects:
2108 for item in perms2.get_share():
2109 for share in projects:
2110 if item.get_tenant() == share[0]:
2111 # project is in the new and old list
2112 # check is the permission number is same
2113 if item.get_tenant_access() == share[1]:
2114 # this project and permission is without change, keep it
2115 final_list.append(item)
2116 break
2117 else:
2118 # project exists but change the permission
2119 final_list.append(ShareType(tenant=share[0],
2120 tenant_access=share[1]))
2121 # show changes
2122 n = str('share-'+share[0])
2123 old_str = "permission for project {0} was {1}"
2124 new_str = "permission for project {0} will be {1}"
2125 old = old_str.format(share[0],
2126 str(item.get_tenant_access()))
2127
2128 new = new_str.format(share[0], str(share[1]))
2129 changes[n] = {'old': old, 'new': new}
2130 break
2131 else:
2132 rm_name = "share-" + item.get_tenant()
2133 changes[rm_name] = item.get_tenant() + " will be removed"
2134
2135 # check for the completly new projects
2136 for item in projects:
2137 for share in final_list:
2138 if item[0] == share.get_tenant():
2139 break
2140 else:
2141 final_list.append(ShareType(tenant=item[0],
2142 tenant_access=item[1]))
2143 name = 'share-' + str(item[0])
2144 c_str = '{0} will be added with permissions {1}'
2145 changes[name] = c_str.format(name, item[1])
2146 else:
2147 for item in perms2.get_share():
2148 rm_name = "share-" + item.get_tenant()
2149 changes[rm_name] = item.get_tenant() + " will be removed"
2150
2151 if __opts__['test']:
2152 ret['result'] = None
2153 ret['comment'] = changes
2154
2155 return ret
2156 else:
2157 ret['comment'] = changes
2158 perms2.set_share(final_list)
2159 fip_obj.set_perms2(perms2)
2160 vnc_client.floating_ip_pool_update(fip_obj)
2161
2162 return ret
Sergey Matov16896ac2018-02-15 15:46:31 +04002163
2164
2165def show_rbac_rules(api_access_list_entries):
2166 if api_access_list_entries is None:
2167 return 'Empty RBAC group!'
2168
2169 rule_list = api_access_list_entries.get_rbac_rule()
2170 response = 'Rules (%d):' % len(rule_list) + '----------\n'
2171 for idx, rule in enumerate(rule_list):
2172 o = rule.rule_object
2173 f = rule.rule_field
2174 ps = ', '.join([p.role_name+':'+p.role_crud for p in rule.rule_perms])
2175 o_f = "%s.%s" % (o, f) if f else o
2176 response += '%2d %-32s %s\n' % (idx, o_f, ps)
2177 return response
2178
2179
2180def vnc_read_obj(vnc, obj_type, fq_name):
2181 method_name = obj_type.replace('-', '_')
2182 method = getattr(vnc, "%s_read" % (method_name))
2183 try:
2184 return method(fq_name=fq_name)
2185 except NoIdError:
2186 print '%s %s not found!' % (obj_type, fq_name)
2187 return None
2188
2189
2190def rbac_show_group(name, uuid, **kwargs):
2191 '''
2192 Show specific RBAC group
2193
2194
2195 CLI Example
2196 .. code-block:: bash
2197 salt-call contrail.rbac_show_group name \
2198 'default-domain:default-project:default'
2199
2200 params:
2201 name - one of pair {name, uuid} addresing to access-list
2202 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2203 including domain and project
2204
2205 '''
2206 vnc = _auth(**kwargs)
2207 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2208 ret = {'name': fq_name,
2209 'changes': {},
2210 'result': True,
2211 'comment': ''}
2212 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2213 if not rg:
2214 ret['comment'] = 'No rules found'
2215 return ret
2216
2217 ret['comment'] = show_rbac_rules(rg.get_api_access_list_entries())
2218 return ret
2219
2220
2221def rbac_create_group(uuid, **kwargs):
2222 '''
2223 Create RBAC group
2224
2225 CLI Example
2226 .. code-block:: bash
2227 salt-call contrail.rbac_create_group name \
2228 'default-domain:default-project:default'
2229
2230 params:
2231 name - one of pair {name, uuid} addresing to access-list
2232 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2233 including domain and project
2234
2235 '''
2236 vnc = _auth(**kwargs)
2237 fq_name = uuid.split(':')
2238 ret = {'name': fq_name,
2239 'changes': {},
2240 'result': True,
2241 'comment': ''}
2242 if len(fq_name) != 2 and len(fq_name) != 3:
2243 ret['comment'] = 'Fully qualified name of rbac group expected'
2244 return ret
2245
2246 name = fq_name[-1]
2247
2248 if len(fq_name) == 2:
2249 if fq_name[0] == 'default-global-system-config':
2250 pobj = vnc.global_system_config_read(fq_name=fq_name[0:1])
2251 else:
2252 pobj = vnc.domain_read(fq_name=fq_name[0:1])
2253 else:
2254 pobj = vnc.project_read(fq_name=fq_name[0:2])
2255
2256 rentry = RbacRuleEntriesType([])
2257 rg = ApiAccessList(name, parent_obj=pobj, api_access_list_entries=rentry)
2258
2259 if __opts__['test']:
2260 ret['result'] = None
2261 ret['comment'] = "RBAC group " + uuid + " will be created"
2262
2263 return ret
2264 else:
2265 vnc.api_access_list_create(rg)
2266 rg2 = vnc.api_access_list_read(fq_name=fq_name)
2267 rge = rg.get_api_access_list_entries()
2268 show_rbac_rules(rge)
2269 ret['comment'] = "RBAC group " + uuid + " has been created"
2270
2271 return ret
2272
2273
2274def rbac_delete_group(name, uuid, **kwargs):
2275 '''
2276 Delete RBAC group
2277
2278 CLI Example
2279 .. code-block:: bash
2280 salt-call contrail.rbac_delete_group name \
2281 'default-domain:default-project:default'
2282
2283 params:
2284 name - one of pair {name, uuid} addresing to access-list
2285 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2286 including domain and project
2287
2288 '''
2289 vnc = _auth(**kwargs)
2290 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2291 ret = {'name': fq_name,
2292 'changes': {},
2293 'result': True,
2294 'comment': ''}
2295 if len(fq_name) != 2 and len(fq_name) != 3:
2296 ret['comment'] = 'Fully qualified name of rbac group expected'
2297 return ret
2298 name = fq_name[-1]
2299
2300 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2301 if not rg:
2302 ret['comment'] = 'No rules found'
2303 return ret
2304 rge = rg.get_api_access_list_entries()
2305 show_rbac_rules(rge)
2306
2307 if __opts__['test']:
2308 ret['result'] = None
2309 ret['comment'] = "RBAC group " + uuid + " will be deleted"
2310
2311 return ret
2312 else:
2313 vnc.api_access_list_delete(fq_name=fq_name)
2314 ret['comment'] = "RBAC group " + uuid + " has been deleted"
2315
2316 return ret
2317
2318
2319def rbac_add_rule(name, uuid, add_rule, **kwargs):
2320 '''
2321 Add rule to specific RBAC group
2322
2323 CLI Example
2324 .. code-block:: bash
2325 salt-call contrail.rbac_add_rule name \
2326 'default-domain:default-project:default' \
2327 '* admin:CRUD'
2328
2329 params:
2330 name - one of pair {name, uuid} addresing to access-list
2331 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2332 including domain and project
2333 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2334 list of <role:CRUD>' to be added
2335
2336 '''
2337 vnc = _auth(**kwargs)
2338 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2339 rule = build_rule(add_rule)
2340 ret = {'name': fq_name,
2341 'changes': {},
2342 'result': True,
2343 'comment': ''}
2344 if rule is None:
2345 ret['comment'] = 'A rule string must be specified for this operation'
2346 return ret
2347
2348 # rbac rule entry consists of one or more rules
2349 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2350 if not rg:
2351 ret['comment'] = 'No rules found'
2352 return ret
2353
2354 rge = rg.get_api_access_list_entries()
2355 if rge is None:
2356 rge = RbacRuleEntriesType([])
2357 show_rbac_rules(rge)
2358
2359 # avoid duplicates
2360 match = find_rule(rge, rule)
2361 if not match:
2362 rge.add_rbac_rule(rule)
2363 else:
2364 build_perms(rge.rbac_rule[match[0]-1], match[3])
2365
2366 show_rbac_rules(rge)
2367
2368 if __opts__['test']:
2369 ret['result'] = None
2370 ret['comment'] = "Rule " + add_rule
2371 ret['comment'] += " will be created for RBAC group " + uuid
2372
2373 return ret
2374 else:
2375 rg.set_api_access_list_entries(rge)
2376 vnc.api_access_list_update(rg)
2377 ret['comment'] = "Rule " + add_rule
2378 ret['comment'] += " has been added for RBAC group " + uuid
2379
2380 return ret
2381
2382
2383def rbac_delete_rule(name, uuid, del_rule, **kwargs):
2384 '''
2385 Delete rule to specific RBAC group
2386
2387 CLI Example
2388 .. code-block:: bash
2389 salt-call contrail.rbac_delete_rule name \
2390 'default-domain:default-project:default' \
2391 '* admin:CRUD'
2392
2393 params:
2394 name - one of pair {name, uuid} addresing to access-list
2395 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2396 including domain and project
2397 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2398 list of <role:CRUD>' to be deleted
2399
2400 '''
2401 vnc = _auth(**kwargs)
2402 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2403 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2404 ret = {'name': fq_name,
2405 'changes': {},
2406 'result': True,
2407 'comment': ''}
2408 if not rg:
2409 ret['comment'] = 'No rules found'
2410 return ret
2411 rge = rg.get_api_access_list_entries()
2412 show_rbac_rules(rge)
2413
2414 del_idx = re.match("^[0-9]+$", del_rule)
2415 if del_idx:
2416 del_idx = int(del_idx.group())
2417 rc = len(rge.rbac_rule)
2418 if del_idx > rc or del_idx < 1:
2419 ret['comment'] = 'Invalid rule index to delete.'
2420 ret['comment'] += 'Value must be 1-%d' % rc
2421 return ret
2422 match = (del_idx, True)
2423 else:
2424 rule = build_rule(del_rule)
2425 match = find_rule(rge, rule)
2426
2427 if not match:
2428 ret['comment'] = "Rule not found. Unchanged"
2429 return ret
2430 elif match[1]:
2431 rge.rbac_rule.pop(match[0]-1)
2432 else:
2433 build_perms(rge.rbac_rule[match[0]-1], match[2])
2434 show_rbac_rules(rge)
2435
2436 if __opts__['test']:
2437 ret['result'] = None
2438 ret['comment'] = "Rule " + del_rule
2439 ret['comment'] += " will be cleared from RBAC group " + uuid
2440
2441 return ret
2442 else:
2443 rg.set_api_access_list_entries(rge)
2444 vnc.api_access_list_update(rg)
2445 ret['comment'] = "Rule " + del_rule
2446 ret['comment'] += " has been cleared from RBAC group " + uuid
2447
2448 return ret