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