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