| 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_appconfiguration_info |
| 14 |
version_added: "3.15.0" |
| 15 |
short_description: Get Azure App Configuration store facts |
| 16 |
description: |
| 17 |
- Get facts for Azure App Configuration stores (Microsoft.AppConfiguration/configurationStores). |
| 18 |
- Supports get by name, list by resource group and list by subscription. |
| 19 |
- Optionally includes store replicas when fetching by name. |
| 20 |
|
| 21 |
options: |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- The name of the resource group to which the App Configuration store belongs. |
| 25 |
type: str |
| 26 |
appconfig_name: |
| 27 |
description: |
| 28 |
- The name of the App Configuration store. |
| 29 |
type: str |
| 30 |
include_replicas: |
| 31 |
description: |
| 32 |
- When C(appconfig_name) is specified, indicates whether to include the store replicas in the response. |
| 33 |
- Disabled for list operations to avoid extra calls by default. |
| 34 |
type: bool |
| 35 |
default: false |
| 36 |
tags: |
| 37 |
description: |
| 38 |
- Limit results by a list of tags. Format tags as C(key) or C(key:value). |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure |
| 44 |
|
| 45 |
author: |
| 46 |
- Zun Yang (@zunyangc) |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = ''' |
| 50 |
- name: Get App Configuration store |
| 51 |
azure_rm_appconfiguration_info: |
| 52 |
resource_group: myRG |
| 53 |
appconfig_name: myAGS |
| 54 |
|
| 55 |
- name: Get App Configuration store with replicas |
| 56 |
azure_rm_appconfiguration_info: |
| 57 |
resource_group: myRG |
| 58 |
appconfig_name: myAGS |
| 59 |
include_replicas: true |
| 60 |
|
| 61 |
- name: List stores in RG |
| 62 |
azure_rm_appconfiguration_info: |
| 63 |
resource_group: myRG |
| 64 |
|
| 65 |
- name: List stores in subscription |
| 66 |
azure_rm_appconfiguration_info: |
| 67 |
''' |
| 68 |
|
| 69 |
RETURN = ''' |
| 70 |
appconfigurations: |
| 71 |
description: List of App Configuration stores. |
| 72 |
returned: always |
| 73 |
type: list |
| 74 |
contains: |
| 75 |
appconfig_name: |
| 76 |
description: |
| 77 |
- Name of the App Configuration store. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: myAGS |
| 81 |
id: |
| 82 |
description: |
| 83 |
- Resource ID of the App Configuration store. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: /subscriptions/xxxx/resourceGroups/myRG/providers/Microsoft.AppConfiguration/configurationStores/myAGS |
| 87 |
location: |
| 88 |
description: |
| 89 |
- Azure location of the App Configuration store. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: eastus |
| 93 |
tags: |
| 94 |
description: |
| 95 |
- List of tags. |
| 96 |
type: dict |
| 97 |
sample: "{'env': 'prod', 'department': 'finance'}" |
| 98 |
sku: |
| 99 |
description: |
| 100 |
- SKU of the App Configuration store. |
| 101 |
returned: always |
| 102 |
type: dict |
| 103 |
contains: |
| 104 |
name: |
| 105 |
description: SKU name. |
| 106 |
type: str |
| 107 |
returned: always |
| 108 |
sample: Standard |
| 109 |
public_network_access: |
| 110 |
description: |
| 111 |
- Whether public network access is enabled. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: Enabled |
| 115 |
disable_local_auth: |
| 116 |
description: |
| 117 |
- Whether local authentication is disabled. |
| 118 |
returned: always |
| 119 |
type: bool |
| 120 |
sample: false |
| 121 |
replicas: |
| 122 |
description: |
| 123 |
- List of replicas for the App Configuration store. |
| 124 |
returned: when include_replicas is true and appconfig_name is specified |
| 125 |
type: list |
| 126 |
elements: dict |
| 127 |
contains: |
| 128 |
name: |
| 129 |
description: |
| 130 |
- Name of the replica. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: myAGSrep |
| 134 |
location: |
| 135 |
description: |
| 136 |
- Azure location of the replica. |
| 137 |
returned: always |
| 138 |
type: str |
| 139 |
sample: westus |
| 140 |
id: |
| 141 |
description: |
| 142 |
- Resource ID of the replica. |
| 143 |
returned: always |
| 144 |
type: str |
| 145 |
sample: /subscriptions/xxxx/resourceGroups/myrg/providers/Microsoft.AppConfiguration/configurationStores/myAGS/replicas/myAGSrep |
| 146 |
soft_delete_retention_in_days: |
| 147 |
description: |
| 148 |
- Number of days to retain soft-deleted items. |
| 149 |
returned: always |
| 150 |
type: int |
| 151 |
sample: 7 |
| 152 |
enable_purge_protection: |
| 153 |
description: |
| 154 |
- Whether purge protection is enabled. |
| 155 |
returned: always |
| 156 |
type: bool |
| 157 |
sample: true |
| 158 |
identity: |
| 159 |
description: |
| 160 |
- Managed identity information. |
| 161 |
returned: always |
| 162 |
type: dict |
| 163 |
contains: |
| 164 |
type: |
| 165 |
description: Type of managed identity. |
| 166 |
type: str |
| 167 |
returned: always |
| 168 |
sample: SystemAssigned |
| 169 |
principal_id: |
| 170 |
description: Principal ID of the managed identity. |
| 171 |
type: str |
| 172 |
returned: always |
| 173 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 174 |
tenant_id: |
| 175 |
description: Tenant ID of the managed identity. |
| 176 |
type: str |
| 177 |
returned: always |
| 178 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 179 |
''' |
| 180 |
|
| 181 |
|
| 182 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 183 |
|
| 184 |
try: |
| 185 |
from azure.mgmt.appconfiguration import AppConfigurationManagementClient |
| 186 |
from azure.core.exceptions import ResourceNotFoundError |
| 187 |
except ImportError: |
| 188 |
|
| 189 |
pass |
| 190 |
|
| 191 |
|
| 192 |
def appconfig_to_dict(appconfig, replicas=None, include_replicas=False): |
| 193 |
s = appconfig.as_dict() |
| 194 |
|
| 195 |
data = dict( |
| 196 |
id=s.get("id"), |
| 197 |
appconfig_name=s.get("name"), |
| 198 |
location=s.get("location"), |
| 199 |
tags=s.get("tags"), |
| 200 |
sku=dict(name=s.get("sku", {}).get("name")), |
| 201 |
|
| 202 |
endpoint=s.get("endpoint"), |
| 203 |
public_network_access=s.get("public_network_access"), |
| 204 |
disable_local_auth=s.get("disable_local_auth"), |
| 205 |
enable_purge_protection=s.get("enable_purge_protection"), |
| 206 |
soft_delete_retention_in_days=s.get("soft_delete_retention_in_days"), |
| 207 |
identity=s.get("identity") |
| 208 |
) |
| 209 |
|
| 210 |
|
| 211 |
if include_replicas: |
| 212 |
data["replicas"] = replicas or [] |
| 213 |
|
| 214 |
return data |
| 215 |
|
| 216 |
|
| 217 |
class AzureRMAppConfigurationInfo(AzureRMModuleBase): |
| 218 |
def __init__(self): |
| 219 |
self.module_arg_spec = dict( |
| 220 |
resource_group=dict(type='str'), |
| 221 |
appconfig_name=dict(type='str'), |
| 222 |
include_replicas=dict(type='bool', default=False), |
| 223 |
tags=dict(type='list', elements='str'), |
| 224 |
) |
| 225 |
|
| 226 |
self.resource_group = None |
| 227 |
self.appconfig_name = None |
| 228 |
self.include_replicas = None |
| 229 |
self.tags = None |
| 230 |
self.results = dict(changed=False) |
| 231 |
self._client = None |
| 232 |
|
| 233 |
super(AzureRMAppConfigurationInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 234 |
supports_check_mode=True, |
| 235 |
supports_tags=False, |
| 236 |
facts_module=True) |
| 237 |
|
| 238 |
def exec_module(self, **kwargs): |
| 239 |
"""Main module execution method""" |
| 240 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 241 |
if hasattr(self, key): |
| 242 |
setattr(self, key, kwargs[key]) |
| 243 |
|
| 244 |
self._client = self.get_mgmt_svc_client(AppConfigurationManagementClient, |
| 245 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 246 |
|
| 247 |
if self.appconfig_name: |
| 248 |
if not self.resource_group: |
| 249 |
self.fail("Parameter 'resource_group' is required when filtering by appconfig_name.") |
| 250 |
self.results['appconfigurations'] = [self.get_by_appconfig_name()] |
| 251 |
|
| 252 |
elif self.resource_group: |
| 253 |
self.results['appconfigurations'] = self.list_appconfig_by_resource_group() |
| 254 |
|
| 255 |
else: |
| 256 |
self.results["appconfigurations"] = self.list_appconfig_by_subscription() |
| 257 |
|
| 258 |
return self.results |
| 259 |
|
| 260 |
def get_by_appconfig_name(self): |
| 261 |
''' |
| 262 |
Get the properties of this specified app configuration store. |
| 263 |
|
| 264 |
:return: deserialized app configuration store state dictionary. |
| 265 |
''' |
| 266 |
try: |
| 267 |
s = self._client.configuration_stores.get(resource_group_name=self.resource_group, |
| 268 |
config_store_name=self.appconfig_name) |
| 269 |
replicas = self.list_appconfig_replicas(self.resource_group, self.appconfig_name) if self.include_replicas else [] |
| 270 |
if self.has_tags(s.tags, self.tags): |
| 271 |
return appconfig_to_dict(s, replicas, self.include_replicas) |
| 272 |
except ResourceNotFoundError as e: |
| 273 |
self.log("Did not find the app configurations {0}: {1}".format(self.appconfig_name, str(e))) |
| 274 |
|
| 275 |
def list_appconfig_by_resource_group(self): |
| 276 |
''' |
| 277 |
List the properties of app configuration store in specific resource group. |
| 278 |
|
| 279 |
:return: deserialized app configuration store state dictionary. |
| 280 |
''' |
| 281 |
results = [] |
| 282 |
try: |
| 283 |
stores = list(self._client.configuration_stores.list_by_resource_group(resource_group_name=self.resource_group)) |
| 284 |
|
| 285 |
if stores: |
| 286 |
for s in stores: |
| 287 |
if not self.has_tags(s.tags, self.tags): |
| 288 |
continue |
| 289 |
|
| 290 |
replicas = self.list_appconfig_replicas(self.resource_group, s.name) if self.include_replicas else [] |
| 291 |
results.append(appconfig_to_dict(s, replicas, self.include_replicas)) |
| 292 |
except Exception as e: |
| 293 |
self.log("Did not find appconfig in resource group {0} : {1}.".format(self.resource_group, str(e))) |
| 294 |
|
| 295 |
return results |
| 296 |
|
| 297 |
def list_appconfig_by_subscription(self): |
| 298 |
''' |
| 299 |
List the properties of app configuration stores in specific subscription. |
| 300 |
|
| 301 |
:return: deserialized app configuration stores state dictionary. |
| 302 |
''' |
| 303 |
results = [] |
| 304 |
try: |
| 305 |
stores = list(self._client.configuration_stores.list()) |
| 306 |
|
| 307 |
for s in stores: |
| 308 |
if not self.has_tags(s.tags, self.tags): |
| 309 |
continue |
| 310 |
|
| 311 |
|
| 312 |
parts = s.id.split("/") |
| 313 |
rg = parts[4] |
| 314 |
name = parts[8] |
| 315 |
|
| 316 |
replicas = self.list_appconfig_replicas(rg, name) if self.include_replicas else [] |
| 317 |
|
| 318 |
store_obj = self._client.configuration_stores.get(rg, name) |
| 319 |
results.append(appconfig_to_dict(store_obj, replicas, self.include_replicas)) |
| 320 |
except Exception as e: |
| 321 |
self.log("Did not find appconfig in current subscription {0}.", format(str(e))) |
| 322 |
return results |
| 323 |
|
| 324 |
def list_appconfig_replicas(self, rg, name): |
| 325 |
''' |
| 326 |
List replicas of the specific app configuration stores |
| 327 |
|
| 328 |
:return: name, id and location of the replicas |
| 329 |
''' |
| 330 |
rlist = [] |
| 331 |
pager = self._client.replicas.list_by_configuration_store(resource_group_name=rg, |
| 332 |
config_store_name=name) |
| 333 |
for r in pager: |
| 334 |
rlist.append(dict(id=r.id, |
| 335 |
name=r.name, |
| 336 |
location=r.location)) |
| 337 |
return rlist |
| 338 |
|
| 339 |
|
| 340 |
def main(): |
| 341 |
"""Main execution""" |
| 342 |
AzureRMAppConfigurationInfo() |
| 343 |
|
| 344 |
|
| 345 |
if __name__ == '__main__': |
| 346 |
main() |