| 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_cdnprofile_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure CDN profile facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific Azure CDN profile or all CDN profiles. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Limit results to a specific CDN profile. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- The resource group to search for the desired CDN profile. |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- azure.azcollection.azure |
| 39 |
|
| 40 |
author: |
| 41 |
- Hai Cao (@caohai) |
| 42 |
- Yunge Zhu (@yungezz) |
| 43 |
''' |
| 44 |
|
| 45 |
EXAMPLES = ''' |
| 46 |
- name: Get facts for one CDN profile |
| 47 |
azure_rm_cdnprofile_info: |
| 48 |
name: Testing |
| 49 |
resource_group: myResourceGroup |
| 50 |
|
| 51 |
- name: Get facts for all CDN profiles |
| 52 |
azure_rm_cdnprofile_info: |
| 53 |
|
| 54 |
- name: Get facts by tags |
| 55 |
azure_rm_cdnprofile_info: |
| 56 |
tags: |
| 57 |
- Environment:Test |
| 58 |
''' |
| 59 |
|
| 60 |
RETURN = ''' |
| 61 |
cdnprofiles: |
| 62 |
description: List of CDN profiles. |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
resource_group: |
| 67 |
description: |
| 68 |
- Name of a resource group where the CDN profile exists. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: myResourceGroup |
| 72 |
name: |
| 73 |
description: |
| 74 |
- Name of the CDN profile. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: Testing |
| 78 |
location: |
| 79 |
description: |
| 80 |
- Location of the CDN profile. |
| 81 |
type: str |
| 82 |
sample: WestUS |
| 83 |
id: |
| 84 |
description: |
| 85 |
- ID of the CDN profile. |
| 86 |
type: str |
| 87 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Cdn/profiles/myCDN |
| 88 |
provisioning_state: |
| 89 |
description: |
| 90 |
- Provisioning status of the profile. |
| 91 |
type: str |
| 92 |
sample: Succeeded |
| 93 |
resource_state: |
| 94 |
description: |
| 95 |
- Resource status of the profile. |
| 96 |
type: str |
| 97 |
sample: Active |
| 98 |
sku: |
| 99 |
description: |
| 100 |
- The pricing tier, defines a CDN provider, feature list and rate of the CDN profile. |
| 101 |
type: str |
| 102 |
sample: standard_verizon |
| 103 |
type: |
| 104 |
description: |
| 105 |
- The type of the CDN profile. |
| 106 |
type: str |
| 107 |
sample: Microsoft.Cdn/profiles |
| 108 |
tags: |
| 109 |
description: |
| 110 |
- The tags of the CDN profile. |
| 111 |
type: list |
| 112 |
sample: [ |
| 113 |
{"foo": "bar"} |
| 114 |
] |
| 115 |
''' |
| 116 |
|
| 117 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 118 |
import re |
| 119 |
|
| 120 |
AZURE_OBJECT_CLASS = 'profiles' |
| 121 |
|
| 122 |
|
| 123 |
class AzureRMCdnprofileInfo(AzureRMModuleBase): |
| 124 |
"""Utility class to get Azure CDN profile facts""" |
| 125 |
|
| 126 |
def __init__(self): |
| 127 |
|
| 128 |
self.module_args = dict( |
| 129 |
name=dict(type='str'), |
| 130 |
resource_group=dict(type='str'), |
| 131 |
tags=dict(type='list', elements='str') |
| 132 |
) |
| 133 |
|
| 134 |
self.results = dict( |
| 135 |
changed=False, |
| 136 |
cdnprofiles=[] |
| 137 |
) |
| 138 |
|
| 139 |
self.name = None |
| 140 |
self.resource_group = None |
| 141 |
self.tags = None |
| 142 |
|
| 143 |
super(AzureRMCdnprofileInfo, self).__init__( |
| 144 |
derived_arg_spec=self.module_args, |
| 145 |
supports_check_mode=True, |
| 146 |
supports_tags=False, |
| 147 |
facts_module=True |
| 148 |
) |
| 149 |
|
| 150 |
def exec_module(self, **kwargs): |
| 151 |
|
| 152 |
for key in self.module_args: |
| 153 |
setattr(self, key, kwargs[key]) |
| 154 |
|
| 155 |
if self.name and not self.resource_group: |
| 156 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 157 |
|
| 158 |
if self.name: |
| 159 |
self.results['cdnprofiles'] = self.get_item() |
| 160 |
elif self.resource_group: |
| 161 |
self.results['cdnprofiles'] = self.list_resource_group() |
| 162 |
else: |
| 163 |
self.results['cdnprofiles'] = self.list_all() |
| 164 |
|
| 165 |
return self.results |
| 166 |
|
| 167 |
def get_item(self): |
| 168 |
"""Get a single Azure CDN profile""" |
| 169 |
|
| 170 |
self.log('Get properties for {0}'.format(self.name)) |
| 171 |
|
| 172 |
item = None |
| 173 |
result = [] |
| 174 |
|
| 175 |
try: |
| 176 |
item = self.cdn_client.profiles.get(self.resource_group, self.name) |
| 177 |
except Exception: |
| 178 |
pass |
| 179 |
|
| 180 |
if item and self.has_tags(item.tags, self.tags): |
| 181 |
result = [self.serialize_cdnprofile(item)] |
| 182 |
|
| 183 |
return result |
| 184 |
|
| 185 |
def list_resource_group(self): |
| 186 |
"""Get all Azure CDN profiles within a resource group""" |
| 187 |
|
| 188 |
self.log('List all Azure CDNs within a resource group') |
| 189 |
|
| 190 |
try: |
| 191 |
response = self.cdn_client.profiles.list_by_resource_group(self.resource_group) |
| 192 |
except Exception as exc: |
| 193 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 194 |
|
| 195 |
results = [] |
| 196 |
for item in response: |
| 197 |
if self.has_tags(item.tags, self.tags): |
| 198 |
results.append(self.serialize_cdnprofile(item)) |
| 199 |
|
| 200 |
return results |
| 201 |
|
| 202 |
def list_all(self): |
| 203 |
"""Get all Azure CDN profiles within a subscription""" |
| 204 |
self.log('List all CDN profiles within a subscription') |
| 205 |
try: |
| 206 |
response = self.cdn_client.profiles.list() |
| 207 |
except Exception as exc: |
| 208 |
self.fail("Error listing all items - {0}".format(str(exc))) |
| 209 |
|
| 210 |
results = [] |
| 211 |
for item in response: |
| 212 |
if self.has_tags(item.tags, self.tags): |
| 213 |
results.append(self.serialize_cdnprofile(item)) |
| 214 |
return results |
| 215 |
|
| 216 |
def serialize_cdnprofile(self, cdnprofile): |
| 217 |
''' |
| 218 |
Convert a CDN profile object to dict. |
| 219 |
:param cdn: CDN profile object |
| 220 |
:return: dict |
| 221 |
''' |
| 222 |
result = self.serialize_obj(cdnprofile, AZURE_OBJECT_CLASS) |
| 223 |
|
| 224 |
new_result = {} |
| 225 |
new_result['id'] = cdnprofile.id |
| 226 |
new_result['resource_group'] = re.sub('\\/.*', '', re.sub('.*resourcegroups\\/', '', result['id'])) |
| 227 |
new_result['name'] = cdnprofile.name |
| 228 |
new_result['type'] = cdnprofile.type |
| 229 |
new_result['location'] = cdnprofile.location |
| 230 |
new_result['resource_state'] = cdnprofile.resource_state |
| 231 |
new_result['sku'] = cdnprofile.sku.name |
| 232 |
new_result['provisioning_state'] = cdnprofile.provisioning_state |
| 233 |
new_result['tags'] = cdnprofile.tags |
| 234 |
new_result['identity'] = cdnprofile.identity.as_dict() if cdnprofile.identity else None |
| 235 |
return new_result |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
"""Main module execution code path""" |
| 240 |
|
| 241 |
AzureRMCdnprofileInfo() |
| 242 |
|
| 243 |
|
| 244 |
if __name__ == '__main__': |
| 245 |
main() |