| 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_privateendpointdnszonegroup |
| 14 |
|
| 15 |
version_added: "1.10.0" |
| 16 |
|
| 17 |
short_description: Create, update, or manage private endpoint DNS zone groups. |
| 18 |
|
| 19 |
description: |
| 20 |
- Create, update, or manage private endpoint DNS zone groups. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- The name of the private endpoint DNS zone group. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
private_endpoint: |
| 29 |
description: |
| 30 |
- Name of private endpoint. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
resource_group: |
| 34 |
description: |
| 35 |
- Resource group of the private endpoint. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
private_dns_zone_configs: |
| 39 |
description: |
| 40 |
- The Private DNS zones configurations. |
| 41 |
type: list |
| 42 |
elements: dict |
| 43 |
suboptions: |
| 44 |
name: |
| 45 |
description: |
| 46 |
- The name of the private dns zone configs. |
| 47 |
type: str |
| 48 |
private_dns_zone: |
| 49 |
description: |
| 50 |
- The name of the Private DNS zone. |
| 51 |
- If set, the Private DNS Zone under the current resource group is obtained. |
| 52 |
type: str |
| 53 |
private_dns_zone_id: |
| 54 |
description: |
| 55 |
- The ID of the private dns zone. |
| 56 |
- If set, gets the value of the specified connection Private DNS Zone. |
| 57 |
type: str |
| 58 |
state: |
| 59 |
description: |
| 60 |
- State of the private endpoint DNS zone group. Use C(present) to create or update and C(absent) to delete. |
| 61 |
default: present |
| 62 |
type: str |
| 63 |
choices: |
| 64 |
- absent |
| 65 |
- present |
| 66 |
|
| 67 |
extends_documentation_fragment: |
| 68 |
- azure.azcollection.azure |
| 69 |
|
| 70 |
author: |
| 71 |
- Ross Bender (@l3ender) |
| 72 |
''' |
| 73 |
|
| 74 |
EXAMPLES = ''' |
| 75 |
- name: Create zone group for private endpoint |
| 76 |
azure_rm_privateendpointdnszonegroup: |
| 77 |
name: "my-zone-group" |
| 78 |
private_endpoint: "my-private-endpoint" |
| 79 |
resource_group: "my-resource-group" |
| 80 |
private_dns_zone_configs: |
| 81 |
- name: "default" |
| 82 |
private_dns_zone: "privatelink.postgres.database.azure.com" |
| 83 |
|
| 84 |
- name: Create zone group for private endpoint |
| 85 |
azure_rm_privateendpointdnszonegroup: |
| 86 |
name: "my-zone-group" |
| 87 |
private_endpoint: "my-private-endpoint" |
| 88 |
resource_group: "my-resource-group" |
| 89 |
state: "absent" |
| 90 |
''' |
| 91 |
|
| 92 |
|
| 93 |
RETURN = ''' |
| 94 |
state: |
| 95 |
description: |
| 96 |
- Current state of the private endpoint zone group. |
| 97 |
returned: always |
| 98 |
type: complex |
| 99 |
contains: |
| 100 |
id: |
| 101 |
description: |
| 102 |
- Resource ID of the private endpoint zone group. |
| 103 |
sample: >- |
| 104 |
/subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateEndpoints/ |
| 105 |
myPrivateEndpoint/privateDnsZoneGroups/myZoneGroup |
| 106 |
returned: always |
| 107 |
type: str |
| 108 |
name: |
| 109 |
description: |
| 110 |
- Name of the private endpoint zone group. |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
sample: myZoneGroup |
| 114 |
private_dns_zone_configs: |
| 115 |
description: |
| 116 |
- List of zone configuration within the zone group. |
| 117 |
returned: always |
| 118 |
type: list |
| 119 |
contains: |
| 120 |
name: |
| 121 |
description: |
| 122 |
- Name of the zone config. |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: default |
| 126 |
private_dns_zone_id: |
| 127 |
description: |
| 128 |
- ID of the private DNS zone. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: >- |
| 132 |
/subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/ |
| 133 |
privateDnsZones/privatelink.postgres.database.azure.com |
| 134 |
record_sets: |
| 135 |
description: |
| 136 |
- List of DNS records for zone. |
| 137 |
returned: always |
| 138 |
type: list |
| 139 |
contains: |
| 140 |
fqdn: |
| 141 |
description: |
| 142 |
- Fully qualified domain name of the record. |
| 143 |
returned: always |
| 144 |
type: str |
| 145 |
sample: myPostgreSqlSrv-123.privatelink.postgres.database.azure.com |
| 146 |
ip_addresses: |
| 147 |
description: |
| 148 |
- IP addresses for the record. |
| 149 |
returned: always |
| 150 |
type: list |
| 151 |
sample: ['10.1.0.4'] |
| 152 |
provisioning_state: |
| 153 |
description: |
| 154 |
- Provisioning state of the resource. |
| 155 |
returned: always |
| 156 |
type: str |
| 157 |
sample: Succeeded |
| 158 |
record_set_name: |
| 159 |
description: |
| 160 |
- Name of the record. |
| 161 |
returned: always |
| 162 |
type: str |
| 163 |
sample: myPostgreSqlSrv-123 |
| 164 |
record_type: |
| 165 |
description: |
| 166 |
- Type of record. |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: A |
| 170 |
ttl: |
| 171 |
description: |
| 172 |
- Time to live value of the record. |
| 173 |
returned: always |
| 174 |
type: int |
| 175 |
sample: 10 |
| 176 |
provisioning_state: |
| 177 |
description: |
| 178 |
- Provisioning state of the resource. |
| 179 |
returned: always |
| 180 |
type: str |
| 181 |
sample: Succeeded |
| 182 |
''' |
| 183 |
|
| 184 |
try: |
| 185 |
from azure.mgmt.core.tools import resource_id |
| 186 |
from azure.core.exceptions import ResourceNotFoundError |
| 187 |
from azure.core.polling import LROPoller |
| 188 |
except ImportError: |
| 189 |
|
| 190 |
pass |
| 191 |
|
| 192 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 193 |
|
| 194 |
|
| 195 |
private_dns_zone_configs_spec = dict( |
| 196 |
name=dict(type="str"), |
| 197 |
private_dns_zone=dict(type="str"), |
| 198 |
private_dns_zone_id=dict(type="str") |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
class Actions: |
| 203 |
NoAction, Create, Update, Delete = range(4) |
| 204 |
|
| 205 |
|
| 206 |
class AzureRMPrivateEndpointDnsZoneGroup(AzureRMModuleBaseExt): |
| 207 |
|
| 208 |
def __init__(self): |
| 209 |
|
| 210 |
self.module_arg_spec = dict( |
| 211 |
name=dict(type="str", required=True), |
| 212 |
private_endpoint=dict(type="str", required=True), |
| 213 |
resource_group=dict(type="str", required=True), |
| 214 |
private_dns_zone_configs=dict(type="list", elements="dict", options=private_dns_zone_configs_spec), |
| 215 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 216 |
) |
| 217 |
|
| 218 |
self.name = None |
| 219 |
self.private_endpoint = None |
| 220 |
self.resource_group = None |
| 221 |
self.state = None |
| 222 |
self.parameters = dict() |
| 223 |
self.results = dict( |
| 224 |
changed=False, |
| 225 |
state=dict() |
| 226 |
) |
| 227 |
self.to_do = Actions.NoAction |
| 228 |
|
| 229 |
super(AzureRMPrivateEndpointDnsZoneGroup, self).__init__(self.module_arg_spec, |
| 230 |
supports_tags=False, |
| 231 |
supports_check_mode=True) |
| 232 |
|
| 233 |
def exec_module(self, **kwargs): |
| 234 |
|
| 235 |
for key in self.module_arg_spec: |
| 236 |
if hasattr(self, key): |
| 237 |
setattr(self, key, kwargs[key]) |
| 238 |
elif kwargs[key] is not None: |
| 239 |
self.parameters[key] = kwargs[key] |
| 240 |
|
| 241 |
for zone_config in self.parameters.get("private_dns_zone_configs", []): |
| 242 |
if zone_config.get("private_dns_zone_id") is not None: |
| 243 |
self.log("The private_dns_zone_id exist, do nothing") |
| 244 |
else: |
| 245 |
zone_name = zone_config.pop("private_dns_zone") |
| 246 |
zone_config["private_dns_zone_id"] = self.private_dns_zone_id(zone_name) |
| 247 |
|
| 248 |
self.log("Fetching private endpoint {0}".format(self.name)) |
| 249 |
old_response = self.get_zone() |
| 250 |
|
| 251 |
if old_response is None or not old_response: |
| 252 |
if self.state == "present": |
| 253 |
self.to_do = Actions.Create |
| 254 |
self.ensure_private_endpoint() |
| 255 |
else: |
| 256 |
if self.state == "absent": |
| 257 |
self.to_do = Actions.Delete |
| 258 |
else: |
| 259 |
self.results["compare"] = [] |
| 260 |
if not self.idempotency_check(old_response, self.parameters): |
| 261 |
self.to_do = Actions.Update |
| 262 |
|
| 263 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 264 |
self.results["changed"] = True |
| 265 |
if self.check_mode: |
| 266 |
return self.results |
| 267 |
response = self.create_update_zone() |
| 268 |
elif self.to_do == Actions.Delete: |
| 269 |
self.results["changed"] = True |
| 270 |
if self.check_mode: |
| 271 |
return self.results |
| 272 |
response = self.delete_zone() |
| 273 |
else: |
| 274 |
self.results["changed"] = False |
| 275 |
response = old_response |
| 276 |
|
| 277 |
if response is not None: |
| 278 |
self.results["state"] = response |
| 279 |
|
| 280 |
return self.results |
| 281 |
|
| 282 |
def get_zone(self): |
| 283 |
try: |
| 284 |
item = self.network_client.private_dns_zone_groups.get(resource_group_name=self.resource_group, |
| 285 |
private_endpoint_name=self.private_endpoint, |
| 286 |
private_dns_zone_group_name=self.name) |
| 287 |
return self.zone_to_dict(item) |
| 288 |
except ResourceNotFoundError: |
| 289 |
self.log("Did not find the private endpoint resource") |
| 290 |
return None |
| 291 |
|
| 292 |
def create_update_zone(self): |
| 293 |
try: |
| 294 |
self.parameters["name"] = self.name |
| 295 |
response = self.network_client.private_dns_zone_groups.begin_create_or_update(resource_group_name=self.resource_group, |
| 296 |
private_endpoint_name=self.private_endpoint, |
| 297 |
private_dns_zone_group_name=self.name, |
| 298 |
parameters=self.parameters) |
| 299 |
if isinstance(response, LROPoller): |
| 300 |
response = self.get_poller_result(response) |
| 301 |
|
| 302 |
return self.zone_to_dict(response) |
| 303 |
except Exception as exc: |
| 304 |
self.fail("Error creating or updating DNS zone group {0} for private endpoint {1}: {2}".format(self.name, self.private_endpoint, str(exc))) |
| 305 |
|
| 306 |
def ensure_private_endpoint(self): |
| 307 |
try: |
| 308 |
self.network_client.private_endpoints.get(resource_group_name=self.resource_group, |
| 309 |
private_endpoint_name=self.private_endpoint) |
| 310 |
except ResourceNotFoundError: |
| 311 |
self.fail("Could not load the private endpoint {0}.".format(self.private_endpoint)) |
| 312 |
|
| 313 |
def delete_zone(self): |
| 314 |
try: |
| 315 |
response = self.network_client.private_dns_zone_groups.begin_delete(resource_group_name=self.resource_group, |
| 316 |
private_endpoint_name=self.private_endpoint, |
| 317 |
private_dns_zone_group_name=self.name) |
| 318 |
if isinstance(response, LROPoller): |
| 319 |
response = self.get_poller_result(response) |
| 320 |
|
| 321 |
return response |
| 322 |
except Exception as exc: |
| 323 |
self.fail("Error deleting private endpoint {0}: {1}".format(self.name, str(exc))) |
| 324 |
|
| 325 |
def zone_to_dict(self, zone): |
| 326 |
if zone is not None: |
| 327 |
zone_dict = zone.as_dict() |
| 328 |
else: |
| 329 |
return None |
| 330 |
return dict( |
| 331 |
id=zone_dict.get("id"), |
| 332 |
name=zone_dict.get("name"), |
| 333 |
private_dns_zone_configs=[self.zone_config_to_dict(zone_config) for zone_config in zone_dict.get("private_dns_zone_configs", [])], |
| 334 |
provisioning_state=zone_dict.get("provisioning_state"), |
| 335 |
) |
| 336 |
|
| 337 |
def zone_config_to_dict(self, zone_config): |
| 338 |
return dict( |
| 339 |
name=zone_config.get("name"), |
| 340 |
private_dns_zone_id=zone_config.get("private_dns_zone_id"), |
| 341 |
record_sets=[self.record_set_to_dict(record_set) for record_set in zone_config.get("record_sets", [])], |
| 342 |
) |
| 343 |
|
| 344 |
def record_set_to_dict(self, record_set): |
| 345 |
return dict( |
| 346 |
fqdn=record_set.get("fqdn"), |
| 347 |
ip_addresses=record_set.get("ip_addresses"), |
| 348 |
provisioning_state=record_set.get("provisioning_state"), |
| 349 |
record_set_name=record_set.get("record_set_name"), |
| 350 |
record_type=record_set.get("record_type"), |
| 351 |
ttl=record_set.get("ttl"), |
| 352 |
) |
| 353 |
|
| 354 |
def private_dns_zone_id(self, name): |
| 355 |
return resource_id(subscription=self.subscription_id, |
| 356 |
resource_group=self.resource_group, |
| 357 |
namespace='Microsoft.Network', |
| 358 |
type='privateDnsZones', |
| 359 |
name=name) |
| 360 |
|
| 361 |
|
| 362 |
def main(): |
| 363 |
AzureRMPrivateEndpointDnsZoneGroup() |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == "__main__": |
| 367 |
main() |