| 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_cdnendpoint_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure CDN endpoint facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific Azure CDN endpoint or all Azure CDN endpoints. |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- Name of resource group where this CDN profile belongs to. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
profile_name: |
| 29 |
description: |
| 30 |
- Name of CDN profile. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Limit results to a specific Azure CDN endpoint. |
| 36 |
type: str |
| 37 |
tags: |
| 38 |
description: |
| 39 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- azure.azcollection.azure |
| 45 |
|
| 46 |
author: |
| 47 |
- Hai Cao (@caohai) |
| 48 |
- Yunge zhu (@yungezz) |
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = ''' |
| 52 |
- name: Get facts for all endpoints in CDN profile |
| 53 |
azure_rm_cdnendpoint_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
profile_name: myCDNProfile |
| 56 |
tags: |
| 57 |
- key |
| 58 |
- key:value |
| 59 |
|
| 60 |
- name: Get facts of specific CDN endpoint |
| 61 |
azure_rm_cdnendpoint_info: |
| 62 |
resource_group: myResourceGroup |
| 63 |
profile_name: myCDNProfile |
| 64 |
name: myEndpoint1 |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = ''' |
| 68 |
cdnendpoints: |
| 69 |
description: List of Azure CDN endpoints. |
| 70 |
returned: always |
| 71 |
type: complex |
| 72 |
contains: |
| 73 |
resource_group: |
| 74 |
description: |
| 75 |
- Name of a resource group where the Azure CDN endpoint exists. |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: myResourceGroup |
| 79 |
name: |
| 80 |
description: |
| 81 |
- Name of the Azure CDN endpoint. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: myEndpoint |
| 85 |
profile_name: |
| 86 |
description: |
| 87 |
- Name of the Azure CDN profile that this endpoint is attached to. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: myProfile |
| 91 |
location: |
| 92 |
description: |
| 93 |
- Location of the Azure CDN endpoint. |
| 94 |
type: str |
| 95 |
sample: WestUS |
| 96 |
id: |
| 97 |
description: |
| 98 |
- ID of the Azure CDN endpoint. |
| 99 |
type: str |
| 100 |
sample: |
| 101 |
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myCDN/providers/Microsoft.Cdn/profiles/myProfile/endpoints/myEndpoint1" |
| 102 |
provisioning_state: |
| 103 |
description: |
| 104 |
- Provisioning status of the Azure CDN endpoint. |
| 105 |
type: str |
| 106 |
sample: Succeeded |
| 107 |
resource_state: |
| 108 |
description: |
| 109 |
- Resource status of the profile. |
| 110 |
type: str |
| 111 |
sample: Running |
| 112 |
is_compression_enabled: |
| 113 |
description: |
| 114 |
- Indicates whether content compression is enabled on CDN. |
| 115 |
type: bool |
| 116 |
sample: true |
| 117 |
is_http_allowed: |
| 118 |
description: |
| 119 |
- Indicates whether HTTP traffic is allowed on the endpoint. |
| 120 |
type: bool |
| 121 |
sample: true |
| 122 |
is_https_allowed: |
| 123 |
description: |
| 124 |
- Indicates whether HTTPS traffic is allowed on the endpoint. |
| 125 |
type: bool |
| 126 |
sample: true |
| 127 |
query_string_caching_behavior: |
| 128 |
description: |
| 129 |
- Defines how CDN caches requests that include query strings. |
| 130 |
type: str |
| 131 |
sample: IgnoreQueryString |
| 132 |
content_types_to_compress: |
| 133 |
description: |
| 134 |
- List of content types on which compression applies. |
| 135 |
type: list |
| 136 |
sample: [ |
| 137 |
"text/plain", |
| 138 |
"text/html", |
| 139 |
"text/css", |
| 140 |
"text/javascript", |
| 141 |
"application/x-javascript", |
| 142 |
"application/javascript", |
| 143 |
"application/json", |
| 144 |
"application/xml" |
| 145 |
] |
| 146 |
origins: |
| 147 |
description: |
| 148 |
- The source of the content being delivered via CDN. |
| 149 |
sample: { |
| 150 |
"host_name": "xxxxxxxx.blob.core.windows.net", |
| 151 |
"http_port": null, |
| 152 |
"https_port": null, |
| 153 |
"name": "xxxxxxxx-blob-core-windows-net" |
| 154 |
} |
| 155 |
type: dict |
| 156 |
origin_host_header: |
| 157 |
description: |
| 158 |
- The host header value sent to the origin with each request. |
| 159 |
type: str |
| 160 |
sample: xxxxxxxx.blob.core.windows.net |
| 161 |
origin_path: |
| 162 |
description: |
| 163 |
- A directory path on the origin that CDN can use to retrieve content from. |
| 164 |
type: str |
| 165 |
sample: /pic/ |
| 166 |
tags: |
| 167 |
description: |
| 168 |
- The tags of the Azure CDN endpoint. |
| 169 |
type: list |
| 170 |
sample: foo |
| 171 |
''' |
| 172 |
|
| 173 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 174 |
import re |
| 175 |
|
| 176 |
AZURE_OBJECT_CLASS = 'endpoints' |
| 177 |
|
| 178 |
|
| 179 |
class AzureRMCdnEndpointInfo(AzureRMModuleBase): |
| 180 |
"""Utility class to get Azure Azure CDN endpoint facts""" |
| 181 |
|
| 182 |
def __init__(self): |
| 183 |
|
| 184 |
self.module_args = dict( |
| 185 |
name=dict(type='str'), |
| 186 |
resource_group=dict( |
| 187 |
type='str', |
| 188 |
required=True |
| 189 |
), |
| 190 |
profile_name=dict( |
| 191 |
type='str', |
| 192 |
required=True |
| 193 |
), |
| 194 |
tags=dict( |
| 195 |
type='list', |
| 196 |
elements='str' |
| 197 |
) |
| 198 |
) |
| 199 |
|
| 200 |
self.results = dict( |
| 201 |
changed=False, |
| 202 |
cdnendpoints=[] |
| 203 |
) |
| 204 |
|
| 205 |
self.name = None |
| 206 |
self.resource_group = None |
| 207 |
self.profile_name = None |
| 208 |
self.tags = None |
| 209 |
|
| 210 |
super(AzureRMCdnEndpointInfo, self).__init__( |
| 211 |
supports_check_mode=True, |
| 212 |
derived_arg_spec=self.module_args, |
| 213 |
supports_tags=False, |
| 214 |
facts_module=True |
| 215 |
) |
| 216 |
|
| 217 |
def exec_module(self, **kwargs): |
| 218 |
|
| 219 |
for key in self.module_args: |
| 220 |
setattr(self, key, kwargs[key]) |
| 221 |
|
| 222 |
if self.name: |
| 223 |
self.results['cdnendpoints'] = self.get_item() |
| 224 |
else: |
| 225 |
self.results['cdnendpoints'] = self.list_by_profile() |
| 226 |
|
| 227 |
return self.results |
| 228 |
|
| 229 |
def get_item(self): |
| 230 |
"""Get a single Azure Azure CDN endpoint""" |
| 231 |
|
| 232 |
self.log('Get properties for {0}'.format(self.name)) |
| 233 |
|
| 234 |
item = None |
| 235 |
result = [] |
| 236 |
|
| 237 |
try: |
| 238 |
item = self.cdn_client.endpoints.get( |
| 239 |
self.resource_group, self.profile_name, self.name) |
| 240 |
except Exception: |
| 241 |
pass |
| 242 |
|
| 243 |
if item and self.has_tags(item.tags, self.tags): |
| 244 |
result = [self.serialize_cdnendpoint(item)] |
| 245 |
|
| 246 |
return result |
| 247 |
|
| 248 |
def list_by_profile(self): |
| 249 |
"""Get all Azure Azure CDN endpoints within an Azure CDN profile""" |
| 250 |
|
| 251 |
self.log('List all Azure CDN endpoints within an Azure CDN profile') |
| 252 |
|
| 253 |
try: |
| 254 |
response = self.cdn_client.endpoints.list_by_profile( |
| 255 |
self.resource_group, self.profile_name) |
| 256 |
except Exception as exc: |
| 257 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 258 |
|
| 259 |
results = [] |
| 260 |
for item in response: |
| 261 |
if self.has_tags(item.tags, self.tags): |
| 262 |
results.append(self.serialize_cdnendpoint(item)) |
| 263 |
|
| 264 |
return results |
| 265 |
|
| 266 |
def serialize_cdnendpoint(self, cdnendpoint): |
| 267 |
''' |
| 268 |
Convert a Azure CDN endpoint object to dict. |
| 269 |
:param cdn: Azure CDN endpoint object |
| 270 |
:return: dict |
| 271 |
''' |
| 272 |
result = self.serialize_obj(cdnendpoint, AZURE_OBJECT_CLASS) |
| 273 |
|
| 274 |
new_result = {} |
| 275 |
new_result['id'] = cdnendpoint.id |
| 276 |
new_result['resource_group'] = re.sub('\\/.*', '', re.sub('.*resourcegroups\\/', '', result['id'])) |
| 277 |
new_result['profile_name'] = re.sub('\\/.*', '', re.sub('.*profiles\\/', '', result['id'])) |
| 278 |
new_result['name'] = cdnendpoint.name |
| 279 |
new_result['type'] = cdnendpoint.type |
| 280 |
new_result['location'] = cdnendpoint.location |
| 281 |
new_result['resource_state'] = cdnendpoint.resource_state |
| 282 |
new_result['provisioning_state'] = cdnendpoint.provisioning_state |
| 283 |
new_result['query_string_caching_behavior'] = cdnendpoint.query_string_caching_behavior |
| 284 |
new_result['is_compression_enabled'] = cdnendpoint.is_compression_enabled |
| 285 |
new_result['is_http_allowed'] = cdnendpoint.is_http_allowed |
| 286 |
new_result['is_https_allowed'] = cdnendpoint.is_https_allowed |
| 287 |
new_result['content_types_to_compress'] = cdnendpoint.content_types_to_compress |
| 288 |
new_result['origin_host_header'] = cdnendpoint.origin_host_header |
| 289 |
new_result['origin_path'] = cdnendpoint.origin_path |
| 290 |
new_result['origin'] = dict( |
| 291 |
name=cdnendpoint.origins[0].name, |
| 292 |
host_name=cdnendpoint.origins[0].host_name, |
| 293 |
http_port=cdnendpoint.origins[0].http_port, |
| 294 |
https_port=cdnendpoint.origins[0].https_port |
| 295 |
) |
| 296 |
new_result['tags'] = cdnendpoint.tags |
| 297 |
return new_result |
| 298 |
|
| 299 |
|
| 300 |
def main(): |
| 301 |
"""Main module execution code path""" |
| 302 |
|
| 303 |
AzureRMCdnEndpointInfo() |
| 304 |
|
| 305 |
|
| 306 |
if __name__ == '__main__': |
| 307 |
main() |