blob: c9741e3d01dcf57d307be5ba580c568edec3cb63 [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,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020076 api_host, api_port, api_base_url, wait_for_connect=True,
77 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
Jan Cachb3092722018-01-31 12:46:16 +0100119def _create_floating_ip_pool(name, vn_obj, **kwargs):
120 vnc_client = _auth(**kwargs)
121 # create floating ip pool
122 fip_obj = FloatingIpPool(name=name, parent_obj=vn_obj)
123 vnc_client.floating_ip_pool_create(fip_obj)
124
125
Ales Komarekad46d2e2017-03-09 17:16:38 +0100126def virtual_router_list(**kwargs):
127 '''
128 Return a list of all Contrail virtual routers
129
130 CLI Example:
131
132 .. code-block:: bash
133
134 salt '*' contrail.virtual_router_list
135 '''
136 ret = {}
137 vnc_client = _auth(**kwargs)
138 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
139 for vrouter_obj in vrouter_objs:
140 ret[vrouter_obj.name] = {
141 'ip_address': vrouter_obj.virtual_router_ip_address,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200142 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled,
143 'uuid': vrouter_obj.uuid
144
Ales Komarekad46d2e2017-03-09 17:16:38 +0100145 }
146 return ret
147
148
149def virtual_router_get(name, **kwargs):
150 '''
151 Return a specific Contrail virtual router
152
153 CLI Example:
154
155 .. code-block:: bash
156
157 salt '*' contrail.virtual_router_get cmp01
158 '''
159 ret = {}
160 vrouter_objs = virtual_router_list(**kwargs)
161 if name in vrouter_objs:
162 ret[name] = vrouter_objs.get(name)
163 if len(ret) == 0:
164 return {'Error': 'Error in retrieving virtual router.'}
165 return ret
166
167
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200168def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +0100169 '''
170 Create specific Contrail virtual router
171
172 CLI Example:
173
174 .. code-block:: bash
175
176 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200177 router_types:
178 - tor-agent
179 - tor-service-node
180 - embedded
Ales Komarekad46d2e2017-03-09 17:16:38 +0100181 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200182 ret = {'name': name,
183 'changes': {},
184 'result': True,
185 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100186 vnc_client = _auth(**kwargs)
187 gsc_obj = _get_config(vnc_client)
188 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200189 router_types = ['tor-agent', 'tor-service-node', 'embedded']
190 if router_type not in router_types:
191 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100192 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200193 vrouter = virtual_router_get(name)
194 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200195 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200196 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200197 changes['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200198 vrouter_obj.set_virtual_router_ip_address(ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200199 if vrouter_obj.get_virtual_router_type() != router_type:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200200 changes['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200201 vrouter_obj.set_virtual_router_type(router_type)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200202 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200203 changes['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200204 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200205 if len(changes) != 0:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200206 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200207 ret['result'] = None
208 ret['comment'] = "Virtual router " + name + " will be updated"
209 else:
210 ret['comment'] = "VirtualRouter " + name + " has been updated"
211 ret['changes'] = changes
212 vnc_client.virtual_router_update(vrouter_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200213 return ret
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200214 ret['comment'] = 'Virtual router ' + name + ' already exists and is updated'
215 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100216 else:
217 vrouter_obj = VirtualRouter(
218 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200219 virtual_router_ip_address=ip_address,
220 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100221 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200222 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200223 ret['result'] = None
224 ret['comment'] = "VirtualRouter " + name + " will be created"
225 else:
226 vnc_client.virtual_router_create(vrouter_obj)
227 ret['comment'] = "VirtualRouter " + name + " has been created"
228 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
229 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100230
231
232def virtual_router_delete(name, **kwargs):
233 '''
234 Delete specific Contrail virtual router
235
236 CLI Example:
237
238 .. code-block:: bash
239
240 salt '*' contrail.virtual_router_delete cmp01
241 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200242 ret = {'name': name,
243 'changes': {},
244 'result': True,
245 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100246 vnc_client = _auth(**kwargs)
247 gsc_obj = _get_config(vnc_client)
248 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200249 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200250 ret['result'] = None
251 ret['comment'] = "VirtualRouter " + name + " will be deleted"
252 else:
253 vnc_client.virtual_router_delete(fq_name=vrouter_obj.get_fq_name())
254 ret['comment'] = "VirtualRouter " + name + " has been deleted"
255 ret['changes'] = {'VirtualRouter': {'old': name, 'new': ''}}
256 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200257
258
259def physical_router_list(**kwargs):
260 '''
261 Return a list of all Contrail physical routers
262
263 CLI Example:
264
265 .. code-block:: bash
266
267 salt '*' contrail.physical_router_list
268 '''
269 ret = {}
270 vnc_client = _auth(**kwargs)
271 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
272 for prouter_obj in prouter_objs:
273 ret[prouter_obj.name] = {
274 'uuid': prouter_obj._uuid,
275 'management_ip': prouter_obj._physical_router_management_ip,
276 'product_name': prouter_obj._physical_router_product_name,
277 }
278
279 return ret
280
281
282def physical_router_get(name, **kwargs):
283 '''
284 Return a specific Contrail physical router
285
286 CLI Example:
287
288 .. code-block:: bash
289
290 salt '*' contrail.physical_router_get router_name
291 '''
292 ret = {}
293 vnc_client = _auth(**kwargs)
294 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
295 for prouter_obj in prouter_objs:
296 if name == prouter_obj.name:
297 ret[name] = prouter_obj.__dict__
298 if len(ret) == 0:
299 return {'Error': 'Error in retrieving physical router.'}
300 return ret
301
302
303def physical_router_create(name, parent_type=None,
304 management_ip=None,
305 dataplane_ip=None, # VTEP address in web GUI
306 vendor_name=None,
307 product_name=None,
308 vnc_managed=None,
309 junos_service_ports=None,
310 agents=None, **kwargs):
311 '''
312 Create specific Contrail physical router
313
314 CLI Example:
315
316 .. code-block:: bash
317
318 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']"
319 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200320 ret = {'name': name,
321 'changes': {},
322 'result': True,
323 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200324 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200325 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200326 prouter_objs = physical_router_list(**kwargs)
327 if name in prouter_objs:
328 prouter = physical_router_get(name)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200329 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200330 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
331 if prouter_obj.physical_router_management_ip != management_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200332 changes['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200333 prouter_obj.set_physical_router_management_ip(management_ip)
334 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200335 changes['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200336 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
337 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200338 changes['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200339 prouter_obj.set_physical_router_vendor_name(vendor_name)
340 if prouter_obj.get_physical_router_product_name() != product_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200341 changes['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200342 prouter_obj.set_physical_router_product_name(product_name)
343 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200344 changes['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200345 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
346 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200347 changes['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
348 'to': junos_service_ports}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200349 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
350
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200351 if len(changes) != 0:
352 if __opts__['test']:
353 ret['result'] = None
354 ret['comment'] = "Physical router " + name + " will be updated"
355 else:
356 ret['comment'] = 'Physical router ' + name + ' already exists and is updated'
357 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200358
359 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
360 c_agents = [] # referenced vrouters
361 for c_agent in prouter_obj.get_virtual_router_refs():
362 c_agents.append(c_agent['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200363 # agent_objs = [] # required state of references
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200364 for vrouter_obj in vrouter_objs:
365 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
366 prouter_obj.add_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200367 changes['vrouter ' + vrouter_obj._display_name] = "Reference added"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200368 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
369 prouter_obj.del_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200370 changes['vrouter ' + vrouter_obj._display_name] = "Reference removed"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200371 vnc_client.physical_router_update(prouter_obj)
372
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200373 if __opts__['test']:
374 ret['result'] = None
375 ret['comment'] = "VirtualRouter " + name + " will be created"
376 else:
377 vnc_client.virtual_router_create(vrouter_obj)
378 ret['comment'] = "VirtualRouter " + name + " has been created"
379 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
380
381 if len(changes) == 0:
382 ret['comment'] = "Physical router exists and is updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200383 return ret
384 else:
385 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200386 ret['result'] = None
387 ret['comment'] = "Physical router " + name + " will be created"
388 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200389 prouter_obj = PhysicalRouter(
390 name=name,
391 parent_obj=None,
392 physical_router_management_ip=management_ip,
393 physical_router_dataplane_ip=dataplane_ip,
394 physical_router_vendor_name=vendor_name,
395 physical_router_product_name=product_name,
396 physical_router_vnc_managed=vnc_managed,
397 physical_router_junos_service_ports=junos_service_ports,
398 )
399 for agent in agents:
400 vrouter = virtual_router_get(agent)
401 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
402 prouter_obj.add_virtual_router(vrouter_obj)
403 vnc_client.physical_router_create(prouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200404 ret['comment'] = "Physical router " + name + " has been created"
405 ret['changes'] = {'PhysicalRouter': {'old': '', 'new': name}}
406 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200407
408
409def physical_router_delete(name, **kwargs):
410 '''
411 Delete specific Contrail physical router
412
413 CLI Example:
414
415 .. code-block:: bash
416
417 salt '*' contrail.physical_router_delete router_name
418 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200419 ret = {'name': name,
420 'changes': {},
421 'result': True,
422 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200423 vnc_client = _auth(**kwargs)
424 gsc_obj = _get_config(vnc_client)
425 prouter_obj = PhysicalRouter(name, gsc_obj)
426 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200427 ret['result'] = None
428 ret['comment'] = "Physical router " + name + " will be deleted"
429 else:
430 vnc_client.physical_router_delete(fq_name=prouter_obj.get_fq_name())
431 ret['comment'] = "Physical router " + name + " has been deleted"
432 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
433 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200434
435
436def physical_interface_list(**kwargs):
437 '''
438 Return a list of all Contrail physical interface
439
440 CLI Example:
441
442 .. code-block:: bash
443
444 salt '*' contrail.physical_interface_list
445 '''
446 ret = {}
447 vnc_client = _auth(**kwargs)
448 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
449 for pinterface_obj in pinterface_objs:
450 ret[pinterface_obj.name] = {
451 'uuid': pinterface_obj._uuid,
452 'fq_name': pinterface_obj.fq_name,
453 'parent_type': pinterface_obj.parent_type,
454 }
455
456 return ret
457
458
459def physical_interface_get(name, physical_router, **kwargs):
460 '''
461 Return a specific Contrail physical interface
462
463 CLI Example:
464
465 .. code-block:: bash
466
467 salt '*' contrail.physical_interface_get interface_name physical_router_name
468 '''
469 ret = {}
470 vnc_client = _auth(**kwargs)
471 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
472 for pinterf_obj in pinterf_objs:
473 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
474 ret[name] = pinterf_obj.__dict__
475 if len(ret) == 0:
476 return {'Error': 'Error in retrieving physical interface.'}
477 return ret
478
479
480def physical_interface_create(name, physical_router, **kwargs):
481 '''
482 Create specific Contrail physical interface
483
484 CLI Example:
485
486 .. code-block:: bash
487
488 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
489 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200490 ret = {'name': name,
491 'changes': {},
492 'result': True,
493 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200494 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200495 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200496 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
497 if 'Error' not in pinterf_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200498 ret['comment'] = 'Physical interface ' + name + ' on ' + physical_router + ' already exists'
499 return ret
500
501 if __opts__['test']:
502 ret['result'] = None
503 ret['comment'] = "Physical interface " + name + " will be created"
504 return ret
505
506 prouter = physical_router_get(physical_router)
507 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
508 pinterf_obj = PhysicalInterface(name, prouter_obj)
509 vnc_client.physical_interface_create(pinterf_obj)
510 ret['comment'] = "Physical interface " + name + " has been created"
511 ret['changes'] = {'Physical interface': {'old': '', 'new': name}}
512 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200513
514
515def physical_interface_delete(name, physical_router, **kwargs):
516 '''
517 Delete specific Contrail physical interface
518
519 CLI Example:
520 .. code-block:: bash
521
522 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
523 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200524 ret = {'name': name,
525 'changes': {},
526 'result': True,
527 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200528 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200529 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200530 piface = physical_interface_get(name, physical_router)
531 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200532 ret['result'] = None
533 ret['comment'] = "Physical interface " + name + " will be deleted"
534 else:
535 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
536 ret['comment'] = "Physical router " + name + " has been deleted"
537 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
538 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200539
540
541def logical_interface_list(**kwargs):
542 '''
543 Return a list of all Contrail logical interfaces
544
545 CLI Example:
546
547 .. code-block:: bash
548
549 salt '*' contrail.logical_interface_list
550 '''
551 ret = []
552 vnc_client = _auth(**kwargs)
553 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
554 for liface_obj in liface_objs:
555 ret.append({
556 'name': liface_obj.name,
557 'uuid': liface_obj._uuid,
558 'fq_name': liface_obj.fq_name,
559 'parent_type': liface_obj.parent_type,
560 })
561 return ret
562
563
564def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
565 '''
566 Return a specific Contrail logical interface
567
568 CLI Example:
569
570 .. code-block:: bash
571
572 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
573 or
574 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
575 or
576 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
577 '''
578 ret = {}
579 vnc_client = _auth(**kwargs)
580 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
581 count = 0
582 for liface_obj in liface_objs:
583 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
584 if parent_type and parent_type == liface_obj.parent_type:
585 count += 1
586 ret[liface_obj.name] = liface_obj.__dict__
587 if not parent_type:
588 count += 1
589 ret[liface_obj.name] = liface_obj.__dict__
590 if len(ret) == 0:
591 return {'Error': 'Error in retrieving logical interface.'}
592 if count > 1:
593 return {
594 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
595 return ret
596
597
598def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300599 vmis=None, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200600 '''
601 Create specific Contrail logical interface
602
603 CLI Example:
604
605 .. code-block:: bash
606
607 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
608 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200609 ret = {'name': name,
610 'changes': {},
611 'result': True,
612 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200613 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200614 # gsc_obj = _get_config(vnc_client)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300615
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200616 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
617 if 'Error' not in liface_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200618 ret['comment'] = 'Logical interface ' + name + ' already exists'
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200619 else:
620 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200621 ret['result'] = None
622 ret['comment'] = "Logical interface " + name + " will be created"
623 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200624 parent_obj = None
625 for router in parent_names:
626 parent_router = physical_router_get(router)
627 if 'Error' not in parent_router:
628 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
629 break
630 if not parent_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200631 ret['result'] = False
632 ret['comment'] = 'Physical router have to be defined'
633 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200634 if parent_type == 'physical-interface':
635 for interface in parent_names:
636 parent_interface = physical_interface_get(interface, parent_obj.name)
637 if 'Error' not in parent_interface:
638 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
639 break
640 if interface_type.lower() == "l3":
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200641 ret['result'] = False
642 ret['comment'] = "Virtual Network have to be defined for L3 interface type"
643 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200644
645 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300646
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200647 if vmis:
648 for vmi_name, vmi in vmis.iteritems():
649 vmi = vnc_client.virtual_machine_interface_read(
650 fq_name=_get_fq_name(vnc_client, resource_name=vmi_name,
651 project_name=kwargs.get('tenant', 'admin')))
652 liface_obj.add_virtual_machine_interface(vmi)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200653 vnc_client.logical_interface_create(liface_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300654
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200655 ret['comment'] = "Logical interface " + name + " has been created"
656 ret['changes'] = {'Logical interface': {'old': '', 'new': name}}
657 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200658
659
660def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
661 '''
662 Delete specific Contrail logical interface
663
664 CLI Example:
665
666 .. code-block:: bash
667
668 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
669 or
670 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
671
672 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200673 ret = {'name': name,
674 'changes': {},
675 'result': True,
676 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200677 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200678 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200679 liface = logical_interface_get(name, parent_names, parent_type)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200680
681 if __opts__['test']:
682 ret['result'] = None
683 ret['comment'] = "Logical interface " + name + " will be deleted"
684 return ret
685 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
686 ret['comment'] = "Logical interface " + name + " has been deleted"
687 ret['changes'] = {'LogicalInterface ': {'old': name, 'new': ''}}
688 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200689
690
691def global_vrouter_config_list(**kwargs):
692 '''
693 Return a list of all Contrail global vrouter configs
694
695 CLI Example:
696
697 .. code-block:: bash"
698
699 salt '*' global_vrouter_config_list
700 '''
701 ret = {}
702 vnc_client = _auth(**kwargs)
703 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
704 for vrouter_conf_obj in vrouter_conf_objs:
Anton Samoylovce3d7772018-11-23 00:00:02 +0400705 ret[vrouter_conf_obj.name] = vrouter_conf_obj.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200706 return ret
707
708
709def global_vrouter_config_get(name, **kwargs):
710 '''
711 Return a specific Contrail global vrouter config
712
713 CLI Example:
714
715 .. code-block:: bash
716
717 salt '*' contrail.global_vrouter_get global-vrouter-config
718 '''
719 ret = {}
720 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
721 if name in vrouter_conf_objs:
722 ret[name] = vrouter_conf_objs.get(name)
723 if len(ret) == 0:
724 return {'Error': 'Error in retrieving global vrouter config.'}
725 return ret
726
727
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100728def 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 +0200729 '''
730 Create specific Contrail global vrouter config
731
732 CLI Example:
733
734 .. code-block:: bash
735
736 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']"
737 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200738 ret = {'name': name,
739 'changes': {},
740 'result': True,
741 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200742 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200743 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200744 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
745 if name in vrouter_conf_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200746 ret['comment'] = 'Global vrouter config ' + name + ' already exists'
747 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200748 else:
749 vrouter_conf_obj = GlobalVrouterConfig(
750 name=name,
751 parent_obj=None,
752 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
753 fq_name=fq_names,
754 vxlan_network_identifier_mode=vxlan_vn_id_mode,
755 parent_type=parent_type,
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100756 flow_export_rate=flow_export_rate,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200757 )
758 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200759 ret['result'] = None
760 ret['comment'] = "Global vRouter config " + name + " will be created"
761 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200762 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200763 ret['comment'] = "Global vRouter config " + name + " has been created"
764 ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}}
765 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200766
767
768def global_vrouter_config_delete(name, **kwargs):
769 '''
770 Delete specific Contrail global vrouter config
771
772 CLI Example:
773
774 .. code-block:: bash
775
776 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
777 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200778 ret = {'name': name,
779 'changes': {},
780 'result': True,
781 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200782 vnc_client = _auth(**kwargs)
783 gsc_obj = _get_config(vnc_client)
784 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
785 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200786 ret['result'] = None
787 ret['comment'] = "Global vRouter config " + name + " will be deleted"
788 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200789 vnc_client.global_vrouter_config_delete(
790 fq_name=vrouter_conf_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200791 ret['comment'] = "Global vRouter config " + name + " has been deleted"
792 ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}}
793 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100794
795
796def analytics_node_list(**kwargs):
797 '''
798 Return a list of all Contrail analytics nodes
799
800 CLI Example:
801
802 .. code-block:: bash
803
804 salt '*' contrail.analytics_node_list
805 '''
806 ret = {}
807 vnc_client = _auth(**kwargs)
808 node_objs = vnc_client._objects_list('analytics-node', detail=True)
809 for node_obj in node_objs:
810 ret[node_obj.name] = node_obj.__dict__
811 return ret
812
813
814def analytics_node_get(name, **kwargs):
815 '''
816 Return a specific Contrail analytics node
817
818 CLI Example:
819
820 .. code-block:: bash
821
822 salt '*' contrail.analytics_node_get nal01
823 '''
824 ret = {}
825 vrouter_objs = analytics_node_list(**kwargs)
826 if name in vrouter_objs:
827 ret[name] = vrouter_objs.get(name)
828 if len(ret) == 0:
829 return {'Error': 'Error in retrieving analytics node.'}
830 return ret
831
832
833def analytics_node_create(name, ip_address, **kwargs):
834 '''
835 Create specific Contrail analytics node
836
837 CLI Example:
838
839 .. code-block:: bash
840
841 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
842 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200843
844 ret = {'name': name,
845 'changes': {},
846 'result': True,
847 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100848 vnc_client = _auth(**kwargs)
849 gsc_obj = _get_config(vnc_client)
850 analytics_node_objs = analytics_node_list(**kwargs)
851 if name in analytics_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200852 ret['comment'] = 'Analytics node %s already exists'
853 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100854 else:
855 analytics_node_obj = AnalyticsNode(
856 name, gsc_obj,
857 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200858 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200859 ret['result'] = None
860 ret['comment'] = "AnalyticsNode " + name + " will be created"
861 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100862 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200863 ret['comment'] = "AnalyticsNode " + name + " has been created"
864 ret['changes'] = {'Analytics Node': {'old': '', 'new': name}}
865 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100866
867
868def analytics_node_delete(name, **kwargs):
869 '''
870 Delete specific Contrail analytics node
871
872 CLI Example:
873
874 .. code-block:: bash
875
876 salt '*' contrail.analytics_node_delete cmp01
877 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200878 ret = {'name': name,
879 'changes': {},
880 'result': True,
881 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100882 vnc_client = _auth(**kwargs)
883 gsc_obj = _get_config(vnc_client)
884 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200885 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200886 ret['result'] = None
887 ret['comment'] = "AnalyticsNode " + name + " will be deleted"
888 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100889 vnc_client.analytics_node_delete(
890 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200891 ret['comment'] = "AnalyticsNode " + name + " has been deleted"
892 ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}}
893 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100894
895
896def config_node_list(**kwargs):
897 '''
898 Return a list of all Contrail config nodes
899
900 CLI Example:
901
902 .. code-block:: bash
903
904 salt '*' contrail.config_node_list
905 '''
906 ret = {}
907 vnc_client = _auth(**kwargs)
908 node_objs = vnc_client._objects_list('config-node', detail=True)
909 for node_obj in node_objs:
910 ret[node_obj.name] = node_obj.__dict__
911 return ret
912
913
914def config_node_get(name, **kwargs):
915 '''
916 Return a specific Contrail config node
917
918 CLI Example:
919
920 .. code-block:: bash
921
922 salt '*' contrail.config_node_get nal01
923 '''
924 ret = {}
925 vrouter_objs = config_node_list(**kwargs)
926 if name in vrouter_objs:
927 ret[name] = vrouter_objs.get(name)
928 if len(ret) == 0:
929 return {'Error': 'Error in retrieving config node.'}
930 return ret
931
932
933def config_node_create(name, ip_address, **kwargs):
934 '''
935 Create specific Contrail config node
936
937 CLI Example:
938
939 .. code-block:: bash
940
941 salt '*' contrail.config_node_create ntw03 10.10.10.103
942 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200943 ret = {'name': name,
944 'changes': {},
945 'result': True,
946 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100947 vnc_client = _auth(**kwargs)
948 gsc_obj = _get_config(vnc_client)
949 config_node_objs = config_node_list(**kwargs)
950 if name in config_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200951 ret['comment'] = 'Config node ' + name + ' already exists'
952 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100953 else:
954 config_node_obj = ConfigNode(
955 name, gsc_obj,
956 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200957 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200958 ret['comment'] = "ConfigNode " + name + " will be created"
959 ret['result'] = None
960 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100961 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200962 ret['comment'] = "ConfigNode " + name + " has been created"
963 ret['changes'] = {'ConfigNode': {'old': '', 'new': name}}
964 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100965
966
967def config_node_delete(name, **kwargs):
968 '''
969 Delete specific Contrail config node
970
971 CLI Example:
972
973 .. code-block:: bash
974
975 salt '*' contrail.config_node_delete cmp01
976 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200977 ret = {'name': name,
978 'changes': {},
979 'result': True,
980 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100981 vnc_client = _auth(**kwargs)
982 gsc_obj = _get_config(vnc_client)
983 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200984 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200985 ret['comment'] = "ConfigNode " + name + " will be deleted"
986 ret['result'] = None
987 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100988 vnc_client.config_node_delete(
989 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200990 ret['comment'] = "ConfigNode " + name + " has been deleted"
991 ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}}
992 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100993
994
995def bgp_router_list(**kwargs):
996 '''
997 Return a list of all Contrail BGP routers
998
999 CLI Example:
1000
1001 .. code-block:: bash
1002
1003 salt '*' contrail.bgp_router_list
1004 '''
1005 ret = {}
1006 vnc_client = _auth(**kwargs)
1007 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
1008 for bgp_router_obj in bgp_router_objs:
1009 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
1010 return ret
1011
1012
1013def bgp_router_get(name, **kwargs):
1014 '''
1015 Return a specific Contrail BGP router
1016
1017 CLI Example:
1018
1019 .. code-block:: bash
1020
1021 salt '*' contrail.bgp_router_get nal01
1022 '''
1023 ret = {}
1024 bgp_router_objs = bgp_router_list(**kwargs)
1025 if name in bgp_router_objs:
1026 ret[name] = bgp_router_objs.get(name)
1027 if len(ret) == 0:
1028 return {'Error': 'Error in retrieving BGP router.'}
1029 return ret
1030
1031
Marek Celoud3097e5b2018-01-09 13:52:14 +01001032def bgp_router_create(name, type, ip_address, asn=64512, key_type=None, key=None, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +01001033 '''
1034 Create specific Contrail control node
1035
1036 CLI Example:
1037
1038 .. code-block:: bash
1039
1040 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
1041 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
1042 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001043 ret = {'name': name,
1044 'changes': {},
1045 'result': True,
1046 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001047 vnc_client = _auth(**kwargs)
1048
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001049 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
1050 'inet6-vpn']
1051 if type != 'control-node':
1052 address_families.remove('erm-vpn')
1053
Marek Celoud3097e5b2018-01-09 13:52:14 +01001054 key_type = None if key_type == 'None' else key_type
1055 key = None if key == 'None' else key
1056
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001057 bgp_addr_fams = AddressFamilies(address_families)
1058 bgp_sess_attrs = [
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001059 BgpSessionAttributes(address_families=bgp_addr_fams)]
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001060 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
1061 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
1062 rt_inst_obj = _get_rt_inst_obj(vnc_client)
1063
Marek Celoud3097e5b2018-01-09 13:52:14 +01001064 bgp_auth_data = None
1065
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001066 if type == 'control-node':
1067 vendor = 'contrail'
1068 elif type == 'router':
1069 vendor = 'mx'
Marek Celoud3097e5b2018-01-09 13:52:14 +01001070 if key_type == 'md5':
1071 key_id = 0
1072 key_items = AuthenticationKeyItem(key_id, key)
1073 bgp_auth_data = AuthenticationData(key_type, [key_items])
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001074 else:
1075 vendor = 'unknown'
1076
1077 router_params = BgpRouterParams(router_type=type,
1078 vendor=vendor, autonomous_system=int(asn),
1079 identifier=_get_ip(ip_address),
1080 address=_get_ip(ip_address),
Marek Celoud3097e5b2018-01-09 13:52:14 +01001081 port=179, address_families=bgp_addr_fams,
1082 auth_data=bgp_auth_data)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001083
Ales Komarekad46d2e2017-03-09 17:16:38 +01001084 bgp_router_objs = bgp_router_list(**kwargs)
1085 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001086 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001087
1088 if bgp_router_obj.bgp_router_parameters.autonomous_system != asn:
1089 ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}})
1090 if bgp_router_obj.bgp_router_parameters.vendor != vendor:
1091 ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}})
1092 if bgp_router_obj.bgp_router_parameters.address != ip_address:
1093 ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}})
Marek Celoud3097e5b2018-01-09 13:52:14 +01001094 try:
1095 if bgp_router_obj.bgp_router_parameters.auth_data.key_type != key_type:
1096 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_type, 'new': key_type}})
1097 except:
1098 if key_type != None:
1099 ret['changes'].update({"key_type": {'old': None, 'new': key_type}})
1100 if key_type == 'md5':
1101 try:
1102 if bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key != key:
1103 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key, 'new': key}})
1104 except:
1105 ret['changes'].update({"key_type": {'old': None, 'new': key}})
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001106
1107 if len(ret['changes']) == 0:
1108 return ret
1109
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001110 bgp_router_obj.set_bgp_router_parameters(router_params)
1111 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001112 ret['result'] = None
1113 ret['comment'] = "BGP router " + name + " will be updated"
1114 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001115 vnc_client.bgp_router_update(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001116 ret['comment'] = "BGP router " + name + " has been updated"
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001117 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001118 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001119 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
1120 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001121 ret['result'] = None
1122 ret['comment'] = "BGP router " + name + " will be created"
1123 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001124 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001125 ret['comment'] = "BGP router " + name + " has been created"
1126 ret['changes'] = {'BGP router': {'old': name, 'new': ''}}
1127 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001128
1129
1130def bgp_router_delete(name, **kwargs):
1131 '''
1132 Delete specific Contrail control node
1133
1134 CLI Example:
1135
1136 .. code-block:: bash
1137
1138 salt '*' contrail.bgp_router_delete mx01
1139 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001140 ret = {'name': name,
1141 'changes': {},
1142 'result': True,
1143 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001144 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001145
1146 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001147 ret['result'] = None
1148 ret['comment'] = "BGP router " + name + " will be deleted"
1149 return ret
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001150
1151 bgp_router = bgp_router_get(name)
1152 if name in bgp_router:
1153 vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name'])
1154 ret['comment'] = "BGP router " + name + " has been deleted"
1155 ret['changes'] = {'BGP router': {'old': '', 'new': name}}
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001156 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001157
Ales Komarekad46d2e2017-03-09 17:16:38 +01001158
1159def database_node_list(**kwargs):
1160 '''
1161 Return a list of all Contrail database nodes
1162
1163 CLI Example:
1164
1165 .. code-block:: bash
1166
1167 salt '*' contrail.database_node_list
1168 '''
1169 ret = {}
1170 vnc_client = _auth(**kwargs)
1171 node_objs = vnc_client._objects_list('database-node', detail=True)
1172 for node_obj in node_objs:
1173 ret[node_obj.name] = node_obj.__dict__
1174 return ret
1175
1176
1177def database_node_get(name, **kwargs):
1178 '''
1179 Return a specific Contrail database node
1180
1181 CLI Example:
1182
1183 .. code-block:: bash
1184
1185 salt '*' contrail.database_node_get nal01
1186 '''
1187 ret = {}
1188 vrouter_objs = database_node_list(**kwargs)
1189 if name in vrouter_objs:
1190 ret[name] = vrouter_objs.get(name)
1191 if len(ret) == 0:
1192 return {'Error': 'Error in retrieving database node.'}
1193 return ret
1194
1195
1196def database_node_create(name, ip_address, **kwargs):
1197 '''
1198 Create specific Contrail database node
1199
1200 CLI Example:
1201
1202 .. code-block:: bash
1203
1204 salt '*' contrail.database_node_create ntw03 10.10.10.103
1205 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001206 ret = {'name': name,
1207 'changes': {},
1208 'result': True,
1209 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001210 vnc_client = _auth(**kwargs)
1211 gsc_obj = _get_config(vnc_client)
1212 database_node_objs = database_node_list(**kwargs)
1213 if name in database_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001214 ret['comment'] = 'Database node ' + name + ' already exists'
1215 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001216 else:
1217 database_node_obj = DatabaseNode(
1218 name, gsc_obj,
1219 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001220 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001221 ret['result'] = None
1222 ret['comment'] = "DatabaseNode " + name + " will be created"
1223 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001224 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001225 ret['comment'] = "DatabaseNode " + name + " has been created"
1226 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1227 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001228
1229
1230def database_node_delete(name, **kwargs):
1231 '''
1232 Delete specific Contrail database node
1233
1234 CLI Example:
1235
1236 .. code-block:: bash
1237
1238 salt '*' contrail.database_node_delete cmp01
1239 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001240 ret = {'name': name,
1241 'changes': {},
1242 'result': True,
1243 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001244 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001245 gsc_obj = _get_config(vnc_client)
1246 database_node_obj = DatabaseNode(name, gsc_obj)
1247 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001248 ret['result'] = None
1249 ret['comment'] = "DatabaseNode " + name + " will be deleted"
1250 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001251 vnc_client.database_node_delete(
1252 fq_name=database_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001253 ret['comment'] = "DatabaseNode " + name + " has been deleted"
1254 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1255 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001256
1257
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001258def _get_vrouter_config(vnc_client, gvc_name=None):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001259 try:
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001260 if not gvc_name:
1261 gvc_list = global_vrouter_config_list()
1262 gvc_name = gvc_list.values()[0]['name']
1263
Petr Jediný5f3efe32017-05-26 17:55:09 +02001264 config = vnc_client.global_vrouter_config_read(
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001265 fq_name=['default-global-system-config', gvc_name])
Petr Jediný5f3efe32017-05-26 17:55:09 +02001266 except Exception:
1267 config = None
1268
1269 return config
1270
1271
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001272def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001273 '''
1274 Return a list of all Contrail link local services
1275
1276 CLI Example:
1277
1278 .. code-block:: bash
1279
1280 salt '*' contrail.linklocal_service_list
1281 '''
1282 ret = {}
1283 vnc_client = _auth(**kwargs)
1284
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001285 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001286 if current_config is None:
1287 return ret
1288
1289 service_list_res = current_config.get_linklocal_services()
1290 if service_list_res is None:
1291 service_list_obj = {'linklocal_service_entry': []}
1292 else:
1293 service_list_obj = service_list_res.__dict__
1294 for _, value in service_list_obj.iteritems():
1295 for entry in value:
1296 service = entry.__dict__
1297 if 'linklocal_service_name' in service:
1298 ret[service['linklocal_service_name']] = service
1299 return ret
1300
1301
1302def linklocal_service_get(name, **kwargs):
1303 '''
1304 Return a specific Contrail link local service
1305
1306 CLI Example:
1307
1308 .. code-block:: bash
1309
1310 salt '*' contrail.linklocal_service_get llservice
1311 '''
1312 ret = {}
1313 services = linklocal_service_list(**kwargs)
1314 if name in services:
1315 ret[name] = services.get(name)
1316 if len(ret) == 0:
1317 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1318 return ret
1319
1320
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001321def 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 +02001322 '''
1323 Create specific Contrail link local service
1324
1325 CLI Example:
1326
1327 .. code-block:: bash
1328
1329 salt '*' contrail.linklocal_service_create \
1330 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1331 salt '*' contrail.linklocal_service_create \
1332 llservice 10.10.10.103 22 link-local.service.dns-name 22
1333 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001334 ret = {'name': name,
1335 'changes': {},
1336 'result': True,
1337 'comment': ''}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001338 vnc_client = _auth(**kwargs)
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001339 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001340 service_entry = LinklocalServiceEntryType(
1341 linklocal_service_name=name,
1342 linklocal_service_ip=lls_ip,
1343 linklocal_service_port=lls_port,
1344 ip_fabric_service_port=ipf_port)
1345 if isinstance(ipf_dns_or_ip, basestring):
1346 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1347 elif isinstance(ipf_dns_or_ip, list):
1348 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1349 service_entry.ip_fabric_DNS_service_name = ''
1350
1351 if current_config is None:
1352 new_services = LinklocalServicesTypes([service_entry])
1353 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001354 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001355 ret['result'] = None
1356 ret['comment'] = "Link local service " + name + " will be created"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001357 else:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001358 ret['comment'] = "Link local service " + name + " has been created"
1359 ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001360 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001361 else:
1362 _current_service_list = current_config.get_linklocal_services()
1363 if _current_service_list is None:
1364 service_list = {'linklocal_service_entry': []}
1365 else:
1366 service_list = _current_service_list.__dict__
1367 new_services = [service_entry]
1368 for key, value in service_list.iteritems():
1369 if key != 'linklocal_service_entry':
1370 continue
1371 for _entry in value:
1372 entry = _entry.__dict__
1373 if 'linklocal_service_name' in entry:
1374 if entry['linklocal_service_name'] == name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001375 ret['comment'] = 'Link local service ' + name + ' already exists'
1376 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001377 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001378 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001379 ret['result'] = None
1380 ret['comment'] = "LinkLocalSevices " + name + " will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001381 service_list[key] = new_services
1382 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001383 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001384 ret['result'] = None
1385 ret['comment'] = "LinkLocalSevices " + name + " will be updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001386 else:
1387 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001388 ret['comment'] = "LinkLocalSevices " + name + " has been created"
1389 ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001390 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001391
1392
1393def linklocal_service_delete(name, **kwargs):
1394 '''
1395 Delete specific link local service entry
1396
1397 CLI Example:
1398
1399 .. code-block:: bash
1400
1401 salt '*' contrail.linklocal_service_delete llservice
1402 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001403 ret = {'name': name,
1404 'changes': {},
1405 'result': True,
1406 'comment': ''}
1407 lls = linklocal_service_get(name)
1408 print (lls)
1409 if name in lls:
1410 if __opts__['test']:
1411 print " ------------ Test only ------------"
1412 ret['result'] = None
1413 ret['comment'] = "Link local service " + name + " will be deleted"
1414 return ret
1415 else:
1416 return ret
1417
Petr Jediný5f3efe32017-05-26 17:55:09 +02001418 vnc_client = _auth(**kwargs)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001419 current_config = _get_vrouter_config(vnc_client)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001420 if current_config is not None:
1421 _current_service_list = current_config.get_linklocal_services()
1422 if _current_service_list is None:
1423 service_list = {'linklocal_service_entry': []}
1424 else:
1425 service_list = _current_service_list.__dict__
1426 new_services = []
1427 for key, value in service_list.iteritems():
1428 if key != 'linklocal_service_entry':
1429 continue
1430 for _entry in value:
1431 entry = _entry.__dict__
1432 if 'linklocal_service_name' in entry:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001433 if entry['linklocal_service_name'] != name:
Petr Jediný5f3efe32017-05-26 17:55:09 +02001434 new_services.append(_entry)
1435 service_list[key] = new_services
1436 new_config = GlobalVrouterConfig(linklocal_services=service_list)
1437 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001438 ret['comment'] = "Link local service " + name + " will be deleted"
1439 ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
1440 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001441
1442
1443def virtual_machine_interface_list(**kwargs):
1444 '''
1445 Return a list of all Contrail virtual machine interfaces
1446
1447 CLI Example:
1448
1449 .. code-block:: bash
1450
1451 salt '*' contrail.virtual_machine_interfaces
1452 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001453 ret = []
1454 vnc_client = _auth(**kwargs)
1455 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1456 project_uuid = project.get_uuid()
1457
1458 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1459 detail=True, parent_id=project_uuid)
1460
1461 for vm_iface in vm_ifaces:
1462 ret.append(vm_iface.__dict__)
1463
1464 return ret
1465
1466
1467def virtual_machine_interface_create(name,
1468 virtual_network,
1469 mac_address=None,
1470 ip_address=None,
1471 security_group=None,
1472 **kwargs):
1473 '''
1474 Create specific Contrail virtual machine interface (Port)
1475
1476 CLI Example:
1477
1478 .. code-block:: bash
1479
1480 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1481 router_types:
1482 - tor-agent
1483 - tor-service-node
1484 - embedded
1485 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001486 ret = {}
1487 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001488 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1489
1490 vm_int = VirtualMachineInterface(name, parent_obj=project)
1491
1492 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001493 mac_address_obj = MacAddressesType([mac_address])
1494 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001495
1496 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001497 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001498 vnc_client, security_group, kwargs.get('tenant', 'admin')))
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001499 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001500
1501 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1502 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1503 vm_int.set_virtual_network(vnet_obj)
1504
1505 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1506 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1507
1508 vm_int.set_port_security_enabled(False)
1509 vnc_client.virtual_machine_interface_update(vm_int)
1510
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001511 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001512 ip = vnc_api.InstanceIp(name + '.ip')
1513 ip.set_virtual_machine_interface(vmi)
1514 ip.set_virtual_network(vnet_obj)
1515
1516 ip_uuid = vnc_client.instance_ip_create(ip)
1517
1518 if ip_address:
1519 ip.set_instance_ip_address(ip_address)
1520 vnc_client.instance_ip_update(ip)
1521
1522 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001523
1524
1525def virtual_network_list(**kwargs):
1526 '''
1527 Return a list of all Contrail virtual network
1528
1529 CLI Example:
1530
1531 .. code-block:: bash
1532
1533 salt '*' contrail.virtual_network
1534 '''
1535
1536 ret = {}
1537 vnc_client = _auth(**kwargs)
1538 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1539 for virtual_network in virtual_networks:
1540 ret[virtual_network.name] = virtual_network.__dict__
1541 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001542
1543
1544def virtual_network_get(name, **kwargs):
1545 '''
1546 Return a specific Contrail virtual network
1547
1548 CLI Example:
1549
1550 .. code-block:: bash
1551
1552 salt '*' contrail.virtual_network_get net01
1553 '''
1554 ret = {}
1555 vnet_objs = virtual_network_list(**kwargs)
1556 if name in vnet_objs:
1557 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001558 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001559 return {'result': False,
1560 'Error': 'Error in retrieving virtual networks.'}
1561 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001562
1563
Jan Cacha859e6b2018-01-09 17:34:18 +01001564def virtual_network_create(name, conf=None, **kwargs):
1565 '''
1566 Create Contrail virtual network
1567 CLI Example:
1568 .. code-block:: bash
1569 salt '*' contrail.virtual_network_create name
1570
1571 salt.cmdRun(pepperEnv, 'ntw01*', 'salt-call contrail.virtual_network_create
1572 "testicek" "{"external":"True","ip":"172.16.111.0","prefix":24,
1573 "asn":64512,"target":10000}" ')
1574
1575 Parameters:
1576 name required - name of the new network
1577
1578 conf (dict) optional:
1579 domain (string) optional - which domain use for vn creation
1580 project (string) optional - which project use for vn creation
1581 ipam_domain (string) optional - domain for ipam
1582 ipam_project (string) optional - project for ipam
1583 ipam_name (string) optional - ipam name
1584 ip_prefix (string) optional - format is xxx.xxx.xxx.xxx
1585 ip_prefix_len (int) optional - format is xx
1586 asn (int) optional - autonomus system number
1587 target (int) optional - route target number
1588 external (boolean) optional - set if network is external
1589
1590 allow_transit (boolean) optional - enable allow transit
1591 forwarding_mode (any of ['l2_l3','l2','l3']) optional
1592 - packet forwarding mode for this virtual network
1593 rpf (any of ['enabled','disabled']) optional
1594 - Enable or disable Reverse Path Forwarding check
1595 for this network
1596 mirror_destination (boolean) optional
1597 - Mark the vn as mirror destination network
1598 '''
1599 if conf is None:
1600 conf = {}
1601
1602 # check for domain, is missing set to default-domain
1603 if 'domain' in conf:
1604 vn_domain = str(conf['domain'])
1605 else:
1606 vn_domain = 'default-domain'
1607 # check for project, is missing set to admin
1608 if 'project' in conf:
1609 vn_project = str(conf['project'])
1610 else:
1611 vn_project = 'admin'
1612 # check for ipam domain,default is default-domain
1613 if 'ipam_domain' in conf:
1614 ipam_domain = str(conf['ipam_domain'])
1615 else:
1616 ipam_domain = 'default-domain'
1617 # check for ipam domain,default is default-domain
1618 if 'ipam_project' in conf:
1619 ipam_project = str(conf['ipam_project'])
1620 else:
1621 ipam_project = 'default-project'
1622
1623 if 'ipam_name' in conf:
1624 ipam_name = conf['ipam_name']
1625 else:
1626 ipam_name = 'default-network-ipam'
1627
1628 ret = {'name': name,
1629 'changes': {},
1630 'result': True,
1631 'comment': ''}
1632
1633 # list of existing vn networks
1634 vn_networks = []
1635 vnc_client = _auth(**kwargs)
Jan Cach03412162018-01-12 10:29:22 +01001636 prj_obj = vnc_client.project_read(fq_name=[vn_domain,
1637 vn_project])
Jan Cacha859e6b2018-01-09 17:34:18 +01001638 # check if the network exists
1639 vn_networks_list = vnc_client._objects_list('virtual_network')
Jan Cachece0d242018-01-12 14:58:36 +01001640 fq = [vn_domain, vn_project, name]
Jan Cacha859e6b2018-01-09 17:34:18 +01001641 for network in vn_networks_list['virtual-networks']:
1642 if fq == network['fq_name']:
1643 ret['comment'] = ("Virtual network with name "
1644 + name + " already exists")
1645 return ret
1646
Jan Cach03412162018-01-12 10:29:22 +01001647 vn_obj = VirtualNetwork(name, prj_obj)
Jan Cacha859e6b2018-01-09 17:34:18 +01001648 vn_type_obj = VirtualNetworkType()
1649 # get ipam from default project and domain
1650 ipam = vnc_client.network_ipam_read(fq_name=[ipam_domain,
1651 ipam_project,
1652 ipam_name])
1653
1654 # create subnet
1655 if 'ip_prefix' in conf and 'ip_prefix_len' in conf:
1656 ipam_subnet_type = IpamSubnetType(subnet=SubnetType(
1657 ip_prefix=conf['ip_prefix'],
1658 ip_prefix_len=conf['ip_prefix_len']))
1659
1660 vn_subnets_type_obj = VnSubnetsType(ipam_subnets=[ipam_subnet_type])
1661 vn_obj.add_network_ipam(ipam, vn_subnets_type_obj)
1662
1663 # add route target to the network
1664 if 'asn' in conf and 'target' in conf:
1665 route_target_list_obj = RouteTargetList(["target:{0}:{1}"
1666 .format(conf['asn'],
1667 conf['target'])])
1668 vn_obj.set_route_target_list(route_target_list_obj)
1669
1670 if 'external' in conf:
1671 vn_obj.set_router_external(conf['external'])
1672
1673 if 'allow_transit' in conf:
1674 vn_type_obj.set_allow_transit(conf['allow_transit'])
1675
1676 if 'forwarding_mode' in conf:
1677 if conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3']:
1678 vn_type_obj.set_forwarding_mode(conf['forwarding_mode'])
1679
1680 if 'rpf' in conf:
1681 vn_type_obj.set_rpf(conf['rpf'])
1682
1683 if 'mirror_destination' in conf:
1684 vn_type_obj.set_mirror_destination(conf['mirror_destination'])
1685
1686 vn_obj.set_virtual_network_properties(vn_type_obj)
1687
1688 # create virtual network
1689 if __opts__['test']:
1690 ret['result'] = None
1691 ret['comment'] = ("Virtual network with name {0} will be created"
1692 .format(name))
1693 else:
1694 vnc_client.virtual_network_create(vn_obj)
Jan Cachb3092722018-01-31 12:46:16 +01001695 # if network is external create floating ip pool
1696 if 'external' in conf:
1697 if conf['external']:
1698 pool_name = 'default'
1699 _create_floating_ip_pool(pool_name,
1700 vn_obj,
1701 **kwargs)
1702
Jan Cacha859e6b2018-01-09 17:34:18 +01001703 ret['comment'] = ("Virtual network with name {0} was created"
1704 .format(name))
1705 return ret
1706
1707
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001708def service_appliance_set_list(**kwargs):
1709 '''
1710 Return a list of Contrail service appliance set
1711
1712 CLI Example:
1713
1714 .. code-block:: bash
1715
1716 salt '*' contrail.service_appliance_set_list
1717 '''
1718 ret = {}
1719 vnc_client = _auth(**kwargs)
1720 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1721 for service_appliance_set in service_appliance_sets:
1722 ret[service_appliance_set.name] = service_appliance_set.__dict__
1723 return ret
1724
1725
1726def service_appliance_set_get(name, **kwargs):
1727 '''
1728 Return a specific Contrail service appliance set
1729
1730 CLI Example:
1731
1732 .. code-block:: bash
1733
1734 salt '*' contrail.service_appliance_set_get name
1735 '''
1736 ret = {}
1737 sas_objs = service_appliance_set_list(**kwargs)
1738 if name in sas_objs:
1739 ret[name] = sas_objs.get(name)
1740 if len(ret) != 1:
1741 return {'result': False,
1742 'Error': "Error in the retrieving service apliance set."}
1743 return ret
1744
1745
1746def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1747 '''
1748 Create Contrail service appliance set
1749
1750 CLI Example:
1751
1752 .. code-block:: bash
1753
1754 salt '*' contrail.service_appliance_set_create name
1755 '''
1756 ret = {'name': name,
1757 'changes': {},
1758 'result': True,
1759 'comment': ''}
1760 vnc_client = _auth(**kwargs)
1761 gsc_obj = _get_config(vnc_client)
1762 sas_objs = service_appliance_set_list(**kwargs)
1763 if name in sas_objs:
1764 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1765 else:
1766 service_appliance_set_obj = ServiceApplianceSet(
1767 name, gsc_obj)
1768 if properties:
1769 pairs = KeyValuePairs()
1770 for k, v in properties.items():
1771 pairs.add_key_value_pair(KeyValuePair(k, v))
1772 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1773 if driver:
1774 service_appliance_set_obj.set_service_appliance_driver(driver)
1775 if ha_mode:
1776 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1777 if __opts__['test']:
1778 ret['result'] = None
1779 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1780 else:
1781 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1782 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1783 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1784 return ret
1785
1786
1787def service_appliance_set_delete(name, **kwargs):
1788 '''
1789 Delete specific Contrail service appliance set
1790
1791 CLI Example:
1792
1793 .. code-block:: bash
1794
1795 salt '*' contrail.service_appliance_set_delete name
1796 '''
1797 ret = {'name': name,
1798 'changes': {},
1799 'result': True,
1800 'comment': ''}
1801 vnc_client = _auth(**kwargs)
1802 gsc_obj = _get_config(vnc_client)
1803 sas_obj = ServiceApplianceSet(name, gsc_obj)
1804 if __opts__['test']:
1805 ret['result'] = None
1806 ret['comment'] = "Service appliance set " + name + " will be deleted"
1807 else:
1808 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1809 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1810 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1811 return ret
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001812
1813def global_system_config_list(**kwargs):
1814 '''
1815 Return a list of all global system configs
1816
1817 CLI Example:
1818
1819 .. code-block:: bash
1820
1821 salt '*' contrail.global_system_config_list
1822 '''
1823
1824 ret = {}
1825 vnc_client = _auth(**kwargs)
1826 gsysconfs = vnc_client._objects_list('global-system-config', detail=True)
1827 for gsysconf in gsysconfs:
1828 ret[gsysconf.name] = gsysconf.__dict__
1829 return ret
1830
1831
1832def global_system_config_get(name, **kwargs):
1833 '''
1834 Return a specific Contrail global system config
1835
1836 CLI Example:
1837
1838 .. code-block:: bash
1839
1840 salt '*' contrail.global_system_config_get name
1841 '''
1842 ret = {}
1843 vnc_client = _auth(**kwargs)
1844 gsc_objs = vnc_client._objects_list('global-system-config', detail=True)
1845 for gsc_obj in gsc_objs:
1846 if name == gsc_obj.name:
1847 ret[name] = gsc_obj.__dict__
1848 if len(ret) == 0:
1849 return {'Error': 'Error in retrieving global system config.'}
1850 return ret
1851
1852
1853def global_system_config_create(name, ans=64512, grp=None, **kwargs):
1854 '''
1855 Create Contrail global system config
1856
1857 CLI Example:
1858
1859 .. code-block:: bash
1860
1861 salt '*' contrail.global_system_config_create name=default-global-system-config ans=64512
1862 '''
1863 ret = {'name': name,
1864 'changes': {},
1865 'result': True,
1866 'comment': ''}
1867 vnc_client = _auth(**kwargs)
1868
1869 gsc_objs = global_system_config_list(**kwargs)
1870 if name in gsc_objs:
1871 config_obj = vnc_client.global_system_config_read(fq_name=[name])
1872 if config_obj.graceful_restart_parameters and not HAS_OLD:
1873 curr_grp = str(config_obj.graceful_restart_parameters).replace(" ", "").split(",")
1874 curr_grpd = dict(item.split('=') for item in curr_grp)
psvimbersky3c84e272018-01-02 10:34:29 +01001875 else:
1876 curr_grpd = None
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001877
1878 if grp and 'enable' in grp and not HAS_OLD:
1879 grp_obj = GracefulRestartParametersType()
1880 if 'enable' in grp:
1881 grp_obj.enable = grp['enable']
1882 if curr_grpd and str(grp['enable']) != str(curr_grpd['enable']):
1883 ret['changes']['enable'] = {"from": str(curr_grpd['enable']), "to": str(grp['enable'])}
1884 elif not curr_grpd:
1885 ret['changes']['enable'] = {"from": None, "to": grp['enable']}
1886 if 'restart_time' in grp:
1887 grp_obj.restart_time = grp['restart_time']
1888 if curr_grpd and grp['restart_time'] != int(curr_grpd['restart_time']):
1889 ret['changes']['restart_time'] = {"from": int(curr_grpd['restart_time']), "to": grp['restart_time']}
1890 elif not curr_grpd:
1891 ret['changes']['restart_time'] = {"from": None, "to": grp['restart_time']}
1892 if 'end_of_rib_timeout' in grp:
1893 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1894 if curr_grpd and grp['end_of_rib_timeout'] != int(curr_grpd['end_of_rib_timeout']):
1895 ret['changes']['end_of_rib_timeout'] = {"from": int(curr_grpd['end_of_rib_timeout']), "to": grp['end_of_rib_timeout']}
1896 elif not curr_grpd:
1897 ret['changes']['end_of_rib_timeout'] = {"from": None, "to": grp['end_of_rib_timeout']}
1898 if 'bgp_helper_enable' in grp:
1899 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1900 if curr_grpd and str(grp['bgp_helper_enable']) != str(curr_grpd['bgp_helper_enable']):
1901 ret['changes']['bgp_helper_enable'] = {"from": str(curr_grpd['bgp_helper_enable']), "to": grp['bgp_helper_enable']}
1902 elif not curr_grpd:
1903 ret['changes']['bgp_helper_enable'] = {"from": None, "to": grp['bgp_helper_enable']}
1904 if 'xmpp_helper_enable' in grp:
1905 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1906 if curr_grpd and str(grp['xmpp_helper_enable']) != str(curr_grpd['xmpp_helper_enable']):
1907 ret['changes']['xmpp_helper_enable'] = {"from": str(curr_grpd['xmpp_helper_enable']), "to": grp['xmpp_helper_enable']}
1908 elif not curr_grpd:
1909 ret['changes']['xmpp_helper_enable'] = {"from": None, "to": grp['xmpp_helper_enable']}
1910 if 'long_lived_restart_time' in grp:
1911 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1912 if curr_grpd and grp['long_lived_restart_time'] != int(curr_grpd['long_lived_restart_time']):
1913 ret['changes']['long_lived_restart_time'] = {"from": int(curr_grpd['long_lived_restart_time']), "to": grp['long_lived_restart_time']}
1914 elif not curr_grpd:
1915 ret['changes']['long_lived_restart_time'] = {"from": None, "to": grp['long_lived_restart_time']}
1916 else:
1917 grp_obj = None
1918
1919 config_obj.graceful_restart_parameters = grp_obj
1920
1921 if ans:
1922 if config_obj.autonomous_system != ans:
1923 ret['changes']['autonomous_system'] = {"from": config_obj.autonomous_system, "to": ans}
1924 config_obj.autonomous_system = ans
1925
1926 vnc_client.global_system_config_update(config_obj)
1927 ret['comment'] = 'Global system config ' + name + ' has been updated'
1928 else:
1929 config_obj = GlobalSystemConfig(name=name)
1930 if grp and not HAS_OLD:
1931 grp_obj = GracefulRestartParametersType()
1932 if 'enable' in grp:
1933 grp_obj.enable = grp['enable']
1934 if 'restart_time' in grp:
1935 grp_obj.restart_time = grp['restart_time']
1936 if 'end_of_rib_timeout' in grp:
1937 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1938 if 'bgp_helper_enable' in grp:
1939 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1940 if 'xmpp_helper_enable' in grp:
1941 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1942 if 'long_lived_restart_time' in grp:
1943 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1944 config_obj.graceful_restart_parameters = grp_obj
1945 if ans:
1946 config_obj.autonomous_system = ans
1947
1948 vnc_client.global_system_config_create(config_obj)
1949 ret['changes'] = {"created": "new"}
1950 ret['comment'] = 'Global system config ' + name + ' has been created '
1951
1952 return ret
1953
1954
1955def global_system_config_delete(name, **kwargs):
1956 '''
1957 Delete specific Contrail global system config
1958
1959 CLI Example:
1960
1961 .. code-block:: bash
1962
1963 salt '*' contrail.global_system_config_delete name
1964 '''
1965 ret = {'name': name,
1966 'changes': {},
1967 'result': True,
1968 'comment': ''}
1969 vnc_client = _auth(**kwargs)
1970
1971 gsc_obj = GlobalSystemConfig(name)
1972 if __opts__['test']:
1973 ret['result'] = None
1974 ret['comment'] = "Global system config " + name + " will be deleted"
1975 else:
1976 vnc_client.global_system_config_delete(fq_name=gsc_obj.get_fq_name())
1977 ret['comment'] = "GlobalSystemConfig " + name + " has been deleted"
1978 ret['changes'] = {'GlobalSystemConfig': {'old': name, 'new': ''}}
1979 return ret
Jan Cachb3092722018-01-31 12:46:16 +01001980
1981
1982def list_floating_ip_pools(**kwargs):
1983 '''
1984 List all floating ip pools
1985
1986 CLI Example:
1987 .. code-block:: bash
1988 salt '*' contrail.list_floating_ip_pools
1989 '''
1990 vnc_client = _auth(**kwargs)
1991 pools = vnc_client.floating_ip_pools_list()
1992 # list of floating ip pools objects
1993 fp_list = []
1994
1995 for pool in vnc_client.floating_ip_pools_list()['floating-ip-pools']:
1996 fip_obj = vnc_client.floating_ip_pool_read(pool['fq_name'])
1997 fp_list.append(fip_obj)
1998 # print given pool
1999 fip_obj.dump()
2000
2001def update_floating_ip_pool(vn_name, vn_project, vn_domain=None,
2002 owner_access=None, global_access=None,
2003 projects=None, **kwargs):
2004 '''
2005 Update specific floating ip pool
2006
2007
2008 CLI Example
2009 .. code-block:: bash
2010 salt-call contrail.update_floating_ip_pool \
2011 'FLOATING-TEST' \
2012 'admin' \
2013 'default-domain' \
2014 7 7 \
2015 [['pepa',4],['karel',7]]
2016
2017
2018 params:
2019 vn_name - name of the virtual network, which to use
2020 vn-project - project which includes virtual network
2021 vn-domain - domain wchich includes vn_project and vn_name
2022 owner_access (int) - Permission rights for owner
2023 global_access (int) - Permission rights for others
2024 projects (list) - list of ShareType(tenant_name,tennat_permissions)
2025 '''
2026 ret = {'name': vn_name + "-default pool",
2027 'changes': {},
2028 'result': True,
2029 'comment': ''}
2030
2031 if vn_domain is None:
2032 vn_domain = 'default-domain'
2033 fip_obj = None
2034
2035 vnc_client = _auth(**kwargs)
2036 p_fq_name = [vn_domain, vn_project, vn_name, 'default']
2037 fip_obj = vnc_client.floating_ip_pool_read(fq_name=p_fq_name)
2038
2039 changes = {}
2040 # get perms from fip_obj (Floatin ip pool)
2041 perms2 = fip_obj.get_perms2()
2042 if owner_access is not None:
2043 if perms2.get_owner_access() != owner_access:
2044 changes['owner_access'] = {'old': str(perms2.get_owner_access()),
2045 'new': str(owner_access)}
2046 perms2.set_owner_access(owner_access)
2047
2048 if global_access is not None:
2049 if perms2.get_global_access() != global_access:
2050 changes['global_access'] = {'old': str(perms2.get_global_access()),
2051 'new': str(global_access)}
2052 perms2.set_global_access(global_access)
2053
2054 # list which represents the new state of perms
2055 final_list = []
2056 if projects:
2057 for item in perms2.get_share():
2058 for share in projects:
2059 if item.get_tenant() == share[0]:
2060 # project is in the new and old list
2061 # check is the permission number is same
2062 if item.get_tenant_access() == share[1]:
2063 # this project and permission is without change, keep it
2064 final_list.append(item)
2065 break
2066 else:
2067 # project exists but change the permission
2068 final_list.append(ShareType(tenant=share[0],
2069 tenant_access=share[1]))
2070 # show changes
2071 n = str('share-'+share[0])
2072 old_str = "permission for project {0} was {1}"
2073 new_str = "permission for project {0} will be {1}"
2074 old = old_str.format(share[0],
2075 str(item.get_tenant_access()))
2076
2077 new = new_str.format(share[0], str(share[1]))
2078 changes[n] = {'old': old, 'new': new}
2079 break
2080 else:
2081 rm_name = "share-" + item.get_tenant()
2082 changes[rm_name] = item.get_tenant() + " will be removed"
2083
2084 # check for the completly new projects
2085 for item in projects:
2086 for share in final_list:
2087 if item[0] == share.get_tenant():
2088 break
2089 else:
2090 final_list.append(ShareType(tenant=item[0],
2091 tenant_access=item[1]))
2092 name = 'share-' + str(item[0])
2093 c_str = '{0} will be added with permissions {1}'
2094 changes[name] = c_str.format(name, item[1])
2095 else:
2096 for item in perms2.get_share():
2097 rm_name = "share-" + item.get_tenant()
2098 changes[rm_name] = item.get_tenant() + " will be removed"
2099
2100 if __opts__['test']:
2101 ret['result'] = None
2102 ret['comment'] = changes
2103
2104 return ret
2105 else:
2106 ret['comment'] = changes
2107 perms2.set_share(final_list)
2108 fip_obj.set_perms2(perms2)
2109 vnc_client.floating_ip_pool_update(fip_obj)
2110
2111 return ret
Sergey Matov16896ac2018-02-15 15:46:31 +04002112
2113
2114def show_rbac_rules(api_access_list_entries):
2115 if api_access_list_entries is None:
2116 return 'Empty RBAC group!'
2117
2118 rule_list = api_access_list_entries.get_rbac_rule()
2119 response = 'Rules (%d):' % len(rule_list) + '----------\n'
2120 for idx, rule in enumerate(rule_list):
2121 o = rule.rule_object
2122 f = rule.rule_field
2123 ps = ', '.join([p.role_name+':'+p.role_crud for p in rule.rule_perms])
2124 o_f = "%s.%s" % (o, f) if f else o
2125 response += '%2d %-32s %s\n' % (idx, o_f, ps)
2126 return response
2127
2128
2129def vnc_read_obj(vnc, obj_type, fq_name):
2130 method_name = obj_type.replace('-', '_')
2131 method = getattr(vnc, "%s_read" % (method_name))
2132 try:
2133 return method(fq_name=fq_name)
2134 except NoIdError:
2135 print '%s %s not found!' % (obj_type, fq_name)
2136 return None
2137
2138
2139def rbac_show_group(name, uuid, **kwargs):
2140 '''
2141 Show specific RBAC group
2142
2143
2144 CLI Example
2145 .. code-block:: bash
2146 salt-call contrail.rbac_show_group name \
2147 'default-domain:default-project:default'
2148
2149 params:
2150 name - one of pair {name, uuid} addresing to access-list
2151 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2152 including domain and project
2153
2154 '''
2155 vnc = _auth(**kwargs)
2156 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2157 ret = {'name': fq_name,
2158 'changes': {},
2159 'result': True,
2160 'comment': ''}
2161 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2162 if not rg:
2163 ret['comment'] = 'No rules found'
2164 return ret
2165
2166 ret['comment'] = show_rbac_rules(rg.get_api_access_list_entries())
2167 return ret
2168
2169
2170def rbac_create_group(uuid, **kwargs):
2171 '''
2172 Create RBAC group
2173
2174 CLI Example
2175 .. code-block:: bash
2176 salt-call contrail.rbac_create_group name \
2177 'default-domain:default-project:default'
2178
2179 params:
2180 name - one of pair {name, uuid} addresing to access-list
2181 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2182 including domain and project
2183
2184 '''
2185 vnc = _auth(**kwargs)
2186 fq_name = uuid.split(':')
2187 ret = {'name': fq_name,
2188 'changes': {},
2189 'result': True,
2190 'comment': ''}
2191 if len(fq_name) != 2 and len(fq_name) != 3:
2192 ret['comment'] = 'Fully qualified name of rbac group expected'
2193 return ret
2194
2195 name = fq_name[-1]
2196
2197 if len(fq_name) == 2:
2198 if fq_name[0] == 'default-global-system-config':
2199 pobj = vnc.global_system_config_read(fq_name=fq_name[0:1])
2200 else:
2201 pobj = vnc.domain_read(fq_name=fq_name[0:1])
2202 else:
2203 pobj = vnc.project_read(fq_name=fq_name[0:2])
2204
2205 rentry = RbacRuleEntriesType([])
2206 rg = ApiAccessList(name, parent_obj=pobj, api_access_list_entries=rentry)
2207
2208 if __opts__['test']:
2209 ret['result'] = None
2210 ret['comment'] = "RBAC group " + uuid + " will be created"
2211
2212 return ret
2213 else:
2214 vnc.api_access_list_create(rg)
2215 rg2 = vnc.api_access_list_read(fq_name=fq_name)
2216 rge = rg.get_api_access_list_entries()
2217 show_rbac_rules(rge)
2218 ret['comment'] = "RBAC group " + uuid + " has been created"
2219
2220 return ret
2221
2222
2223def rbac_delete_group(name, uuid, **kwargs):
2224 '''
2225 Delete RBAC group
2226
2227 CLI Example
2228 .. code-block:: bash
2229 salt-call contrail.rbac_delete_group name \
2230 'default-domain:default-project:default'
2231
2232 params:
2233 name - one of pair {name, uuid} addresing to access-list
2234 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2235 including domain and project
2236
2237 '''
2238 vnc = _auth(**kwargs)
2239 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2240 ret = {'name': fq_name,
2241 'changes': {},
2242 'result': True,
2243 'comment': ''}
2244 if len(fq_name) != 2 and len(fq_name) != 3:
2245 ret['comment'] = 'Fully qualified name of rbac group expected'
2246 return ret
2247 name = fq_name[-1]
2248
2249 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2250 if not rg:
2251 ret['comment'] = 'No rules found'
2252 return ret
2253 rge = rg.get_api_access_list_entries()
2254 show_rbac_rules(rge)
2255
2256 if __opts__['test']:
2257 ret['result'] = None
2258 ret['comment'] = "RBAC group " + uuid + " will be deleted"
2259
2260 return ret
2261 else:
2262 vnc.api_access_list_delete(fq_name=fq_name)
2263 ret['comment'] = "RBAC group " + uuid + " has been deleted"
2264
2265 return ret
2266
2267
2268def rbac_add_rule(name, uuid, add_rule, **kwargs):
2269 '''
2270 Add rule to specific RBAC group
2271
2272 CLI Example
2273 .. code-block:: bash
2274 salt-call contrail.rbac_add_rule name \
2275 'default-domain:default-project:default' \
2276 '* admin:CRUD'
2277
2278 params:
2279 name - one of pair {name, uuid} addresing to access-list
2280 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2281 including domain and project
2282 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2283 list of <role:CRUD>' to be added
2284
2285 '''
2286 vnc = _auth(**kwargs)
2287 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2288 rule = build_rule(add_rule)
2289 ret = {'name': fq_name,
2290 'changes': {},
2291 'result': True,
2292 'comment': ''}
2293 if rule is None:
2294 ret['comment'] = 'A rule string must be specified for this operation'
2295 return ret
2296
2297 # rbac rule entry consists of one or more rules
2298 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2299 if not rg:
2300 ret['comment'] = 'No rules found'
2301 return ret
2302
2303 rge = rg.get_api_access_list_entries()
2304 if rge is None:
2305 rge = RbacRuleEntriesType([])
2306 show_rbac_rules(rge)
2307
2308 # avoid duplicates
2309 match = find_rule(rge, rule)
2310 if not match:
2311 rge.add_rbac_rule(rule)
2312 else:
2313 build_perms(rge.rbac_rule[match[0]-1], match[3])
2314
2315 show_rbac_rules(rge)
2316
2317 if __opts__['test']:
2318 ret['result'] = None
2319 ret['comment'] = "Rule " + add_rule
2320 ret['comment'] += " will be created for RBAC group " + uuid
2321
2322 return ret
2323 else:
2324 rg.set_api_access_list_entries(rge)
2325 vnc.api_access_list_update(rg)
2326 ret['comment'] = "Rule " + add_rule
2327 ret['comment'] += " has been added for RBAC group " + uuid
2328
2329 return ret
2330
2331
2332def rbac_delete_rule(name, uuid, del_rule, **kwargs):
2333 '''
2334 Delete rule to specific RBAC group
2335
2336 CLI Example
2337 .. code-block:: bash
2338 salt-call contrail.rbac_delete_rule name \
2339 'default-domain:default-project:default' \
2340 '* admin:CRUD'
2341
2342 params:
2343 name - one of pair {name, uuid} addresing to access-list
2344 uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \
2345 including domain and project
2346 rule(str) - Appropriate RBAC-based rule in format '<object, field> \
2347 list of <role:CRUD>' to be deleted
2348
2349 '''
2350 vnc = _auth(**kwargs)
2351 fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':')
2352 rg = vnc_read_obj(vnc, 'api-access-list', fq_name)
2353 ret = {'name': fq_name,
2354 'changes': {},
2355 'result': True,
2356 'comment': ''}
2357 if not rg:
2358 ret['comment'] = 'No rules found'
2359 return ret
2360 rge = rg.get_api_access_list_entries()
2361 show_rbac_rules(rge)
2362
2363 del_idx = re.match("^[0-9]+$", del_rule)
2364 if del_idx:
2365 del_idx = int(del_idx.group())
2366 rc = len(rge.rbac_rule)
2367 if del_idx > rc or del_idx < 1:
2368 ret['comment'] = 'Invalid rule index to delete.'
2369 ret['comment'] += 'Value must be 1-%d' % rc
2370 return ret
2371 match = (del_idx, True)
2372 else:
2373 rule = build_rule(del_rule)
2374 match = find_rule(rge, rule)
2375
2376 if not match:
2377 ret['comment'] = "Rule not found. Unchanged"
2378 return ret
2379 elif match[1]:
2380 rge.rbac_rule.pop(match[0]-1)
2381 else:
2382 build_perms(rge.rbac_rule[match[0]-1], match[2])
2383 show_rbac_rules(rge)
2384
2385 if __opts__['test']:
2386 ret['result'] = None
2387 ret['comment'] = "Rule " + del_rule
2388 ret['comment'] += " will be cleared from RBAC group " + uuid
2389
2390 return ret
2391 else:
2392 rg.set_api_access_list_entries(rge)
2393 vnc.api_access_list_update(rg)
2394 ret['comment'] = "Rule " + del_rule
2395 ret['comment'] += " has been cleared from RBAC group " + uuid
2396
2397 return ret