| 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_rediscache_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure Cache for Redis instance facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for Azure Cache for Redis instance. |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The resource group to search for the desired Azure Cache for Redis. |
| 26 |
required: True |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Limit results to a specific Azure Cache for Redis. |
| 31 |
type: str |
| 32 |
return_access_keys: |
| 33 |
description: |
| 34 |
- Indicate weather to return access keys of the Azure Cache for Redis. |
| 35 |
default: False |
| 36 |
type: bool |
| 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 |
- Yunge Zhu (@yungezz) |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = ''' |
| 51 |
- name: Get Azure Cache for Redis by name |
| 52 |
azure_rm_rediscache_info: |
| 53 |
resource_group: myResourceGroup |
| 54 |
name: myRedis |
| 55 |
|
| 56 |
- name: Get Azure Cache for Redis with access keys by name |
| 57 |
azure_rm_rediscache_info: |
| 58 |
resource_group: myResourceGroup |
| 59 |
name: myRedis |
| 60 |
return_access_keys: true |
| 61 |
|
| 62 |
- name: Get Azure Cache for Redis in specific resource group |
| 63 |
azure_rm_rediscache_info: |
| 64 |
resource_group: myResourceGroup |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = ''' |
| 68 |
rediscaches: |
| 69 |
description: |
| 70 |
- List of Azure Cache for Redis instances. |
| 71 |
returned: always |
| 72 |
type: complex |
| 73 |
contains: |
| 74 |
resource_group: |
| 75 |
description: |
| 76 |
- Name of a resource group where the Azure Cache for Redis belongs to. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: myResourceGroup |
| 80 |
name: |
| 81 |
description: |
| 82 |
- Name of the Azure Cache for Redis. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: myRedis |
| 86 |
id: |
| 87 |
description: |
| 88 |
- Id of the Azure Cache for Redis. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myRedis |
| 92 |
provisioning_state: |
| 93 |
description: |
| 94 |
- Provisioning state of the Redis cache. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: Creating |
| 98 |
location: |
| 99 |
description: |
| 100 |
- Location of the Azure Cache for Redis. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: WestUS |
| 104 |
enable_non_ssl_port: |
| 105 |
description: |
| 106 |
- Specifies whether the non-ssl Redis server port (6379) is enabled. |
| 107 |
returned: always |
| 108 |
type: bool |
| 109 |
sample: false |
| 110 |
sku: |
| 111 |
description: |
| 112 |
- Dict of SKU information. |
| 113 |
returned: always |
| 114 |
type: dict |
| 115 |
contains: |
| 116 |
name: |
| 117 |
description: |
| 118 |
- Name of the SKU. |
| 119 |
returned: always |
| 120 |
type: str |
| 121 |
sample: standard |
| 122 |
size: |
| 123 |
description: |
| 124 |
- Size of the Azure Cache for Redis. |
| 125 |
returned: always |
| 126 |
type: str |
| 127 |
sample: C1 |
| 128 |
static_ip: |
| 129 |
description: |
| 130 |
- Static IP address. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: 10.75.0.11 |
| 134 |
subnet: |
| 135 |
description: |
| 136 |
- The full resource ID of a subnet in a virtual network to deploy the Azure Cache for Redis in. |
| 137 |
returned: always |
| 138 |
type: str |
| 139 |
sample: |
| 140 |
- "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/Microsoft.Network/VirtualNetworks/myVirtualNetwo |
| 141 |
rk/subnets/mySubnet" |
| 142 |
configuration: |
| 143 |
description: |
| 144 |
- Dict of Redis configuration. |
| 145 |
returned: always |
| 146 |
type: dict |
| 147 |
sample: maxmeory_reserved |
| 148 |
host_name: |
| 149 |
description: |
| 150 |
- Redis host name. |
| 151 |
returned: always |
| 152 |
type: str |
| 153 |
sample: testRedis.redis.cache.windows.net |
| 154 |
minimum_tls_version: |
| 155 |
description: |
| 156 |
- The version TLS clients at which must connect. |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: 1.2 |
| 160 |
version_added: "1.10.0" |
| 161 |
public_network_access: |
| 162 |
description: |
| 163 |
- Whether or not public endpoint access is allowed for this cache. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: Enabled |
| 167 |
version_added: "1.10.0" |
| 168 |
redis_version: |
| 169 |
description: |
| 170 |
- The version of Redis. |
| 171 |
returned: always |
| 172 |
type: str |
| 173 |
sample: 6.0.14 |
| 174 |
version_added: "1.10.0" |
| 175 |
shard_count: |
| 176 |
description: |
| 177 |
- The number of shards on a Premium Cluster Cache. |
| 178 |
returned: always |
| 179 |
type: int |
| 180 |
sample: 1 |
| 181 |
tenant_settings: |
| 182 |
description: |
| 183 |
- Dict of tenant settings. |
| 184 |
returned: always |
| 185 |
type: dict |
| 186 |
sample: { "key1": "value1" } |
| 187 |
tags: |
| 188 |
description: |
| 189 |
- Resource tags. |
| 190 |
returned: always |
| 191 |
type: dict |
| 192 |
sample: { "key1": "value1" } |
| 193 |
access_keys: |
| 194 |
description: |
| 195 |
- Azure Cache for Redis access keys. |
| 196 |
type: dict |
| 197 |
returned: when I(return_access_keys=true) |
| 198 |
contains: |
| 199 |
primary: |
| 200 |
description: |
| 201 |
- The current primary key that clients can use to authenticate the Redis cache. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: X2xXXxx7xxxxxx5xxxx0xxxxx75xxxxxxxxXXXxxxxx= |
| 205 |
secondary: |
| 206 |
description: |
| 207 |
- The current secondary key that clients can use to authenticate the Redis cache. |
| 208 |
returned: always |
| 209 |
type: str |
| 210 |
sample: X2xXXxx7xxxxxx5xxxx0xxxxx75xxxxxxxxXXXxxxxx= |
| 211 |
''' |
| 212 |
|
| 213 |
try: |
| 214 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 215 |
from azure.mgmt.redis import RedisManagementClient |
| 216 |
from azure.core.exceptions import ResourceNotFoundError |
| 217 |
except ImportError: |
| 218 |
|
| 219 |
pass |
| 220 |
|
| 221 |
import re |
| 222 |
|
| 223 |
|
| 224 |
class AzureRMRedisCacheInfo(AzureRMModuleBase): |
| 225 |
"""Utility class to get Azure Cache for Redis facts""" |
| 226 |
|
| 227 |
def __init__(self): |
| 228 |
|
| 229 |
self.module_args = dict( |
| 230 |
name=dict(type='str'), |
| 231 |
resource_group=dict( |
| 232 |
type='str', |
| 233 |
required=True |
| 234 |
), |
| 235 |
return_access_keys=dict( |
| 236 |
type='bool', |
| 237 |
default=False |
| 238 |
), |
| 239 |
tags=dict(type='list', elements='str') |
| 240 |
) |
| 241 |
|
| 242 |
self.results = dict( |
| 243 |
changed=False, |
| 244 |
rediscaches=[] |
| 245 |
) |
| 246 |
|
| 247 |
self.name = None |
| 248 |
self.resource_group = None |
| 249 |
self.profile_name = None |
| 250 |
self.tags = None |
| 251 |
|
| 252 |
self._client = None |
| 253 |
|
| 254 |
super(AzureRMRedisCacheInfo, self).__init__( |
| 255 |
derived_arg_spec=self.module_args, |
| 256 |
supports_check_mode=True, |
| 257 |
supports_tags=False, |
| 258 |
facts_module=True |
| 259 |
) |
| 260 |
|
| 261 |
def exec_module(self, **kwargs): |
| 262 |
|
| 263 |
for key in self.module_args: |
| 264 |
setattr(self, key, kwargs[key]) |
| 265 |
|
| 266 |
|
| 267 |
self._client = self.get_mgmt_svc_client(RedisManagementClient, |
| 268 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 269 |
api_version='2024-03-01') |
| 270 |
|
| 271 |
if self.name: |
| 272 |
self.results['rediscaches'] = self.get_item() |
| 273 |
else: |
| 274 |
self.results['rediscaches'] = self.list_by_resourcegroup() |
| 275 |
|
| 276 |
return self.results |
| 277 |
|
| 278 |
def get_item(self): |
| 279 |
"""Get a single Azure Cache for Redis""" |
| 280 |
|
| 281 |
self.log('Get properties for {0}'.format(self.name)) |
| 282 |
|
| 283 |
item = None |
| 284 |
result = [] |
| 285 |
|
| 286 |
try: |
| 287 |
item = self._client.redis.get(resource_group_name=self.resource_group, name=self.name) |
| 288 |
except ResourceNotFoundError: |
| 289 |
pass |
| 290 |
|
| 291 |
if item and self.has_tags(item.tags, self.tags): |
| 292 |
result = [self.serialize_rediscache(item)] |
| 293 |
|
| 294 |
return result |
| 295 |
|
| 296 |
def list_by_resourcegroup(self): |
| 297 |
"""Get all Azure Cache for Redis within a resource group""" |
| 298 |
|
| 299 |
self.log('List all Azure Cache for Redis within a resource group') |
| 300 |
|
| 301 |
try: |
| 302 |
response = self._client.redis.list_by_resource_group(self.resource_group) |
| 303 |
except Exception as exc: |
| 304 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 305 |
|
| 306 |
results = [] |
| 307 |
for item in response: |
| 308 |
if self.has_tags(item.tags, self.tags): |
| 309 |
results.append(self.serialize_rediscache(item)) |
| 310 |
|
| 311 |
return results |
| 312 |
|
| 313 |
def list_keys(self): |
| 314 |
"""List Azure Cache for Redis keys""" |
| 315 |
|
| 316 |
self.log('List keys for {0}'.format(self.name)) |
| 317 |
|
| 318 |
item = None |
| 319 |
|
| 320 |
try: |
| 321 |
item = self._client.redis.list_keys(resource_group_name=self.resource_group, name=self.name) |
| 322 |
except Exception as exc: |
| 323 |
self.fail("Failed to list redis keys of {0} - {1}".format(self.name, str(exc))) |
| 324 |
|
| 325 |
return item |
| 326 |
|
| 327 |
def serialize_rediscache(self, rediscache): |
| 328 |
''' |
| 329 |
Convert an Azure Cache for Redis object to dict. |
| 330 |
:param rediscache: Azure Cache for Redis object |
| 331 |
:return: dict |
| 332 |
''' |
| 333 |
new_result = dict( |
| 334 |
id=rediscache.id, |
| 335 |
resource_group=re.sub('\\/.*', '', re.sub('.*resourceGroups\\/', '', rediscache.id)), |
| 336 |
name=rediscache.name, |
| 337 |
location=rediscache.location, |
| 338 |
provisioning_state=rediscache.provisioning_state, |
| 339 |
tenant_settings=rediscache.tenant_settings, |
| 340 |
minimum_tls_version=rediscache.minimum_tls_version, |
| 341 |
public_network_access=rediscache.public_network_access, |
| 342 |
redis_version=rediscache.redis_version, |
| 343 |
shard_count=rediscache.shard_count, |
| 344 |
enable_non_ssl_port=rediscache.enable_non_ssl_port, |
| 345 |
static_ip=rediscache.static_ip, |
| 346 |
subnet=rediscache.subnet_id, |
| 347 |
host_name=rediscache.host_name, |
| 348 |
tags=rediscache.tags, |
| 349 |
identity=rediscache.identity.as_dict() if rediscache.identity else None, |
| 350 |
configuration=rediscache.redis_configuration.as_dict() if rediscache.redis_configuration else None |
| 351 |
) |
| 352 |
|
| 353 |
if rediscache.sku: |
| 354 |
new_result['sku'] = dict( |
| 355 |
name=rediscache.sku.name.lower(), |
| 356 |
size=rediscache.sku.family + str(rediscache.sku.capacity) |
| 357 |
) |
| 358 |
if self.return_access_keys: |
| 359 |
access_keys = self.list_keys() |
| 360 |
if access_keys: |
| 361 |
new_result['access_keys'] = dict( |
| 362 |
primary=access_keys.primary_key, |
| 363 |
secondary=access_keys.secondary_key |
| 364 |
) |
| 365 |
return new_result |
| 366 |
|
| 367 |
|
| 368 |
def main(): |
| 369 |
"""Main module execution code path""" |
| 370 |
|
| 371 |
AzureRMRedisCacheInfo() |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == '__main__': |
| 375 |
main() |