| 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_publicipprefix_info |
| 14 |
|
| 15 |
version_added: "2.2.0" |
| 16 |
|
| 17 |
short_description: Get public IP prefix facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific public IP prefix. |
| 21 |
- Get all facts for a specific public IP prefixes within a resource group. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the public IP prefix. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- Limit results by resource group. Required when using name parameter. |
| 31 |
type: str |
| 32 |
tags: |
| 33 |
description: |
| 34 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- azure.azcollection.azure |
| 40 |
|
| 41 |
author: |
| 42 |
- xuzhang3 (@xuzhang3) |
| 43 |
- Fred-sun (@Fred-sun) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get facts for one Public IP Prefix |
| 48 |
azure_rm_publicipprefix_info: |
| 49 |
resource_group: myResourceGroup |
| 50 |
name: publicipprefix |
| 51 |
|
| 52 |
- name: Get facts for all Public IPs within a resource groups |
| 53 |
azure_rm_publicipprefix_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
tags: |
| 56 |
- key:value |
| 57 |
''' |
| 58 |
|
| 59 |
RETURN = ''' |
| 60 |
publicipprefix: |
| 61 |
description: |
| 62 |
- List of public IP prefixes dicts. |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
id: |
| 67 |
description: |
| 68 |
- Resource ID. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: /subscriptions/xxx---xxxxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/publicIPPrefixes/pipb57dc95224 |
| 72 |
name: |
| 73 |
description: |
| 74 |
- Name of the public IP prefix. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: prefix57dc95224 |
| 78 |
type: |
| 79 |
description: |
| 80 |
- Resource type. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: "Microsoft.Network/publicIPPrefixes" |
| 84 |
location: |
| 85 |
description: |
| 86 |
- Resource location. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: eastus |
| 90 |
tags: |
| 91 |
description: |
| 92 |
- Resource tags. |
| 93 |
returned: always |
| 94 |
type: dict |
| 95 |
sample: { |
| 96 |
"delete": "on-exit", |
| 97 |
"testing": "testing" |
| 98 |
} |
| 99 |
public_ip_address_version: |
| 100 |
description: |
| 101 |
- The public IP address version. |
| 102 |
- Possible values are C(IPv4) and C(IPv6). |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: IPv4 |
| 106 |
ip_tags: |
| 107 |
description: |
| 108 |
- The list of tags associated with the public IP prefixes. |
| 109 |
returned: always |
| 110 |
type: list |
| 111 |
sample: [ |
| 112 |
{ |
| 113 |
"type": "FirstPartyUsage", |
| 114 |
"value": "Storage" |
| 115 |
} |
| 116 |
] |
| 117 |
provisioning_state: |
| 118 |
description: |
| 119 |
- The provisioning state of the PublicIP Prefix resource. |
| 120 |
- Possible values is C(Succeeded). |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: Succeeded |
| 124 |
etag: |
| 125 |
description: |
| 126 |
- A unique read-only string that changes whenever the resource is updated. |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
sample: "W/'1905ee13-7623-45b1-bc6b-4a12b2fb9d15'" |
| 130 |
sku: |
| 131 |
description: |
| 132 |
- The public IP prefix SKU. |
| 133 |
returned: always |
| 134 |
type: dict |
| 135 |
sample: {'name': 'standard', 'tier': 'Regional'} |
| 136 |
zones: |
| 137 |
description: |
| 138 |
- A list of availability zones denoting the IP allocated for the resource needs to come from. |
| 139 |
returned: always |
| 140 |
type: list |
| 141 |
sample: ['1', '2'] |
| 142 |
prefix_length: |
| 143 |
description: |
| 144 |
- The Length of the Public IP Prefix. |
| 145 |
type: int |
| 146 |
returned: always |
| 147 |
sample: 29 |
| 148 |
extended_location: |
| 149 |
description: |
| 150 |
- The extended location of the public ip address. |
| 151 |
type: str |
| 152 |
returned: always |
| 153 |
sample: 'eastus2' |
| 154 |
custom_ip_prefix: |
| 155 |
description: |
| 156 |
- The customIpPrefix that this prefix is associated with. |
| 157 |
type: dict |
| 158 |
returned: always |
| 159 |
sample: {} |
| 160 |
public_ip_addresses: |
| 161 |
description: |
| 162 |
- The list of all referenced PublicIPAddresses. |
| 163 |
type: list |
| 164 |
sample: [] |
| 165 |
resource_guid: |
| 166 |
description: |
| 167 |
- The resource GUID property of the public IP prefix resource. |
| 168 |
type: str |
| 169 |
sample: "47cafa04-851d-4579-894d-74ad6afe3233" |
| 170 |
ip_prefix: |
| 171 |
description: |
| 172 |
- The allocated Prefix. |
| 173 |
type: str |
| 174 |
sample: 20.199.95.80/29 |
| 175 |
''' |
| 176 |
try: |
| 177 |
from azure.core.exceptions import ResourceNotFoundError |
| 178 |
except Exception: |
| 179 |
|
| 180 |
pass |
| 181 |
|
| 182 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 183 |
|
| 184 |
AZURE_OBJECT_CLASS = 'PublicIpPrefix' |
| 185 |
|
| 186 |
|
| 187 |
class AzureRMPublicIPPrefixInfo(AzureRMModuleBase): |
| 188 |
|
| 189 |
def __init__(self): |
| 190 |
|
| 191 |
self.module_arg_spec = dict( |
| 192 |
name=dict(type='str'), |
| 193 |
resource_group=dict(type='str'), |
| 194 |
tags=dict(type='list', elements='str') |
| 195 |
) |
| 196 |
|
| 197 |
self.results = dict( |
| 198 |
changed=False, |
| 199 |
) |
| 200 |
|
| 201 |
self.name = None |
| 202 |
self.resource_group = None |
| 203 |
self.tags = None |
| 204 |
|
| 205 |
super(AzureRMPublicIPPrefixInfo, self).__init__(self.module_arg_spec, |
| 206 |
supports_check_mode=True, |
| 207 |
supports_tags=False, |
| 208 |
facts_module=True) |
| 209 |
|
| 210 |
def exec_module(self, **kwargs): |
| 211 |
for key in self.module_arg_spec: |
| 212 |
setattr(self, key, kwargs[key]) |
| 213 |
|
| 214 |
result = [] |
| 215 |
if self.name is not None and self.resource_group is not None: |
| 216 |
result = self.get_item() |
| 217 |
elif self.resource_group: |
| 218 |
result = self.list_resource_group() |
| 219 |
else: |
| 220 |
result = self.list_all() |
| 221 |
|
| 222 |
raw = self.filter(result) |
| 223 |
|
| 224 |
self.results['publicipprefixes'] = self.format(raw) |
| 225 |
|
| 226 |
return self.results |
| 227 |
|
| 228 |
def format(self, raw): |
| 229 |
return [self.prefix_to_dict(item) for item in raw] |
| 230 |
|
| 231 |
def filter(self, response): |
| 232 |
return [item for item in response if self.has_tags(item.tags, self.tags)] |
| 233 |
|
| 234 |
def prefix_to_dict(self, prefix): |
| 235 |
result = dict( |
| 236 |
id=prefix.id, |
| 237 |
name=prefix.name, |
| 238 |
tags=prefix.tags, |
| 239 |
type=prefix.type, |
| 240 |
location=prefix.location, |
| 241 |
public_ip_address_version=prefix.public_ip_address_version, |
| 242 |
prefix_length=prefix.prefix_length, |
| 243 |
provisioning_state=prefix.provisioning_state, |
| 244 |
etag=prefix.etag, |
| 245 |
zones=prefix.zones, |
| 246 |
sku=dict(), |
| 247 |
ip_tags=list(), |
| 248 |
custom_ip_prefix=dict(), |
| 249 |
ip_prefix=prefix.ip_prefix, |
| 250 |
resource_guid=prefix.resource_guid, |
| 251 |
public_ip_addresses=list(), |
| 252 |
extended_location=prefix.extended_location |
| 253 |
) |
| 254 |
if prefix.public_ip_addresses: |
| 255 |
result['public_ip_addresses'] = [x.id for x in prefix.public_ip_addresses] |
| 256 |
if prefix.sku: |
| 257 |
result['sku']['name'] = prefix.sku.name |
| 258 |
result['sku']['tier'] = prefix.sku.tier |
| 259 |
if prefix.custom_ip_prefix: |
| 260 |
result['custom_ip_prefix']['id'] = prefix.custom_ip_prefix.id |
| 261 |
if prefix.ip_tags: |
| 262 |
result['ip_tags'] = [dict(type=x.ip_tag_type, value=x.tag) for x in prefix.ip_tags] |
| 263 |
return result |
| 264 |
|
| 265 |
def get_item(self): |
| 266 |
self.log('Get properties for {0}'.format(self.name)) |
| 267 |
item = None |
| 268 |
try: |
| 269 |
item = self.network_client.public_ip_prefixes.get(self.resource_group, self.name) |
| 270 |
except ResourceNotFoundError: |
| 271 |
pass |
| 272 |
return [item] if item else [] |
| 273 |
|
| 274 |
def list_resource_group(self): |
| 275 |
self.log('List items in resource groups') |
| 276 |
try: |
| 277 |
response = self.network_client.public_ip_prefixes.list(self.resource_group) |
| 278 |
except ResourceNotFoundError as exc: |
| 279 |
self.fail("Error listing items in resource groups {0} - {1}".format(self.resource_group, str(exc))) |
| 280 |
return response |
| 281 |
|
| 282 |
def list_all(self): |
| 283 |
self.log('List all items') |
| 284 |
try: |
| 285 |
response = self.network_client.public_ip_prefixes.list_all() |
| 286 |
except ResourceNotFoundError as exc: |
| 287 |
self.fail("Error listing all items - {0}".format(str(exc))) |
| 288 |
return response |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
AzureRMPublicIPPrefixInfo() |
| 293 |
|
| 294 |
|
| 295 |
if __name__ == '__main__': |
| 296 |
main() |