| 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_appconfigurationkey_info |
| 14 |
version_added: "3.15.0" |
| 15 |
short_description: Get key-values (and Key Vault references) from Azure App Configuration. |
| 16 |
description: |
| 17 |
- Read configuration settings (key-values or Key Vault references) from an Azure App Configuration store. |
| 18 |
- Supports lookup by key, by label, or by full listing. |
| 19 |
|
| 20 |
options: |
| 21 |
endpoint: |
| 22 |
description: |
| 23 |
- The App Configuration data-plane endpoint, e.g. C(https://myappconf.azconfig.io). |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
key: |
| 27 |
description: |
| 28 |
- Optional key filter. Supports exact key. |
| 29 |
type: str |
| 30 |
label: |
| 31 |
description: |
| 32 |
- Optional label filter. |
| 33 |
type: str |
| 34 |
|
| 35 |
extends_documentation_fragment: |
| 36 |
- azure.azcollection.azure |
| 37 |
|
| 38 |
author: |
| 39 |
- Zun Yang (@zunyangc) |
| 40 |
''' |
| 41 |
|
| 42 |
EXAMPLES = ''' |
| 43 |
- name: List all settings via endpoint |
| 44 |
azure.azcollection.azure_rm_appconfigurationkey_info: |
| 45 |
endpoint: https://myappconf.azconfig.io |
| 46 |
|
| 47 |
- name: Get settings with a specific key + label |
| 48 |
azure.azcollection.azure_rm_appconfigurationkey_info: |
| 49 |
endpoint: https://myappconf.azconfig.io |
| 50 |
key: "Payments:TimeoutSeconds" |
| 51 |
label: "prod" |
| 52 |
''' |
| 53 |
|
| 54 |
RETURN = ''' |
| 55 |
settings: |
| 56 |
description: |
| 57 |
- List of configuration settings returned from the store. |
| 58 |
returned: always |
| 59 |
type: list |
| 60 |
contains: |
| 61 |
key: |
| 62 |
description: |
| 63 |
- The key of the configuration setting. |
| 64 |
type: str |
| 65 |
returned: always |
| 66 |
sample: "Payments:TimeoutSeconds" |
| 67 |
label: |
| 68 |
description: |
| 69 |
- The label of the configuration setting. |
| 70 |
type: str |
| 71 |
returned: always |
| 72 |
sample: "prod" |
| 73 |
value: |
| 74 |
description: |
| 75 |
- The value for C(type=kv). |
| 76 |
type: str |
| 77 |
returned: always |
| 78 |
sample: "30" |
| 79 |
content_type: |
| 80 |
description: |
| 81 |
- Content type metadata for the setting. |
| 82 |
type: str |
| 83 |
returned: always |
| 84 |
sample: "application/json" |
| 85 |
read_only: |
| 86 |
description: |
| 87 |
- Whether the setting is read-only in App Configuration. |
| 88 |
type: bool |
| 89 |
returned: always |
| 90 |
sample: false |
| 91 |
type: |
| 92 |
description: |
| 93 |
- The type of the configuration setting. Possible values are C(kv) and C(vault). |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
sample: "kv" |
| 97 |
vault_key_reference: |
| 98 |
description: |
| 99 |
- The Key Vault reference details for C(type=vault). |
| 100 |
type: str |
| 101 |
returned: when applicable |
| 102 |
sample: "https://myvault.vault.azure.net/secrets/mysecret/xxxx" |
| 103 |
id: |
| 104 |
description: |
| 105 |
- The unique identifier of the configuration setting. |
| 106 |
type: str |
| 107 |
returned: always |
| 108 |
sample: "https://myappconf.azconfig.io/kv/Payments:TimeoutSeconds?label=prod" |
| 109 |
''' |
| 110 |
|
| 111 |
|
| 112 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 113 |
|
| 114 |
try: |
| 115 |
from azure.appconfiguration import AzureAppConfigurationClient |
| 116 |
except ImportError: |
| 117 |
pass |
| 118 |
|
| 119 |
|
| 120 |
def normalize_setting(s, endpoint): |
| 121 |
d = s.as_dict() |
| 122 |
|
| 123 |
ct = (d.get("content_type") or "").lower() |
| 124 |
if ct.startswith("application/vnd.microsoft.appconfig.keyvaultref"): |
| 125 |
d["type"] = "vault" |
| 126 |
try: |
| 127 |
import json |
| 128 |
body = json.loads(d.get("value") or "{}") |
| 129 |
d["vault_key_reference"] = body.get("uri") |
| 130 |
except Exception: |
| 131 |
d["vault_key_reference"] = None |
| 132 |
else: |
| 133 |
d["type"] = "kv" |
| 134 |
d["vault_key_reference"] = None |
| 135 |
|
| 136 |
|
| 137 |
base = endpoint.rstrip('/') |
| 138 |
key = d.get("key") |
| 139 |
label = d.get("label") |
| 140 |
if label: |
| 141 |
d["id"] = f"{base}/kv/{key}?label={label}" |
| 142 |
else: |
| 143 |
d["id"] = f"{base}/kv/{key}" |
| 144 |
return d |
| 145 |
|
| 146 |
|
| 147 |
class AzureRMAppConfigurationKeyInfo(AzureRMModuleBase): |
| 148 |
def __init__(self): |
| 149 |
self.module_arg_spec = dict( |
| 150 |
endpoint=dict(type='str', required=True), |
| 151 |
key=dict(type='str', no_log=False), |
| 152 |
label=dict(type='str') |
| 153 |
) |
| 154 |
|
| 155 |
self.endpoint = None |
| 156 |
self.key = None |
| 157 |
self.label = None |
| 158 |
|
| 159 |
self.dataplane_client = None |
| 160 |
self.mgmt_client = None |
| 161 |
self.results = dict(changed=False) |
| 162 |
|
| 163 |
super(AzureRMAppConfigurationKeyInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 164 |
supports_tags=False, |
| 165 |
supports_check_mode=True) |
| 166 |
|
| 167 |
def exec_module(self, **kwargs): |
| 168 |
for k in self.module_arg_spec.keys(): |
| 169 |
setattr(self, k, kwargs.get(k)) |
| 170 |
|
| 171 |
self.dataplane_client = AzureAppConfigurationClient(self.endpoint, |
| 172 |
credential=self.azure_auth.azure_credential_track2) |
| 173 |
|
| 174 |
out = [] |
| 175 |
items = self.dataplane_client.list_configuration_settings(key_filter=self.key, label_filter=self.label) |
| 176 |
|
| 177 |
for s in items: |
| 178 |
nd = normalize_setting(s, self.endpoint) |
| 179 |
out.append(nd) |
| 180 |
|
| 181 |
self.results["settings"] = out |
| 182 |
return self.results |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
AzureRMAppConfigurationKeyInfo() |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == '__main__': |
| 190 |
main() |