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