blob: cfdc79336ce7f134d09d967839db07a27076aa0e [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
16from netaddr import IPNetwork
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020017from vnc_api.vnc_api import PhysicalRouter, PhysicalInterface, LogicalInterface
18from vnc_api.vnc_api import EncapsulationPrioritiesType
Vasyl Saienkob10b7202017-09-05 14:19:03 +030019from vnc_api.vnc_api import VirtualMachineInterface, MacAddressesType
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020020from vnc_api.vnc_api import ServiceApplianceSet, KeyValuePairs, KeyValuePair
Ales Komarekad46d2e2017-03-09 17:16:38 +010021
22try:
23 from vnc_api import vnc_api
Petr Jediný5f3efe32017-05-26 17:55:09 +020024 from vnc_api.vnc_api import LinklocalServiceEntryType, \
25 LinklocalServicesTypes, GlobalVrouterConfig
Ales Komarekad46d2e2017-03-09 17:16:38 +010026 from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \
27 ConfigNode, DatabaseNode, BgpRouter
28 from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \
29 BgpSession, BgpPeeringAttributes, BgpRouterParams
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020030
Ales Komarekad46d2e2017-03-09 17:16:38 +010031 HAS_CONTRAIL = True
32except ImportError:
33 HAS_CONTRAIL = False
34
35__opts__ = {}
36
37
38def __virtual__():
39 '''
40 Only load this module if vnc_api library is installed.
41 '''
42 if HAS_CONTRAIL:
43 return 'contrail'
44
45 return False
46
47
48def _auth(**kwargs):
49 '''
50 Set up Contrail API credentials.
51 '''
52 user = kwargs.get('user')
53 password = kwargs.get('password')
54 tenant_name = kwargs.get('project')
55 api_host = kwargs.get('api_server_ip')
56 api_port = kwargs.get('api_server_port')
57 api_base_url = kwargs.get('api_base_url')
58 use_ssl = False
59 auth_host = kwargs.get('auth_host_ip')
60 vnc_lib = vnc_api.VncApi(user, password, tenant_name,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020061 api_host, api_port, api_base_url, wait_for_connect=True,
62 api_server_use_ssl=use_ssl, auth_host=auth_host)
Ales Komarekad46d2e2017-03-09 17:16:38 +010063
64 return vnc_lib
65
66
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020067def _get_config(vnc_client, global_system_config='default-global-system-config'):
Ales Komarekad46d2e2017-03-09 17:16:38 +010068 try:
69 gsc_obj = vnc_client.global_system_config_read(id=global_system_config)
70 except vnc_api.NoIdError:
71 gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config)
72 except:
73 gsc_obj = None
74
75 return gsc_obj
76
77
78def _get_rt_inst_obj(vnc_client):
Ales Komarekad46d2e2017-03-09 17:16:38 +010079 # TODO pick fqname hardcode from common
80 rt_inst_obj = vnc_client.routing_instance_read(
81 fq_name=['default-domain', 'default-project',
82 'ip-fabric', '__default__'])
83
84 return rt_inst_obj
85
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020086
Vasyl Saienkob10b7202017-09-05 14:19:03 +030087def _get_fq_name(vnc_client, resource_name, project_name, domain='default-domain'):
88 res = [domain]
89 if project_name:
90 res.append(project_name)
91 if resource_name:
92 res.append(resource_name)
93 return res
94
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020095
Vasyl Saienkob10b7202017-09-05 14:19:03 +030096def _get_project_obj(vnc_client, name, domain='default-domain'):
97 return vnc_client.project_read(fq_name=[domain, name])
98
Ales Komarekad46d2e2017-03-09 17:16:38 +010099
100def _get_ip(ip_w_pfx):
101 return str(IPNetwork(ip_w_pfx).ip)
102
103
Ales Komarekad46d2e2017-03-09 17:16:38 +0100104def virtual_router_list(**kwargs):
105 '''
106 Return a list of all Contrail virtual routers
107
108 CLI Example:
109
110 .. code-block:: bash
111
112 salt '*' contrail.virtual_router_list
113 '''
114 ret = {}
115 vnc_client = _auth(**kwargs)
116 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
117 for vrouter_obj in vrouter_objs:
118 ret[vrouter_obj.name] = {
119 'ip_address': vrouter_obj.virtual_router_ip_address,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200120 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled,
121 'uuid': vrouter_obj.uuid
122
Ales Komarekad46d2e2017-03-09 17:16:38 +0100123 }
124 return ret
125
126
127def virtual_router_get(name, **kwargs):
128 '''
129 Return a specific Contrail virtual router
130
131 CLI Example:
132
133 .. code-block:: bash
134
135 salt '*' contrail.virtual_router_get cmp01
136 '''
137 ret = {}
138 vrouter_objs = virtual_router_list(**kwargs)
139 if name in vrouter_objs:
140 ret[name] = vrouter_objs.get(name)
141 if len(ret) == 0:
142 return {'Error': 'Error in retrieving virtual router.'}
143 return ret
144
145
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200146def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +0100147 '''
148 Create specific Contrail virtual router
149
150 CLI Example:
151
152 .. code-block:: bash
153
154 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200155 router_types:
156 - tor-agent
157 - tor-service-node
158 - embedded
Ales Komarekad46d2e2017-03-09 17:16:38 +0100159 '''
160 ret = {}
161 vnc_client = _auth(**kwargs)
162 gsc_obj = _get_config(vnc_client)
163 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200164 router_types = ['tor-agent', 'tor-service-node', 'embedded']
165 if router_type not in router_types:
166 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100167 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200168 vrouter = virtual_router_get(name)
169 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
170 changed = False
171 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
172 ret['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
173 vrouter_obj.set_virtual_router_ip_address(ip_address)
174 changed = True
175 if vrouter_obj.get_virtual_router_type() != router_type:
176 ret['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
177 vrouter_obj.set_virtual_router_type(router_type)
178 changed = True
179 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
180 ret['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
181 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
182 changed = True
183 if changed:
184 if __opts__['test']:
185 return "Virtual router " + name + " will be updated"
186 vnc_client.virtual_router_update(vrouter_obj)
187 return ret
188 return {'OK': 'Virtual router %s already exists and is updated' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100189 else:
190 vrouter_obj = VirtualRouter(
191 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200192 virtual_router_ip_address=ip_address,
193 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100194 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200195 if __opts__['test']:
196 return "Virtual router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100197 vnc_client.virtual_router_create(vrouter_obj)
198 ret = virtual_router_list(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200199 return "Create"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100200
201
202def virtual_router_delete(name, **kwargs):
203 '''
204 Delete specific Contrail virtual router
205
206 CLI Example:
207
208 .. code-block:: bash
209
210 salt '*' contrail.virtual_router_delete cmp01
211 '''
212 vnc_client = _auth(**kwargs)
213 gsc_obj = _get_config(vnc_client)
214 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200215 if __opts__['test']:
216 return "Virtual router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100217 vnc_client.virtual_router_delete(
218 fq_name=vrouter_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200219 return "Deleted"
220
221
222def physical_router_list(**kwargs):
223 '''
224 Return a list of all Contrail physical routers
225
226 CLI Example:
227
228 .. code-block:: bash
229
230 salt '*' contrail.physical_router_list
231 '''
232 ret = {}
233 vnc_client = _auth(**kwargs)
234 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
235 for prouter_obj in prouter_objs:
236 ret[prouter_obj.name] = {
237 'uuid': prouter_obj._uuid,
238 'management_ip': prouter_obj._physical_router_management_ip,
239 'product_name': prouter_obj._physical_router_product_name,
240 }
241
242 return ret
243
244
245def physical_router_get(name, **kwargs):
246 '''
247 Return a specific Contrail physical router
248
249 CLI Example:
250
251 .. code-block:: bash
252
253 salt '*' contrail.physical_router_get router_name
254 '''
255 ret = {}
256 vnc_client = _auth(**kwargs)
257 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
258 for prouter_obj in prouter_objs:
259 if name == prouter_obj.name:
260 ret[name] = prouter_obj.__dict__
261 if len(ret) == 0:
262 return {'Error': 'Error in retrieving physical router.'}
263 return ret
264
265
266def physical_router_create(name, parent_type=None,
267 management_ip=None,
268 dataplane_ip=None, # VTEP address in web GUI
269 vendor_name=None,
270 product_name=None,
271 vnc_managed=None,
272 junos_service_ports=None,
273 agents=None, **kwargs):
274 '''
275 Create specific Contrail physical router
276
277 CLI Example:
278
279 .. code-block:: bash
280
281 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']"
282 '''
283 ret = {}
284 vnc_client = _auth(**kwargs)
285 gsc_obj = _get_config(vnc_client)
286 prouter_objs = physical_router_list(**kwargs)
287 if name in prouter_objs:
288 prouter = physical_router_get(name)
289 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
290 if prouter_obj.physical_router_management_ip != management_ip:
291 ret['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
292 prouter_obj.set_physical_router_management_ip(management_ip)
293 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
294 ret['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
295 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
296 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
297 ret['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
298 prouter_obj.set_physical_router_vendor_name(vendor_name)
299 if prouter_obj.get_physical_router_product_name() != product_name:
300 ret['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
301 prouter_obj.set_physical_router_product_name(product_name)
302 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
303 ret['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
304 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
305 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
306 ret['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
307 "to": junos_service_ports}
308 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
309
310 if __opts__['test']:
311 if len(ret) != 0:
312 return "Physical router " + name + " will be updated"
313 return {"OK": "Physical router exists and is updated"}
314
315 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
316 c_agents = [] # referenced vrouters
317 for c_agent in prouter_obj.get_virtual_router_refs():
318 c_agents.append(c_agent['uuid'])
319 agent_objs = [] # required state of references
320 for vrouter_obj in vrouter_objs:
321 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
322 prouter_obj.add_virtual_router(vrouter_obj)
323 ret['vrouter ' + vrouter_obj._display_name] = "Reference added"
324 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
325 prouter_obj.del_virtual_router(vrouter_obj)
326 ret['vrouter ' + vrouter_obj._display_name] = "Reference removed"
327 vnc_client.physical_router_update(prouter_obj)
328
329 if len(ret) == 0:
330 return {"OK": "Physical router exists and is updated"}
331 return ret
332 else:
333 if __opts__['test']:
334 return "Physical router " + name + " will be created"
335 prouter_obj = PhysicalRouter(
336 name=name,
337 parent_obj=None,
338 physical_router_management_ip=management_ip,
339 physical_router_dataplane_ip=dataplane_ip,
340 physical_router_vendor_name=vendor_name,
341 physical_router_product_name=product_name,
342 physical_router_vnc_managed=vnc_managed,
343 physical_router_junos_service_ports=junos_service_ports,
344 )
345 for agent in agents:
346 vrouter = virtual_router_get(agent)
347 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
348 prouter_obj.add_virtual_router(vrouter_obj)
349 vnc_client.physical_router_create(prouter_obj)
350 return "Created"
351
352
353def physical_router_delete(name, **kwargs):
354 '''
355 Delete specific Contrail physical router
356
357 CLI Example:
358
359 .. code-block:: bash
360
361 salt '*' contrail.physical_router_delete router_name
362 '''
363 vnc_client = _auth(**kwargs)
364 gsc_obj = _get_config(vnc_client)
365 prouter_obj = PhysicalRouter(name, gsc_obj)
366 if __opts__['test']:
367 return "Physical router " + name + " will be deleted"
368 vnc_client.physical_router_delete(
369 fq_name=prouter_obj.get_fq_name())
370 return "Deleted"
371
372
373def physical_interface_list(**kwargs):
374 '''
375 Return a list of all Contrail physical interface
376
377 CLI Example:
378
379 .. code-block:: bash
380
381 salt '*' contrail.physical_interface_list
382 '''
383 ret = {}
384 vnc_client = _auth(**kwargs)
385 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
386 for pinterface_obj in pinterface_objs:
387 ret[pinterface_obj.name] = {
388 'uuid': pinterface_obj._uuid,
389 'fq_name': pinterface_obj.fq_name,
390 'parent_type': pinterface_obj.parent_type,
391 }
392
393 return ret
394
395
396def physical_interface_get(name, physical_router, **kwargs):
397 '''
398 Return a specific Contrail physical interface
399
400 CLI Example:
401
402 .. code-block:: bash
403
404 salt '*' contrail.physical_interface_get interface_name physical_router_name
405 '''
406 ret = {}
407 vnc_client = _auth(**kwargs)
408 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
409 for pinterf_obj in pinterf_objs:
410 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
411 ret[name] = pinterf_obj.__dict__
412 if len(ret) == 0:
413 return {'Error': 'Error in retrieving physical interface.'}
414 return ret
415
416
417def physical_interface_create(name, physical_router, **kwargs):
418 '''
419 Create specific Contrail physical interface
420
421 CLI Example:
422
423 .. code-block:: bash
424
425 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
426 '''
427 ret = {}
428 vnc_client = _auth(**kwargs)
429 gsc_obj = _get_config(vnc_client)
430 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
431 if 'Error' not in pinterf_obj:
432 return {'OK': 'Physical interface ' + name + ' on ' + physical_router + ' already exists'}
433 else:
434 if __opts__['test']:
435 return "Physical interface " + name + " will be created"
436 prouter = physical_router_get(physical_router)
437 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
438 pinterf_obj = PhysicalInterface(name, prouter_obj)
439 vnc_client.physical_interface_create(pinterf_obj)
440 return "Created"
441
442
443def physical_interface_delete(name, physical_router, **kwargs):
444 '''
445 Delete specific Contrail physical interface
446
447 CLI Example:
448 .. code-block:: bash
449
450 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
451 '''
452 vnc_client = _auth(**kwargs)
453 gsc_obj = _get_config(vnc_client)
454 piface = physical_interface_get(name, physical_router)
455 if __opts__['test']:
456 return "Physical interface " + name + " will be deleted"
457 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
458 return "Deleted"
459
460
461def logical_interface_list(**kwargs):
462 '''
463 Return a list of all Contrail logical interfaces
464
465 CLI Example:
466
467 .. code-block:: bash
468
469 salt '*' contrail.logical_interface_list
470 '''
471 ret = []
472 vnc_client = _auth(**kwargs)
473 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
474 for liface_obj in liface_objs:
475 ret.append({
476 'name': liface_obj.name,
477 'uuid': liface_obj._uuid,
478 'fq_name': liface_obj.fq_name,
479 'parent_type': liface_obj.parent_type,
480 })
481 return ret
482
483
484def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
485 '''
486 Return a specific Contrail logical interface
487
488 CLI Example:
489
490 .. code-block:: bash
491
492 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
493 or
494 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
495 or
496 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
497 '''
498 ret = {}
499 vnc_client = _auth(**kwargs)
500 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
501 count = 0
502 for liface_obj in liface_objs:
503 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
504 if parent_type and parent_type == liface_obj.parent_type:
505 count += 1
506 ret[liface_obj.name] = liface_obj.__dict__
507 if not parent_type:
508 count += 1
509 ret[liface_obj.name] = liface_obj.__dict__
510 if len(ret) == 0:
511 return {'Error': 'Error in retrieving logical interface.'}
512 if count > 1:
513 return {
514 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
515 return ret
516
517
518def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300519 vmis=None, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200520 '''
521 Create specific Contrail logical interface
522
523 CLI Example:
524
525 .. code-block:: bash
526
527 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
528 '''
529 ret = {}
530 vnc_client = _auth(**kwargs)
531 gsc_obj = _get_config(vnc_client)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300532
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200533 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
534 if 'Error' not in liface_obj:
535 return {'OK': 'Logical interface ' + name + ' already exists'}
536 else:
537 if __opts__['test']:
538 return "Logical interface " + name + " will be created"
539 parent_obj = None
540 for router in parent_names:
541 parent_router = physical_router_get(router)
542 if 'Error' not in parent_router:
543 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
544 break
545 if not parent_obj:
546 return {'Error': 'Physical router have to be defined'}
547 if parent_type == 'physical-interface':
548 for interface in parent_names:
549 parent_interface = physical_interface_get(interface, parent_obj.name)
550 if 'Error' not in parent_interface:
551 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
552 break
553 if interface_type.lower() == "l3":
554 return {'Error': "Virtual Network have to be defined for L3 interface type"}
555
556 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300557
558 for vmi_name, vmi in vmis.iteritems():
559 vmi = vnc_client.virtual_machine_interface_read(
560 fq_name=_get_fq_name(vnc_client, resource_name=vmi_name,
561 project_name=kwargs.get('tenant', 'admin')))
562 liface_obj.add_virtual_machine_interface(vmi)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200563 vnc_client.logical_interface_create(liface_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300564
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200565 return "Created"
566
567
568def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
569 '''
570 Delete specific Contrail logical interface
571
572 CLI Example:
573
574 .. code-block:: bash
575
576 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
577 or
578 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
579
580 '''
581 vnc_client = _auth(**kwargs)
582 gsc_obj = _get_config(vnc_client)
583 liface = logical_interface_get(name, parent_names, parent_type)
584 if 'Error' not in liface:
585 if __opts__['test']:
586 return "Logical interface " + name + " will be deleted"
587 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
588 return "Deleted"
589 else:
590 return liface
591
592
593def global_vrouter_config_list(**kwargs):
594 '''
595 Return a list of all Contrail global vrouter configs
596
597 CLI Example:
598
599 .. code-block:: bash"
600
601 salt '*' global_vrouter_config_list
602 '''
603 ret = {}
604 vnc_client = _auth(**kwargs)
605 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
606 for vrouter_conf_obj in vrouter_conf_objs:
607 ret[vrouter_conf_obj._display_name] = vrouter_conf_obj.__dict__
608 return ret
609
610
611def global_vrouter_config_get(name, **kwargs):
612 '''
613 Return a specific Contrail global vrouter config
614
615 CLI Example:
616
617 .. code-block:: bash
618
619 salt '*' contrail.global_vrouter_get global-vrouter-config
620 '''
621 ret = {}
622 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
623 if name in vrouter_conf_objs:
624 ret[name] = vrouter_conf_objs.get(name)
625 if len(ret) == 0:
626 return {'Error': 'Error in retrieving global vrouter config.'}
627 return ret
628
629
630def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, *fq_names, **kwargs):
631 '''
632 Create specific Contrail global vrouter config
633
634 CLI Example:
635
636 .. code-block:: bash
637
638 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']"
639 '''
640 ret = {}
641 vnc_client = _auth(**kwargs)
642 gsc_obj = _get_config(vnc_client)
643 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
644 if name in vrouter_conf_objs:
645 return {'OK': 'Global vrouter config %s already exists' % name}
646 else:
647 vrouter_conf_obj = GlobalVrouterConfig(
648 name=name,
649 parent_obj=None,
650 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
651 fq_name=fq_names,
652 vxlan_network_identifier_mode=vxlan_vn_id_mode,
653 parent_type=parent_type,
654 )
655 if __opts__['test']:
656 return "Global vRouter config " + name + " will be created"
657 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
658 return "Created"
659
660
661def global_vrouter_config_delete(name, **kwargs):
662 '''
663 Delete specific Contrail global vrouter config
664
665 CLI Example:
666
667 .. code-block:: bash
668
669 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
670 '''
671 vnc_client = _auth(**kwargs)
672 gsc_obj = _get_config(vnc_client)
673 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
674 if __opts__['test']:
675 return "Global vRouter config " + name + " will be deleted"
676 vnc_client.global_vrouter_config_delete(
677 fq_name=vrouter_conf_obj.get_fq_name())
678 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100679
680
681def analytics_node_list(**kwargs):
682 '''
683 Return a list of all Contrail analytics nodes
684
685 CLI Example:
686
687 .. code-block:: bash
688
689 salt '*' contrail.analytics_node_list
690 '''
691 ret = {}
692 vnc_client = _auth(**kwargs)
693 node_objs = vnc_client._objects_list('analytics-node', detail=True)
694 for node_obj in node_objs:
695 ret[node_obj.name] = node_obj.__dict__
696 return ret
697
698
699def analytics_node_get(name, **kwargs):
700 '''
701 Return a specific Contrail analytics node
702
703 CLI Example:
704
705 .. code-block:: bash
706
707 salt '*' contrail.analytics_node_get nal01
708 '''
709 ret = {}
710 vrouter_objs = analytics_node_list(**kwargs)
711 if name in vrouter_objs:
712 ret[name] = vrouter_objs.get(name)
713 if len(ret) == 0:
714 return {'Error': 'Error in retrieving analytics node.'}
715 return ret
716
717
718def analytics_node_create(name, ip_address, **kwargs):
719 '''
720 Create specific Contrail analytics node
721
722 CLI Example:
723
724 .. code-block:: bash
725
726 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
727 '''
728 ret = {}
729 vnc_client = _auth(**kwargs)
730 gsc_obj = _get_config(vnc_client)
731 analytics_node_objs = analytics_node_list(**kwargs)
732 if name in analytics_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200733 return {'OK': 'Analytics node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100734 else:
735 analytics_node_obj = AnalyticsNode(
736 name, gsc_obj,
737 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200738 if __opts__['test']:
739 return "AnalyticsNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100740 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200741 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100742
743
744def analytics_node_delete(name, **kwargs):
745 '''
746 Delete specific Contrail analytics node
747
748 CLI Example:
749
750 .. code-block:: bash
751
752 salt '*' contrail.analytics_node_delete cmp01
753 '''
754 vnc_client = _auth(**kwargs)
755 gsc_obj = _get_config(vnc_client)
756 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200757 if __opts__['test']:
758 return "AnalyticsNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100759 vnc_client.analytics_node_delete(
760 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200761 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100762
763
764def config_node_list(**kwargs):
765 '''
766 Return a list of all Contrail config nodes
767
768 CLI Example:
769
770 .. code-block:: bash
771
772 salt '*' contrail.config_node_list
773 '''
774 ret = {}
775 vnc_client = _auth(**kwargs)
776 node_objs = vnc_client._objects_list('config-node', detail=True)
777 for node_obj in node_objs:
778 ret[node_obj.name] = node_obj.__dict__
779 return ret
780
781
782def config_node_get(name, **kwargs):
783 '''
784 Return a specific Contrail config node
785
786 CLI Example:
787
788 .. code-block:: bash
789
790 salt '*' contrail.config_node_get nal01
791 '''
792 ret = {}
793 vrouter_objs = config_node_list(**kwargs)
794 if name in vrouter_objs:
795 ret[name] = vrouter_objs.get(name)
796 if len(ret) == 0:
797 return {'Error': 'Error in retrieving config node.'}
798 return ret
799
800
801def config_node_create(name, ip_address, **kwargs):
802 '''
803 Create specific Contrail config node
804
805 CLI Example:
806
807 .. code-block:: bash
808
809 salt '*' contrail.config_node_create ntw03 10.10.10.103
810 '''
811 ret = {}
812 vnc_client = _auth(**kwargs)
813 gsc_obj = _get_config(vnc_client)
814 config_node_objs = config_node_list(**kwargs)
815 if name in config_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200816 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100817 else:
818 config_node_obj = ConfigNode(
819 name, gsc_obj,
820 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200821 if __opts__['test']:
822 return "ConfigNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100823 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200824 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100825
826
827def config_node_delete(name, **kwargs):
828 '''
829 Delete specific Contrail config node
830
831 CLI Example:
832
833 .. code-block:: bash
834
835 salt '*' contrail.config_node_delete cmp01
836 '''
837 vnc_client = _auth(**kwargs)
838 gsc_obj = _get_config(vnc_client)
839 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200840 if __opts__['test']:
841 return "ConfigNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100842 vnc_client.config_node_delete(
843 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200844 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100845
846
847def bgp_router_list(**kwargs):
848 '''
849 Return a list of all Contrail BGP routers
850
851 CLI Example:
852
853 .. code-block:: bash
854
855 salt '*' contrail.bgp_router_list
856 '''
857 ret = {}
858 vnc_client = _auth(**kwargs)
859 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
860 for bgp_router_obj in bgp_router_objs:
861 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
862 return ret
863
864
865def bgp_router_get(name, **kwargs):
866 '''
867 Return a specific Contrail BGP router
868
869 CLI Example:
870
871 .. code-block:: bash
872
873 salt '*' contrail.bgp_router_get nal01
874 '''
875 ret = {}
876 bgp_router_objs = bgp_router_list(**kwargs)
877 if name in bgp_router_objs:
878 ret[name] = bgp_router_objs.get(name)
879 if len(ret) == 0:
880 return {'Error': 'Error in retrieving BGP router.'}
881 return ret
882
883
884def bgp_router_create(name, type, ip_address, asn=64512, **kwargs):
885 '''
886 Create specific Contrail control node
887
888 CLI Example:
889
890 .. code-block:: bash
891
892 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
893 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
894 '''
895 ret = {}
896 vnc_client = _auth(**kwargs)
897
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200898 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
899 'inet6-vpn']
900 if type != 'control-node':
901 address_families.remove('erm-vpn')
902
903 bgp_addr_fams = AddressFamilies(address_families)
904 bgp_sess_attrs = [
905 BgpSessionAttributes(address_families=bgp_addr_fams)]
906 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
907 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
908 rt_inst_obj = _get_rt_inst_obj(vnc_client)
909
910 if type == 'control-node':
911 vendor = 'contrail'
912 elif type == 'router':
913 vendor = 'mx'
914 else:
915 vendor = 'unknown'
916
917 router_params = BgpRouterParams(router_type=type,
918 vendor=vendor, autonomous_system=int(asn),
919 identifier=_get_ip(ip_address),
920 address=_get_ip(ip_address),
921 port=179, address_families=bgp_addr_fams)
922
Ales Komarekad46d2e2017-03-09 17:16:38 +0100923 bgp_router_objs = bgp_router_list(**kwargs)
924 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200925 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
926 bgp_router_obj.set_bgp_router_parameters(router_params)
927 if __opts__['test']:
928 return "BGP router " + name + " will be updated"
929 vnc_client.bgp_router_update(bgp_router_obj)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100930 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200931 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
932 if __opts__['test']:
933 return "BGP router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100934 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200935 return "Created"
936 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100937
938
939def bgp_router_delete(name, **kwargs):
940 '''
941 Delete specific Contrail control node
942
943 CLI Example:
944
945 .. code-block:: bash
946
947 salt '*' contrail.bgp_router_delete mx01
948 '''
949 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200950 gsc_obj = _get_config(vnc_client)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100951 bgp_router_obj = BgpRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200952
953 if __opts__['test']:
954 return "BGP router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100955 vnc_client.bgp_router_delete(
956 fq_name=bgp_router_obj.get_fq_name())
957
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200958 return "Deleted"
959
Ales Komarekad46d2e2017-03-09 17:16:38 +0100960
961def database_node_list(**kwargs):
962 '''
963 Return a list of all Contrail database nodes
964
965 CLI Example:
966
967 .. code-block:: bash
968
969 salt '*' contrail.database_node_list
970 '''
971 ret = {}
972 vnc_client = _auth(**kwargs)
973 node_objs = vnc_client._objects_list('database-node', detail=True)
974 for node_obj in node_objs:
975 ret[node_obj.name] = node_obj.__dict__
976 return ret
977
978
979def database_node_get(name, **kwargs):
980 '''
981 Return a specific Contrail database node
982
983 CLI Example:
984
985 .. code-block:: bash
986
987 salt '*' contrail.database_node_get nal01
988 '''
989 ret = {}
990 vrouter_objs = database_node_list(**kwargs)
991 if name in vrouter_objs:
992 ret[name] = vrouter_objs.get(name)
993 if len(ret) == 0:
994 return {'Error': 'Error in retrieving database node.'}
995 return ret
996
997
998def database_node_create(name, ip_address, **kwargs):
999 '''
1000 Create specific Contrail database node
1001
1002 CLI Example:
1003
1004 .. code-block:: bash
1005
1006 salt '*' contrail.database_node_create ntw03 10.10.10.103
1007 '''
1008 ret = {}
1009 vnc_client = _auth(**kwargs)
1010 gsc_obj = _get_config(vnc_client)
1011 database_node_objs = database_node_list(**kwargs)
1012 if name in database_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001013 return {'OK': 'Database node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001014 else:
1015 database_node_obj = DatabaseNode(
1016 name, gsc_obj,
1017 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001018 if __opts__['test']:
1019 return "DatabaseNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +01001020 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001021 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +01001022
1023
1024def database_node_delete(name, **kwargs):
1025 '''
1026 Delete specific Contrail database node
1027
1028 CLI Example:
1029
1030 .. code-block:: bash
1031
1032 salt '*' contrail.database_node_delete cmp01
1033 '''
1034 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001035 gsc_obj = _get_config(vnc_client)
1036 database_node_obj = DatabaseNode(name, gsc_obj)
1037 if __opts__['test']:
1038 return "DatabaseNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +01001039 vnc_client.database_node_delete(
1040 fq_name=database_node_obj.get_fq_name())
Petr Jediný5f3efe32017-05-26 17:55:09 +02001041
1042
Petr Jediný5f3efe32017-05-26 17:55:09 +02001043def _get_vrouter_config(vnc_client):
1044 try:
1045 config = vnc_client.global_vrouter_config_read(
1046 fq_name=['default-global-system-config', 'default-global-vrouter-config'])
1047 except Exception:
1048 config = None
1049
1050 return config
1051
1052
Petr Jediný5f3efe32017-05-26 17:55:09 +02001053def linklocal_service_list(**kwargs):
1054 '''
1055 Return a list of all Contrail link local services
1056
1057 CLI Example:
1058
1059 .. code-block:: bash
1060
1061 salt '*' contrail.linklocal_service_list
1062 '''
1063 ret = {}
1064 vnc_client = _auth(**kwargs)
1065
1066 current_config = _get_vrouter_config(vnc_client)
1067 if current_config is None:
1068 return ret
1069
1070 service_list_res = current_config.get_linklocal_services()
1071 if service_list_res is None:
1072 service_list_obj = {'linklocal_service_entry': []}
1073 else:
1074 service_list_obj = service_list_res.__dict__
1075 for _, value in service_list_obj.iteritems():
1076 for entry in value:
1077 service = entry.__dict__
1078 if 'linklocal_service_name' in service:
1079 ret[service['linklocal_service_name']] = service
1080 return ret
1081
1082
1083def linklocal_service_get(name, **kwargs):
1084 '''
1085 Return a specific Contrail link local service
1086
1087 CLI Example:
1088
1089 .. code-block:: bash
1090
1091 salt '*' contrail.linklocal_service_get llservice
1092 '''
1093 ret = {}
1094 services = linklocal_service_list(**kwargs)
1095 if name in services:
1096 ret[name] = services.get(name)
1097 if len(ret) == 0:
1098 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1099 return ret
1100
1101
1102def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, **kwargs):
1103 '''
1104 Create specific Contrail link local service
1105
1106 CLI Example:
1107
1108 .. code-block:: bash
1109
1110 salt '*' contrail.linklocal_service_create \
1111 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1112 salt '*' contrail.linklocal_service_create \
1113 llservice 10.10.10.103 22 link-local.service.dns-name 22
1114 '''
1115 ret = {}
1116 vnc_client = _auth(**kwargs)
1117
1118 current_config = _get_vrouter_config(vnc_client)
1119
1120 service_entry = LinklocalServiceEntryType(
1121 linklocal_service_name=name,
1122 linklocal_service_ip=lls_ip,
1123 linklocal_service_port=lls_port,
1124 ip_fabric_service_port=ipf_port)
1125 if isinstance(ipf_dns_or_ip, basestring):
1126 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1127 elif isinstance(ipf_dns_or_ip, list):
1128 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1129 service_entry.ip_fabric_DNS_service_name = ''
1130
1131 if current_config is None:
1132 new_services = LinklocalServicesTypes([service_entry])
1133 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001134 if __opts__['test']:
1135 ret['GlobalVrouterConfig'] = "Global vRouter Config will be created"
1136 else:
1137 ret = "Created"
1138 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001139 else:
1140 _current_service_list = current_config.get_linklocal_services()
1141 if _current_service_list is None:
1142 service_list = {'linklocal_service_entry': []}
1143 else:
1144 service_list = _current_service_list.__dict__
1145 new_services = [service_entry]
1146 for key, value in service_list.iteritems():
1147 if key != 'linklocal_service_entry':
1148 continue
1149 for _entry in value:
1150 entry = _entry.__dict__
1151 if 'linklocal_service_name' in entry:
1152 if entry['linklocal_service_name'] == name:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001153 return {'OK': 'Link local service "{0}" already exists'.format(name)}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001154 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001155 if __opts__['test']:
1156 ret['Test'] = "LinkLocalSevices will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001157 service_list[key] = new_services
1158 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001159 if __opts__['test']:
1160 ret['GlobalVrouterConfig'] = "Global vRouter Config will be updated"
1161 else:
1162 vnc_client.global_vrouter_config_update(new_config)
1163 ret = "Created"
1164 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001165
1166
1167def linklocal_service_delete(name, **kwargs):
1168 '''
1169 Delete specific link local service entry
1170
1171 CLI Example:
1172
1173 .. code-block:: bash
1174
1175 salt '*' contrail.linklocal_service_delete llservice
1176 '''
1177 vnc_client = _auth(**kwargs)
1178
1179 current_config = _get_vrouter_config(vnc_client)
1180
1181 found = False
1182 if current_config is not None:
1183 _current_service_list = current_config.get_linklocal_services()
1184 if _current_service_list is None:
1185 service_list = {'linklocal_service_entry': []}
1186 else:
1187 service_list = _current_service_list.__dict__
1188 new_services = []
1189 for key, value in service_list.iteritems():
1190 if key != 'linklocal_service_entry':
1191 continue
1192 for _entry in value:
1193 entry = _entry.__dict__
1194 if 'linklocal_service_name' in entry:
1195 if entry['linklocal_service_name'] == name:
1196 found = True
1197 else:
1198 new_services.append(_entry)
1199 service_list[key] = new_services
1200 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001201 if __opts__['test']:
1202 return "Link local service " + name + " will be deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001203 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001204 return "Deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001205 if not found:
1206 return {'Error': 'Link local service "{0}" not found'.format(name)}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001207
1208
1209def virtual_machine_interface_list(**kwargs):
1210 '''
1211 Return a list of all Contrail virtual machine interfaces
1212
1213 CLI Example:
1214
1215 .. code-block:: bash
1216
1217 salt '*' contrail.virtual_machine_interfaces
1218 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001219 ret = []
1220 vnc_client = _auth(**kwargs)
1221 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1222 project_uuid = project.get_uuid()
1223
1224 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1225 detail=True, parent_id=project_uuid)
1226
1227 for vm_iface in vm_ifaces:
1228 ret.append(vm_iface.__dict__)
1229
1230 return ret
1231
1232
1233def virtual_machine_interface_create(name,
1234 virtual_network,
1235 mac_address=None,
1236 ip_address=None,
1237 security_group=None,
1238 **kwargs):
1239 '''
1240 Create specific Contrail virtual machine interface (Port)
1241
1242 CLI Example:
1243
1244 .. code-block:: bash
1245
1246 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1247 router_types:
1248 - tor-agent
1249 - tor-service-node
1250 - embedded
1251 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001252 ret = {}
1253 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001254 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1255
1256 vm_int = VirtualMachineInterface(name, parent_obj=project)
1257
1258 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001259 mac_address_obj = MacAddressesType([mac_address])
1260 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001261
1262 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001263 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
1264 vnc_client, security_group, kwargs.get('tenant', 'admin')))
1265 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001266
1267 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1268 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1269 vm_int.set_virtual_network(vnet_obj)
1270
1271 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1272 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1273
1274 vm_int.set_port_security_enabled(False)
1275 vnc_client.virtual_machine_interface_update(vm_int)
1276
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001277 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001278 ip = vnc_api.InstanceIp(name + '.ip')
1279 ip.set_virtual_machine_interface(vmi)
1280 ip.set_virtual_network(vnet_obj)
1281
1282 ip_uuid = vnc_client.instance_ip_create(ip)
1283
1284 if ip_address:
1285 ip.set_instance_ip_address(ip_address)
1286 vnc_client.instance_ip_update(ip)
1287
1288 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001289
1290
1291def virtual_network_list(**kwargs):
1292 '''
1293 Return a list of all Contrail virtual network
1294
1295 CLI Example:
1296
1297 .. code-block:: bash
1298
1299 salt '*' contrail.virtual_network
1300 '''
1301
1302 ret = {}
1303 vnc_client = _auth(**kwargs)
1304 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1305 for virtual_network in virtual_networks:
1306 ret[virtual_network.name] = virtual_network.__dict__
1307 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001308
1309
1310def virtual_network_get(name, **kwargs):
1311 '''
1312 Return a specific Contrail virtual network
1313
1314 CLI Example:
1315
1316 .. code-block:: bash
1317
1318 salt '*' contrail.virtual_network_get net01
1319 '''
1320 ret = {}
1321 vnet_objs = virtual_network_list(**kwargs)
1322 if name in vnet_objs:
1323 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001324 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001325 return {'result': False,
1326 'Error': 'Error in retrieving virtual networks.'}
1327 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001328
1329
1330def service_appliance_set_list(**kwargs):
1331 '''
1332 Return a list of Contrail service appliance set
1333
1334 CLI Example:
1335
1336 .. code-block:: bash
1337
1338 salt '*' contrail.service_appliance_set_list
1339 '''
1340 ret = {}
1341 vnc_client = _auth(**kwargs)
1342 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1343 for service_appliance_set in service_appliance_sets:
1344 ret[service_appliance_set.name] = service_appliance_set.__dict__
1345 return ret
1346
1347
1348def service_appliance_set_get(name, **kwargs):
1349 '''
1350 Return a specific Contrail service appliance set
1351
1352 CLI Example:
1353
1354 .. code-block:: bash
1355
1356 salt '*' contrail.service_appliance_set_get name
1357 '''
1358 ret = {}
1359 sas_objs = service_appliance_set_list(**kwargs)
1360 if name in sas_objs:
1361 ret[name] = sas_objs.get(name)
1362 if len(ret) != 1:
1363 return {'result': False,
1364 'Error': "Error in the retrieving service apliance set."}
1365 return ret
1366
1367
1368def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1369 '''
1370 Create Contrail service appliance set
1371
1372 CLI Example:
1373
1374 .. code-block:: bash
1375
1376 salt '*' contrail.service_appliance_set_create name
1377 '''
1378 ret = {'name': name,
1379 'changes': {},
1380 'result': True,
1381 'comment': ''}
1382 vnc_client = _auth(**kwargs)
1383 gsc_obj = _get_config(vnc_client)
1384 sas_objs = service_appliance_set_list(**kwargs)
1385 if name in sas_objs:
1386 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1387 else:
1388 service_appliance_set_obj = ServiceApplianceSet(
1389 name, gsc_obj)
1390 if properties:
1391 pairs = KeyValuePairs()
1392 for k, v in properties.items():
1393 pairs.add_key_value_pair(KeyValuePair(k, v))
1394 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1395 if driver:
1396 service_appliance_set_obj.set_service_appliance_driver(driver)
1397 if ha_mode:
1398 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1399 if __opts__['test']:
1400 ret['result'] = None
1401 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1402 else:
1403 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1404 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1405 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1406 return ret
1407
1408
1409def service_appliance_set_delete(name, **kwargs):
1410 '''
1411 Delete specific Contrail service appliance set
1412
1413 CLI Example:
1414
1415 .. code-block:: bash
1416
1417 salt '*' contrail.service_appliance_set_delete name
1418 '''
1419 ret = {'name': name,
1420 'changes': {},
1421 'result': True,
1422 'comment': ''}
1423 vnc_client = _auth(**kwargs)
1424 gsc_obj = _get_config(vnc_client)
1425 sas_obj = ServiceApplianceSet(name, gsc_obj)
1426 if __opts__['test']:
1427 ret['result'] = None
1428 ret['comment'] = "Service appliance set " + name + " will be deleted"
1429 else:
1430 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1431 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1432 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1433 return ret