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