| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_serviceendpointpolicydefinition |
| 11 |
version_added: "3.9.0" |
| 12 |
short_description: Managed the service endpoint policy definition |
| 13 |
description: |
| 14 |
- Create, update or delete the service endpoint policy definition. |
| 15 |
|
| 16 |
options: |
| 17 |
resource_group: |
| 18 |
description: |
| 19 |
- The name of the resource group. |
| 20 |
type: str |
| 21 |
required: true |
| 22 |
service_endpoint_policy_name: |
| 23 |
description: |
| 24 |
- The name of the service endpoint policy name. |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the service endpoint policy definition name. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
description: |
| 33 |
description: |
| 34 |
- A description for this rule. Restricted to 140 chars. |
| 35 |
type: str |
| 36 |
service: |
| 37 |
description: |
| 38 |
- Service endpoint name. |
| 39 |
type: str |
| 40 |
service_resources: |
| 41 |
description: |
| 42 |
- A list of service resources. |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Set to C(present) to create or update the service endpoint policy definition. |
| 48 |
- Set to C(absent) to remove the service endpoint policy definition. |
| 49 |
default: present |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- absent |
| 53 |
- present |
| 54 |
extends_documentation_fragment: |
| 55 |
- azure.azcollection.azure |
| 56 |
|
| 57 |
author: |
| 58 |
- magodo (@magodo) |
| 59 |
- Fred Sun (@Fred-sun) |
| 60 |
''' |
| 61 |
|
| 62 |
EXAMPLES = ''' |
| 63 |
- name: Create a new service endpoint policy definition |
| 64 |
azure.azcollection.azure_rm_serviceendpointpolicydefinition: |
| 65 |
resource_group: testRG |
| 66 |
service_endpoint_policy_name: policy01 |
| 67 |
name: definition01 |
| 68 |
description: definition-test |
| 69 |
service: Microsoft.Storage |
| 70 |
service_resources: |
| 71 |
- "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Storage/storageAccounts/account01" |
| 72 |
|
| 73 |
- name: Delete the service endpoint policy definition |
| 74 |
azure.azcollection.azure_rm_serviceendpointpolicydefinition: |
| 75 |
resource_group: testRG |
| 76 |
service_endpoint_policy_name: policy01 |
| 77 |
name: definition01 |
| 78 |
state: absent |
| 79 |
''' |
| 80 |
|
| 81 |
RETURN = ''' |
| 82 |
serviceendpointpolicydefinition: |
| 83 |
description: |
| 84 |
- The fact of the service endpoint policy definitions in a service end point policy. |
| 85 |
type: complex |
| 86 |
returned: always |
| 87 |
contains: |
| 88 |
id: |
| 89 |
description: |
| 90 |
- Fully qualified ID of the resource. |
| 91 |
type: str |
| 92 |
returned: always |
| 93 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/serviceEndpointPolicies/\ |
| 94 |
policy01/serviceEndpointPolicyDefinitions/definition01" |
| 95 |
name: |
| 96 |
description: |
| 97 |
- The name of the resource that is unique within a resource group. |
| 98 |
type: str |
| 99 |
returned: always |
| 100 |
sample: definition01 |
| 101 |
etag: |
| 102 |
description: |
| 103 |
- A unique read-only string that changes whenever the resource is updated. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: "4a66fa1c-3d17-43c9-9b9d-da07ffdbb695" |
| 107 |
resource_group: |
| 108 |
description: |
| 109 |
- The name of the resource group. |
| 110 |
type: str |
| 111 |
returned: always |
| 112 |
sample: TestRG |
| 113 |
description: |
| 114 |
description: |
| 115 |
- A description for this rule. Restricted to 140 chars. |
| 116 |
type: str |
| 117 |
returned: always |
| 118 |
sample: definition-test |
| 119 |
service_endpoint_policy_name: |
| 120 |
description: |
| 121 |
- The name of the service endpoint policy name. |
| 122 |
type: str |
| 123 |
returned: always |
| 124 |
sample: policy01 |
| 125 |
type: |
| 126 |
description: |
| 127 |
- The type of the resource. |
| 128 |
type: str |
| 129 |
returned: always |
| 130 |
sample: "Microsoft.Network/serviceEndpointPolicies/serviceEndpointPolicyDefinitions" |
| 131 |
provisioning_state: |
| 132 |
description: |
| 133 |
- The provisioning state of the service endpoint policy definition resource. |
| 134 |
type: str |
| 135 |
returned: always |
| 136 |
sample: Successded |
| 137 |
service: |
| 138 |
description: |
| 139 |
- Service endpoint name. |
| 140 |
type: str |
| 141 |
returned: always |
| 142 |
sample: Microsoft.Storage |
| 143 |
service_resources: |
| 144 |
description: |
| 145 |
- A list of service resources. |
| 146 |
type: list |
| 147 |
returned: always |
| 148 |
sample: ["/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Storage/storageAccounts/account01"] |
| 149 |
''' |
| 150 |
|
| 151 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 152 |
try: |
| 153 |
from azure.core.polling import LROPoller |
| 154 |
except ImportError: |
| 155 |
|
| 156 |
pass |
| 157 |
|
| 158 |
|
| 159 |
class AzureRMServiceEndpointPolicyDefinition(AzureRMModuleBase): |
| 160 |
"""Information class for an Azure RM Data Collection Rules""" |
| 161 |
|
| 162 |
def __init__(self): |
| 163 |
self.module_arg_spec = dict( |
| 164 |
resource_group=dict(type='str', required=True), |
| 165 |
service_endpoint_policy_name=dict(type='str', required=True), |
| 166 |
name=dict(type='str', required=True), |
| 167 |
description=dict(type='str'), |
| 168 |
service=dict(type='str'), |
| 169 |
service_resources=dict(type='list', elements='str'), |
| 170 |
state=dict(type='str', default='present', choices=['present', 'absent']) |
| 171 |
) |
| 172 |
|
| 173 |
self.resource_group = None |
| 174 |
self.service_endpoint_policy_name = None |
| 175 |
self.name = None |
| 176 |
self.description = None |
| 177 |
self.service = None |
| 178 |
self.service_resurces = None |
| 179 |
self.state = None |
| 180 |
self.log_path = None |
| 181 |
self.log_mode = None |
| 182 |
|
| 183 |
self.results = dict( |
| 184 |
changed=False, |
| 185 |
serviceendpointpolicydefinition=dict() |
| 186 |
) |
| 187 |
|
| 188 |
super(AzureRMServiceEndpointPolicyDefinition, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 189 |
supports_check_mode=True, |
| 190 |
supports_tags=False, |
| 191 |
facts_module=True) |
| 192 |
|
| 193 |
def exec_module(self, **kwargs): |
| 194 |
"""Main module execution method""" |
| 195 |
|
| 196 |
for key in self.module_arg_spec: |
| 197 |
setattr(self, key, kwargs[key]) |
| 198 |
|
| 199 |
response = self.get_definition(self.name) |
| 200 |
changed = False |
| 201 |
if self.state == 'present': |
| 202 |
if response: |
| 203 |
if self.description and self.description != response.get('description'): |
| 204 |
changed = True |
| 205 |
else: |
| 206 |
self.description = response.get('description') |
| 207 |
if self.service and self.service != response.get('service'): |
| 208 |
changed = True |
| 209 |
else: |
| 210 |
self.service = response.get('service') |
| 211 |
if self.service_resources: |
| 212 |
new_service_resources = response.get('service_resources', []) |
| 213 |
for item in self.service_resources: |
| 214 |
if item not in response.get('service_resources', []): |
| 215 |
changed = True |
| 216 |
new_service_resources.append(item) |
| 217 |
self.service_resources = new_service_resources |
| 218 |
else: |
| 219 |
self.service_resources = response.get('service_resources') |
| 220 |
else: |
| 221 |
changed = True |
| 222 |
if not self.check_mode and changed: |
| 223 |
response = self.create_or_update() |
| 224 |
else: |
| 225 |
if response: |
| 226 |
changed = True |
| 227 |
if self.check_mode: |
| 228 |
self.log("The service endpoint policy definition already exist, will be delete") |
| 229 |
else: |
| 230 |
response = self.delete() |
| 231 |
else: |
| 232 |
if self.check_mode: |
| 233 |
self.log("There is no service endpoint policy definition.") |
| 234 |
|
| 235 |
self.results['changed'] = changed |
| 236 |
self.results['serviceendpointpolicydefinition'] = response |
| 237 |
return self.results |
| 238 |
|
| 239 |
def get_definition(self, name): |
| 240 |
''' |
| 241 |
Gets the specified service endpoint policy definition |
| 242 |
''' |
| 243 |
response = None |
| 244 |
try: |
| 245 |
response = self.network_client.service_endpoint_policy_definitions.get(self.resource_group, self.service_endpoint_policy_name, self.name) |
| 246 |
except Exception: |
| 247 |
self.log("Could not get the specified service endpoint policy definition {0}".format(self.name)) |
| 248 |
return self.format_item(response) if response else None |
| 249 |
|
| 250 |
def create_or_update(self): |
| 251 |
''' |
| 252 |
Create or update the service endpoint policy definition. |
| 253 |
''' |
| 254 |
response = None |
| 255 |
try: |
| 256 |
response = self.network_client.service_endpoint_policy_definitions.begin_create_or_update(self.resource_group, |
| 257 |
self.service_endpoint_policy_name, |
| 258 |
self.name, |
| 259 |
dict(description=self.description, |
| 260 |
service=self.service, |
| 261 |
service_resources=self.service_resources)) |
| 262 |
if isinstance(response, LROPoller): |
| 263 |
response = self.get_poller_result(response) |
| 264 |
except Exception as ex: |
| 265 |
self.fail("Could not create or update the service endpoint policy definition, Exception as {0}".format(ex)) |
| 266 |
return self.format_item(response) if response else None |
| 267 |
|
| 268 |
def delete(self): |
| 269 |
''' |
| 270 |
Delete the service endpoint policy definition. |
| 271 |
''' |
| 272 |
try: |
| 273 |
response = self.network_client.service_endpoint_policy_definitions.begin_delete(self.resource_group, self.service_endpoint_policy_name, self.name) |
| 274 |
if isinstance(response, LROPoller): |
| 275 |
self.get_poller_result(response) |
| 276 |
except Exception as ex: |
| 277 |
self.fail("Could not delete the service endpoint policy definition, Exception as {0}".format(ex)) |
| 278 |
|
| 279 |
def format_item(self, item): |
| 280 |
response = item.as_dict() |
| 281 |
response['resource_group'] = self.resource_group |
| 282 |
response['service_endpoint_policy_name'] = self.service_endpoint_policy_name |
| 283 |
return response |
| 284 |
|
| 285 |
|
| 286 |
def main(): |
| 287 |
"""Main execution""" |
| 288 |
AzureRMServiceEndpointPolicyDefinition() |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == '__main__': |
| 292 |
main() |