blob: 2596b1a14503d25f38cd50711aa9902419c4bb0e [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 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200160 ret = {'name': name,
161 'changes': {},
162 'result': True,
163 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100164 vnc_client = _auth(**kwargs)
165 gsc_obj = _get_config(vnc_client)
166 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200167 router_types = ['tor-agent', 'tor-service-node', 'embedded']
168 if router_type not in router_types:
169 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100170 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200171 vrouter = virtual_router_get(name)
172 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200173 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200174 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200175 changes['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200176 vrouter_obj.set_virtual_router_ip_address(ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200177 if vrouter_obj.get_virtual_router_type() != router_type:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200178 changes['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200179 vrouter_obj.set_virtual_router_type(router_type)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200180 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200181 changes['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200182 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200183 if len(changes) != 0:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200184 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200185 ret['result'] = None
186 ret['comment'] = "Virtual router " + name + " will be updated"
187 else:
188 ret['comment'] = "VirtualRouter " + name + " has been updated"
189 ret['changes'] = changes
190 vnc_client.virtual_router_update(vrouter_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200191 return ret
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200192 ret['comment'] = 'Virtual router ' + name + ' already exists and is updated'
193 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100194 else:
195 vrouter_obj = VirtualRouter(
196 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200197 virtual_router_ip_address=ip_address,
198 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100199 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200200 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200201 ret['result'] = None
202 ret['comment'] = "VirtualRouter " + name + " will be created"
203 else:
204 vnc_client.virtual_router_create(vrouter_obj)
205 ret['comment'] = "VirtualRouter " + name + " has been created"
206 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
207 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100208
209
210def virtual_router_delete(name, **kwargs):
211 '''
212 Delete specific Contrail virtual router
213
214 CLI Example:
215
216 .. code-block:: bash
217
218 salt '*' contrail.virtual_router_delete cmp01
219 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200220 ret = {'name': name,
221 'changes': {},
222 'result': True,
223 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100224 vnc_client = _auth(**kwargs)
225 gsc_obj = _get_config(vnc_client)
226 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200227 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200228 ret['result'] = None
229 ret['comment'] = "VirtualRouter " + name + " will be deleted"
230 else:
231 vnc_client.virtual_router_delete(fq_name=vrouter_obj.get_fq_name())
232 ret['comment'] = "VirtualRouter " + name + " has been deleted"
233 ret['changes'] = {'VirtualRouter': {'old': name, 'new': ''}}
234 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200235
236
237def physical_router_list(**kwargs):
238 '''
239 Return a list of all Contrail physical routers
240
241 CLI Example:
242
243 .. code-block:: bash
244
245 salt '*' contrail.physical_router_list
246 '''
247 ret = {}
248 vnc_client = _auth(**kwargs)
249 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
250 for prouter_obj in prouter_objs:
251 ret[prouter_obj.name] = {
252 'uuid': prouter_obj._uuid,
253 'management_ip': prouter_obj._physical_router_management_ip,
254 'product_name': prouter_obj._physical_router_product_name,
255 }
256
257 return ret
258
259
260def physical_router_get(name, **kwargs):
261 '''
262 Return a specific Contrail physical router
263
264 CLI Example:
265
266 .. code-block:: bash
267
268 salt '*' contrail.physical_router_get router_name
269 '''
270 ret = {}
271 vnc_client = _auth(**kwargs)
272 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
273 for prouter_obj in prouter_objs:
274 if name == prouter_obj.name:
275 ret[name] = prouter_obj.__dict__
276 if len(ret) == 0:
277 return {'Error': 'Error in retrieving physical router.'}
278 return ret
279
280
281def physical_router_create(name, parent_type=None,
282 management_ip=None,
283 dataplane_ip=None, # VTEP address in web GUI
284 vendor_name=None,
285 product_name=None,
286 vnc_managed=None,
287 junos_service_ports=None,
288 agents=None, **kwargs):
289 '''
290 Create specific Contrail physical router
291
292 CLI Example:
293
294 .. code-block:: bash
295
296 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']"
297 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200298 ret = {'name': name,
299 'changes': {},
300 'result': True,
301 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200302 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200303 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200304 prouter_objs = physical_router_list(**kwargs)
305 if name in prouter_objs:
306 prouter = physical_router_get(name)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200307 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200308 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
309 if prouter_obj.physical_router_management_ip != management_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200310 changes['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200311 prouter_obj.set_physical_router_management_ip(management_ip)
312 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200313 changes['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200314 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
315 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200316 changes['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200317 prouter_obj.set_physical_router_vendor_name(vendor_name)
318 if prouter_obj.get_physical_router_product_name() != product_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200319 changes['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200320 prouter_obj.set_physical_router_product_name(product_name)
321 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200322 changes['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200323 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
324 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200325 changes['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
326 'to': junos_service_ports}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200327 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
328
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200329 if len(changes) != 0:
330 if __opts__['test']:
331 ret['result'] = None
332 ret['comment'] = "Physical router " + name + " will be updated"
333 else:
334 ret['comment'] = 'Physical router ' + name + ' already exists and is updated'
335 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200336
337 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
338 c_agents = [] # referenced vrouters
339 for c_agent in prouter_obj.get_virtual_router_refs():
340 c_agents.append(c_agent['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200341 # agent_objs = [] # required state of references
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200342 for vrouter_obj in vrouter_objs:
343 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
344 prouter_obj.add_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200345 changes['vrouter ' + vrouter_obj._display_name] = "Reference added"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200346 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
347 prouter_obj.del_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200348 changes['vrouter ' + vrouter_obj._display_name] = "Reference removed"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200349 vnc_client.physical_router_update(prouter_obj)
350
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200351 if __opts__['test']:
352 ret['result'] = None
353 ret['comment'] = "VirtualRouter " + name + " will be created"
354 else:
355 vnc_client.virtual_router_create(vrouter_obj)
356 ret['comment'] = "VirtualRouter " + name + " has been created"
357 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
358
359 if len(changes) == 0:
360 ret['comment'] = "Physical router exists and is updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200361 return ret
362 else:
363 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200364 ret['result'] = None
365 ret['comment'] = "Physical router " + name + " will be created"
366 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200367 prouter_obj = PhysicalRouter(
368 name=name,
369 parent_obj=None,
370 physical_router_management_ip=management_ip,
371 physical_router_dataplane_ip=dataplane_ip,
372 physical_router_vendor_name=vendor_name,
373 physical_router_product_name=product_name,
374 physical_router_vnc_managed=vnc_managed,
375 physical_router_junos_service_ports=junos_service_ports,
376 )
377 for agent in agents:
378 vrouter = virtual_router_get(agent)
379 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
380 prouter_obj.add_virtual_router(vrouter_obj)
381 vnc_client.physical_router_create(prouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200382 ret['comment'] = "Physical router " + name + " has been created"
383 ret['changes'] = {'PhysicalRouter': {'old': '', 'new': name}}
384 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200385
386
387def physical_router_delete(name, **kwargs):
388 '''
389 Delete specific Contrail physical router
390
391 CLI Example:
392
393 .. code-block:: bash
394
395 salt '*' contrail.physical_router_delete router_name
396 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200397 ret = {'name': name,
398 'changes': {},
399 'result': True,
400 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200401 vnc_client = _auth(**kwargs)
402 gsc_obj = _get_config(vnc_client)
403 prouter_obj = PhysicalRouter(name, gsc_obj)
404 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200405 ret['result'] = None
406 ret['comment'] = "Physical router " + name + " will be deleted"
407 else:
408 vnc_client.physical_router_delete(fq_name=prouter_obj.get_fq_name())
409 ret['comment'] = "Physical router " + name + " has been deleted"
410 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
411 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200412
413
414def physical_interface_list(**kwargs):
415 '''
416 Return a list of all Contrail physical interface
417
418 CLI Example:
419
420 .. code-block:: bash
421
422 salt '*' contrail.physical_interface_list
423 '''
424 ret = {}
425 vnc_client = _auth(**kwargs)
426 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
427 for pinterface_obj in pinterface_objs:
428 ret[pinterface_obj.name] = {
429 'uuid': pinterface_obj._uuid,
430 'fq_name': pinterface_obj.fq_name,
431 'parent_type': pinterface_obj.parent_type,
432 }
433
434 return ret
435
436
437def physical_interface_get(name, physical_router, **kwargs):
438 '''
439 Return a specific Contrail physical interface
440
441 CLI Example:
442
443 .. code-block:: bash
444
445 salt '*' contrail.physical_interface_get interface_name physical_router_name
446 '''
447 ret = {}
448 vnc_client = _auth(**kwargs)
449 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
450 for pinterf_obj in pinterf_objs:
451 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
452 ret[name] = pinterf_obj.__dict__
453 if len(ret) == 0:
454 return {'Error': 'Error in retrieving physical interface.'}
455 return ret
456
457
458def physical_interface_create(name, physical_router, **kwargs):
459 '''
460 Create specific Contrail physical interface
461
462 CLI Example:
463
464 .. code-block:: bash
465
466 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
467 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200468 ret = {'name': name,
469 'changes': {},
470 'result': True,
471 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200472 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200473 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200474 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
475 if 'Error' not in pinterf_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200476 ret['comment'] = 'Physical interface ' + name + ' on ' + physical_router + ' already exists'
477 return ret
478
479 if __opts__['test']:
480 ret['result'] = None
481 ret['comment'] = "Physical interface " + name + " will be created"
482 return ret
483
484 prouter = physical_router_get(physical_router)
485 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
486 pinterf_obj = PhysicalInterface(name, prouter_obj)
487 vnc_client.physical_interface_create(pinterf_obj)
488 ret['comment'] = "Physical interface " + name + " has been created"
489 ret['changes'] = {'Physical interface': {'old': '', 'new': name}}
490 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200491
492
493def physical_interface_delete(name, physical_router, **kwargs):
494 '''
495 Delete specific Contrail physical interface
496
497 CLI Example:
498 .. code-block:: bash
499
500 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
501 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200502 ret = {'name': name,
503 'changes': {},
504 'result': True,
505 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200506 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200507 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200508 piface = physical_interface_get(name, physical_router)
509 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200510 ret['result'] = None
511 ret['comment'] = "Physical interface " + name + " will be deleted"
512 else:
513 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
514 ret['comment'] = "Physical router " + name + " has been deleted"
515 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
516 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200517
518
519def logical_interface_list(**kwargs):
520 '''
521 Return a list of all Contrail logical interfaces
522
523 CLI Example:
524
525 .. code-block:: bash
526
527 salt '*' contrail.logical_interface_list
528 '''
529 ret = []
530 vnc_client = _auth(**kwargs)
531 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
532 for liface_obj in liface_objs:
533 ret.append({
534 'name': liface_obj.name,
535 'uuid': liface_obj._uuid,
536 'fq_name': liface_obj.fq_name,
537 'parent_type': liface_obj.parent_type,
538 })
539 return ret
540
541
542def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
543 '''
544 Return a specific Contrail logical interface
545
546 CLI Example:
547
548 .. code-block:: bash
549
550 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
551 or
552 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
553 or
554 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
555 '''
556 ret = {}
557 vnc_client = _auth(**kwargs)
558 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
559 count = 0
560 for liface_obj in liface_objs:
561 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
562 if parent_type and parent_type == liface_obj.parent_type:
563 count += 1
564 ret[liface_obj.name] = liface_obj.__dict__
565 if not parent_type:
566 count += 1
567 ret[liface_obj.name] = liface_obj.__dict__
568 if len(ret) == 0:
569 return {'Error': 'Error in retrieving logical interface.'}
570 if count > 1:
571 return {
572 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
573 return ret
574
575
576def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300577 vmis=None, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200578 '''
579 Create specific Contrail logical interface
580
581 CLI Example:
582
583 .. code-block:: bash
584
585 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
586 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200587 ret = {'name': name,
588 'changes': {},
589 'result': True,
590 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200591 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200592 # gsc_obj = _get_config(vnc_client)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300593
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200594 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
595 if 'Error' not in liface_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200596 ret['comment'] = 'Logical interface ' + name + ' already exists'
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200597 else:
598 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200599 ret['result'] = None
600 ret['comment'] = "Logical interface " + name + " will be created"
601 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200602 parent_obj = None
603 for router in parent_names:
604 parent_router = physical_router_get(router)
605 if 'Error' not in parent_router:
606 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
607 break
608 if not parent_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200609 ret['result'] = False
610 ret['comment'] = 'Physical router have to be defined'
611 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200612 if parent_type == 'physical-interface':
613 for interface in parent_names:
614 parent_interface = physical_interface_get(interface, parent_obj.name)
615 if 'Error' not in parent_interface:
616 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
617 break
618 if interface_type.lower() == "l3":
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200619 ret['result'] = False
620 ret['comment'] = "Virtual Network have to be defined for L3 interface type"
621 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200622
623 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300624
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200625 if vmis:
626 for vmi_name, vmi in vmis.iteritems():
627 vmi = vnc_client.virtual_machine_interface_read(
628 fq_name=_get_fq_name(vnc_client, resource_name=vmi_name,
629 project_name=kwargs.get('tenant', 'admin')))
630 liface_obj.add_virtual_machine_interface(vmi)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200631 vnc_client.logical_interface_create(liface_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300632
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200633 ret['comment'] = "Logical interface " + name + " has been created"
634 ret['changes'] = {'Logical interface': {'old': '', 'new': name}}
635 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200636
637
638def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
639 '''
640 Delete specific Contrail logical interface
641
642 CLI Example:
643
644 .. code-block:: bash
645
646 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
647 or
648 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
649
650 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200651 ret = {'name': name,
652 'changes': {},
653 'result': True,
654 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200655 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200656 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200657 liface = logical_interface_get(name, parent_names, parent_type)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200658
659 if __opts__['test']:
660 ret['result'] = None
661 ret['comment'] = "Logical interface " + name + " will be deleted"
662 return ret
663 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
664 ret['comment'] = "Logical interface " + name + " has been deleted"
665 ret['changes'] = {'LogicalInterface ': {'old': name, 'new': ''}}
666 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200667
668
669def global_vrouter_config_list(**kwargs):
670 '''
671 Return a list of all Contrail global vrouter configs
672
673 CLI Example:
674
675 .. code-block:: bash"
676
677 salt '*' global_vrouter_config_list
678 '''
679 ret = {}
680 vnc_client = _auth(**kwargs)
681 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
682 for vrouter_conf_obj in vrouter_conf_objs:
683 ret[vrouter_conf_obj._display_name] = vrouter_conf_obj.__dict__
684 return ret
685
686
687def global_vrouter_config_get(name, **kwargs):
688 '''
689 Return a specific Contrail global vrouter config
690
691 CLI Example:
692
693 .. code-block:: bash
694
695 salt '*' contrail.global_vrouter_get global-vrouter-config
696 '''
697 ret = {}
698 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
699 if name in vrouter_conf_objs:
700 ret[name] = vrouter_conf_objs.get(name)
701 if len(ret) == 0:
702 return {'Error': 'Error in retrieving global vrouter config.'}
703 return ret
704
705
706def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, *fq_names, **kwargs):
707 '''
708 Create specific Contrail global vrouter config
709
710 CLI Example:
711
712 .. code-block:: bash
713
714 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']"
715 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200716 ret = {'name': name,
717 'changes': {},
718 'result': True,
719 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200720 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200721 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200722 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
723 if name in vrouter_conf_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200724 ret['comment'] = 'Global vrouter config ' + name + ' already exists'
725 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200726 else:
727 vrouter_conf_obj = GlobalVrouterConfig(
728 name=name,
729 parent_obj=None,
730 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
731 fq_name=fq_names,
732 vxlan_network_identifier_mode=vxlan_vn_id_mode,
733 parent_type=parent_type,
734 )
735 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200736 ret['result'] = None
737 ret['comment'] = "Global vRouter config " + name + " will be created"
738 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200739 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200740 ret['comment'] = "Global vRouter config " + name + " has been created"
741 ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}}
742 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200743
744
745def global_vrouter_config_delete(name, **kwargs):
746 '''
747 Delete specific Contrail global vrouter config
748
749 CLI Example:
750
751 .. code-block:: bash
752
753 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
754 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200755 ret = {'name': name,
756 'changes': {},
757 'result': True,
758 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200759 vnc_client = _auth(**kwargs)
760 gsc_obj = _get_config(vnc_client)
761 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
762 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200763 ret['result'] = None
764 ret['comment'] = "Global vRouter config " + name + " will be deleted"
765 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200766 vnc_client.global_vrouter_config_delete(
767 fq_name=vrouter_conf_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200768 ret['comment'] = "Global vRouter config " + name + " has been deleted"
769 ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}}
770 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100771
772
773def analytics_node_list(**kwargs):
774 '''
775 Return a list of all Contrail analytics nodes
776
777 CLI Example:
778
779 .. code-block:: bash
780
781 salt '*' contrail.analytics_node_list
782 '''
783 ret = {}
784 vnc_client = _auth(**kwargs)
785 node_objs = vnc_client._objects_list('analytics-node', detail=True)
786 for node_obj in node_objs:
787 ret[node_obj.name] = node_obj.__dict__
788 return ret
789
790
791def analytics_node_get(name, **kwargs):
792 '''
793 Return a specific Contrail analytics node
794
795 CLI Example:
796
797 .. code-block:: bash
798
799 salt '*' contrail.analytics_node_get nal01
800 '''
801 ret = {}
802 vrouter_objs = analytics_node_list(**kwargs)
803 if name in vrouter_objs:
804 ret[name] = vrouter_objs.get(name)
805 if len(ret) == 0:
806 return {'Error': 'Error in retrieving analytics node.'}
807 return ret
808
809
810def analytics_node_create(name, ip_address, **kwargs):
811 '''
812 Create specific Contrail analytics node
813
814 CLI Example:
815
816 .. code-block:: bash
817
818 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
819 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200820
821 ret = {'name': name,
822 'changes': {},
823 'result': True,
824 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100825 vnc_client = _auth(**kwargs)
826 gsc_obj = _get_config(vnc_client)
827 analytics_node_objs = analytics_node_list(**kwargs)
828 if name in analytics_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200829 ret['comment'] = 'Analytics node %s already exists'
830 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100831 else:
832 analytics_node_obj = AnalyticsNode(
833 name, gsc_obj,
834 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200835 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200836 ret['result'] = None
837 ret['comment'] = "AnalyticsNode " + name + " will be created"
838 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100839 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200840 ret['comment'] = "AnalyticsNode " + name + " has been created"
841 ret['changes'] = {'Analytics Node': {'old': '', 'new': name}}
842 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100843
844
845def analytics_node_delete(name, **kwargs):
846 '''
847 Delete specific Contrail analytics node
848
849 CLI Example:
850
851 .. code-block:: bash
852
853 salt '*' contrail.analytics_node_delete cmp01
854 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200855 ret = {'name': name,
856 'changes': {},
857 'result': True,
858 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100859 vnc_client = _auth(**kwargs)
860 gsc_obj = _get_config(vnc_client)
861 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200862 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200863 ret['result'] = None
864 ret['comment'] = "AnalyticsNode " + name + " will be deleted"
865 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100866 vnc_client.analytics_node_delete(
867 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200868 ret['comment'] = "AnalyticsNode " + name + " has been deleted"
869 ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}}
870 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100871
872
873def config_node_list(**kwargs):
874 '''
875 Return a list of all Contrail config nodes
876
877 CLI Example:
878
879 .. code-block:: bash
880
881 salt '*' contrail.config_node_list
882 '''
883 ret = {}
884 vnc_client = _auth(**kwargs)
885 node_objs = vnc_client._objects_list('config-node', detail=True)
886 for node_obj in node_objs:
887 ret[node_obj.name] = node_obj.__dict__
888 return ret
889
890
891def config_node_get(name, **kwargs):
892 '''
893 Return a specific Contrail config node
894
895 CLI Example:
896
897 .. code-block:: bash
898
899 salt '*' contrail.config_node_get nal01
900 '''
901 ret = {}
902 vrouter_objs = config_node_list(**kwargs)
903 if name in vrouter_objs:
904 ret[name] = vrouter_objs.get(name)
905 if len(ret) == 0:
906 return {'Error': 'Error in retrieving config node.'}
907 return ret
908
909
910def config_node_create(name, ip_address, **kwargs):
911 '''
912 Create specific Contrail config node
913
914 CLI Example:
915
916 .. code-block:: bash
917
918 salt '*' contrail.config_node_create ntw03 10.10.10.103
919 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200920 ret = {'name': name,
921 'changes': {},
922 'result': True,
923 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100924 vnc_client = _auth(**kwargs)
925 gsc_obj = _get_config(vnc_client)
926 config_node_objs = config_node_list(**kwargs)
927 if name in config_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200928 ret['comment'] = 'Config node ' + name + ' already exists'
929 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100930 else:
931 config_node_obj = ConfigNode(
932 name, gsc_obj,
933 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200934 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200935 ret['comment'] = "ConfigNode " + name + " will be created"
936 ret['result'] = None
937 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100938 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200939 ret['comment'] = "ConfigNode " + name + " has been created"
940 ret['changes'] = {'ConfigNode': {'old': '', 'new': name}}
941 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100942
943
944def config_node_delete(name, **kwargs):
945 '''
946 Delete specific Contrail config node
947
948 CLI Example:
949
950 .. code-block:: bash
951
952 salt '*' contrail.config_node_delete cmp01
953 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200954 ret = {'name': name,
955 'changes': {},
956 'result': True,
957 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100958 vnc_client = _auth(**kwargs)
959 gsc_obj = _get_config(vnc_client)
960 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200961 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200962 ret['comment'] = "ConfigNode " + name + " will be deleted"
963 ret['result'] = None
964 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100965 vnc_client.config_node_delete(
966 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200967 ret['comment'] = "ConfigNode " + name + " has been deleted"
968 ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}}
969 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100970
971
972def bgp_router_list(**kwargs):
973 '''
974 Return a list of all Contrail BGP routers
975
976 CLI Example:
977
978 .. code-block:: bash
979
980 salt '*' contrail.bgp_router_list
981 '''
982 ret = {}
983 vnc_client = _auth(**kwargs)
984 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
985 for bgp_router_obj in bgp_router_objs:
986 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
987 return ret
988
989
990def bgp_router_get(name, **kwargs):
991 '''
992 Return a specific Contrail BGP router
993
994 CLI Example:
995
996 .. code-block:: bash
997
998 salt '*' contrail.bgp_router_get nal01
999 '''
1000 ret = {}
1001 bgp_router_objs = bgp_router_list(**kwargs)
1002 if name in bgp_router_objs:
1003 ret[name] = bgp_router_objs.get(name)
1004 if len(ret) == 0:
1005 return {'Error': 'Error in retrieving BGP router.'}
1006 return ret
1007
1008
1009def bgp_router_create(name, type, ip_address, asn=64512, **kwargs):
1010 '''
1011 Create specific Contrail control node
1012
1013 CLI Example:
1014
1015 .. code-block:: bash
1016
1017 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
1018 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
1019 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001020 ret = {'name': name,
1021 'changes': {},
1022 'result': True,
1023 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001024 vnc_client = _auth(**kwargs)
1025
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001026 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
1027 'inet6-vpn']
1028 if type != 'control-node':
1029 address_families.remove('erm-vpn')
1030
1031 bgp_addr_fams = AddressFamilies(address_families)
1032 bgp_sess_attrs = [
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001033 BgpSessionAttributes(address_families=bgp_addr_fams)]
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001034 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
1035 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
1036 rt_inst_obj = _get_rt_inst_obj(vnc_client)
1037
1038 if type == 'control-node':
1039 vendor = 'contrail'
1040 elif type == 'router':
1041 vendor = 'mx'
1042 else:
1043 vendor = 'unknown'
1044
1045 router_params = BgpRouterParams(router_type=type,
1046 vendor=vendor, autonomous_system=int(asn),
1047 identifier=_get_ip(ip_address),
1048 address=_get_ip(ip_address),
1049 port=179, address_families=bgp_addr_fams)
1050
Ales Komarekad46d2e2017-03-09 17:16:38 +01001051 bgp_router_objs = bgp_router_list(**kwargs)
1052 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001053 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001054
1055 if bgp_router_obj.bgp_router_parameters.autonomous_system != asn:
1056 ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}})
1057 if bgp_router_obj.bgp_router_parameters.vendor != vendor:
1058 ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}})
1059 if bgp_router_obj.bgp_router_parameters.address != ip_address:
1060 ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}})
1061
1062 if len(ret['changes']) == 0:
1063 return ret
1064
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001065 bgp_router_obj.set_bgp_router_parameters(router_params)
1066 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001067 ret['result'] = None
1068 ret['comment'] = "BGP router " + name + " will be updated"
1069 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001070 vnc_client.bgp_router_update(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001071 ret['comment'] = "BGP router " + name + " has been updated"
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001072 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001073 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001074 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
1075 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001076 ret['result'] = None
1077 ret['comment'] = "BGP router " + name + " will be created"
1078 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001079 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001080 ret['comment'] = "BGP router " + name + " has been created"
1081 ret['changes'] = {'BGP router': {'old': name, 'new': ''}}
1082 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001083
1084
1085def bgp_router_delete(name, **kwargs):
1086 '''
1087 Delete specific Contrail control node
1088
1089 CLI Example:
1090
1091 .. code-block:: bash
1092
1093 salt '*' contrail.bgp_router_delete mx01
1094 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001095 ret = {'name': name,
1096 'changes': {},
1097 'result': True,
1098 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001099 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001100
1101 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001102 ret['result'] = None
1103 ret['comment'] = "BGP router " + name + " will be deleted"
1104 return ret
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001105
1106 bgp_router = bgp_router_get(name)
1107 if name in bgp_router:
1108 vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name'])
1109 ret['comment'] = "BGP router " + name + " has been deleted"
1110 ret['changes'] = {'BGP router': {'old': '', 'new': name}}
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001111 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001112
Ales Komarekad46d2e2017-03-09 17:16:38 +01001113
1114def database_node_list(**kwargs):
1115 '''
1116 Return a list of all Contrail database nodes
1117
1118 CLI Example:
1119
1120 .. code-block:: bash
1121
1122 salt '*' contrail.database_node_list
1123 '''
1124 ret = {}
1125 vnc_client = _auth(**kwargs)
1126 node_objs = vnc_client._objects_list('database-node', detail=True)
1127 for node_obj in node_objs:
1128 ret[node_obj.name] = node_obj.__dict__
1129 return ret
1130
1131
1132def database_node_get(name, **kwargs):
1133 '''
1134 Return a specific Contrail database node
1135
1136 CLI Example:
1137
1138 .. code-block:: bash
1139
1140 salt '*' contrail.database_node_get nal01
1141 '''
1142 ret = {}
1143 vrouter_objs = database_node_list(**kwargs)
1144 if name in vrouter_objs:
1145 ret[name] = vrouter_objs.get(name)
1146 if len(ret) == 0:
1147 return {'Error': 'Error in retrieving database node.'}
1148 return ret
1149
1150
1151def database_node_create(name, ip_address, **kwargs):
1152 '''
1153 Create specific Contrail database node
1154
1155 CLI Example:
1156
1157 .. code-block:: bash
1158
1159 salt '*' contrail.database_node_create ntw03 10.10.10.103
1160 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001161 ret = {'name': name,
1162 'changes': {},
1163 'result': True,
1164 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001165 vnc_client = _auth(**kwargs)
1166 gsc_obj = _get_config(vnc_client)
1167 database_node_objs = database_node_list(**kwargs)
1168 if name in database_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001169 ret['comment'] = 'Database node ' + name + ' already exists'
1170 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001171 else:
1172 database_node_obj = DatabaseNode(
1173 name, gsc_obj,
1174 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001175 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001176 ret['result'] = None
1177 ret['comment'] = "DatabaseNode " + name + " will be created"
1178 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001179 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001180 ret['comment'] = "DatabaseNode " + name + " has been created"
1181 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1182 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001183
1184
1185def database_node_delete(name, **kwargs):
1186 '''
1187 Delete specific Contrail database node
1188
1189 CLI Example:
1190
1191 .. code-block:: bash
1192
1193 salt '*' contrail.database_node_delete cmp01
1194 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001195 ret = {'name': name,
1196 'changes': {},
1197 'result': True,
1198 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001199 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001200 gsc_obj = _get_config(vnc_client)
1201 database_node_obj = DatabaseNode(name, gsc_obj)
1202 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001203 ret['result'] = None
1204 ret['comment'] = "DatabaseNode " + name + " will be deleted"
1205 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001206 vnc_client.database_node_delete(
1207 fq_name=database_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001208 ret['comment'] = "DatabaseNode " + name + " has been deleted"
1209 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1210 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001211
1212
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001213def _get_vrouter_config(vnc_client, gvc_name=None):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001214 try:
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001215 if not gvc_name:
1216 gvc_list = global_vrouter_config_list()
1217 gvc_name = gvc_list.values()[0]['name']
1218
Petr Jediný5f3efe32017-05-26 17:55:09 +02001219 config = vnc_client.global_vrouter_config_read(
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001220 fq_name=['default-global-system-config', gvc_name])
Petr Jediný5f3efe32017-05-26 17:55:09 +02001221 except Exception:
1222 config = None
1223
1224 return config
1225
1226
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001227def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001228 '''
1229 Return a list of all Contrail link local services
1230
1231 CLI Example:
1232
1233 .. code-block:: bash
1234
1235 salt '*' contrail.linklocal_service_list
1236 '''
1237 ret = {}
1238 vnc_client = _auth(**kwargs)
1239
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001240 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001241 if current_config is None:
1242 return ret
1243
1244 service_list_res = current_config.get_linklocal_services()
1245 if service_list_res is None:
1246 service_list_obj = {'linklocal_service_entry': []}
1247 else:
1248 service_list_obj = service_list_res.__dict__
1249 for _, value in service_list_obj.iteritems():
1250 for entry in value:
1251 service = entry.__dict__
1252 if 'linklocal_service_name' in service:
1253 ret[service['linklocal_service_name']] = service
1254 return ret
1255
1256
1257def linklocal_service_get(name, **kwargs):
1258 '''
1259 Return a specific Contrail link local service
1260
1261 CLI Example:
1262
1263 .. code-block:: bash
1264
1265 salt '*' contrail.linklocal_service_get llservice
1266 '''
1267 ret = {}
1268 services = linklocal_service_list(**kwargs)
1269 if name in services:
1270 ret[name] = services.get(name)
1271 if len(ret) == 0:
1272 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1273 return ret
1274
1275
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001276def 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 +02001277 '''
1278 Create specific Contrail link local service
1279
1280 CLI Example:
1281
1282 .. code-block:: bash
1283
1284 salt '*' contrail.linklocal_service_create \
1285 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1286 salt '*' contrail.linklocal_service_create \
1287 llservice 10.10.10.103 22 link-local.service.dns-name 22
1288 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001289 ret = {'name': name,
1290 'changes': {},
1291 'result': True,
1292 'comment': ''}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001293 vnc_client = _auth(**kwargs)
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001294 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001295 service_entry = LinklocalServiceEntryType(
1296 linklocal_service_name=name,
1297 linklocal_service_ip=lls_ip,
1298 linklocal_service_port=lls_port,
1299 ip_fabric_service_port=ipf_port)
1300 if isinstance(ipf_dns_or_ip, basestring):
1301 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1302 elif isinstance(ipf_dns_or_ip, list):
1303 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1304 service_entry.ip_fabric_DNS_service_name = ''
1305
1306 if current_config is None:
1307 new_services = LinklocalServicesTypes([service_entry])
1308 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001309 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001310 ret['result'] = None
1311 ret['comment'] = "Link local service " + name + " will be created"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001312 else:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001313 ret['comment'] = "Link local service " + name + " has been created"
1314 ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001315 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001316 else:
1317 _current_service_list = current_config.get_linklocal_services()
1318 if _current_service_list is None:
1319 service_list = {'linklocal_service_entry': []}
1320 else:
1321 service_list = _current_service_list.__dict__
1322 new_services = [service_entry]
1323 for key, value in service_list.iteritems():
1324 if key != 'linklocal_service_entry':
1325 continue
1326 for _entry in value:
1327 entry = _entry.__dict__
1328 if 'linklocal_service_name' in entry:
1329 if entry['linklocal_service_name'] == name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001330 ret['comment'] = 'Link local service ' + name + ' already exists'
1331 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001332 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001333 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001334 ret['result'] = None
1335 ret['comment'] = "LinkLocalSevices " + name + " will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001336 service_list[key] = new_services
1337 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001338 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001339 ret['result'] = None
1340 ret['comment'] = "LinkLocalSevices " + name + " will be updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001341 else:
1342 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001343 ret['comment'] = "LinkLocalSevices " + name + " has been created"
1344 ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001345 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001346
1347
1348def linklocal_service_delete(name, **kwargs):
1349 '''
1350 Delete specific link local service entry
1351
1352 CLI Example:
1353
1354 .. code-block:: bash
1355
1356 salt '*' contrail.linklocal_service_delete llservice
1357 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001358 ret = {'name': name,
1359 'changes': {},
1360 'result': True,
1361 'comment': ''}
1362 lls = linklocal_service_get(name)
1363 print (lls)
1364 if name in lls:
1365 if __opts__['test']:
1366 print " ------------ Test only ------------"
1367 ret['result'] = None
1368 ret['comment'] = "Link local service " + name + " will be deleted"
1369 return ret
1370 else:
1371 return ret
1372
Petr Jediný5f3efe32017-05-26 17:55:09 +02001373 vnc_client = _auth(**kwargs)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001374 current_config = _get_vrouter_config(vnc_client)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001375 if current_config is not None:
1376 _current_service_list = current_config.get_linklocal_services()
1377 if _current_service_list is None:
1378 service_list = {'linklocal_service_entry': []}
1379 else:
1380 service_list = _current_service_list.__dict__
1381 new_services = []
1382 for key, value in service_list.iteritems():
1383 if key != 'linklocal_service_entry':
1384 continue
1385 for _entry in value:
1386 entry = _entry.__dict__
1387 if 'linklocal_service_name' in entry:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001388 if entry['linklocal_service_name'] != name:
Petr Jediný5f3efe32017-05-26 17:55:09 +02001389 new_services.append(_entry)
1390 service_list[key] = new_services
1391 new_config = GlobalVrouterConfig(linklocal_services=service_list)
1392 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001393 ret['comment'] = "Link local service " + name + " will be deleted"
1394 ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
1395 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001396
1397
1398def virtual_machine_interface_list(**kwargs):
1399 '''
1400 Return a list of all Contrail virtual machine interfaces
1401
1402 CLI Example:
1403
1404 .. code-block:: bash
1405
1406 salt '*' contrail.virtual_machine_interfaces
1407 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001408 ret = []
1409 vnc_client = _auth(**kwargs)
1410 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1411 project_uuid = project.get_uuid()
1412
1413 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1414 detail=True, parent_id=project_uuid)
1415
1416 for vm_iface in vm_ifaces:
1417 ret.append(vm_iface.__dict__)
1418
1419 return ret
1420
1421
1422def virtual_machine_interface_create(name,
1423 virtual_network,
1424 mac_address=None,
1425 ip_address=None,
1426 security_group=None,
1427 **kwargs):
1428 '''
1429 Create specific Contrail virtual machine interface (Port)
1430
1431 CLI Example:
1432
1433 .. code-block:: bash
1434
1435 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1436 router_types:
1437 - tor-agent
1438 - tor-service-node
1439 - embedded
1440 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001441 ret = {}
1442 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001443 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1444
1445 vm_int = VirtualMachineInterface(name, parent_obj=project)
1446
1447 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001448 mac_address_obj = MacAddressesType([mac_address])
1449 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001450
1451 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001452 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001453 vnc_client, security_group, kwargs.get('tenant', 'admin')))
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001454 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001455
1456 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1457 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1458 vm_int.set_virtual_network(vnet_obj)
1459
1460 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1461 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1462
1463 vm_int.set_port_security_enabled(False)
1464 vnc_client.virtual_machine_interface_update(vm_int)
1465
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001466 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001467 ip = vnc_api.InstanceIp(name + '.ip')
1468 ip.set_virtual_machine_interface(vmi)
1469 ip.set_virtual_network(vnet_obj)
1470
1471 ip_uuid = vnc_client.instance_ip_create(ip)
1472
1473 if ip_address:
1474 ip.set_instance_ip_address(ip_address)
1475 vnc_client.instance_ip_update(ip)
1476
1477 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001478
1479
1480def virtual_network_list(**kwargs):
1481 '''
1482 Return a list of all Contrail virtual network
1483
1484 CLI Example:
1485
1486 .. code-block:: bash
1487
1488 salt '*' contrail.virtual_network
1489 '''
1490
1491 ret = {}
1492 vnc_client = _auth(**kwargs)
1493 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1494 for virtual_network in virtual_networks:
1495 ret[virtual_network.name] = virtual_network.__dict__
1496 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001497
1498
1499def virtual_network_get(name, **kwargs):
1500 '''
1501 Return a specific Contrail virtual network
1502
1503 CLI Example:
1504
1505 .. code-block:: bash
1506
1507 salt '*' contrail.virtual_network_get net01
1508 '''
1509 ret = {}
1510 vnet_objs = virtual_network_list(**kwargs)
1511 if name in vnet_objs:
1512 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001513 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001514 return {'result': False,
1515 'Error': 'Error in retrieving virtual networks.'}
1516 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001517
1518
1519def service_appliance_set_list(**kwargs):
1520 '''
1521 Return a list of Contrail service appliance set
1522
1523 CLI Example:
1524
1525 .. code-block:: bash
1526
1527 salt '*' contrail.service_appliance_set_list
1528 '''
1529 ret = {}
1530 vnc_client = _auth(**kwargs)
1531 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1532 for service_appliance_set in service_appliance_sets:
1533 ret[service_appliance_set.name] = service_appliance_set.__dict__
1534 return ret
1535
1536
1537def service_appliance_set_get(name, **kwargs):
1538 '''
1539 Return a specific Contrail service appliance set
1540
1541 CLI Example:
1542
1543 .. code-block:: bash
1544
1545 salt '*' contrail.service_appliance_set_get name
1546 '''
1547 ret = {}
1548 sas_objs = service_appliance_set_list(**kwargs)
1549 if name in sas_objs:
1550 ret[name] = sas_objs.get(name)
1551 if len(ret) != 1:
1552 return {'result': False,
1553 'Error': "Error in the retrieving service apliance set."}
1554 return ret
1555
1556
1557def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1558 '''
1559 Create Contrail service appliance set
1560
1561 CLI Example:
1562
1563 .. code-block:: bash
1564
1565 salt '*' contrail.service_appliance_set_create name
1566 '''
1567 ret = {'name': name,
1568 'changes': {},
1569 'result': True,
1570 'comment': ''}
1571 vnc_client = _auth(**kwargs)
1572 gsc_obj = _get_config(vnc_client)
1573 sas_objs = service_appliance_set_list(**kwargs)
1574 if name in sas_objs:
1575 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1576 else:
1577 service_appliance_set_obj = ServiceApplianceSet(
1578 name, gsc_obj)
1579 if properties:
1580 pairs = KeyValuePairs()
1581 for k, v in properties.items():
1582 pairs.add_key_value_pair(KeyValuePair(k, v))
1583 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1584 if driver:
1585 service_appliance_set_obj.set_service_appliance_driver(driver)
1586 if ha_mode:
1587 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1588 if __opts__['test']:
1589 ret['result'] = None
1590 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1591 else:
1592 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1593 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1594 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1595 return ret
1596
1597
1598def service_appliance_set_delete(name, **kwargs):
1599 '''
1600 Delete specific Contrail service appliance set
1601
1602 CLI Example:
1603
1604 .. code-block:: bash
1605
1606 salt '*' contrail.service_appliance_set_delete name
1607 '''
1608 ret = {'name': name,
1609 'changes': {},
1610 'result': True,
1611 'comment': ''}
1612 vnc_client = _auth(**kwargs)
1613 gsc_obj = _get_config(vnc_client)
1614 sas_obj = ServiceApplianceSet(name, gsc_obj)
1615 if __opts__['test']:
1616 ret['result'] = None
1617 ret['comment'] = "Service appliance set " + name + " will be deleted"
1618 else:
1619 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1620 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1621 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1622 return ret