| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_webappvnetconnection |
| 14 |
version_added: "1.8.0" |
| 15 |
short_description: Manage web app virtual network connection |
| 16 |
description: |
| 17 |
- Add, remove, or update the virtual network connection for a web app. |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Name of the web app. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
resource_group: |
| 25 |
description: |
| 26 |
- Resource group of the web app. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- State of the virtual network connection. Use C(present) to create or update and C(absent) to delete. |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
vnet_name: |
| 38 |
description: |
| 39 |
- Name of the virtual network. Required if adding or updating. |
| 40 |
type: str |
| 41 |
subnet: |
| 42 |
description: |
| 43 |
- Name of the virtual network's subnet. Required if adding or updating. |
| 44 |
type: str |
| 45 |
vnet_resource_group: |
| 46 |
description: |
| 47 |
- Name of the resource group for the virtual network. Defaults to main C(resource_group) value. |
| 48 |
type: str |
| 49 |
|
| 50 |
extends_documentation_fragment: |
| 51 |
- azure.azcollection.azure |
| 52 |
|
| 53 |
author: |
| 54 |
- Ross Bender (@l3ender) |
| 55 |
''' |
| 56 |
|
| 57 |
EXAMPLES = ''' |
| 58 |
- name: Configure web app with virtual network |
| 59 |
azure.azcollection.azure_rm_webappvnetconnection: |
| 60 |
name: "MyWebapp" |
| 61 |
resource_group: "MyResourceGroup" |
| 62 |
vnet_name: "MyVnetName" |
| 63 |
subnet: "MySubnetName" |
| 64 |
|
| 65 |
- name: Configure web app with virtual network in different resource group |
| 66 |
azure.azcollection.azure_rm_webappvnetconnection: |
| 67 |
name: "MyWebapp" |
| 68 |
resource_group: "MyResourceGroup" |
| 69 |
vnet_name: "MyVnetName" |
| 70 |
subnet: "MySubnetName" |
| 71 |
vnet_resource_group: "MyOtherResourceGroup" |
| 72 |
|
| 73 |
- name: Delete web app virtual network |
| 74 |
azure.azcollection.azure_rm_webappvnetconnection: |
| 75 |
name: "MyWebapp" |
| 76 |
resource_group: "MyResourceGroup" |
| 77 |
state: "absent" |
| 78 |
''' |
| 79 |
|
| 80 |
RETURN = ''' |
| 81 |
connection: |
| 82 |
description: |
| 83 |
- The web app's virtual network connection. |
| 84 |
returned: always |
| 85 |
type: complex |
| 86 |
contains: |
| 87 |
id: |
| 88 |
description: |
| 89 |
- ID of the web app virtual network connection. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/myWebApp/virtualNetworkConnections/yyy-yyy_subnet |
| 93 |
name: |
| 94 |
description: |
| 95 |
- Name of the web app virtual network connection. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: yyy-yyy_subnet |
| 99 |
subnet_name: |
| 100 |
description: |
| 101 |
- Name of the subnet connected to the web app. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: mySubnet |
| 105 |
vnet_name: |
| 106 |
description: |
| 107 |
- Name of the virtual network connected to the web app. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
sample: myVnet |
| 111 |
vnet_resource_group: |
| 112 |
description: |
| 113 |
- Name of the resource group the virtual network is in. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: myResourceGroup |
| 117 |
vnet_resource_id: |
| 118 |
description: |
| 119 |
- ID of the virtual network/subnet connected to the web app. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet |
| 123 |
''' |
| 124 |
|
| 125 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 126 |
|
| 127 |
try: |
| 128 |
from azure.mgmt.web.models import SwiftVirtualNetwork |
| 129 |
except Exception: |
| 130 |
|
| 131 |
pass |
| 132 |
|
| 133 |
|
| 134 |
class AzureRMWebAppVnetConnection(AzureRMModuleBase): |
| 135 |
|
| 136 |
def __init__(self): |
| 137 |
|
| 138 |
self.module_arg_spec = dict( |
| 139 |
name=dict(type='str', required=True), |
| 140 |
resource_group=dict(type='str', required=True), |
| 141 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 142 |
vnet_name=dict(type='str'), |
| 143 |
subnet=dict(type='str'), |
| 144 |
vnet_resource_group=dict(type='str'), |
| 145 |
) |
| 146 |
|
| 147 |
self.results = dict( |
| 148 |
changed=False, |
| 149 |
connection=dict(), |
| 150 |
) |
| 151 |
|
| 152 |
self.state = None |
| 153 |
self.name = None |
| 154 |
self.resource_group = None |
| 155 |
self.vnet_name = None |
| 156 |
self.subnet = None |
| 157 |
self.vnet_resource_group = None |
| 158 |
|
| 159 |
super(AzureRMWebAppVnetConnection, self).__init__(self.module_arg_spec, |
| 160 |
supports_check_mode=True, |
| 161 |
supports_tags=False) |
| 162 |
|
| 163 |
def exec_module(self, **kwargs): |
| 164 |
for key in self.module_arg_spec: |
| 165 |
setattr(self, key, kwargs[key]) |
| 166 |
|
| 167 |
changed = False |
| 168 |
vnet = self.get_vnet_connection() |
| 169 |
if vnet: |
| 170 |
self.results['connection'] = self.set_results(vnet) |
| 171 |
|
| 172 |
if self.state == 'absent' and vnet: |
| 173 |
changed = True |
| 174 |
if not self.check_mode: |
| 175 |
self.log('Deleting vnet connection for webapp {0}'.format(self.name)) |
| 176 |
self.delete_vnet_connection() |
| 177 |
self.results['connection'] = dict() |
| 178 |
elif self.state == 'present': |
| 179 |
self.vnet_resource_group = self.vnet_resource_group or self.resource_group |
| 180 |
|
| 181 |
if not vnet: |
| 182 |
self.log('Adding vnet connection for webapp {0}'.format(self.name)) |
| 183 |
changed = True |
| 184 |
else: |
| 185 |
subnet_detail = self.get_subnet_detail(vnet.vnet_resource_id) |
| 186 |
if (subnet_detail['resource_group'] != self.vnet_resource_group |
| 187 |
or subnet_detail['vnet_name'] != self.vnet_name |
| 188 |
or subnet_detail['subnet_name'] != self.subnet): |
| 189 |
self.log('Detected change in vnet connection for webapp {0}'.format(self.name)) |
| 190 |
changed = True |
| 191 |
|
| 192 |
if changed: |
| 193 |
if not self.check_mode: |
| 194 |
self.log('Updating vnet connection for webapp {0}'.format(self.name)) |
| 195 |
subnet = self.get_subnet() |
| 196 |
param = SwiftVirtualNetwork(subnet_resource_id=subnet.id) |
| 197 |
self.create_or_update_vnet_connection(param) |
| 198 |
vnet = self.get_vnet_connection() |
| 199 |
self.results['connection'] = self.set_results(vnet) |
| 200 |
|
| 201 |
self.results['changed'] = changed |
| 202 |
return self.results |
| 203 |
|
| 204 |
def get_vnet_connection(self): |
| 205 |
connections = self.list_vnet_connections() |
| 206 |
for connection in connections: |
| 207 |
if connection.is_swift: |
| 208 |
return connection |
| 209 |
|
| 210 |
return None |
| 211 |
|
| 212 |
def list_vnet_connections(self): |
| 213 |
try: |
| 214 |
return self.web_client.web_apps.list_vnet_connections(resource_group_name=self.resource_group, name=self.name) |
| 215 |
except Exception as exc: |
| 216 |
self.fail("Error getting webapp vnet connections {0} (rg={1}) - {2}".format(self.name, self.resource_group, str(exc))) |
| 217 |
|
| 218 |
def delete_vnet_connection(self): |
| 219 |
try: |
| 220 |
return self.web_client.web_apps.delete_swift_virtual_network(resource_group_name=self.resource_group, name=self.name) |
| 221 |
except Exception as exc: |
| 222 |
self.fail("Error deleting webapp vnet connection {0} (rg={1}) - {3}".format(self.name, self.resource_group, str(exc))) |
| 223 |
|
| 224 |
def create_or_update_vnet_connection(self, vnet): |
| 225 |
try: |
| 226 |
return self.web_client.web_apps.create_or_update_swift_virtual_network_connection_with_check( |
| 227 |
resource_group_name=self.resource_group, name=self.name, connection_envelope=vnet) |
| 228 |
except Exception as exc: |
| 229 |
self.fail("Error creating/updating webapp vnet connection {0} (vnet={1}, rg={2}) - {3}".format( |
| 230 |
self.name, self.vnet_name, self.resource_group, str(exc))) |
| 231 |
|
| 232 |
def get_subnet(self): |
| 233 |
try: |
| 234 |
return self.network_client.subnets.get(resource_group_name=self.vnet_resource_group, virtual_network_name=self.vnet_name, subnet_name=self.subnet) |
| 235 |
except Exception as exc: |
| 236 |
self.fail("Error getting subnet {0} in vnet={1} (rg={2}) - {3}".format(self.subnet, self.vnet_name, self.vnet_resource_group, str(exc))) |
| 237 |
|
| 238 |
def set_results(self, vnet): |
| 239 |
vnet_dict = vnet.as_dict() |
| 240 |
|
| 241 |
output = dict() |
| 242 |
output['id'] = vnet_dict['id'] |
| 243 |
output['name'] = vnet_dict['name'] |
| 244 |
subnet_id = vnet_dict.get('subnet_resource_id', vnet_dict.get('vnet_resource_id')) |
| 245 |
output['vnet_resource_id'] = subnet_id |
| 246 |
subnet_detail = self.get_subnet_detail(subnet_id) |
| 247 |
output['vnet_resource_group'] = subnet_detail['resource_group'] |
| 248 |
output['vnet_name'] = subnet_detail['vnet_name'] |
| 249 |
output['subnet_name'] = subnet_detail['subnet_name'] |
| 250 |
|
| 251 |
return output |
| 252 |
|
| 253 |
|
| 254 |
def main(): |
| 255 |
AzureRMWebAppVnetConnection() |
| 256 |
|
| 257 |
|
| 258 |
if __name__ == '__main__': |
| 259 |
main() |