Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2017 Mirantis, Inc. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sergey Matov | 16896ac | 2018-02-15 15:46:31 +0400 | [diff] [blame] | 16 | from vnc_api.vnc_api import * |
| 17 | from vnc_api.gen.resource_xsd import * |
| 18 | from cfgm_common.exceptions import * |
| 19 | from cfgm_common.rbaclib import * |
| 20 | import cfgm_common |
| 21 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 22 | from netaddr import IPNetwork |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 23 | from vnc_api.vnc_api import PhysicalRouter, PhysicalInterface, LogicalInterface |
| 24 | from vnc_api.vnc_api import EncapsulationPrioritiesType |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 25 | from vnc_api.vnc_api import VirtualMachineInterface, MacAddressesType |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 26 | from vnc_api.vnc_api import ServiceApplianceSet, KeyValuePairs, KeyValuePair |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 27 | |
| 28 | try: |
| 29 | from vnc_api import vnc_api |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 30 | from vnc_api.vnc_api import LinklocalServiceEntryType, \ |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 31 | LinklocalServicesTypes, GlobalVrouterConfig, GlobalSystemConfig |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 32 | from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \ |
Jan Cach | b309272 | 2018-01-31 12:46:16 +0100 | [diff] [blame] | 33 | ConfigNode, DatabaseNode, BgpRouter, VirtualNetwork, FloatingIpPool |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 34 | from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \ |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 35 | BgpSession, BgpPeeringAttributes, BgpRouterParams, AuthenticationData, \ |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 36 | AuthenticationKeyItem, VirtualNetworkType, IpamSubnetType, SubnetType, \ |
Jan Cach | b309272 | 2018-01-31 12:46:16 +0100 | [diff] [blame] | 37 | VnSubnetsType, RouteTargetList, ShareType |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 38 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 39 | HAS_CONTRAIL = True |
| 40 | except ImportError: |
| 41 | HAS_CONTRAIL = False |
| 42 | |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 43 | |
| 44 | try: |
| 45 | from vnc_api.gen.resource_xsd import GracefulRestartParametersType |
| 46 | HAS_OLD = False |
| 47 | except ImportError: |
| 48 | HAS_OLD = True |
| 49 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 50 | __opts__ = {} |
| 51 | |
| 52 | |
| 53 | def __virtual__(): |
| 54 | ''' |
| 55 | Only load this module if vnc_api library is installed. |
| 56 | ''' |
| 57 | if HAS_CONTRAIL: |
| 58 | return 'contrail' |
| 59 | |
| 60 | return False |
| 61 | |
| 62 | |
| 63 | def _auth(**kwargs): |
| 64 | ''' |
| 65 | Set up Contrail API credentials. |
| 66 | ''' |
| 67 | user = kwargs.get('user') |
| 68 | password = kwargs.get('password') |
| 69 | tenant_name = kwargs.get('project') |
| 70 | api_host = kwargs.get('api_server_ip') |
| 71 | api_port = kwargs.get('api_server_port') |
| 72 | api_base_url = kwargs.get('api_base_url') |
Anton Samoylov | 67be52c | 2019-06-07 20:26:44 +0400 | [diff] [blame] | 73 | use_ssl = kwargs.get('api_server_use_ssl') |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 74 | auth_host = kwargs.get('auth_host_ip') |
| 75 | vnc_lib = vnc_api.VncApi(user, password, tenant_name, |
Anton Samoylov | ffb5fdc | 2019-03-11 20:03:02 +0400 | [diff] [blame] | 76 | api_host, api_port, api_base_url, wait_for_connect=False, |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 77 | api_server_use_ssl=use_ssl, auth_host=auth_host) |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 78 | |
| 79 | return vnc_lib |
| 80 | |
| 81 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 82 | def _get_config(vnc_client, global_system_config='default-global-system-config'): |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 83 | try: |
| 84 | gsc_obj = vnc_client.global_system_config_read(id=global_system_config) |
| 85 | except vnc_api.NoIdError: |
| 86 | gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config) |
| 87 | except: |
| 88 | gsc_obj = None |
| 89 | |
| 90 | return gsc_obj |
| 91 | |
| 92 | |
| 93 | def _get_rt_inst_obj(vnc_client): |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 94 | # TODO pick fqname hardcode from common |
| 95 | rt_inst_obj = vnc_client.routing_instance_read( |
| 96 | fq_name=['default-domain', 'default-project', |
| 97 | 'ip-fabric', '__default__']) |
| 98 | |
| 99 | return rt_inst_obj |
| 100 | |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 101 | |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 102 | def _get_fq_name(vnc_client, resource_name, project_name, domain='default-domain'): |
| 103 | res = [domain] |
| 104 | if project_name: |
| 105 | res.append(project_name) |
| 106 | if resource_name: |
| 107 | res.append(resource_name) |
| 108 | return res |
| 109 | |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 110 | |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 111 | def _get_project_obj(vnc_client, name, domain='default-domain'): |
| 112 | return vnc_client.project_read(fq_name=[domain, name]) |
| 113 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 114 | |
| 115 | def _get_ip(ip_w_pfx): |
| 116 | return str(IPNetwork(ip_w_pfx).ip) |
| 117 | |
| 118 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 119 | def virtual_router_list(**kwargs): |
| 120 | ''' |
| 121 | Return a list of all Contrail virtual routers |
| 122 | |
| 123 | CLI Example: |
| 124 | |
| 125 | .. code-block:: bash |
| 126 | |
| 127 | salt '*' contrail.virtual_router_list |
| 128 | ''' |
| 129 | ret = {} |
| 130 | vnc_client = _auth(**kwargs) |
| 131 | vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) |
| 132 | for vrouter_obj in vrouter_objs: |
| 133 | ret[vrouter_obj.name] = { |
| 134 | 'ip_address': vrouter_obj.virtual_router_ip_address, |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 135 | 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled, |
| 136 | 'uuid': vrouter_obj.uuid |
| 137 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 138 | } |
| 139 | return ret |
| 140 | |
| 141 | |
| 142 | def virtual_router_get(name, **kwargs): |
| 143 | ''' |
| 144 | Return a specific Contrail virtual router |
| 145 | |
| 146 | CLI Example: |
| 147 | |
| 148 | .. code-block:: bash |
| 149 | |
| 150 | salt '*' contrail.virtual_router_get cmp01 |
| 151 | ''' |
| 152 | ret = {} |
| 153 | vrouter_objs = virtual_router_list(**kwargs) |
| 154 | if name in vrouter_objs: |
| 155 | ret[name] = vrouter_objs.get(name) |
| 156 | if len(ret) == 0: |
| 157 | return {'Error': 'Error in retrieving virtual router.'} |
| 158 | return ret |
| 159 | |
| 160 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 161 | def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs): |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 162 | ''' |
| 163 | Create specific Contrail virtual router |
| 164 | |
| 165 | CLI Example: |
| 166 | |
| 167 | .. code-block:: bash |
| 168 | |
| 169 | salt '*' contrail.virtual_router_create cmp02 10.10.10.102 |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 170 | router_types: |
| 171 | - tor-agent |
| 172 | - tor-service-node |
| 173 | - embedded |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 174 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 175 | ret = {'name': name, |
| 176 | 'changes': {}, |
| 177 | 'result': True, |
| 178 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 179 | vnc_client = _auth(**kwargs) |
| 180 | gsc_obj = _get_config(vnc_client) |
| 181 | vrouter_objs = virtual_router_list(**kwargs) |
Pavel Svimbersky | a314055 | 2017-08-28 16:55:44 +0200 | [diff] [blame] | 182 | router_types = ['tor-agent', 'tor-service-node', 'embedded'] |
| 183 | if router_type not in router_types: |
| 184 | router_type = None |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 185 | if name in vrouter_objs: |
Anton Samoylov | 67be52c | 2019-06-07 20:26:44 +0400 | [diff] [blame] | 186 | vrouter = virtual_router_get(name, **kwargs) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 187 | vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid']) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 188 | changes = {} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 189 | if vrouter_obj.get_virtual_router_ip_address() != ip_address: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 190 | changes['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 191 | vrouter_obj.set_virtual_router_ip_address(ip_address) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 192 | if vrouter_obj.get_virtual_router_type() != router_type: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 193 | changes['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 194 | vrouter_obj.set_virtual_router_type(router_type) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 195 | if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 196 | changes['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 197 | vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 198 | if len(changes) != 0: |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 199 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 200 | ret['result'] = None |
| 201 | ret['comment'] = "Virtual router " + name + " will be updated" |
| 202 | else: |
| 203 | ret['comment'] = "VirtualRouter " + name + " has been updated" |
| 204 | ret['changes'] = changes |
| 205 | vnc_client.virtual_router_update(vrouter_obj) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 206 | return ret |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 207 | ret['comment'] = 'Virtual router ' + name + ' already exists and is updated' |
| 208 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 209 | else: |
| 210 | vrouter_obj = VirtualRouter( |
| 211 | name, gsc_obj, |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 212 | virtual_router_ip_address=ip_address, |
| 213 | virtual_router_type=router_type) |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 214 | vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 215 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 216 | ret['result'] = None |
| 217 | ret['comment'] = "VirtualRouter " + name + " will be created" |
| 218 | else: |
| 219 | vnc_client.virtual_router_create(vrouter_obj) |
| 220 | ret['comment'] = "VirtualRouter " + name + " has been created" |
| 221 | ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}} |
| 222 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def virtual_router_delete(name, **kwargs): |
| 226 | ''' |
| 227 | Delete specific Contrail virtual router |
| 228 | |
| 229 | CLI Example: |
| 230 | |
| 231 | .. code-block:: bash |
| 232 | |
| 233 | salt '*' contrail.virtual_router_delete cmp01 |
| 234 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 235 | ret = {'name': name, |
| 236 | 'changes': {}, |
| 237 | 'result': True, |
| 238 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 239 | vnc_client = _auth(**kwargs) |
| 240 | gsc_obj = _get_config(vnc_client) |
| 241 | vrouter_obj = VirtualRouter(name, gsc_obj) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 242 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 243 | ret['result'] = None |
| 244 | ret['comment'] = "VirtualRouter " + name + " will be deleted" |
| 245 | else: |
| 246 | vnc_client.virtual_router_delete(fq_name=vrouter_obj.get_fq_name()) |
| 247 | ret['comment'] = "VirtualRouter " + name + " has been deleted" |
| 248 | ret['changes'] = {'VirtualRouter': {'old': name, 'new': ''}} |
| 249 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 250 | |
| 251 | |
| 252 | def physical_router_list(**kwargs): |
| 253 | ''' |
| 254 | Return a list of all Contrail physical routers |
| 255 | |
| 256 | CLI Example: |
| 257 | |
| 258 | .. code-block:: bash |
| 259 | |
| 260 | salt '*' contrail.physical_router_list |
| 261 | ''' |
| 262 | ret = {} |
| 263 | vnc_client = _auth(**kwargs) |
| 264 | prouter_objs = vnc_client._objects_list('physical-router', detail=True) |
| 265 | for prouter_obj in prouter_objs: |
| 266 | ret[prouter_obj.name] = { |
| 267 | 'uuid': prouter_obj._uuid, |
| 268 | 'management_ip': prouter_obj._physical_router_management_ip, |
| 269 | 'product_name': prouter_obj._physical_router_product_name, |
| 270 | } |
| 271 | |
| 272 | return ret |
| 273 | |
| 274 | |
| 275 | def physical_router_get(name, **kwargs): |
| 276 | ''' |
| 277 | Return a specific Contrail physical router |
| 278 | |
| 279 | CLI Example: |
| 280 | |
| 281 | .. code-block:: bash |
| 282 | |
| 283 | salt '*' contrail.physical_router_get router_name |
| 284 | ''' |
| 285 | ret = {} |
| 286 | vnc_client = _auth(**kwargs) |
| 287 | prouter_objs = vnc_client._objects_list('physical-router', detail=True) |
| 288 | for prouter_obj in prouter_objs: |
| 289 | if name == prouter_obj.name: |
| 290 | ret[name] = prouter_obj.__dict__ |
| 291 | if len(ret) == 0: |
| 292 | return {'Error': 'Error in retrieving physical router.'} |
| 293 | return ret |
| 294 | |
| 295 | |
| 296 | def physical_router_create(name, parent_type=None, |
| 297 | management_ip=None, |
| 298 | dataplane_ip=None, # VTEP address in web GUI |
| 299 | vendor_name=None, |
| 300 | product_name=None, |
| 301 | vnc_managed=None, |
| 302 | junos_service_ports=None, |
| 303 | agents=None, **kwargs): |
| 304 | ''' |
| 305 | Create specific Contrail physical router |
| 306 | |
| 307 | CLI Example: |
| 308 | |
| 309 | .. code-block:: bash |
| 310 | |
| 311 | salt '*' contrail.physical_router_create OVSDB_router management_ip=10.167.4.202 dataplane_ip=172.16.20.15 vendor_name=MyVendor product_name=MyProduct agents="['tor01','tns01']" |
| 312 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 313 | ret = {'name': name, |
| 314 | 'changes': {}, |
| 315 | 'result': True, |
| 316 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 317 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 318 | # gsc_obj = _get_config(vnc_client) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 319 | prouter_objs = physical_router_list(**kwargs) |
| 320 | if name in prouter_objs: |
| 321 | prouter = physical_router_get(name) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 322 | changes = {} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 323 | prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid']) |
| 324 | if prouter_obj.physical_router_management_ip != management_ip: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 325 | changes['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 326 | prouter_obj.set_physical_router_management_ip(management_ip) |
| 327 | if prouter_obj.physical_router_dataplane_ip != dataplane_ip: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 328 | changes['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 329 | prouter_obj.set_physical_router_dataplane_ip(dataplane_ip) |
| 330 | if prouter_obj.get_physical_router_vendor_name() != vendor_name: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 331 | changes['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 332 | prouter_obj.set_physical_router_vendor_name(vendor_name) |
| 333 | if prouter_obj.get_physical_router_product_name() != product_name: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 334 | changes['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 335 | prouter_obj.set_physical_router_product_name(product_name) |
| 336 | if prouter_obj.get_physical_router_vnc_managed() != vnc_managed: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 337 | changes['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 338 | prouter_obj.set_physical_router_vnc_managed(vnc_managed) |
| 339 | if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 340 | changes['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(), |
| 341 | 'to': junos_service_ports} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 342 | prouter_obj.set_physical_router_junos_service_ports(junos_service_ports) |
| 343 | |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 344 | if len(changes) != 0: |
| 345 | if __opts__['test']: |
| 346 | ret['result'] = None |
| 347 | ret['comment'] = "Physical router " + name + " will be updated" |
| 348 | else: |
| 349 | ret['comment'] = 'Physical router ' + name + ' already exists and is updated' |
| 350 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 351 | |
| 352 | vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects |
| 353 | c_agents = [] # referenced vrouters |
| 354 | for c_agent in prouter_obj.get_virtual_router_refs(): |
| 355 | c_agents.append(c_agent['uuid']) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 356 | # agent_objs = [] # required state of references |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 357 | for vrouter_obj in vrouter_objs: |
| 358 | if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents: |
| 359 | prouter_obj.add_virtual_router(vrouter_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 360 | changes['vrouter ' + vrouter_obj._display_name] = "Reference added" |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 361 | if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents: |
| 362 | prouter_obj.del_virtual_router(vrouter_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 363 | changes['vrouter ' + vrouter_obj._display_name] = "Reference removed" |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 364 | vnc_client.physical_router_update(prouter_obj) |
| 365 | |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 366 | if __opts__['test']: |
| 367 | ret['result'] = None |
| 368 | ret['comment'] = "VirtualRouter " + name + " will be created" |
| 369 | else: |
| 370 | vnc_client.virtual_router_create(vrouter_obj) |
| 371 | ret['comment'] = "VirtualRouter " + name + " has been created" |
| 372 | ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}} |
| 373 | |
| 374 | if len(changes) == 0: |
| 375 | ret['comment'] = "Physical router exists and is updated" |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 376 | return ret |
| 377 | else: |
| 378 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 379 | ret['result'] = None |
| 380 | ret['comment'] = "Physical router " + name + " will be created" |
| 381 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 382 | prouter_obj = PhysicalRouter( |
| 383 | name=name, |
| 384 | parent_obj=None, |
| 385 | physical_router_management_ip=management_ip, |
| 386 | physical_router_dataplane_ip=dataplane_ip, |
| 387 | physical_router_vendor_name=vendor_name, |
| 388 | physical_router_product_name=product_name, |
| 389 | physical_router_vnc_managed=vnc_managed, |
| 390 | physical_router_junos_service_ports=junos_service_ports, |
| 391 | ) |
| 392 | for agent in agents: |
| 393 | vrouter = virtual_router_get(agent) |
| 394 | vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid']) |
| 395 | prouter_obj.add_virtual_router(vrouter_obj) |
| 396 | vnc_client.physical_router_create(prouter_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 397 | ret['comment'] = "Physical router " + name + " has been created" |
| 398 | ret['changes'] = {'PhysicalRouter': {'old': '', 'new': name}} |
| 399 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 400 | |
| 401 | |
| 402 | def physical_router_delete(name, **kwargs): |
| 403 | ''' |
| 404 | Delete specific Contrail physical router |
| 405 | |
| 406 | CLI Example: |
| 407 | |
| 408 | .. code-block:: bash |
| 409 | |
| 410 | salt '*' contrail.physical_router_delete router_name |
| 411 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 412 | ret = {'name': name, |
| 413 | 'changes': {}, |
| 414 | 'result': True, |
| 415 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 416 | vnc_client = _auth(**kwargs) |
| 417 | gsc_obj = _get_config(vnc_client) |
| 418 | prouter_obj = PhysicalRouter(name, gsc_obj) |
| 419 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 420 | ret['result'] = None |
| 421 | ret['comment'] = "Physical router " + name + " will be deleted" |
| 422 | else: |
| 423 | vnc_client.physical_router_delete(fq_name=prouter_obj.get_fq_name()) |
| 424 | ret['comment'] = "Physical router " + name + " has been deleted" |
| 425 | ret['changes'] = {'Physical router': {'old': name, 'new': ''}} |
| 426 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 427 | |
| 428 | |
| 429 | def physical_interface_list(**kwargs): |
| 430 | ''' |
| 431 | Return a list of all Contrail physical interface |
| 432 | |
| 433 | CLI Example: |
| 434 | |
| 435 | .. code-block:: bash |
| 436 | |
| 437 | salt '*' contrail.physical_interface_list |
| 438 | ''' |
| 439 | ret = {} |
| 440 | vnc_client = _auth(**kwargs) |
| 441 | pinterface_objs = vnc_client._objects_list('physical-interface', detail=True) |
| 442 | for pinterface_obj in pinterface_objs: |
| 443 | ret[pinterface_obj.name] = { |
| 444 | 'uuid': pinterface_obj._uuid, |
| 445 | 'fq_name': pinterface_obj.fq_name, |
| 446 | 'parent_type': pinterface_obj.parent_type, |
| 447 | } |
| 448 | |
| 449 | return ret |
| 450 | |
| 451 | |
| 452 | def physical_interface_get(name, physical_router, **kwargs): |
| 453 | ''' |
| 454 | Return a specific Contrail physical interface |
| 455 | |
| 456 | CLI Example: |
| 457 | |
| 458 | .. code-block:: bash |
| 459 | |
| 460 | salt '*' contrail.physical_interface_get interface_name physical_router_name |
| 461 | ''' |
| 462 | ret = {} |
| 463 | vnc_client = _auth(**kwargs) |
| 464 | pinterf_objs = vnc_client._objects_list('physical-interface', detail=True) |
| 465 | for pinterf_obj in pinterf_objs: |
| 466 | if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name: |
| 467 | ret[name] = pinterf_obj.__dict__ |
| 468 | if len(ret) == 0: |
| 469 | return {'Error': 'Error in retrieving physical interface.'} |
| 470 | return ret |
| 471 | |
| 472 | |
| 473 | def physical_interface_create(name, physical_router, **kwargs): |
| 474 | ''' |
| 475 | Create specific Contrail physical interface |
| 476 | |
| 477 | CLI Example: |
| 478 | |
| 479 | .. code-block:: bash |
| 480 | |
| 481 | salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name |
| 482 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 483 | ret = {'name': name, |
| 484 | 'changes': {}, |
| 485 | 'result': True, |
| 486 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 487 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 488 | # gsc_obj = _get_config(vnc_client) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 489 | pinterf_obj = physical_interface_get(name, physical_router, **kwargs) |
| 490 | if 'Error' not in pinterf_obj: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 491 | ret['comment'] = 'Physical interface ' + name + ' on ' + physical_router + ' already exists' |
| 492 | return ret |
| 493 | |
| 494 | if __opts__['test']: |
| 495 | ret['result'] = None |
| 496 | ret['comment'] = "Physical interface " + name + " will be created" |
| 497 | return ret |
| 498 | |
| 499 | prouter = physical_router_get(physical_router) |
| 500 | prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid']) |
| 501 | pinterf_obj = PhysicalInterface(name, prouter_obj) |
| 502 | vnc_client.physical_interface_create(pinterf_obj) |
| 503 | ret['comment'] = "Physical interface " + name + " has been created" |
| 504 | ret['changes'] = {'Physical interface': {'old': '', 'new': name}} |
| 505 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 506 | |
| 507 | |
| 508 | def physical_interface_delete(name, physical_router, **kwargs): |
| 509 | ''' |
| 510 | Delete specific Contrail physical interface |
| 511 | |
| 512 | CLI Example: |
| 513 | .. code-block:: bash |
| 514 | |
| 515 | salt '*' contrail.physical_interface_delete ge-0/0/0 phr01 |
| 516 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 517 | ret = {'name': name, |
| 518 | 'changes': {}, |
| 519 | 'result': True, |
| 520 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 521 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 522 | # gsc_obj = _get_config(vnc_client) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 523 | piface = physical_interface_get(name, physical_router) |
| 524 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 525 | ret['result'] = None |
| 526 | ret['comment'] = "Physical interface " + name + " will be deleted" |
| 527 | else: |
| 528 | vnc_client.physical_interface_delete(id=piface[name]['_uuid']) |
| 529 | ret['comment'] = "Physical router " + name + " has been deleted" |
| 530 | ret['changes'] = {'Physical router': {'old': name, 'new': ''}} |
| 531 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 532 | |
| 533 | |
| 534 | def logical_interface_list(**kwargs): |
| 535 | ''' |
| 536 | Return a list of all Contrail logical interfaces |
| 537 | |
| 538 | CLI Example: |
| 539 | |
| 540 | .. code-block:: bash |
| 541 | |
| 542 | salt '*' contrail.logical_interface_list |
| 543 | ''' |
| 544 | ret = [] |
| 545 | vnc_client = _auth(**kwargs) |
| 546 | liface_objs = vnc_client._objects_list('logical-interface', detail=True) |
| 547 | for liface_obj in liface_objs: |
| 548 | ret.append({ |
| 549 | 'name': liface_obj.name, |
| 550 | 'uuid': liface_obj._uuid, |
| 551 | 'fq_name': liface_obj.fq_name, |
| 552 | 'parent_type': liface_obj.parent_type, |
| 553 | }) |
| 554 | return ret |
| 555 | |
| 556 | |
| 557 | def logical_interface_get(name, parent_names, parent_type=None, **kwargs): |
| 558 | ''' |
| 559 | Return a specific Contrail logical interface |
| 560 | |
| 561 | CLI Example: |
| 562 | |
| 563 | .. code-block:: bash |
| 564 | |
| 565 | salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] |
| 566 | or |
| 567 | salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01'] |
| 568 | or |
| 569 | salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface |
| 570 | ''' |
| 571 | ret = {} |
| 572 | vnc_client = _auth(**kwargs) |
| 573 | liface_objs = vnc_client._objects_list('logical-interface', detail=True) |
| 574 | count = 0 |
| 575 | for liface_obj in liface_objs: |
| 576 | if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name): |
| 577 | if parent_type and parent_type == liface_obj.parent_type: |
| 578 | count += 1 |
| 579 | ret[liface_obj.name] = liface_obj.__dict__ |
| 580 | if not parent_type: |
| 581 | count += 1 |
| 582 | ret[liface_obj.name] = liface_obj.__dict__ |
| 583 | if len(ret) == 0: |
| 584 | return {'Error': 'Error in retrieving logical interface.'} |
| 585 | if count > 1: |
| 586 | return { |
| 587 | 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'} |
| 588 | return ret |
| 589 | |
| 590 | |
| 591 | def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2", |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 592 | vmis=None, **kwargs): |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 593 | ''' |
| 594 | Create specific Contrail logical interface |
| 595 | |
| 596 | CLI Example: |
| 597 | |
| 598 | .. code-block:: bash |
| 599 | |
| 600 | salt '*' contrail.logical_interface_create ge-0/0/10.11 parent_names="['ge-0/0/0','phr1']" parent_type=physical-interface vlan_tag=1025 interface_type=L2 |
| 601 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 602 | ret = {'name': name, |
| 603 | 'changes': {}, |
| 604 | 'result': True, |
| 605 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 606 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 607 | # gsc_obj = _get_config(vnc_client) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 608 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 609 | liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs) |
| 610 | if 'Error' not in liface_obj: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 611 | ret['comment'] = 'Logical interface ' + name + ' already exists' |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 612 | else: |
| 613 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 614 | ret['result'] = None |
| 615 | ret['comment'] = "Logical interface " + name + " will be created" |
| 616 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 617 | parent_obj = None |
| 618 | for router in parent_names: |
| 619 | parent_router = physical_router_get(router) |
| 620 | if 'Error' not in parent_router: |
| 621 | parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid']) |
| 622 | break |
| 623 | if not parent_obj: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 624 | ret['result'] = False |
| 625 | ret['comment'] = 'Physical router have to be defined' |
| 626 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 627 | if parent_type == 'physical-interface': |
| 628 | for interface in parent_names: |
| 629 | parent_interface = physical_interface_get(interface, parent_obj.name) |
| 630 | if 'Error' not in parent_interface: |
| 631 | parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid']) |
| 632 | break |
| 633 | if interface_type.lower() == "l3": |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 634 | ret['result'] = False |
| 635 | ret['comment'] = "Virtual Network have to be defined for L3 interface type" |
| 636 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 637 | |
| 638 | liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower()) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 639 | |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 640 | if vmis: |
| 641 | for vmi_name, vmi in vmis.iteritems(): |
| 642 | vmi = vnc_client.virtual_machine_interface_read( |
| 643 | fq_name=_get_fq_name(vnc_client, resource_name=vmi_name, |
| 644 | project_name=kwargs.get('tenant', 'admin'))) |
| 645 | liface_obj.add_virtual_machine_interface(vmi) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 646 | vnc_client.logical_interface_create(liface_obj) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 647 | |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 648 | ret['comment'] = "Logical interface " + name + " has been created" |
| 649 | ret['changes'] = {'Logical interface': {'old': '', 'new': name}} |
| 650 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 651 | |
| 652 | |
| 653 | def logical_interface_delete(name, parent_names, parent_type=None, **kwargs): |
| 654 | ''' |
| 655 | Delete specific Contrail logical interface |
| 656 | |
| 657 | CLI Example: |
| 658 | |
| 659 | .. code-block:: bash |
| 660 | |
| 661 | salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01'] |
| 662 | or |
| 663 | salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router |
| 664 | |
| 665 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 666 | ret = {'name': name, |
| 667 | 'changes': {}, |
| 668 | 'result': True, |
| 669 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 670 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 671 | # gsc_obj = _get_config(vnc_client) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 672 | liface = logical_interface_get(name, parent_names, parent_type) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 673 | |
| 674 | if __opts__['test']: |
| 675 | ret['result'] = None |
| 676 | ret['comment'] = "Logical interface " + name + " will be deleted" |
| 677 | return ret |
| 678 | vnc_client.logical_interface_delete(id=liface[name]['_uuid']) |
| 679 | ret['comment'] = "Logical interface " + name + " has been deleted" |
| 680 | ret['changes'] = {'LogicalInterface ': {'old': name, 'new': ''}} |
| 681 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 682 | |
| 683 | |
| 684 | def global_vrouter_config_list(**kwargs): |
| 685 | ''' |
| 686 | Return a list of all Contrail global vrouter configs |
| 687 | |
| 688 | CLI Example: |
| 689 | |
| 690 | .. code-block:: bash" |
| 691 | |
| 692 | salt '*' global_vrouter_config_list |
| 693 | ''' |
| 694 | ret = {} |
| 695 | vnc_client = _auth(**kwargs) |
| 696 | vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True) |
| 697 | for vrouter_conf_obj in vrouter_conf_objs: |
Anton Samoylov | ce3d777 | 2018-11-23 00:00:02 +0400 | [diff] [blame] | 698 | ret[vrouter_conf_obj.name] = vrouter_conf_obj.__dict__ |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 699 | return ret |
| 700 | |
| 701 | |
| 702 | def global_vrouter_config_get(name, **kwargs): |
| 703 | ''' |
| 704 | Return a specific Contrail global vrouter config |
| 705 | |
| 706 | CLI Example: |
| 707 | |
| 708 | .. code-block:: bash |
| 709 | |
| 710 | salt '*' contrail.global_vrouter_get global-vrouter-config |
| 711 | ''' |
| 712 | ret = {} |
| 713 | vrouter_conf_objs = global_vrouter_config_list(**kwargs) |
| 714 | if name in vrouter_conf_objs: |
| 715 | ret[name] = vrouter_conf_objs.get(name) |
| 716 | if len(ret) == 0: |
| 717 | return {'Error': 'Error in retrieving global vrouter config.'} |
| 718 | return ret |
| 719 | |
| 720 | |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 721 | def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, flow_export_rate, *fq_names, **kwargs): |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 722 | ''' |
| 723 | Create specific Contrail global vrouter config |
| 724 | |
| 725 | CLI Example: |
| 726 | |
| 727 | .. code-block:: bash |
| 728 | |
Anton Samoylov | f1a5ef8 | 2019-04-15 12:42:54 +0400 | [diff] [blame] | 729 | salt '*' contrail.global_vrouter_config_create \ |
| 730 | name=global-vrouter-config parent_type=global-system-config \ |
| 731 | encap_priority="MPLSoUDP,MPLSoGRE" vxlan_vn_id_mode="automatic" \ |
| 732 | flow_export_rate=None \ |
| 733 | fq_names="['default-global-system-config', 'default-global-vrouter-config']" |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 734 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 735 | ret = {'name': name, |
| 736 | 'changes': {}, |
| 737 | 'result': True, |
| 738 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 739 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 740 | # gsc_obj = _get_config(vnc_client) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 741 | vrouter_conf_objs = global_vrouter_config_list(**kwargs) |
| 742 | if name in vrouter_conf_objs: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 743 | ret['comment'] = 'Global vrouter config ' + name + ' already exists' |
| 744 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 745 | else: |
| 746 | vrouter_conf_obj = GlobalVrouterConfig( |
| 747 | name=name, |
| 748 | parent_obj=None, |
| 749 | encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")), |
| 750 | fq_name=fq_names, |
| 751 | vxlan_network_identifier_mode=vxlan_vn_id_mode, |
| 752 | parent_type=parent_type, |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 753 | flow_export_rate=flow_export_rate, |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 754 | ) |
| 755 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 756 | ret['result'] = None |
| 757 | ret['comment'] = "Global vRouter config " + name + " will be created" |
| 758 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 759 | vnc_client.global_vrouter_config_create(vrouter_conf_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 760 | ret['comment'] = "Global vRouter config " + name + " has been created" |
| 761 | ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}} |
| 762 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 763 | |
| 764 | |
| 765 | def global_vrouter_config_delete(name, **kwargs): |
| 766 | ''' |
| 767 | Delete specific Contrail global vrouter config |
| 768 | |
| 769 | CLI Example: |
| 770 | |
| 771 | .. code-block:: bash |
| 772 | |
| 773 | salt '*' contrail.global_vrouter_config_delete global-vrouter-config |
| 774 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 775 | ret = {'name': name, |
| 776 | 'changes': {}, |
| 777 | 'result': True, |
| 778 | 'comment': ''} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 779 | vnc_client = _auth(**kwargs) |
| 780 | gsc_obj = _get_config(vnc_client) |
| 781 | vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj) |
| 782 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 783 | ret['result'] = None |
| 784 | ret['comment'] = "Global vRouter config " + name + " will be deleted" |
| 785 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 786 | vnc_client.global_vrouter_config_delete( |
| 787 | fq_name=vrouter_conf_obj.get_fq_name()) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 788 | ret['comment'] = "Global vRouter config " + name + " has been deleted" |
| 789 | ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}} |
| 790 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 791 | |
| 792 | |
| 793 | def analytics_node_list(**kwargs): |
| 794 | ''' |
| 795 | Return a list of all Contrail analytics nodes |
| 796 | |
| 797 | CLI Example: |
| 798 | |
| 799 | .. code-block:: bash |
| 800 | |
| 801 | salt '*' contrail.analytics_node_list |
| 802 | ''' |
| 803 | ret = {} |
| 804 | vnc_client = _auth(**kwargs) |
| 805 | node_objs = vnc_client._objects_list('analytics-node', detail=True) |
| 806 | for node_obj in node_objs: |
| 807 | ret[node_obj.name] = node_obj.__dict__ |
| 808 | return ret |
| 809 | |
| 810 | |
| 811 | def analytics_node_get(name, **kwargs): |
| 812 | ''' |
| 813 | Return a specific Contrail analytics node |
| 814 | |
| 815 | CLI Example: |
| 816 | |
| 817 | .. code-block:: bash |
| 818 | |
| 819 | salt '*' contrail.analytics_node_get nal01 |
| 820 | ''' |
| 821 | ret = {} |
| 822 | vrouter_objs = analytics_node_list(**kwargs) |
| 823 | if name in vrouter_objs: |
| 824 | ret[name] = vrouter_objs.get(name) |
| 825 | if len(ret) == 0: |
| 826 | return {'Error': 'Error in retrieving analytics node.'} |
| 827 | return ret |
| 828 | |
| 829 | |
| 830 | def analytics_node_create(name, ip_address, **kwargs): |
| 831 | ''' |
| 832 | Create specific Contrail analytics node |
| 833 | |
| 834 | CLI Example: |
| 835 | |
| 836 | .. code-block:: bash |
| 837 | |
| 838 | salt '*' contrail.analytics_node_create ntw03 10.10.10.103 |
| 839 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 840 | |
| 841 | ret = {'name': name, |
| 842 | 'changes': {}, |
| 843 | 'result': True, |
| 844 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 845 | vnc_client = _auth(**kwargs) |
| 846 | gsc_obj = _get_config(vnc_client) |
| 847 | analytics_node_objs = analytics_node_list(**kwargs) |
| 848 | if name in analytics_node_objs: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 849 | ret['comment'] = 'Analytics node %s already exists' |
| 850 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 851 | else: |
| 852 | analytics_node_obj = AnalyticsNode( |
| 853 | name, gsc_obj, |
| 854 | analytics_node_ip_address=ip_address) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 855 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 856 | ret['result'] = None |
| 857 | ret['comment'] = "AnalyticsNode " + name + " will be created" |
| 858 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 859 | vnc_client.analytics_node_create(analytics_node_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 860 | ret['comment'] = "AnalyticsNode " + name + " has been created" |
| 861 | ret['changes'] = {'Analytics Node': {'old': '', 'new': name}} |
| 862 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 863 | |
| 864 | |
| 865 | def analytics_node_delete(name, **kwargs): |
| 866 | ''' |
| 867 | Delete specific Contrail analytics node |
| 868 | |
| 869 | CLI Example: |
| 870 | |
| 871 | .. code-block:: bash |
| 872 | |
| 873 | salt '*' contrail.analytics_node_delete cmp01 |
| 874 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 875 | ret = {'name': name, |
| 876 | 'changes': {}, |
| 877 | 'result': True, |
| 878 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 879 | vnc_client = _auth(**kwargs) |
| 880 | gsc_obj = _get_config(vnc_client) |
| 881 | analytics_node_obj = AnalyticsNode(name, gsc_obj) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 882 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 883 | ret['result'] = None |
| 884 | ret['comment'] = "AnalyticsNode " + name + " will be deleted" |
| 885 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 886 | vnc_client.analytics_node_delete( |
| 887 | fq_name=analytics_node_obj.get_fq_name()) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 888 | ret['comment'] = "AnalyticsNode " + name + " has been deleted" |
| 889 | ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}} |
| 890 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 891 | |
| 892 | |
| 893 | def config_node_list(**kwargs): |
| 894 | ''' |
| 895 | Return a list of all Contrail config nodes |
| 896 | |
| 897 | CLI Example: |
| 898 | |
| 899 | .. code-block:: bash |
| 900 | |
| 901 | salt '*' contrail.config_node_list |
| 902 | ''' |
| 903 | ret = {} |
| 904 | vnc_client = _auth(**kwargs) |
| 905 | node_objs = vnc_client._objects_list('config-node', detail=True) |
| 906 | for node_obj in node_objs: |
| 907 | ret[node_obj.name] = node_obj.__dict__ |
| 908 | return ret |
| 909 | |
| 910 | |
| 911 | def config_node_get(name, **kwargs): |
| 912 | ''' |
| 913 | Return a specific Contrail config node |
| 914 | |
| 915 | CLI Example: |
| 916 | |
| 917 | .. code-block:: bash |
| 918 | |
| 919 | salt '*' contrail.config_node_get nal01 |
| 920 | ''' |
| 921 | ret = {} |
| 922 | vrouter_objs = config_node_list(**kwargs) |
| 923 | if name in vrouter_objs: |
| 924 | ret[name] = vrouter_objs.get(name) |
| 925 | if len(ret) == 0: |
| 926 | return {'Error': 'Error in retrieving config node.'} |
| 927 | return ret |
| 928 | |
| 929 | |
| 930 | def config_node_create(name, ip_address, **kwargs): |
| 931 | ''' |
| 932 | Create specific Contrail config node |
| 933 | |
| 934 | CLI Example: |
| 935 | |
| 936 | .. code-block:: bash |
| 937 | |
| 938 | salt '*' contrail.config_node_create ntw03 10.10.10.103 |
| 939 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 940 | ret = {'name': name, |
| 941 | 'changes': {}, |
| 942 | 'result': True, |
| 943 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 944 | vnc_client = _auth(**kwargs) |
| 945 | gsc_obj = _get_config(vnc_client) |
| 946 | config_node_objs = config_node_list(**kwargs) |
| 947 | if name in config_node_objs: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 948 | ret['comment'] = 'Config node ' + name + ' already exists' |
| 949 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 950 | else: |
| 951 | config_node_obj = ConfigNode( |
| 952 | name, gsc_obj, |
| 953 | config_node_ip_address=ip_address) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 954 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 955 | ret['comment'] = "ConfigNode " + name + " will be created" |
| 956 | ret['result'] = None |
| 957 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 958 | vnc_client.config_node_create(config_node_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 959 | ret['comment'] = "ConfigNode " + name + " has been created" |
| 960 | ret['changes'] = {'ConfigNode': {'old': '', 'new': name}} |
| 961 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 962 | |
| 963 | |
| 964 | def config_node_delete(name, **kwargs): |
| 965 | ''' |
| 966 | Delete specific Contrail config node |
| 967 | |
| 968 | CLI Example: |
| 969 | |
| 970 | .. code-block:: bash |
| 971 | |
| 972 | salt '*' contrail.config_node_delete cmp01 |
| 973 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 974 | ret = {'name': name, |
| 975 | 'changes': {}, |
| 976 | 'result': True, |
| 977 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 978 | vnc_client = _auth(**kwargs) |
| 979 | gsc_obj = _get_config(vnc_client) |
| 980 | config_node_obj = ConfigNode(name, gsc_obj) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 981 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 982 | ret['comment'] = "ConfigNode " + name + " will be deleted" |
| 983 | ret['result'] = None |
| 984 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 985 | vnc_client.config_node_delete( |
| 986 | fq_name=config_node_obj.get_fq_name()) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 987 | ret['comment'] = "ConfigNode " + name + " has been deleted" |
| 988 | ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}} |
| 989 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 990 | |
| 991 | |
| 992 | def bgp_router_list(**kwargs): |
| 993 | ''' |
| 994 | Return a list of all Contrail BGP routers |
| 995 | |
| 996 | CLI Example: |
| 997 | |
| 998 | .. code-block:: bash |
| 999 | |
| 1000 | salt '*' contrail.bgp_router_list |
| 1001 | ''' |
| 1002 | ret = {} |
| 1003 | vnc_client = _auth(**kwargs) |
| 1004 | bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True) |
| 1005 | for bgp_router_obj in bgp_router_objs: |
| 1006 | ret[bgp_router_obj.name] = bgp_router_obj.__dict__ |
| 1007 | return ret |
| 1008 | |
| 1009 | |
| 1010 | def bgp_router_get(name, **kwargs): |
| 1011 | ''' |
| 1012 | Return a specific Contrail BGP router |
| 1013 | |
| 1014 | CLI Example: |
| 1015 | |
| 1016 | .. code-block:: bash |
| 1017 | |
| 1018 | salt '*' contrail.bgp_router_get nal01 |
| 1019 | ''' |
| 1020 | ret = {} |
| 1021 | bgp_router_objs = bgp_router_list(**kwargs) |
| 1022 | if name in bgp_router_objs: |
| 1023 | ret[name] = bgp_router_objs.get(name) |
| 1024 | if len(ret) == 0: |
| 1025 | return {'Error': 'Error in retrieving BGP router.'} |
| 1026 | return ret |
| 1027 | |
| 1028 | |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1029 | def bgp_router_create(name, type, ip_address, asn=64512, key_type=None, key=None, **kwargs): |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1030 | ''' |
| 1031 | Create specific Contrail control node |
| 1032 | |
| 1033 | CLI Example: |
| 1034 | |
| 1035 | .. code-block:: bash |
| 1036 | |
| 1037 | salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103 |
| 1038 | salt '*' contrail.bgp_router_create mx01 router 10.10.10.105 |
| 1039 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1040 | ret = {'name': name, |
| 1041 | 'changes': {}, |
| 1042 | 'result': True, |
| 1043 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1044 | vnc_client = _auth(**kwargs) |
| 1045 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1046 | address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn', |
| 1047 | 'inet6-vpn'] |
| 1048 | if type != 'control-node': |
| 1049 | address_families.remove('erm-vpn') |
| 1050 | |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1051 | key_type = None if key_type == 'None' else key_type |
| 1052 | key = None if key == 'None' else key |
| 1053 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1054 | bgp_addr_fams = AddressFamilies(address_families) |
| 1055 | bgp_sess_attrs = [ |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1056 | BgpSessionAttributes(address_families=bgp_addr_fams)] |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1057 | bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)] |
| 1058 | bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions) |
| 1059 | rt_inst_obj = _get_rt_inst_obj(vnc_client) |
| 1060 | |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1061 | bgp_auth_data = None |
| 1062 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1063 | if type == 'control-node': |
| 1064 | vendor = 'contrail' |
| 1065 | elif type == 'router': |
| 1066 | vendor = 'mx' |
| 1067 | else: |
| 1068 | vendor = 'unknown' |
| 1069 | |
Sergey Kreys | a4e2648 | 2019-09-19 16:01:57 -0500 | [diff] [blame] | 1070 | if key_type == 'md5': |
| 1071 | key_id = 0 |
| 1072 | key_items = AuthenticationKeyItem(key_id, key) |
| 1073 | bgp_auth_data = AuthenticationData(key_type, [key_items]) |
| 1074 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1075 | router_params = BgpRouterParams(router_type=type, |
| 1076 | vendor=vendor, autonomous_system=int(asn), |
| 1077 | identifier=_get_ip(ip_address), |
| 1078 | address=_get_ip(ip_address), |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1079 | port=179, address_families=bgp_addr_fams, |
| 1080 | auth_data=bgp_auth_data) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1081 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1082 | bgp_router_objs = bgp_router_list(**kwargs) |
| 1083 | if name in bgp_router_objs: |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1084 | bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid']) |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1085 | |
| 1086 | if bgp_router_obj.bgp_router_parameters.autonomous_system != asn: |
| 1087 | ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}}) |
| 1088 | if bgp_router_obj.bgp_router_parameters.vendor != vendor: |
| 1089 | ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}}) |
| 1090 | if bgp_router_obj.bgp_router_parameters.address != ip_address: |
| 1091 | ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}}) |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1092 | try: |
| 1093 | if bgp_router_obj.bgp_router_parameters.auth_data.key_type != key_type: |
| 1094 | ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_type, 'new': key_type}}) |
| 1095 | except: |
| 1096 | if key_type != None: |
| 1097 | ret['changes'].update({"key_type": {'old': None, 'new': key_type}}) |
| 1098 | if key_type == 'md5': |
| 1099 | try: |
| 1100 | if bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key != key: |
Sergey Kreys | a4e2648 | 2019-09-19 16:01:57 -0500 | [diff] [blame] | 1101 | ret['changes'].update({"key": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key, 'new': key}}) |
Marek Celoud | 3097e5b | 2018-01-09 13:52:14 +0100 | [diff] [blame] | 1102 | except: |
Sergey Kreys | a4e2648 | 2019-09-19 16:01:57 -0500 | [diff] [blame] | 1103 | ret['changes'].update({"key": {'old': None, 'new': key}}) |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1104 | |
| 1105 | if len(ret['changes']) == 0: |
| 1106 | return ret |
| 1107 | |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1108 | bgp_router_obj.set_bgp_router_parameters(router_params) |
| 1109 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1110 | ret['result'] = None |
| 1111 | ret['comment'] = "BGP router " + name + " will be updated" |
| 1112 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1113 | vnc_client.bgp_router_update(bgp_router_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1114 | ret['comment'] = "BGP router " + name + " has been updated" |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1115 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1116 | else: |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1117 | bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params) |
| 1118 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1119 | ret['result'] = None |
| 1120 | ret['comment'] = "BGP router " + name + " will be created" |
| 1121 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1122 | vnc_client.bgp_router_create(bgp_router_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1123 | ret['comment'] = "BGP router " + name + " has been created" |
| 1124 | ret['changes'] = {'BGP router': {'old': name, 'new': ''}} |
| 1125 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1126 | |
| 1127 | |
| 1128 | def bgp_router_delete(name, **kwargs): |
| 1129 | ''' |
| 1130 | Delete specific Contrail control node |
| 1131 | |
| 1132 | CLI Example: |
| 1133 | |
| 1134 | .. code-block:: bash |
| 1135 | |
| 1136 | salt '*' contrail.bgp_router_delete mx01 |
| 1137 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1138 | ret = {'name': name, |
| 1139 | 'changes': {}, |
| 1140 | 'result': True, |
| 1141 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1142 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1143 | |
| 1144 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1145 | ret['result'] = None |
| 1146 | ret['comment'] = "BGP router " + name + " will be deleted" |
| 1147 | return ret |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1148 | |
| 1149 | bgp_router = bgp_router_get(name) |
| 1150 | if name in bgp_router: |
| 1151 | vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name']) |
| 1152 | ret['comment'] = "BGP router " + name + " has been deleted" |
| 1153 | ret['changes'] = {'BGP router': {'old': '', 'new': name}} |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1154 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1155 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1156 | |
| 1157 | def database_node_list(**kwargs): |
| 1158 | ''' |
| 1159 | Return a list of all Contrail database nodes |
| 1160 | |
| 1161 | CLI Example: |
| 1162 | |
| 1163 | .. code-block:: bash |
| 1164 | |
| 1165 | salt '*' contrail.database_node_list |
| 1166 | ''' |
| 1167 | ret = {} |
| 1168 | vnc_client = _auth(**kwargs) |
| 1169 | node_objs = vnc_client._objects_list('database-node', detail=True) |
| 1170 | for node_obj in node_objs: |
| 1171 | ret[node_obj.name] = node_obj.__dict__ |
| 1172 | return ret |
| 1173 | |
| 1174 | |
| 1175 | def database_node_get(name, **kwargs): |
| 1176 | ''' |
| 1177 | Return a specific Contrail database node |
| 1178 | |
| 1179 | CLI Example: |
| 1180 | |
| 1181 | .. code-block:: bash |
| 1182 | |
| 1183 | salt '*' contrail.database_node_get nal01 |
| 1184 | ''' |
| 1185 | ret = {} |
| 1186 | vrouter_objs = database_node_list(**kwargs) |
| 1187 | if name in vrouter_objs: |
| 1188 | ret[name] = vrouter_objs.get(name) |
| 1189 | if len(ret) == 0: |
| 1190 | return {'Error': 'Error in retrieving database node.'} |
| 1191 | return ret |
| 1192 | |
| 1193 | |
| 1194 | def database_node_create(name, ip_address, **kwargs): |
| 1195 | ''' |
| 1196 | Create specific Contrail database node |
| 1197 | |
| 1198 | CLI Example: |
| 1199 | |
| 1200 | .. code-block:: bash |
| 1201 | |
| 1202 | salt '*' contrail.database_node_create ntw03 10.10.10.103 |
| 1203 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1204 | ret = {'name': name, |
| 1205 | 'changes': {}, |
| 1206 | 'result': True, |
| 1207 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1208 | vnc_client = _auth(**kwargs) |
| 1209 | gsc_obj = _get_config(vnc_client) |
| 1210 | database_node_objs = database_node_list(**kwargs) |
| 1211 | if name in database_node_objs: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1212 | ret['comment'] = 'Database node ' + name + ' already exists' |
| 1213 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1214 | else: |
| 1215 | database_node_obj = DatabaseNode( |
| 1216 | name, gsc_obj, |
| 1217 | database_node_ip_address=ip_address) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1218 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1219 | ret['result'] = None |
| 1220 | ret['comment'] = "DatabaseNode " + name + " will be created" |
| 1221 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1222 | vnc_client.database_node_create(database_node_obj) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1223 | ret['comment'] = "DatabaseNode " + name + " has been created" |
| 1224 | ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}} |
| 1225 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1226 | |
| 1227 | |
| 1228 | def database_node_delete(name, **kwargs): |
| 1229 | ''' |
| 1230 | Delete specific Contrail database node |
| 1231 | |
| 1232 | CLI Example: |
| 1233 | |
| 1234 | .. code-block:: bash |
| 1235 | |
| 1236 | salt '*' contrail.database_node_delete cmp01 |
| 1237 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1238 | ret = {'name': name, |
| 1239 | 'changes': {}, |
| 1240 | 'result': True, |
| 1241 | 'comment': ''} |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1242 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1243 | gsc_obj = _get_config(vnc_client) |
| 1244 | database_node_obj = DatabaseNode(name, gsc_obj) |
| 1245 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1246 | ret['result'] = None |
| 1247 | ret['comment'] = "DatabaseNode " + name + " will be deleted" |
| 1248 | return ret |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1249 | vnc_client.database_node_delete( |
| 1250 | fq_name=database_node_obj.get_fq_name()) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1251 | ret['comment'] = "DatabaseNode " + name + " has been deleted" |
| 1252 | ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}} |
| 1253 | return ret |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1254 | |
| 1255 | |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1256 | def _get_vrouter_config(vnc_client, gvc_name=None): |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1257 | try: |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1258 | vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True) |
| 1259 | if len(vrouter_conf_objs) == 0: |
| 1260 | return None |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1261 | if not gvc_name: |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1262 | return vrouter_conf_objs[0] |
| 1263 | for gvc in vrouter_conf_objs: |
| 1264 | if gvc.name == gvc_name: |
| 1265 | return gvc |
| 1266 | except Exception as e: |
| 1267 | print("Error: {}".format(e)) |
| 1268 | pass |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1269 | |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1270 | return None |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1271 | |
| 1272 | |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1273 | def linklocal_service_list(global_vrouter_config_name=None, **kwargs): |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1274 | ''' |
| 1275 | Return a list of all Contrail link local services |
| 1276 | |
| 1277 | CLI Example: |
| 1278 | |
| 1279 | .. code-block:: bash |
| 1280 | |
| 1281 | salt '*' contrail.linklocal_service_list |
| 1282 | ''' |
| 1283 | ret = {} |
| 1284 | vnc_client = _auth(**kwargs) |
| 1285 | |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1286 | current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name) |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1287 | if current_config is None: |
| 1288 | return ret |
| 1289 | |
| 1290 | service_list_res = current_config.get_linklocal_services() |
| 1291 | if service_list_res is None: |
| 1292 | service_list_obj = {'linklocal_service_entry': []} |
| 1293 | else: |
| 1294 | service_list_obj = service_list_res.__dict__ |
| 1295 | for _, value in service_list_obj.iteritems(): |
| 1296 | for entry in value: |
| 1297 | service = entry.__dict__ |
| 1298 | if 'linklocal_service_name' in service: |
| 1299 | ret[service['linklocal_service_name']] = service |
| 1300 | return ret |
| 1301 | |
| 1302 | |
| 1303 | def linklocal_service_get(name, **kwargs): |
| 1304 | ''' |
| 1305 | Return a specific Contrail link local service |
| 1306 | |
| 1307 | CLI Example: |
| 1308 | |
| 1309 | .. code-block:: bash |
| 1310 | |
| 1311 | salt '*' contrail.linklocal_service_get llservice |
| 1312 | ''' |
| 1313 | ret = {} |
| 1314 | services = linklocal_service_list(**kwargs) |
| 1315 | if name in services: |
| 1316 | ret[name] = services.get(name) |
| 1317 | if len(ret) == 0: |
| 1318 | return {'Error': 'Error in retrieving link local service "{0}"'.format(name)} |
| 1319 | return ret |
| 1320 | |
| 1321 | |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1322 | def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, global_vrouter_config_name=None, **kwargs): |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1323 | ''' |
| 1324 | Create specific Contrail link local service |
| 1325 | |
| 1326 | CLI Example: |
| 1327 | |
| 1328 | .. code-block:: bash |
| 1329 | |
| 1330 | salt '*' contrail.linklocal_service_create \ |
| 1331 | llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22 |
| 1332 | salt '*' contrail.linklocal_service_create \ |
| 1333 | llservice 10.10.10.103 22 link-local.service.dns-name 22 |
| 1334 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1335 | ret = {'name': name, |
| 1336 | 'changes': {}, |
| 1337 | 'result': True, |
| 1338 | 'comment': ''} |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1339 | vnc_client = _auth(**kwargs) |
Pavel Svimbersky | 2fe0c27 | 2017-11-30 14:58:16 +0100 | [diff] [blame] | 1340 | current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name) |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1341 | if current_config is None: |
| 1342 | ret['result'] = False |
| 1343 | ret['comment'] = "Global Vrouter Config doesn't exist and is required for LinkLocalSevices" |
| 1344 | return ret |
| 1345 | |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1346 | service_entry = LinklocalServiceEntryType( |
| 1347 | linklocal_service_name=name, |
| 1348 | linklocal_service_ip=lls_ip, |
| 1349 | linklocal_service_port=lls_port, |
| 1350 | ip_fabric_service_port=ipf_port) |
| 1351 | if isinstance(ipf_dns_or_ip, basestring): |
| 1352 | service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip |
| 1353 | elif isinstance(ipf_dns_or_ip, list): |
| 1354 | service_entry.ip_fabric_service_ip = ipf_dns_or_ip |
| 1355 | service_entry.ip_fabric_DNS_service_name = '' |
| 1356 | |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1357 | _current_service_list = current_config.get_linklocal_services() |
| 1358 | if _current_service_list is None: |
| 1359 | service_list = {'linklocal_service_entry': []} |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1360 | else: |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1361 | service_list = _current_service_list.__dict__ |
| 1362 | new_services = [service_entry] |
| 1363 | value = service_list.get('linklocal_service_entry') |
| 1364 | for _entry in value: |
| 1365 | entry = _entry.__dict__ |
| 1366 | if 'linklocal_service_name' in entry: |
| 1367 | if entry['linklocal_service_name'] == name: |
| 1368 | ret['comment'] = 'Link local service ' + name + ' already exists' |
| 1369 | return ret |
| 1370 | new_services.append(_entry) |
| 1371 | if __opts__['test']: |
| 1372 | ret['result'] = None |
| 1373 | ret['comment'] = "LinkLocalSevices " + name + " will be created" |
| 1374 | service_list['linklocal_service_entry'] = new_services |
| 1375 | new_config = GlobalVrouterConfig(linklocal_services=service_list) |
| 1376 | if __opts__['test']: |
| 1377 | ret['result'] = None |
| 1378 | ret['comment'] = "LinkLocalSevices " + name + " will be updated" |
| 1379 | else: |
| 1380 | vnc_client.global_vrouter_config_update(new_config) |
| 1381 | ret['comment'] = "LinkLocalSevices " + name + " has been created" |
| 1382 | ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}} |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1383 | return ret |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1384 | |
| 1385 | |
| 1386 | def linklocal_service_delete(name, **kwargs): |
| 1387 | ''' |
| 1388 | Delete specific link local service entry |
| 1389 | |
| 1390 | CLI Example: |
| 1391 | |
| 1392 | .. code-block:: bash |
| 1393 | |
| 1394 | salt '*' contrail.linklocal_service_delete llservice |
| 1395 | ''' |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1396 | ret = {'name': name, |
| 1397 | 'changes': {}, |
| 1398 | 'result': True, |
| 1399 | 'comment': ''} |
| 1400 | lls = linklocal_service_get(name) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1401 | if name in lls: |
| 1402 | if __opts__['test']: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1403 | ret['result'] = None |
| 1404 | ret['comment'] = "Link local service " + name + " will be deleted" |
| 1405 | return ret |
| 1406 | else: |
| 1407 | return ret |
| 1408 | |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1409 | vnc_client = _auth(**kwargs) |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1410 | current_config = _get_vrouter_config(vnc_client) |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1411 | if current_config is not None: |
| 1412 | _current_service_list = current_config.get_linklocal_services() |
| 1413 | if _current_service_list is None: |
| 1414 | service_list = {'linklocal_service_entry': []} |
| 1415 | else: |
| 1416 | service_list = _current_service_list.__dict__ |
| 1417 | new_services = [] |
| 1418 | for key, value in service_list.iteritems(): |
| 1419 | if key != 'linklocal_service_entry': |
| 1420 | continue |
| 1421 | for _entry in value: |
| 1422 | entry = _entry.__dict__ |
| 1423 | if 'linklocal_service_name' in entry: |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1424 | if entry['linklocal_service_name'] != name: |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 1425 | new_services.append(_entry) |
| 1426 | service_list[key] = new_services |
| 1427 | new_config = GlobalVrouterConfig(linklocal_services=service_list) |
| 1428 | vnc_client.global_vrouter_config_update(new_config) |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1429 | ret['comment'] = "Link local service " + name + " will be deleted" |
Svimba | b39c1bb | 2019-01-30 17:24:37 +0100 | [diff] [blame] | 1430 | ret['changes'] = {'LinkLocalService': {'old': name, 'new': ''}} |
| 1431 | else: |
| 1432 | ret['result'] = False |
| 1433 | ret['comment'] = "Global Vrouter Config doesn't exist and is required for LinkLocalSevices" |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1434 | return ret |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1435 | |
| 1436 | |
| 1437 | def virtual_machine_interface_list(**kwargs): |
| 1438 | ''' |
| 1439 | Return a list of all Contrail virtual machine interfaces |
| 1440 | |
| 1441 | CLI Example: |
| 1442 | |
| 1443 | .. code-block:: bash |
| 1444 | |
| 1445 | salt '*' contrail.virtual_machine_interfaces |
| 1446 | ''' |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1447 | ret = [] |
| 1448 | vnc_client = _auth(**kwargs) |
| 1449 | project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin')) |
| 1450 | project_uuid = project.get_uuid() |
| 1451 | |
| 1452 | vm_ifaces = vnc_client.virtual_machine_interfaces_list( |
| 1453 | detail=True, parent_id=project_uuid) |
| 1454 | |
| 1455 | for vm_iface in vm_ifaces: |
| 1456 | ret.append(vm_iface.__dict__) |
| 1457 | |
| 1458 | return ret |
| 1459 | |
| 1460 | |
| 1461 | def virtual_machine_interface_create(name, |
| 1462 | virtual_network, |
| 1463 | mac_address=None, |
| 1464 | ip_address=None, |
| 1465 | security_group=None, |
| 1466 | **kwargs): |
| 1467 | ''' |
| 1468 | Create specific Contrail virtual machine interface (Port) |
| 1469 | |
| 1470 | CLI Example: |
| 1471 | |
| 1472 | .. code-block:: bash |
| 1473 | |
| 1474 | salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06' |
| 1475 | router_types: |
| 1476 | - tor-agent |
| 1477 | - tor-service-node |
| 1478 | - embedded |
| 1479 | ''' |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1480 | ret = {} |
| 1481 | vnc_client = _auth(**kwargs) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1482 | project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin')) |
| 1483 | |
| 1484 | vm_int = VirtualMachineInterface(name, parent_obj=project) |
| 1485 | |
| 1486 | if mac_address: |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1487 | mac_address_obj = MacAddressesType([mac_address]) |
| 1488 | vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1489 | |
| 1490 | if security_group: |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1491 | sgo = vnc_client.security_group_read(fq_name=_get_fq_name( |
Pavel Svimbersky | 4358c35 | 2017-09-27 13:23:46 +0200 | [diff] [blame] | 1492 | vnc_client, security_group, kwargs.get('tenant', 'admin'))) |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1493 | vm_int.set_security_group(sgo) |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1494 | |
| 1495 | vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid'] |
| 1496 | vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid) |
| 1497 | vm_int.set_virtual_network(vnet_obj) |
| 1498 | |
| 1499 | vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int) |
| 1500 | vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid) |
| 1501 | |
| 1502 | vm_int.set_port_security_enabled(False) |
| 1503 | vnc_client.virtual_machine_interface_update(vm_int) |
| 1504 | |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1505 | # Allocate IP to VMI |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1506 | ip = vnc_api.InstanceIp(name + '.ip') |
| 1507 | ip.set_virtual_machine_interface(vmi) |
| 1508 | ip.set_virtual_network(vnet_obj) |
| 1509 | |
| 1510 | ip_uuid = vnc_client.instance_ip_create(ip) |
| 1511 | |
| 1512 | if ip_address: |
| 1513 | ip.set_instance_ip_address(ip_address) |
| 1514 | vnc_client.instance_ip_update(ip) |
| 1515 | |
| 1516 | return vmi.__dict__ |
Pavel Svimbersky | 483a19e | 2017-08-22 09:50:29 +0200 | [diff] [blame] | 1517 | |
| 1518 | |
| 1519 | def virtual_network_list(**kwargs): |
| 1520 | ''' |
| 1521 | Return a list of all Contrail virtual network |
| 1522 | |
| 1523 | CLI Example: |
| 1524 | |
| 1525 | .. code-block:: bash |
| 1526 | |
| 1527 | salt '*' contrail.virtual_network |
| 1528 | ''' |
| 1529 | |
| 1530 | ret = {} |
| 1531 | vnc_client = _auth(**kwargs) |
| 1532 | virtual_networks = vnc_client._objects_list('virtual-network', detail=True) |
| 1533 | for virtual_network in virtual_networks: |
| 1534 | ret[virtual_network.name] = virtual_network.__dict__ |
| 1535 | return ret |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1536 | |
| 1537 | |
| 1538 | def virtual_network_get(name, **kwargs): |
| 1539 | ''' |
| 1540 | Return a specific Contrail virtual network |
| 1541 | |
| 1542 | CLI Example: |
| 1543 | |
| 1544 | .. code-block:: bash |
| 1545 | |
| 1546 | salt '*' contrail.virtual_network_get net01 |
| 1547 | ''' |
| 1548 | ret = {} |
| 1549 | vnet_objs = virtual_network_list(**kwargs) |
| 1550 | if name in vnet_objs: |
| 1551 | ret[name] = vnet_objs.get(name) |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1552 | if len(ret) != 1: |
Vasyl Saienko | b10b720 | 2017-09-05 14:19:03 +0300 | [diff] [blame] | 1553 | return {'result': False, |
| 1554 | 'Error': 'Error in retrieving virtual networks.'} |
| 1555 | return ret |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1556 | |
| 1557 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1558 | def virtual_network_create(name, domain='default-domain', project='admin', |
| 1559 | ipam_domain='default-domain', |
| 1560 | ipam_project='default-project', |
| 1561 | ipam_name='default-network-ipam', |
| 1562 | router_external=None, route_target_list=None, |
| 1563 | subnet_conf=None, vntype_conf=None, **kwargs): |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1564 | ''' |
| 1565 | Create Contrail virtual network |
| 1566 | CLI Example: |
| 1567 | .. code-block:: bash |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1568 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1569 | salt -C 'I@opencontrail:control:role:primary' |
| 1570 | contrail.virtual_network_create public router_external=True |
| 1571 | route_target_list=["target:64512:10000"] |
| 1572 | subnet_conf=["ip_prefix":"172.16.111.0","ip_prefix_len":24] |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1573 | |
| 1574 | Parameters: |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1575 | name (str): name of the network (required) |
| 1576 | domain (str): name of domain (optional) |
| 1577 | project (str): name of project (optional) |
| 1578 | ipam_domain (str): domain for ipam (optional) |
| 1579 | ipam_project (str): project for ipam (optional) |
| 1580 | ipam_name (str): name of IP Address Management (optional) |
| 1581 | router_external (bool): When true, this virtual network is openstack router |
| 1582 | external network. (optional) |
| 1583 | route_target_list (list): route target list - format is |
| 1584 | ['target:<asn>:<target>'] |
| 1585 | subnet_conf (dict): |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1586 | ip_prefix (string) optional - format is xxx.xxx.xxx.xxx |
| 1587 | ip_prefix_len (int) optional - format is xx |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1588 | vntype_conf (dict): |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1589 | allow_transit (boolean) optional - enable allow transit |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1590 | vxlan_network_identifier (int) - VxLAN VNI value for network |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1591 | forwarding_mode (any of ['l2_l3','l2','l3']) optional |
| 1592 | - packet forwarding mode for this virtual network |
| 1593 | rpf (any of ['enabled','disabled']) optional |
| 1594 | - Enable or disable Reverse Path Forwarding check |
| 1595 | for this network |
| 1596 | mirror_destination (boolean) optional |
| 1597 | - Mark the vn as mirror destination network |
| 1598 | ''' |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1599 | ret = {'name': name, |
| 1600 | 'changes': {}, |
| 1601 | 'result': True, |
| 1602 | 'comment': ''} |
| 1603 | |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1604 | vnc_client = _auth(**kwargs) |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1605 | fq_name = [domain, project, name] |
| 1606 | prj_obj = vnc_client.project_read(fq_name=[domain, |
| 1607 | project]) |
| 1608 | ipam_obj = vnc_client.network_ipam_read(fq_name=[ipam_domain, |
| 1609 | ipam_project, |
| 1610 | ipam_name]) |
| 1611 | if route_target_list: |
| 1612 | route_target_list = RouteTargetList(route_target_list) |
| 1613 | if subnet_conf: |
| 1614 | if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf: |
| 1615 | ipam_sn = IpamSubnetType(subnet=SubnetType( |
| 1616 | ip_prefix=subnet_conf['ip_prefix'], |
| 1617 | ip_prefix_len=subnet_conf['ip_prefix_len'])) |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1618 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1619 | # Check if the network exists |
| 1620 | vn_obj_list = vnc_client.virtual_networks_list(parent_id=prj_obj.uuid) |
| 1621 | if name in list(map(lambda x: x['fq_name'][2], |
| 1622 | vn_obj_list['virtual-networks'])): |
| 1623 | changes = {} |
| 1624 | vn_obj = vnc_client.virtual_network_read(fq_name=fq_name) |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1625 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1626 | # Update IPAM properties |
| 1627 | ipam_refs = vn_obj.get_network_ipam_refs() |
| 1628 | subnet_detected = False |
| 1629 | if subnet_conf and ipam_refs: |
| 1630 | for ipam in ipam_refs: |
| 1631 | if ipam_obj.get_uuid() == ipam['uuid']: |
| 1632 | ipam_subnet_list = ipam['attr'].get_ipam_subnets() |
| 1633 | for ipam_subnet_type in ipam_subnet_list: |
| 1634 | subnet_obj = ipam_subnet_type.get_subnet() |
| 1635 | ip_prefix = subnet_obj.get_ip_prefix() |
| 1636 | ip_prefix_len = subnet_obj.get_ip_prefix_len() |
| 1637 | if subnet_conf['ip_prefix'] == ip_prefix and \ |
| 1638 | subnet_conf['ip_prefix_len'] == ip_prefix_len: |
| 1639 | subnet_detected = True |
| 1640 | if subnet_conf and not subnet_detected: |
| 1641 | changes['ipam_subnet'] = \ |
| 1642 | {'added': '{0}/{1}'.format(subnet_conf['ip_prefix'], |
| 1643 | subnet_conf['ip_prefix_len'])} |
| 1644 | vn_obj.add_network_ipam(ipam_obj, |
| 1645 | VnSubnetsType(ipam_subnets=[ipam_sn])) |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1646 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1647 | # Update VirtualNetwork properties |
| 1648 | external = vn_obj.get_router_external() |
| 1649 | if router_external is not None and router_external != external: |
| 1650 | changes['router_external'] = {'from': external, |
| 1651 | 'to': router_external} |
| 1652 | vn_obj.set_router_external(router_external) |
| 1653 | if route_target_list: |
| 1654 | changes['router_list'] = \ |
| 1655 | {'from': str(vn_obj.get_route_target_list()), |
| 1656 | 'to': str(route_target_list)} |
| 1657 | vn_obj.set_route_target_list(route_target_list) |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1658 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1659 | # Update VirtualNetworkType properties |
| 1660 | if vntype_conf: |
| 1661 | vn_type_obj = vn_obj.get_virtual_network_properties() |
| 1662 | if vn_type_obj is None: |
| 1663 | vn_type_obj = VirtualNetworkType() |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1664 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1665 | if 'allow_transit' in vntype_conf: |
| 1666 | allow_transit_attr = vn_type_obj.get_allow_transit() |
| 1667 | if vntype_conf['allow_transit'] != allow_transit_attr: |
| 1668 | changes['allow_transit'] = \ |
| 1669 | {'from': allow_transit_attr, |
| 1670 | 'to': vntype_conf['allow_transit']} |
| 1671 | vn_type_obj.set_allow_transit(vntype_conf['allow_transit']) |
| 1672 | if 'vxlan_network_identifier' in vntype_conf: |
| 1673 | vxlan_net_id = vn_type_obj.get_vxlan_network_identifier() |
| 1674 | if vntype_conf['vxlan_network_identifier'] != vxlan_net_id: |
| 1675 | vn_type_obj.set_vxlan_network_identifier( |
| 1676 | vntype_conf['vxlan_network_identifier']) |
| 1677 | changes['vxlan_network_identifier'] = \ |
| 1678 | {'from': vxlan_net_id, |
| 1679 | 'to': vntype_conf['vxlan_network_identifier']} |
| 1680 | if 'forwarding_mode' in vntype_conf: |
| 1681 | forwarding_mode_attr = vn_type_obj.get_forwarding_mode() |
| 1682 | if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3'] and \ |
| 1683 | vntype_conf['forwarding_mode'] != forwarding_mode_attr: |
| 1684 | changes['forwarding_mode'] = \ |
| 1685 | {'from': forwarding_mode_attr, |
| 1686 | 'to': vntype_conf['forwarding_mode']} |
| 1687 | vn_type_obj.set_forwarding_mode( |
| 1688 | vntype_conf['forwarding_mode']) |
| 1689 | if 'mirror_destination' in vntype_conf: |
| 1690 | mirror_dst_attr = vn_type_obj.get_mirror_destination() |
| 1691 | if vntype_conf['mirror_destination'] != mirror_dst_attr: |
| 1692 | changes['mirror_destination'] = \ |
| 1693 | {'from': mirror_dst_attr, |
| 1694 | 'to': vntype_conf['mirror_destination']} |
| 1695 | vn_type_obj.set_mirror_destination( |
| 1696 | vntype_conf['mirror_destination']) |
| 1697 | if 'rpf' in vntype_conf: |
| 1698 | rpf_attr = vn_type_obj.get_rpf() |
| 1699 | if vntype_conf['rpf'] != rpf_attr: |
| 1700 | changes['rpf'] = \ |
| 1701 | {'from': rpf_attr, |
| 1702 | 'to': vntype_conf['rpf']} |
| 1703 | vn_type_obj.set_rpf(vntype_conf['rpf']) |
| 1704 | vn_obj.set_virtual_network_properties(vn_type_obj) |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1705 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1706 | if changes: |
| 1707 | ret['changes'] = changes |
| 1708 | if __opts__['test']: |
| 1709 | ret['result'] = None |
| 1710 | ret['comment'] = "Virtual network " + name + " will be updated" |
| 1711 | else: |
| 1712 | ret['comment'] = "VirtualNetwork " + name + " has been updated" |
| 1713 | vnc_client.virtual_network_update(vn_obj) |
| 1714 | else: |
| 1715 | ret['comment'] = 'Virtual network ' + name + \ |
| 1716 | ' already exists and is updated' |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1717 | else: |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1718 | vn_obj = VirtualNetwork(name, prj_obj) |
Jan Cach | b309272 | 2018-01-31 12:46:16 +0100 | [diff] [blame] | 1719 | |
ibumarskov | 6ec99c4 | 2019-02-20 11:44:22 +0400 | [diff] [blame] | 1720 | # Configure IPAM properties |
| 1721 | if subnet_conf: |
| 1722 | if 'ip_prefix' in subnet_conf and 'ip_prefix_len' in subnet_conf: |
| 1723 | vn_obj.add_network_ipam(ipam_obj, |
| 1724 | VnSubnetsType(ipam_subnets=[ipam_sn])) |
| 1725 | |
| 1726 | # Configure VirtualNetwork properties |
| 1727 | if router_external is not None: |
| 1728 | vn_obj.set_router_external(router_external) |
| 1729 | if route_target_list is not None: |
| 1730 | vn_obj.set_route_target_list(route_target_list) |
| 1731 | |
| 1732 | # Configure VirtualNetworkType properties |
| 1733 | if vntype_conf: |
| 1734 | vn_type_obj = VirtualNetworkType() |
| 1735 | if 'allow_transit' in vntype_conf: |
| 1736 | vn_type_obj.set_allow_transit(vntype_conf['allow_transit']) |
| 1737 | if 'vxlan_network_identifier' in vntype_conf: |
| 1738 | vn_type_obj.set_vxlan_network_identifier( |
| 1739 | vntype_conf['vxlan_network_identifier']) |
| 1740 | if 'forwarding_mode' in vntype_conf: |
| 1741 | if vntype_conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3']: |
| 1742 | vn_type_obj.set_forwarding_mode( |
| 1743 | vntype_conf['forwarding_mode']) |
| 1744 | if 'mirror_destination' in vntype_conf: |
| 1745 | vn_type_obj.set_mirror_destination(vntype_conf['mirror_destination']) |
| 1746 | if 'rpf' in vntype_conf: |
| 1747 | vn_type_obj.set_rpf(vntype_conf['rpf']) |
| 1748 | vn_obj.set_virtual_network_properties(vn_type_obj) |
| 1749 | |
| 1750 | if __opts__['test']: |
| 1751 | ret['result'] = None |
| 1752 | ret['comment'] = "VirtualNetwork " + name + " will be created" |
| 1753 | else: |
| 1754 | vnc_client.virtual_network_create(vn_obj) |
| 1755 | ret['comment'] = "VirtualNetwork " + name + " has been created" |
| 1756 | ret['changes'] = {'VirtualNetwork': {'created': name}} |
Jan Cach | a859e6b | 2018-01-09 17:34:18 +0100 | [diff] [blame] | 1757 | return ret |
| 1758 | |
| 1759 | |
Pavel Svimbersky | 5ba8a7b | 2017-09-21 11:07:48 +0200 | [diff] [blame] | 1760 | def service_appliance_set_list(**kwargs): |
| 1761 | ''' |
| 1762 | Return a list of Contrail service appliance set |
| 1763 | |
| 1764 | CLI Example: |
| 1765 | |
| 1766 | .. code-block:: bash |
| 1767 | |
| 1768 | salt '*' contrail.service_appliance_set_list |
| 1769 | ''' |
| 1770 | ret = {} |
| 1771 | vnc_client = _auth(**kwargs) |
| 1772 | service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True) |
| 1773 | for service_appliance_set in service_appliance_sets: |
| 1774 | ret[service_appliance_set.name] = service_appliance_set.__dict__ |
| 1775 | return ret |
| 1776 | |
| 1777 | |
| 1778 | def service_appliance_set_get(name, **kwargs): |
| 1779 | ''' |
| 1780 | Return a specific Contrail service appliance set |
| 1781 | |
| 1782 | CLI Example: |
| 1783 | |
| 1784 | .. code-block:: bash |
| 1785 | |
| 1786 | salt '*' contrail.service_appliance_set_get name |
| 1787 | ''' |
| 1788 | ret = {} |
| 1789 | sas_objs = service_appliance_set_list(**kwargs) |
| 1790 | if name in sas_objs: |
| 1791 | ret[name] = sas_objs.get(name) |
| 1792 | if len(ret) != 1: |
| 1793 | return {'result': False, |
| 1794 | 'Error': "Error in the retrieving service apliance set."} |
| 1795 | return ret |
| 1796 | |
| 1797 | |
| 1798 | def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs): |
| 1799 | ''' |
| 1800 | Create Contrail service appliance set |
| 1801 | |
| 1802 | CLI Example: |
| 1803 | |
| 1804 | .. code-block:: bash |
| 1805 | |
| 1806 | salt '*' contrail.service_appliance_set_create name |
| 1807 | ''' |
| 1808 | ret = {'name': name, |
| 1809 | 'changes': {}, |
| 1810 | 'result': True, |
| 1811 | 'comment': ''} |
| 1812 | vnc_client = _auth(**kwargs) |
| 1813 | gsc_obj = _get_config(vnc_client) |
| 1814 | sas_objs = service_appliance_set_list(**kwargs) |
| 1815 | if name in sas_objs: |
| 1816 | ret['commnet'] = 'Service appliance set ' + name + ' already exists' |
| 1817 | else: |
| 1818 | service_appliance_set_obj = ServiceApplianceSet( |
| 1819 | name, gsc_obj) |
| 1820 | if properties: |
| 1821 | pairs = KeyValuePairs() |
| 1822 | for k, v in properties.items(): |
| 1823 | pairs.add_key_value_pair(KeyValuePair(k, v)) |
| 1824 | service_appliance_set_obj.set_service_appliance_set_properties(pairs) |
| 1825 | if driver: |
| 1826 | service_appliance_set_obj.set_service_appliance_driver(driver) |
| 1827 | if ha_mode: |
| 1828 | service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode) |
| 1829 | if __opts__['test']: |
| 1830 | ret['result'] = None |
| 1831 | ret['comment'] = "ServiceApplianceSet " + name + " will be created" |
| 1832 | else: |
| 1833 | vnc_client.service_appliance_set_create(service_appliance_set_obj) |
| 1834 | ret['comment'] = "ServiceApplianceSet " + name + " has been created" |
| 1835 | ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}} |
| 1836 | return ret |
| 1837 | |
| 1838 | |
| 1839 | def service_appliance_set_delete(name, **kwargs): |
| 1840 | ''' |
| 1841 | Delete specific Contrail service appliance set |
| 1842 | |
| 1843 | CLI Example: |
| 1844 | |
| 1845 | .. code-block:: bash |
| 1846 | |
| 1847 | salt '*' contrail.service_appliance_set_delete name |
| 1848 | ''' |
| 1849 | ret = {'name': name, |
| 1850 | 'changes': {}, |
| 1851 | 'result': True, |
| 1852 | 'comment': ''} |
| 1853 | vnc_client = _auth(**kwargs) |
| 1854 | gsc_obj = _get_config(vnc_client) |
| 1855 | sas_obj = ServiceApplianceSet(name, gsc_obj) |
| 1856 | if __opts__['test']: |
| 1857 | ret['result'] = None |
| 1858 | ret['comment'] = "Service appliance set " + name + " will be deleted" |
| 1859 | else: |
| 1860 | vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name()) |
| 1861 | ret['comment'] = "ServiceApplianceSet " + name + " has been deleted" |
| 1862 | ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}} |
| 1863 | return ret |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 1864 | |
| 1865 | def global_system_config_list(**kwargs): |
| 1866 | ''' |
| 1867 | Return a list of all global system configs |
| 1868 | |
| 1869 | CLI Example: |
| 1870 | |
| 1871 | .. code-block:: bash |
| 1872 | |
| 1873 | salt '*' contrail.global_system_config_list |
| 1874 | ''' |
| 1875 | |
| 1876 | ret = {} |
| 1877 | vnc_client = _auth(**kwargs) |
| 1878 | gsysconfs = vnc_client._objects_list('global-system-config', detail=True) |
| 1879 | for gsysconf in gsysconfs: |
| 1880 | ret[gsysconf.name] = gsysconf.__dict__ |
| 1881 | return ret |
| 1882 | |
| 1883 | |
| 1884 | def global_system_config_get(name, **kwargs): |
| 1885 | ''' |
| 1886 | Return a specific Contrail global system config |
| 1887 | |
| 1888 | CLI Example: |
| 1889 | |
| 1890 | .. code-block:: bash |
| 1891 | |
| 1892 | salt '*' contrail.global_system_config_get name |
| 1893 | ''' |
| 1894 | ret = {} |
| 1895 | vnc_client = _auth(**kwargs) |
| 1896 | gsc_objs = vnc_client._objects_list('global-system-config', detail=True) |
| 1897 | for gsc_obj in gsc_objs: |
| 1898 | if name == gsc_obj.name: |
| 1899 | ret[name] = gsc_obj.__dict__ |
| 1900 | if len(ret) == 0: |
| 1901 | return {'Error': 'Error in retrieving global system config.'} |
| 1902 | return ret |
| 1903 | |
| 1904 | |
| 1905 | def global_system_config_create(name, ans=64512, grp=None, **kwargs): |
| 1906 | ''' |
| 1907 | Create Contrail global system config |
| 1908 | |
| 1909 | CLI Example: |
| 1910 | |
| 1911 | .. code-block:: bash |
| 1912 | |
| 1913 | salt '*' contrail.global_system_config_create name=default-global-system-config ans=64512 |
| 1914 | ''' |
| 1915 | ret = {'name': name, |
| 1916 | 'changes': {}, |
| 1917 | 'result': True, |
| 1918 | 'comment': ''} |
| 1919 | vnc_client = _auth(**kwargs) |
| 1920 | |
| 1921 | gsc_objs = global_system_config_list(**kwargs) |
| 1922 | if name in gsc_objs: |
| 1923 | config_obj = vnc_client.global_system_config_read(fq_name=[name]) |
| 1924 | if config_obj.graceful_restart_parameters and not HAS_OLD: |
| 1925 | curr_grp = str(config_obj.graceful_restart_parameters).replace(" ", "").split(",") |
| 1926 | curr_grpd = dict(item.split('=') for item in curr_grp) |
psvimbersky | 3c84e27 | 2018-01-02 10:34:29 +0100 | [diff] [blame] | 1927 | else: |
| 1928 | curr_grpd = None |
Pavel Svimbersky | dbd52ea | 2017-12-05 18:05:25 +0100 | [diff] [blame] | 1929 | |
| 1930 | if grp and 'enable' in grp and not HAS_OLD: |
| 1931 | grp_obj = GracefulRestartParametersType() |
| 1932 | if 'enable' in grp: |
| 1933 | grp_obj.enable = grp['enable'] |
| 1934 | if curr_grpd and str(grp['enable']) != str(curr_grpd['enable']): |
| 1935 | ret['changes']['enable'] = {"from": str(curr_grpd['enable']), "to": str(grp['enable'])} |
| 1936 | elif not curr_grpd: |
| 1937 | ret['changes']['enable'] = {"from": None, "to": grp['enable']} |
| 1938 | if 'restart_time' in grp: |
| 1939 | grp_obj.restart_time = grp['restart_time'] |
| 1940 | if curr_grpd and grp['restart_time'] != int(curr_grpd['restart_time']): |
| 1941 | ret['changes']['restart_time'] = {"from": int(curr_grpd['restart_time']), "to": grp['restart_time']} |
| 1942 | elif not curr_grpd: |
| 1943 | ret['changes']['restart_time'] = {"from": None, "to": grp['restart_time']} |
| 1944 | if 'end_of_rib_timeout' in grp: |
| 1945 | grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout'] |
| 1946 | if curr_grpd and grp['end_of_rib_timeout'] != int(curr_grpd['end_of_rib_timeout']): |
| 1947 | ret['changes']['end_of_rib_timeout'] = {"from": int(curr_grpd['end_of_rib_timeout']), "to": grp['end_of_rib_timeout']} |
| 1948 | elif not curr_grpd: |
| 1949 | ret['changes']['end_of_rib_timeout'] = {"from": None, "to": grp['end_of_rib_timeout']} |
| 1950 | if 'bgp_helper_enable' in grp: |
| 1951 | grp_obj.bgp_helper_enable = grp['bgp_helper_enable'] |
| 1952 | if curr_grpd and str(grp['bgp_helper_enable']) != str(curr_grpd['bgp_helper_enable']): |
| 1953 | ret['changes']['bgp_helper_enable'] = {"from": str(curr_grpd['bgp_helper_enable']), "to": grp['bgp_helper_enable']} |
| 1954 | elif not curr_grpd: |
| 1955 | ret['changes']['bgp_helper_enable'] = {"from": None, "to": grp['bgp_helper_enable']} |
| 1956 | if 'xmpp_helper_enable' in grp: |
| 1957 | grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable'] |
| 1958 | if curr_grpd and str(grp['xmpp_helper_enable']) != str(curr_grpd['xmpp_helper_enable']): |
| 1959 | ret['changes']['xmpp_helper_enable'] = {"from": str(curr_grpd['xmpp_helper_enable']), "to": grp['xmpp_helper_enable']} |
| 1960 | elif not curr_grpd: |
| 1961 | ret['changes']['xmpp_helper_enable'] = {"from": None, "to": grp['xmpp_helper_enable']} |
| 1962 | if 'long_lived_restart_time' in grp: |
| 1963 | grp_obj.long_lived_restart_time = grp['long_lived_restart_time'] |
| 1964 | if curr_grpd and grp['long_lived_restart_time'] != int(curr_grpd['long_lived_restart_time']): |
| 1965 | ret['changes']['long_lived_restart_time'] = {"from": int(curr_grpd['long_lived_restart_time']), "to": grp['long_lived_restart_time']} |
| 1966 | elif not curr_grpd: |
| 1967 | ret['changes']['long_lived_restart_time'] = {"from": None, "to": grp['long_lived_restart_time']} |
| 1968 | else: |
| 1969 | grp_obj = None |
| 1970 | |
| 1971 | config_obj.graceful_restart_parameters = grp_obj |
| 1972 | |
| 1973 | if ans: |
| 1974 | if config_obj.autonomous_system != ans: |
| 1975 | ret['changes']['autonomous_system'] = {"from": config_obj.autonomous_system, "to": ans} |
| 1976 | config_obj.autonomous_system = ans |
| 1977 | |
| 1978 | vnc_client.global_system_config_update(config_obj) |
| 1979 | ret['comment'] = 'Global system config ' + name + ' has been updated' |
| 1980 | else: |
| 1981 | config_obj = GlobalSystemConfig(name=name) |
| 1982 | if grp and not HAS_OLD: |
| 1983 | grp_obj = GracefulRestartParametersType() |
| 1984 | if 'enable' in grp: |
| 1985 | grp_obj.enable = grp['enable'] |
| 1986 | if 'restart_time' in grp: |
| 1987 | grp_obj.restart_time = grp['restart_time'] |
| 1988 | if 'end_of_rib_timeout' in grp: |
| 1989 | grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout'] |
| 1990 | if 'bgp_helper_enable' in grp: |
| 1991 | grp_obj.bgp_helper_enable = grp['bgp_helper_enable'] |
| 1992 | if 'xmpp_helper_enable' in grp: |
| 1993 | grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable'] |
| 1994 | if 'long_lived_restart_time' in grp: |
| 1995 | grp_obj.long_lived_restart_time = grp['long_lived_restart_time'] |
| 1996 | config_obj.graceful_restart_parameters = grp_obj |
| 1997 | if ans: |
| 1998 | config_obj.autonomous_system = ans |
| 1999 | |
| 2000 | vnc_client.global_system_config_create(config_obj) |
| 2001 | ret['changes'] = {"created": "new"} |
| 2002 | ret['comment'] = 'Global system config ' + name + ' has been created ' |
| 2003 | |
| 2004 | return ret |
| 2005 | |
| 2006 | |
| 2007 | def global_system_config_delete(name, **kwargs): |
| 2008 | ''' |
| 2009 | Delete specific Contrail global system config |
| 2010 | |
| 2011 | CLI Example: |
| 2012 | |
| 2013 | .. code-block:: bash |
| 2014 | |
| 2015 | salt '*' contrail.global_system_config_delete name |
| 2016 | ''' |
| 2017 | ret = {'name': name, |
| 2018 | 'changes': {}, |
| 2019 | 'result': True, |
| 2020 | 'comment': ''} |
| 2021 | vnc_client = _auth(**kwargs) |
| 2022 | |
| 2023 | gsc_obj = GlobalSystemConfig(name) |
| 2024 | if __opts__['test']: |
| 2025 | ret['result'] = None |
| 2026 | ret['comment'] = "Global system config " + name + " will be deleted" |
| 2027 | else: |
| 2028 | vnc_client.global_system_config_delete(fq_name=gsc_obj.get_fq_name()) |
| 2029 | ret['comment'] = "GlobalSystemConfig " + name + " has been deleted" |
| 2030 | ret['changes'] = {'GlobalSystemConfig': {'old': name, 'new': ''}} |
| 2031 | return ret |
Jan Cach | b309272 | 2018-01-31 12:46:16 +0100 | [diff] [blame] | 2032 | |
| 2033 | |
| 2034 | def list_floating_ip_pools(**kwargs): |
| 2035 | ''' |
| 2036 | List all floating ip pools |
| 2037 | |
| 2038 | CLI Example: |
| 2039 | .. code-block:: bash |
| 2040 | salt '*' contrail.list_floating_ip_pools |
| 2041 | ''' |
| 2042 | vnc_client = _auth(**kwargs) |
| 2043 | pools = vnc_client.floating_ip_pools_list() |
| 2044 | # list of floating ip pools objects |
| 2045 | fp_list = [] |
| 2046 | |
| 2047 | for pool in vnc_client.floating_ip_pools_list()['floating-ip-pools']: |
| 2048 | fip_obj = vnc_client.floating_ip_pool_read(pool['fq_name']) |
| 2049 | fp_list.append(fip_obj) |
| 2050 | # print given pool |
| 2051 | fip_obj.dump() |
| 2052 | |
| 2053 | def update_floating_ip_pool(vn_name, vn_project, vn_domain=None, |
| 2054 | owner_access=None, global_access=None, |
| 2055 | projects=None, **kwargs): |
| 2056 | ''' |
| 2057 | Update specific floating ip pool |
| 2058 | |
| 2059 | |
| 2060 | CLI Example |
| 2061 | .. code-block:: bash |
| 2062 | salt-call contrail.update_floating_ip_pool \ |
| 2063 | 'FLOATING-TEST' \ |
| 2064 | 'admin' \ |
| 2065 | 'default-domain' \ |
| 2066 | 7 7 \ |
| 2067 | [['pepa',4],['karel',7]] |
| 2068 | |
| 2069 | |
| 2070 | params: |
| 2071 | vn_name - name of the virtual network, which to use |
| 2072 | vn-project - project which includes virtual network |
| 2073 | vn-domain - domain wchich includes vn_project and vn_name |
| 2074 | owner_access (int) - Permission rights for owner |
| 2075 | global_access (int) - Permission rights for others |
| 2076 | projects (list) - list of ShareType(tenant_name,tennat_permissions) |
| 2077 | ''' |
| 2078 | ret = {'name': vn_name + "-default pool", |
| 2079 | 'changes': {}, |
| 2080 | 'result': True, |
| 2081 | 'comment': ''} |
| 2082 | |
| 2083 | if vn_domain is None: |
| 2084 | vn_domain = 'default-domain' |
| 2085 | fip_obj = None |
| 2086 | |
| 2087 | vnc_client = _auth(**kwargs) |
| 2088 | p_fq_name = [vn_domain, vn_project, vn_name, 'default'] |
| 2089 | fip_obj = vnc_client.floating_ip_pool_read(fq_name=p_fq_name) |
| 2090 | |
| 2091 | changes = {} |
| 2092 | # get perms from fip_obj (Floatin ip pool) |
| 2093 | perms2 = fip_obj.get_perms2() |
| 2094 | if owner_access is not None: |
| 2095 | if perms2.get_owner_access() != owner_access: |
| 2096 | changes['owner_access'] = {'old': str(perms2.get_owner_access()), |
| 2097 | 'new': str(owner_access)} |
| 2098 | perms2.set_owner_access(owner_access) |
| 2099 | |
| 2100 | if global_access is not None: |
| 2101 | if perms2.get_global_access() != global_access: |
| 2102 | changes['global_access'] = {'old': str(perms2.get_global_access()), |
| 2103 | 'new': str(global_access)} |
| 2104 | perms2.set_global_access(global_access) |
| 2105 | |
| 2106 | # list which represents the new state of perms |
| 2107 | final_list = [] |
| 2108 | if projects: |
| 2109 | for item in perms2.get_share(): |
| 2110 | for share in projects: |
| 2111 | if item.get_tenant() == share[0]: |
| 2112 | # project is in the new and old list |
| 2113 | # check is the permission number is same |
| 2114 | if item.get_tenant_access() == share[1]: |
| 2115 | # this project and permission is without change, keep it |
| 2116 | final_list.append(item) |
| 2117 | break |
| 2118 | else: |
| 2119 | # project exists but change the permission |
| 2120 | final_list.append(ShareType(tenant=share[0], |
| 2121 | tenant_access=share[1])) |
| 2122 | # show changes |
| 2123 | n = str('share-'+share[0]) |
| 2124 | old_str = "permission for project {0} was {1}" |
| 2125 | new_str = "permission for project {0} will be {1}" |
| 2126 | old = old_str.format(share[0], |
| 2127 | str(item.get_tenant_access())) |
| 2128 | |
| 2129 | new = new_str.format(share[0], str(share[1])) |
| 2130 | changes[n] = {'old': old, 'new': new} |
| 2131 | break |
| 2132 | else: |
| 2133 | rm_name = "share-" + item.get_tenant() |
| 2134 | changes[rm_name] = item.get_tenant() + " will be removed" |
| 2135 | |
| 2136 | # check for the completly new projects |
| 2137 | for item in projects: |
| 2138 | for share in final_list: |
| 2139 | if item[0] == share.get_tenant(): |
| 2140 | break |
| 2141 | else: |
| 2142 | final_list.append(ShareType(tenant=item[0], |
| 2143 | tenant_access=item[1])) |
| 2144 | name = 'share-' + str(item[0]) |
| 2145 | c_str = '{0} will be added with permissions {1}' |
| 2146 | changes[name] = c_str.format(name, item[1]) |
| 2147 | else: |
| 2148 | for item in perms2.get_share(): |
| 2149 | rm_name = "share-" + item.get_tenant() |
| 2150 | changes[rm_name] = item.get_tenant() + " will be removed" |
| 2151 | |
| 2152 | if __opts__['test']: |
| 2153 | ret['result'] = None |
| 2154 | ret['comment'] = changes |
| 2155 | |
| 2156 | return ret |
| 2157 | else: |
| 2158 | ret['comment'] = changes |
| 2159 | perms2.set_share(final_list) |
| 2160 | fip_obj.set_perms2(perms2) |
| 2161 | vnc_client.floating_ip_pool_update(fip_obj) |
| 2162 | |
| 2163 | return ret |
Sergey Matov | 16896ac | 2018-02-15 15:46:31 +0400 | [diff] [blame] | 2164 | |
| 2165 | |
| 2166 | def show_rbac_rules(api_access_list_entries): |
| 2167 | if api_access_list_entries is None: |
| 2168 | return 'Empty RBAC group!' |
| 2169 | |
| 2170 | rule_list = api_access_list_entries.get_rbac_rule() |
| 2171 | response = 'Rules (%d):' % len(rule_list) + '----------\n' |
| 2172 | for idx, rule in enumerate(rule_list): |
| 2173 | o = rule.rule_object |
| 2174 | f = rule.rule_field |
| 2175 | ps = ', '.join([p.role_name+':'+p.role_crud for p in rule.rule_perms]) |
| 2176 | o_f = "%s.%s" % (o, f) if f else o |
| 2177 | response += '%2d %-32s %s\n' % (idx, o_f, ps) |
| 2178 | return response |
| 2179 | |
| 2180 | |
| 2181 | def vnc_read_obj(vnc, obj_type, fq_name): |
| 2182 | method_name = obj_type.replace('-', '_') |
| 2183 | method = getattr(vnc, "%s_read" % (method_name)) |
| 2184 | try: |
| 2185 | return method(fq_name=fq_name) |
| 2186 | except NoIdError: |
| 2187 | print '%s %s not found!' % (obj_type, fq_name) |
| 2188 | return None |
| 2189 | |
| 2190 | |
| 2191 | def rbac_show_group(name, uuid, **kwargs): |
| 2192 | ''' |
| 2193 | Show specific RBAC group |
| 2194 | |
| 2195 | |
| 2196 | CLI Example |
| 2197 | .. code-block:: bash |
| 2198 | salt-call contrail.rbac_show_group name \ |
| 2199 | 'default-domain:default-project:default' |
| 2200 | |
| 2201 | params: |
| 2202 | name - one of pair {name, uuid} addresing to access-list |
| 2203 | uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \ |
| 2204 | including domain and project |
| 2205 | |
| 2206 | ''' |
| 2207 | vnc = _auth(**kwargs) |
| 2208 | fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':') |
| 2209 | ret = {'name': fq_name, |
| 2210 | 'changes': {}, |
| 2211 | 'result': True, |
| 2212 | 'comment': ''} |
| 2213 | rg = vnc_read_obj(vnc, 'api-access-list', fq_name) |
| 2214 | if not rg: |
| 2215 | ret['comment'] = 'No rules found' |
| 2216 | return ret |
| 2217 | |
| 2218 | ret['comment'] = show_rbac_rules(rg.get_api_access_list_entries()) |
| 2219 | return ret |
| 2220 | |
| 2221 | |
| 2222 | def rbac_create_group(uuid, **kwargs): |
| 2223 | ''' |
| 2224 | Create RBAC group |
| 2225 | |
| 2226 | CLI Example |
| 2227 | .. code-block:: bash |
| 2228 | salt-call contrail.rbac_create_group name \ |
| 2229 | 'default-domain:default-project:default' |
| 2230 | |
| 2231 | params: |
| 2232 | name - one of pair {name, uuid} addresing to access-list |
| 2233 | uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \ |
| 2234 | including domain and project |
| 2235 | |
| 2236 | ''' |
| 2237 | vnc = _auth(**kwargs) |
| 2238 | fq_name = uuid.split(':') |
| 2239 | ret = {'name': fq_name, |
| 2240 | 'changes': {}, |
| 2241 | 'result': True, |
| 2242 | 'comment': ''} |
| 2243 | if len(fq_name) != 2 and len(fq_name) != 3: |
| 2244 | ret['comment'] = 'Fully qualified name of rbac group expected' |
| 2245 | return ret |
| 2246 | |
| 2247 | name = fq_name[-1] |
| 2248 | |
| 2249 | if len(fq_name) == 2: |
| 2250 | if fq_name[0] == 'default-global-system-config': |
| 2251 | pobj = vnc.global_system_config_read(fq_name=fq_name[0:1]) |
| 2252 | else: |
| 2253 | pobj = vnc.domain_read(fq_name=fq_name[0:1]) |
| 2254 | else: |
| 2255 | pobj = vnc.project_read(fq_name=fq_name[0:2]) |
| 2256 | |
| 2257 | rentry = RbacRuleEntriesType([]) |
| 2258 | rg = ApiAccessList(name, parent_obj=pobj, api_access_list_entries=rentry) |
| 2259 | |
| 2260 | if __opts__['test']: |
| 2261 | ret['result'] = None |
| 2262 | ret['comment'] = "RBAC group " + uuid + " will be created" |
| 2263 | |
| 2264 | return ret |
| 2265 | else: |
| 2266 | vnc.api_access_list_create(rg) |
| 2267 | rg2 = vnc.api_access_list_read(fq_name=fq_name) |
| 2268 | rge = rg.get_api_access_list_entries() |
| 2269 | show_rbac_rules(rge) |
| 2270 | ret['comment'] = "RBAC group " + uuid + " has been created" |
| 2271 | |
| 2272 | return ret |
| 2273 | |
| 2274 | |
| 2275 | def rbac_delete_group(name, uuid, **kwargs): |
| 2276 | ''' |
| 2277 | Delete RBAC group |
| 2278 | |
| 2279 | CLI Example |
| 2280 | .. code-block:: bash |
| 2281 | salt-call contrail.rbac_delete_group name \ |
| 2282 | 'default-domain:default-project:default' |
| 2283 | |
| 2284 | params: |
| 2285 | name - one of pair {name, uuid} addresing to access-list |
| 2286 | uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \ |
| 2287 | including domain and project |
| 2288 | |
| 2289 | ''' |
| 2290 | vnc = _auth(**kwargs) |
| 2291 | fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':') |
| 2292 | ret = {'name': fq_name, |
| 2293 | 'changes': {}, |
| 2294 | 'result': True, |
| 2295 | 'comment': ''} |
| 2296 | if len(fq_name) != 2 and len(fq_name) != 3: |
| 2297 | ret['comment'] = 'Fully qualified name of rbac group expected' |
| 2298 | return ret |
| 2299 | name = fq_name[-1] |
| 2300 | |
| 2301 | rg = vnc_read_obj(vnc, 'api-access-list', fq_name) |
| 2302 | if not rg: |
| 2303 | ret['comment'] = 'No rules found' |
| 2304 | return ret |
| 2305 | rge = rg.get_api_access_list_entries() |
| 2306 | show_rbac_rules(rge) |
| 2307 | |
| 2308 | if __opts__['test']: |
| 2309 | ret['result'] = None |
| 2310 | ret['comment'] = "RBAC group " + uuid + " will be deleted" |
| 2311 | |
| 2312 | return ret |
| 2313 | else: |
| 2314 | vnc.api_access_list_delete(fq_name=fq_name) |
| 2315 | ret['comment'] = "RBAC group " + uuid + " has been deleted" |
| 2316 | |
| 2317 | return ret |
| 2318 | |
| 2319 | |
| 2320 | def rbac_add_rule(name, uuid, add_rule, **kwargs): |
| 2321 | ''' |
| 2322 | Add rule to specific RBAC group |
| 2323 | |
| 2324 | CLI Example |
| 2325 | .. code-block:: bash |
| 2326 | salt-call contrail.rbac_add_rule name \ |
| 2327 | 'default-domain:default-project:default' \ |
| 2328 | '* admin:CRUD' |
| 2329 | |
| 2330 | params: |
| 2331 | name - one of pair {name, uuid} addresing to access-list |
| 2332 | uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \ |
| 2333 | including domain and project |
| 2334 | rule(str) - Appropriate RBAC-based rule in format '<object, field> \ |
| 2335 | list of <role:CRUD>' to be added |
| 2336 | |
| 2337 | ''' |
| 2338 | vnc = _auth(**kwargs) |
| 2339 | fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':') |
| 2340 | rule = build_rule(add_rule) |
| 2341 | ret = {'name': fq_name, |
| 2342 | 'changes': {}, |
| 2343 | 'result': True, |
| 2344 | 'comment': ''} |
| 2345 | if rule is None: |
| 2346 | ret['comment'] = 'A rule string must be specified for this operation' |
| 2347 | return ret |
| 2348 | |
| 2349 | # rbac rule entry consists of one or more rules |
| 2350 | rg = vnc_read_obj(vnc, 'api-access-list', fq_name) |
| 2351 | if not rg: |
| 2352 | ret['comment'] = 'No rules found' |
| 2353 | return ret |
| 2354 | |
| 2355 | rge = rg.get_api_access_list_entries() |
| 2356 | if rge is None: |
| 2357 | rge = RbacRuleEntriesType([]) |
| 2358 | show_rbac_rules(rge) |
| 2359 | |
| 2360 | # avoid duplicates |
| 2361 | match = find_rule(rge, rule) |
| 2362 | if not match: |
| 2363 | rge.add_rbac_rule(rule) |
| 2364 | else: |
| 2365 | build_perms(rge.rbac_rule[match[0]-1], match[3]) |
| 2366 | |
| 2367 | show_rbac_rules(rge) |
| 2368 | |
| 2369 | if __opts__['test']: |
| 2370 | ret['result'] = None |
| 2371 | ret['comment'] = "Rule " + add_rule |
| 2372 | ret['comment'] += " will be created for RBAC group " + uuid |
| 2373 | |
| 2374 | return ret |
| 2375 | else: |
| 2376 | rg.set_api_access_list_entries(rge) |
| 2377 | vnc.api_access_list_update(rg) |
| 2378 | ret['comment'] = "Rule " + add_rule |
| 2379 | ret['comment'] += " has been added for RBAC group " + uuid |
| 2380 | |
| 2381 | return ret |
| 2382 | |
| 2383 | |
| 2384 | def rbac_delete_rule(name, uuid, del_rule, **kwargs): |
| 2385 | ''' |
| 2386 | Delete rule to specific RBAC group |
| 2387 | |
| 2388 | CLI Example |
| 2389 | .. code-block:: bash |
| 2390 | salt-call contrail.rbac_delete_rule name \ |
| 2391 | 'default-domain:default-project:default' \ |
| 2392 | '* admin:CRUD' |
| 2393 | |
| 2394 | params: |
| 2395 | name - one of pair {name, uuid} addresing to access-list |
| 2396 | uuid(str) - UUID in case of "uuid" specified OR full RBAC group name \ |
| 2397 | including domain and project |
| 2398 | rule(str) - Appropriate RBAC-based rule in format '<object, field> \ |
| 2399 | list of <role:CRUD>' to be deleted |
| 2400 | |
| 2401 | ''' |
| 2402 | vnc = _auth(**kwargs) |
| 2403 | fq_name = vnc.id_to_fq_name(uuid) if name == 'uuid' else uuid.split(':') |
| 2404 | rg = vnc_read_obj(vnc, 'api-access-list', fq_name) |
| 2405 | ret = {'name': fq_name, |
| 2406 | 'changes': {}, |
| 2407 | 'result': True, |
| 2408 | 'comment': ''} |
| 2409 | if not rg: |
| 2410 | ret['comment'] = 'No rules found' |
| 2411 | return ret |
| 2412 | rge = rg.get_api_access_list_entries() |
| 2413 | show_rbac_rules(rge) |
| 2414 | |
| 2415 | del_idx = re.match("^[0-9]+$", del_rule) |
| 2416 | if del_idx: |
| 2417 | del_idx = int(del_idx.group()) |
| 2418 | rc = len(rge.rbac_rule) |
| 2419 | if del_idx > rc or del_idx < 1: |
| 2420 | ret['comment'] = 'Invalid rule index to delete.' |
| 2421 | ret['comment'] += 'Value must be 1-%d' % rc |
| 2422 | return ret |
| 2423 | match = (del_idx, True) |
| 2424 | else: |
| 2425 | rule = build_rule(del_rule) |
| 2426 | match = find_rule(rge, rule) |
| 2427 | |
| 2428 | if not match: |
| 2429 | ret['comment'] = "Rule not found. Unchanged" |
| 2430 | return ret |
| 2431 | elif match[1]: |
| 2432 | rge.rbac_rule.pop(match[0]-1) |
| 2433 | else: |
| 2434 | build_perms(rge.rbac_rule[match[0]-1], match[2]) |
| 2435 | show_rbac_rules(rge) |
| 2436 | |
| 2437 | if __opts__['test']: |
| 2438 | ret['result'] = None |
| 2439 | ret['comment'] = "Rule " + del_rule |
| 2440 | ret['comment'] += " will be cleared from RBAC group " + uuid |
| 2441 | |
| 2442 | return ret |
| 2443 | else: |
| 2444 | rg.set_api_access_list_entries(rge) |
| 2445 | vnc.api_access_list_update(rg) |
| 2446 | ret['comment'] = "Rule " + del_rule |
| 2447 | ret['comment'] += " has been cleared from RBAC group " + uuid |
| 2448 | |
| 2449 | return ret |