| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: azure_rm_expressroute |
| 12 |
version_added: "1.7.0" |
| 13 |
short_description: Manage Express Route Circuits |
| 14 |
description: |
| 15 |
- Create, update and delete instance of Express Route. |
| 16 |
options: |
| 17 |
resource_group: |
| 18 |
description: |
| 19 |
- Name of the resource group to which the resource belongs. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Unique name of the app service plan to create or update. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
location: |
| 28 |
description: |
| 29 |
- Resource location. If not set, location from the resource group will be used as default. |
| 30 |
type: str |
| 31 |
service_provider_properties: |
| 32 |
type: dict |
| 33 |
description: |
| 34 |
- The service Provider properties |
| 35 |
suboptions: |
| 36 |
peering_location: |
| 37 |
description: |
| 38 |
- The peering location |
| 39 |
type: str |
| 40 |
bandwidth_in_mbps: |
| 41 |
description: |
| 42 |
- The bandwidth of the circuit when the circuit is provisioned on an ExpressRoutePort resource. |
| 43 |
type: str |
| 44 |
service_provider_name: |
| 45 |
description: |
| 46 |
- Name of service provider |
| 47 |
type: str |
| 48 |
sku: |
| 49 |
description: |
| 50 |
- The name of the SKU. |
| 51 |
- Please see L(https://azure.microsoft.com/en-in/pricing/details/expressroute/,) |
| 52 |
- Required sku when I(state=present). |
| 53 |
type: dict |
| 54 |
suboptions: |
| 55 |
tier: |
| 56 |
description: |
| 57 |
- The tier of the SKU |
| 58 |
type: str |
| 59 |
required: true |
| 60 |
choices: |
| 61 |
- standard |
| 62 |
- premium |
| 63 |
family: |
| 64 |
description: |
| 65 |
- the family of the SKU |
| 66 |
type: str |
| 67 |
required: true |
| 68 |
choices: |
| 69 |
- metereddata |
| 70 |
- unlimiteddata |
| 71 |
global_reach_enabled: |
| 72 |
description: |
| 73 |
- Flag denoting global reach status. |
| 74 |
type: bool |
| 75 |
authorizations: |
| 76 |
description: |
| 77 |
- The list of authorizations. |
| 78 |
type: list |
| 79 |
elements: dict |
| 80 |
suboptions: |
| 81 |
name: |
| 82 |
description: Name of the authorization. |
| 83 |
required: true |
| 84 |
type: str |
| 85 |
allow_classic_operations: |
| 86 |
description: |
| 87 |
- Support for classic operations. |
| 88 |
type: bool |
| 89 |
state: |
| 90 |
description: |
| 91 |
- Assert the state of the express route. |
| 92 |
- Use C(present) to create or update an express route and C(absent) to delete it. |
| 93 |
type: str |
| 94 |
default: present |
| 95 |
choices: |
| 96 |
- absent |
| 97 |
- present |
| 98 |
extends_documentation_fragment: |
| 99 |
- azure.azcollection.azure |
| 100 |
- azure.azcollection.azure_tags |
| 101 |
|
| 102 |
author: |
| 103 |
- Praveen Ghuge (@praveenghuge) |
| 104 |
- Karl Dasan (@ikarldasan) |
| 105 |
- Sakar Mehra (@sakar97) |
| 106 |
''' |
| 107 |
EXAMPLES = ''' |
| 108 |
- name: "Create Express route" |
| 109 |
azure_rm_expressroute: |
| 110 |
resource_group: rg |
| 111 |
location: eastus |
| 112 |
name: exp |
| 113 |
allow_classic_operations: true |
| 114 |
global_reach_enabled: false |
| 115 |
tags: |
| 116 |
- a: b |
| 117 |
authorizations: |
| 118 |
- name: authorization_test |
| 119 |
service_provider_properties: |
| 120 |
service_provider_name: Aryaka Networks |
| 121 |
peering_location: Seattle |
| 122 |
bandwidth_in_mbps: '200' |
| 123 |
sku: |
| 124 |
tier: premium |
| 125 |
family: metereddata |
| 126 |
|
| 127 |
- name: Delete Express route |
| 128 |
azure_rm_expressroute: |
| 129 |
resource_group: rg |
| 130 |
name: exp |
| 131 |
state: absent |
| 132 |
''' |
| 133 |
|
| 134 |
RETURN = ''' |
| 135 |
state: |
| 136 |
description: |
| 137 |
- Current state of the express route. |
| 138 |
returned: always |
| 139 |
type: dict |
| 140 |
|
| 141 |
sample: { |
| 142 |
"additional_properties": {}, |
| 143 |
"allow_classic_operations": true, |
| 144 |
"authorizations": [ |
| 145 |
{ |
| 146 |
"authorization_key": "d83e18b5-0200-4e0b-9cdb-6fdf95b00267", |
| 147 |
"authorization_use_status": "Available", |
| 148 |
"etag": "W/'09572845-c667-410c-b664-ed8e39242c13'", |
| 149 |
"id": "/subscriptions/subs_id/resourceGroups/rg/providers/Microsoft.Network/expressRouteCircuits/exp/authorizations/az", |
| 150 |
"name": "authorization_test", |
| 151 |
"provisioning_state": "Succeeded", |
| 152 |
"type": "Microsoft.Network/expressRouteCircuits/authorizations" |
| 153 |
} |
| 154 |
], |
| 155 |
"bandwidth_in_gbps": null, |
| 156 |
"circuit_provisioning_state": "Enabled", |
| 157 |
"express_route_port": null, |
| 158 |
"gateway_manager_etag": "", |
| 159 |
"global_reach_enabled": false, |
| 160 |
"id": "/subscriptions/subs_id/resourceGroups/rg/providers/Microsoft.Network/expressRouteCircuits/exp", |
| 161 |
"location": "eastus", |
| 162 |
"name": "exp", |
| 163 |
"peerings": [], |
| 164 |
"provisioning_state": "Succeeded", |
| 165 |
"service_key": "e1956383-63b6-4709-8baa-3615bbf5d22b", |
| 166 |
"service_provider_notes": null, |
| 167 |
"service_provider_provisioning_state": "NotProvisioned", |
| 168 |
"stag": 27, |
| 169 |
"status": "Deleted", |
| 170 |
"tags": { |
| 171 |
"a": "b" |
| 172 |
}, |
| 173 |
"type": "Microsoft.Network/expressRouteCircuits" |
| 174 |
} |
| 175 |
''' |
| 176 |
|
| 177 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 178 |
|
| 179 |
try: |
| 180 |
from azure.core.exceptions import ResourceNotFoundError |
| 181 |
except ImportError: |
| 182 |
|
| 183 |
pass |
| 184 |
|
| 185 |
|
| 186 |
class AzureExpressRoute(AzureRMModuleBase): |
| 187 |
|
| 188 |
def __init__(self): |
| 189 |
|
| 190 |
|
| 191 |
self.service_provider_properties_spec = dict( |
| 192 |
service_provider_name=dict(type='str'), |
| 193 |
peering_location=dict(type='str'), |
| 194 |
bandwidth_in_mbps=dict(type='str') |
| 195 |
) |
| 196 |
|
| 197 |
self.sku_spec = dict( |
| 198 |
tier=dict(type='str', choices=[ |
| 199 |
'standard', 'premium'], required=True), |
| 200 |
family=dict(type='str', choices=[ |
| 201 |
'unlimiteddata', 'metereddata'], required=True) |
| 202 |
) |
| 203 |
|
| 204 |
self.authorizations_spec = dict( |
| 205 |
name=dict(type='str', required=True) |
| 206 |
) |
| 207 |
|
| 208 |
self.module_arg_spec = dict( |
| 209 |
resource_group=dict(type='str', required=True), |
| 210 |
name=dict(type='str', required=True), |
| 211 |
location=dict(type='str'), |
| 212 |
sku=dict(type='dict', options=self.sku_spec), |
| 213 |
allow_classic_operations=dict(type='bool'), |
| 214 |
authorizations=dict(type='list', options=self.authorizations_spec, elements='dict'), |
| 215 |
state=dict(choices=['present', 'absent'], |
| 216 |
default='present', type='str'), |
| 217 |
service_provider_properties=dict( |
| 218 |
type='dict', options=self.service_provider_properties_spec), |
| 219 |
global_reach_enabled=dict(type='bool'), |
| 220 |
) |
| 221 |
|
| 222 |
self.resource_group = None |
| 223 |
self.name = None |
| 224 |
self.location = None |
| 225 |
self.allow_classic_operations = None |
| 226 |
self.authorizations = None |
| 227 |
self.service_provider_properties = None |
| 228 |
self.global_reach_enabled = None |
| 229 |
self.sku = None |
| 230 |
self.tags = None |
| 231 |
self.state = None |
| 232 |
self.results = dict( |
| 233 |
changed=False, |
| 234 |
state=dict() |
| 235 |
) |
| 236 |
|
| 237 |
super(AzureExpressRoute, self).__init__(self.module_arg_spec, |
| 238 |
supports_check_mode=True, |
| 239 |
supports_tags=True) |
| 240 |
|
| 241 |
def exec_module(self, **kwargs): |
| 242 |
|
| 243 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 244 |
setattr(self, key, kwargs[key]) |
| 245 |
|
| 246 |
self.results['check_mode'] = self.check_mode |
| 247 |
|
| 248 |
|
| 249 |
resource_group = self.get_resource_group(self.resource_group) |
| 250 |
|
| 251 |
results = dict() |
| 252 |
changed = False |
| 253 |
|
| 254 |
try: |
| 255 |
self.log('Fetching Express Route Circuits {0}'.format(self.name)) |
| 256 |
express_route_circuit = self.network_client.express_route_circuits.get( |
| 257 |
self.resource_group, self.name) |
| 258 |
|
| 259 |
results = express_route_to_dict(express_route_circuit) |
| 260 |
|
| 261 |
|
| 262 |
if self.state == 'present': |
| 263 |
changed = False |
| 264 |
|
| 265 |
update_tags, results['tags'] = self.update_tags( |
| 266 |
results['tags']) |
| 267 |
if update_tags: |
| 268 |
changed = True |
| 269 |
|
| 270 |
elif self.state == 'absent': |
| 271 |
changed = True |
| 272 |
|
| 273 |
except ResourceNotFoundError: |
| 274 |
|
| 275 |
if self.state == 'present': |
| 276 |
changed = True |
| 277 |
else: |
| 278 |
|
| 279 |
changed = False |
| 280 |
|
| 281 |
self.results['changed'] = changed |
| 282 |
self.results['state'] = results |
| 283 |
|
| 284 |
|
| 285 |
if self.check_mode: |
| 286 |
return self.results |
| 287 |
|
| 288 |
if changed: |
| 289 |
if self.state == "present": |
| 290 |
self.results['state'] = self.create_or_update_express_route( |
| 291 |
self.module.params) |
| 292 |
elif self.state == "absent": |
| 293 |
|
| 294 |
self.delete_expressroute() |
| 295 |
self.results['state']['status'] = 'Deleted' |
| 296 |
|
| 297 |
return self.results |
| 298 |
|
| 299 |
def create_or_update_express_route(self, params): |
| 300 |
''' |
| 301 |
Create or update Express route. |
| 302 |
:return: create or update Express route instance state dictionary |
| 303 |
''' |
| 304 |
self.log("create or update Express Route {0}".format(self.name)) |
| 305 |
try: |
| 306 |
params["sku"]["name"] = params.get("sku").get("tier") + "_" + params.get("sku").get("family") |
| 307 |
poller = self.network_client.express_route_circuits.begin_create_or_update( |
| 308 |
resource_group_name=params.get("resource_group"), |
| 309 |
circuit_name=params.get("name"), |
| 310 |
parameters=params) |
| 311 |
result = self.get_poller_result(poller) |
| 312 |
self.log("Response : {0}".format(result)) |
| 313 |
except Exception as ex: |
| 314 |
self.fail("Failed to create express route {0} in resource group {1}: {2}".format( |
| 315 |
self.name, self.resource_group, str(ex))) |
| 316 |
return express_route_to_dict(result) |
| 317 |
|
| 318 |
def delete_expressroute(self): |
| 319 |
''' |
| 320 |
Deletes specified express route circuit |
| 321 |
:return True |
| 322 |
''' |
| 323 |
self.log("Deleting the express route {0}".format(self.name)) |
| 324 |
try: |
| 325 |
poller = self.network_client.express_route_circuits.begin_delete( |
| 326 |
self.resource_group, self.name) |
| 327 |
result = self.get_poller_result(poller) |
| 328 |
except Exception as e: |
| 329 |
self.log('Error attempting to delete express route.') |
| 330 |
self.fail( |
| 331 |
"Error deleting the express route : {0}".format(str(e))) |
| 332 |
return result |
| 333 |
|
| 334 |
|
| 335 |
def express_route_to_dict(item): |
| 336 |
|
| 337 |
express_route = item.as_dict() |
| 338 |
result = dict( |
| 339 |
additional_properties=express_route.get('additional_properties', {}), |
| 340 |
id=express_route.get('id', None), |
| 341 |
name=express_route.get('name', None), |
| 342 |
type=express_route.get('type', None), |
| 343 |
location=express_route.get('location', '').replace(' ', '').lower(), |
| 344 |
tags=express_route.get('tags', None), |
| 345 |
allow_classic_operations=express_route.get( |
| 346 |
'allow_classic_operations', None), |
| 347 |
circuit_provisioning_state=express_route.get( |
| 348 |
'circuit_provisioning_state', None), |
| 349 |
service_provider_provisioning_state=express_route.get( |
| 350 |
'service_provider_provisioning_state', None), |
| 351 |
authorizations=express_route.get('authorizations', []), |
| 352 |
peerings=express_route.get('peerings', []), |
| 353 |
service_key=express_route.get('service_key', None), |
| 354 |
service_provider_notes=express_route.get( |
| 355 |
'service_provider_notes', None), |
| 356 |
express_route_port=express_route.get('express_route_port', None), |
| 357 |
bandwidth_in_gbps=express_route.get('bandwidth_in_gbps', None), |
| 358 |
stag=express_route.get('stag', None), |
| 359 |
provisioning_state=express_route.get('provisioning_state', None), |
| 360 |
gateway_manager_etag=express_route.get('gateway_manager_etag', ''), |
| 361 |
global_reach_enabled=express_route.get('global_reach_enabled', '') |
| 362 |
) |
| 363 |
return result |
| 364 |
|
| 365 |
|
| 366 |
def main(): |
| 367 |
AzureExpressRoute() |
| 368 |
|
| 369 |
|
| 370 |
if __name__ == '__main__': |
| 371 |
main() |