| 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_applicationsecuritygroup |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Application Security Group |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Application Security Group. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
name: |
| 26 |
description: |
| 27 |
- The name of the application security group. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
location: |
| 31 |
description: |
| 32 |
- Resource location. If not set, location from the resource group will be used as default. |
| 33 |
type: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Assert the state of the Application Security Group. |
| 37 |
- Use C(present) to create or update an Application Security Group and C(absent) to delete it. |
| 38 |
type: str |
| 39 |
default: present |
| 40 |
choices: |
| 41 |
- absent |
| 42 |
- present |
| 43 |
|
| 44 |
extends_documentation_fragment: |
| 45 |
- azure.azcollection.azure |
| 46 |
- azure.azcollection.azure_tags |
| 47 |
|
| 48 |
author: |
| 49 |
- Yunge Zhu (@yungezz) |
| 50 |
|
| 51 |
''' |
| 52 |
|
| 53 |
EXAMPLES = ''' |
| 54 |
- name: Create application security group |
| 55 |
azure_rm_applicationsecuritygroup: |
| 56 |
resource_group: myResourceGroup |
| 57 |
name: mySecurityGroup |
| 58 |
location: eastus |
| 59 |
tags: |
| 60 |
foo: bar |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = ''' |
| 64 |
id: |
| 65 |
description: |
| 66 |
- Resource id of the application security group. |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationSecurityGroups/ |
| 70 |
mySecurityGroup" |
| 71 |
''' |
| 72 |
|
| 73 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 74 |
|
| 75 |
try: |
| 76 |
from azure.core.exceptions import ResourceNotFoundError |
| 77 |
from azure.core.polling import LROPoller |
| 78 |
except ImportError: |
| 79 |
|
| 80 |
pass |
| 81 |
|
| 82 |
|
| 83 |
class Actions: |
| 84 |
NoAction, CreateOrUpdate, Delete = range(3) |
| 85 |
|
| 86 |
|
| 87 |
class AzureRMApplicationSecurityGroup(AzureRMModuleBase): |
| 88 |
"""Configuration class for an Azure RM Application Security Group resource""" |
| 89 |
|
| 90 |
def __init__(self): |
| 91 |
self.module_arg_spec = dict( |
| 92 |
resource_group=dict( |
| 93 |
type='str', |
| 94 |
required=True |
| 95 |
), |
| 96 |
name=dict( |
| 97 |
type='str', |
| 98 |
required=True |
| 99 |
), |
| 100 |
location=dict( |
| 101 |
type='str' |
| 102 |
), |
| 103 |
state=dict( |
| 104 |
type='str', |
| 105 |
default='present', |
| 106 |
choices=['present', 'absent'] |
| 107 |
) |
| 108 |
) |
| 109 |
|
| 110 |
self.resource_group = None |
| 111 |
self.location = None |
| 112 |
self.name = None |
| 113 |
self.tags = None |
| 114 |
|
| 115 |
self.state = None |
| 116 |
|
| 117 |
self.results = dict(changed=False) |
| 118 |
|
| 119 |
self.to_do = Actions.NoAction |
| 120 |
|
| 121 |
super(AzureRMApplicationSecurityGroup, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 122 |
supports_check_mode=True, |
| 123 |
supports_tags=True) |
| 124 |
|
| 125 |
def exec_module(self, **kwargs): |
| 126 |
"""Main module execution method""" |
| 127 |
|
| 128 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 129 |
if hasattr(self, key): |
| 130 |
setattr(self, key, kwargs[key]) |
| 131 |
|
| 132 |
resource_group = self.get_resource_group(self.resource_group) |
| 133 |
|
| 134 |
if not self.location: |
| 135 |
self.location = resource_group.location |
| 136 |
|
| 137 |
old_response = self.get_applicationsecuritygroup() |
| 138 |
|
| 139 |
if not old_response: |
| 140 |
self.log("Application Security Group instance doesn't exist") |
| 141 |
if self.state == 'present': |
| 142 |
self.to_do = Actions.CreateOrUpdate |
| 143 |
else: |
| 144 |
self.log("Old instance didn't exist") |
| 145 |
else: |
| 146 |
self.log("Application Security Group instance already exists") |
| 147 |
if self.state == 'present': |
| 148 |
if self.check_update(old_response): |
| 149 |
self.to_do = Actions.CreateOrUpdate |
| 150 |
|
| 151 |
update_tags, self.tags = self.update_tags(old_response.get('tags', None)) |
| 152 |
if update_tags: |
| 153 |
self.to_do = Actions.CreateOrUpdate |
| 154 |
|
| 155 |
elif self.state == 'absent': |
| 156 |
self.to_do = Actions.Delete |
| 157 |
|
| 158 |
if self.to_do == Actions.CreateOrUpdate: |
| 159 |
self.log("Need to Create / Update the Application Security Group instance") |
| 160 |
self.results['changed'] = True |
| 161 |
|
| 162 |
if self.check_mode: |
| 163 |
return self.results |
| 164 |
|
| 165 |
response = self.create_update_applicationsecuritygroup() |
| 166 |
self.results['id'] = response['id'] |
| 167 |
|
| 168 |
elif self.to_do == Actions.Delete: |
| 169 |
self.log("Delete Application Security Group instance") |
| 170 |
self.results['changed'] = True |
| 171 |
|
| 172 |
if self.check_mode: |
| 173 |
return self.results |
| 174 |
|
| 175 |
self.delete_applicationsecuritygroup() |
| 176 |
|
| 177 |
return self.results |
| 178 |
|
| 179 |
def check_update(self, existing_asg): |
| 180 |
if self.location and self.location.lower() != existing_asg['location'].lower(): |
| 181 |
self.module.warn("location cannot be updated. Existing {0}, input {1}".format(existing_asg['location'], self.location)) |
| 182 |
return False |
| 183 |
|
| 184 |
def create_update_applicationsecuritygroup(self): |
| 185 |
''' |
| 186 |
Create or update Application Security Group. |
| 187 |
|
| 188 |
:return: deserialized Application Security Group instance state dictionary |
| 189 |
''' |
| 190 |
self.log("Creating / Updating the Application Security Group instance {0}".format(self.name)) |
| 191 |
|
| 192 |
param = dict(name=self.name, |
| 193 |
tags=self.tags, |
| 194 |
location=self.location) |
| 195 |
try: |
| 196 |
response = self.network_client.application_security_groups.begin_create_or_update(resource_group_name=self.resource_group, |
| 197 |
application_security_group_name=self.name, |
| 198 |
parameters=param) |
| 199 |
if isinstance(response, LROPoller): |
| 200 |
response = self.get_poller_result(response) |
| 201 |
|
| 202 |
except Exception as exc: |
| 203 |
self.log('Error creating/updating Application Security Group instance.') |
| 204 |
self.fail("Error creating/updating Application Security Group instance: {0}".format(str(exc))) |
| 205 |
return response.as_dict() |
| 206 |
|
| 207 |
def delete_applicationsecuritygroup(self): |
| 208 |
''' |
| 209 |
Deletes specified Application Security Group instance. |
| 210 |
|
| 211 |
:return: True |
| 212 |
''' |
| 213 |
self.log("Deleting the Application Security Group instance {0}".format(self.name)) |
| 214 |
try: |
| 215 |
response = self.network_client.application_security_groups.begin_delete(resource_group_name=self.resource_group, |
| 216 |
application_security_group_name=self.name) |
| 217 |
except Exception as e: |
| 218 |
self.log('Error deleting the Application Security Group instance.') |
| 219 |
self.fail("Error deleting the Application Security Group instance: {0}".format(str(e))) |
| 220 |
|
| 221 |
return True |
| 222 |
|
| 223 |
def get_applicationsecuritygroup(self): |
| 224 |
''' |
| 225 |
Gets the properties of the specified Application Security Group. |
| 226 |
|
| 227 |
:return: deserialized Application Security Group instance state dictionary |
| 228 |
''' |
| 229 |
self.log("Checking if the Application Security Group instance {0} is present".format(self.name)) |
| 230 |
found = False |
| 231 |
try: |
| 232 |
response = self.network_client.application_security_groups.get(resource_group_name=self.resource_group, |
| 233 |
application_security_group_name=self.name) |
| 234 |
self.log("Response : {0}".format(response)) |
| 235 |
self.log("Application Security Group instance : {0} found".format(response.name)) |
| 236 |
return response.as_dict() |
| 237 |
except ResourceNotFoundError as e: |
| 238 |
self.log('Did not find the Application Security Group instance.') |
| 239 |
return False |
| 240 |
|
| 241 |
|
| 242 |
def main(): |
| 243 |
"""Main execution""" |
| 244 |
AzureRMApplicationSecurityGroup() |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == '__main__': |
| 248 |
main() |