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