| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 10 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 11 |
|
| 12 |
|
| 13 |
class AzureRMModuleBaseExt(AzureRMModuleBase): |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
managed_identity_multiple_spec = dict( |
| 18 |
type=dict( |
| 19 |
type='str', |
| 20 |
choices=['SystemAssigned', |
| 21 |
'UserAssigned', |
| 22 |
'SystemAssigned, UserAssigned', |
| 23 |
'None'], |
| 24 |
default='None' |
| 25 |
), |
| 26 |
user_assigned_identities=dict( |
| 27 |
type='dict', |
| 28 |
options=dict( |
| 29 |
id=dict( |
| 30 |
type='list', |
| 31 |
default=[], |
| 32 |
elements='str' |
| 33 |
), |
| 34 |
append=dict( |
| 35 |
type='bool', |
| 36 |
default=True |
| 37 |
) |
| 38 |
), |
| 39 |
default={} |
| 40 |
), |
| 41 |
) |
| 42 |
|
| 43 |
managed_identity_multiple_user_assigned_spec = dict( |
| 44 |
type=dict( |
| 45 |
type='str', |
| 46 |
choices=['SystemAssigned', |
| 47 |
'UserAssigned', |
| 48 |
'None'], |
| 49 |
default='None' |
| 50 |
), |
| 51 |
user_assigned_identities=dict( |
| 52 |
type='dict', |
| 53 |
options=dict( |
| 54 |
id=dict( |
| 55 |
type='list', |
| 56 |
default=[], |
| 57 |
elements='str' |
| 58 |
), |
| 59 |
append=dict( |
| 60 |
type='bool', |
| 61 |
default=True |
| 62 |
) |
| 63 |
), |
| 64 |
default={} |
| 65 |
), |
| 66 |
) |
| 67 |
|
| 68 |
managed_identity_single_required_spec = dict( |
| 69 |
type=dict( |
| 70 |
type='str', |
| 71 |
choices=['SystemAssigned', |
| 72 |
'UserAssigned', |
| 73 |
'SystemAssigned, UserAssigned'], |
| 74 |
default='SystemAssigned' |
| 75 |
), |
| 76 |
user_assigned_identity=dict( |
| 77 |
type="str", |
| 78 |
), |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
managed_identity_single_spec = dict( |
| 83 |
type=dict( |
| 84 |
type="str", |
| 85 |
choices=[ |
| 86 |
"SystemAssigned", |
| 87 |
"UserAssigned", |
| 88 |
"None" |
| 89 |
], |
| 90 |
default="None" |
| 91 |
), |
| 92 |
user_assigned_identity=dict( |
| 93 |
type="str", |
| 94 |
), |
| 95 |
) |
| 96 |
|
| 97 |
def inflate_parameters(self, spec, body, level): |
| 98 |
if isinstance(body, list): |
| 99 |
for item in body: |
| 100 |
self.inflate_parameters(spec, item, level) |
| 101 |
return |
| 102 |
for name in spec.keys(): |
| 103 |
|
| 104 |
param = body.get(name) |
| 105 |
if param is None: |
| 106 |
if spec[name].get('purgeIfNone', False): |
| 107 |
body.pop(name, None) |
| 108 |
continue |
| 109 |
|
| 110 |
pattern = spec[name].get('pattern', None) |
| 111 |
if pattern: |
| 112 |
if pattern == 'camelize': |
| 113 |
param = _snake_to_camel(param, True) |
| 114 |
elif isinstance(pattern, list): |
| 115 |
normalized = None |
| 116 |
for p in pattern: |
| 117 |
normalized = self.normalize_resource_id(param, p) |
| 118 |
body[name] = normalized |
| 119 |
if normalized is not None: |
| 120 |
break |
| 121 |
else: |
| 122 |
param = self.normalize_resource_id(param, pattern) |
| 123 |
body[name] = param |
| 124 |
disposition = spec[name].get('disposition', '*') |
| 125 |
if level == 0 and not disposition.startswith('/'): |
| 126 |
continue |
| 127 |
if disposition == '/': |
| 128 |
disposition = '/*' |
| 129 |
parts = disposition.split('/') |
| 130 |
if parts[0] == '': |
| 131 |
|
| 132 |
parts.pop(0) |
| 133 |
target_dict = body |
| 134 |
elem = body.pop(name) |
| 135 |
while len(parts) > 1: |
| 136 |
target_dict = target_dict.setdefault(parts.pop(0), {}) |
| 137 |
targetName = parts[0] if parts[0] != '*' else name |
| 138 |
target_dict[targetName] = elem |
| 139 |
if spec[name].get('options'): |
| 140 |
self.inflate_parameters(spec[name].get('options'), target_dict[targetName], level + 1) |
| 141 |
|
| 142 |
def normalize_resource_id(self, value, pattern): |
| 143 |
''' |
| 144 |
Return a proper resource id string.. |
| 145 |
|
| 146 |
:param resource_id: It could be a resource name, resource id or dict containing parts from the pattern. |
| 147 |
:param pattern: pattern of resource is, just like in Azure Swagger |
| 148 |
''' |
| 149 |
value_dict = {} |
| 150 |
if isinstance(value, str): |
| 151 |
value_parts = value.split('/') |
| 152 |
if len(value_parts) == 1: |
| 153 |
value_dict['name'] = value |
| 154 |
else: |
| 155 |
pattern_parts = pattern.split('/') |
| 156 |
if len(value_parts) != len(pattern_parts): |
| 157 |
return None |
| 158 |
for i in range(len(value_parts)): |
| 159 |
if pattern_parts[i].startswith('{'): |
| 160 |
value_dict[pattern_parts[i][1:-1]] = value_parts[i] |
| 161 |
elif value_parts[i].lower() != pattern_parts[i].lower(): |
| 162 |
return None |
| 163 |
elif isinstance(value, dict): |
| 164 |
value_dict = value |
| 165 |
else: |
| 166 |
return None |
| 167 |
if not value_dict.get('subscription_id'): |
| 168 |
value_dict['subscription_id'] = self.subscription_id |
| 169 |
if not value_dict.get('resource_group'): |
| 170 |
value_dict['resource_group'] = self.resource_group |
| 171 |
|
| 172 |
|
| 173 |
for k in value_dict: |
| 174 |
if not ('{' + k + '}') in pattern: |
| 175 |
return None |
| 176 |
|
| 177 |
return pattern.format(**value_dict) |
| 178 |
|
| 179 |
def idempotency_check(self, old_params, new_params): |
| 180 |
''' |
| 181 |
Return True if something changed. Function will use fields from module_arg_spec to perform dependency checks. |
| 182 |
:param old_params: old parameters dictionary, body from Get request. |
| 183 |
:param new_params: new parameters dictionary, unpacked module parameters. |
| 184 |
''' |
| 185 |
modifiers = {} |
| 186 |
result = {} |
| 187 |
self.create_compare_modifiers(self.module.argument_spec, '', modifiers) |
| 188 |
self.results['modifiers'] = modifiers |
| 189 |
return self.default_compare(modifiers, new_params, old_params, '', self.results) |
| 190 |
|
| 191 |
def create_compare_modifiers(self, arg_spec, path, result): |
| 192 |
for k in arg_spec.keys(): |
| 193 |
o = arg_spec[k] |
| 194 |
updatable = o.get('updatable', True) |
| 195 |
comparison = o.get('comparison', 'default') |
| 196 |
disposition = o.get('disposition', '*') |
| 197 |
if disposition == '/': |
| 198 |
disposition = '/*' |
| 199 |
p = (path + |
| 200 |
('/' if len(path) > 0 else '') + |
| 201 |
disposition.replace('*', k) + |
| 202 |
('/*' if o['type'] == 'list' else '')) |
| 203 |
if comparison != 'default' or not updatable: |
| 204 |
result[p] = {'updatable': updatable, 'comparison': comparison} |
| 205 |
if o.get('options'): |
| 206 |
self.create_compare_modifiers(o.get('options'), p, result) |
| 207 |
|
| 208 |
def default_compare(self, modifiers, new, old, path, result): |
| 209 |
''' |
| 210 |
Default dictionary comparison. |
| 211 |
This function will work well with most of the Azure resources. |
| 212 |
It correctly handles "location" comparison. |
| 213 |
|
| 214 |
Value handling: |
| 215 |
- if "new" value is None, it will be taken from "old" dictionary if "incremental_update" |
| 216 |
is enabled. |
| 217 |
List handling: |
| 218 |
- if list contains "name" field it will be sorted by "name" before comparison is done. |
| 219 |
- if module has "incremental_update" set, items missing in the new list will be copied |
| 220 |
from the old list |
| 221 |
|
| 222 |
Warnings: |
| 223 |
If field is marked as non-updatable, appropriate warning will be printed out and |
| 224 |
"new" structure will be updated to old value. |
| 225 |
|
| 226 |
:modifiers: Optional dictionary of modifiers, where key is the path and value is dict of modifiers |
| 227 |
:param new: New version |
| 228 |
:param old: Old version |
| 229 |
|
| 230 |
Returns True if no difference between structures has been detected. |
| 231 |
Returns False if difference was detected. |
| 232 |
''' |
| 233 |
if new is None: |
| 234 |
return True |
| 235 |
elif isinstance(new, dict): |
| 236 |
comparison_result = True |
| 237 |
if not isinstance(old, dict): |
| 238 |
result['compare'].append('changed [' + path + '] old dict is null') |
| 239 |
comparison_result = False |
| 240 |
else: |
| 241 |
for k in set(new.keys()) | set(old.keys()): |
| 242 |
new_item = new.get(k, None) |
| 243 |
old_item = old.get(k, None) |
| 244 |
if new_item is None: |
| 245 |
if isinstance(old_item, dict): |
| 246 |
new[k] = old_item |
| 247 |
result['compare'].append('new item was empty, using old [' + path + '][ ' + k + ' ]') |
| 248 |
elif not self.default_compare(modifiers, new_item, old_item, path + '/' + k, result): |
| 249 |
comparison_result = False |
| 250 |
return comparison_result |
| 251 |
elif isinstance(new, list): |
| 252 |
comparison_result = True |
| 253 |
if not isinstance(old, list) or len(new) != len(old): |
| 254 |
result['compare'].append('changed [' + path + '] length is different or old value is null') |
| 255 |
comparison_result = False |
| 256 |
elif len(old) > 0: |
| 257 |
if isinstance(old[0], dict): |
| 258 |
key = None |
| 259 |
if 'id' in old[0] and 'id' in new[0]: |
| 260 |
key = 'id' |
| 261 |
elif 'name' in old[0] and 'name' in new[0]: |
| 262 |
key = 'name' |
| 263 |
else: |
| 264 |
try: |
| 265 |
key = next(iter(old[0])) |
| 266 |
new = sorted(new, key=lambda x: x.get(key, None)) |
| 267 |
old = sorted(old, key=lambda x: x.get(key, None)) |
| 268 |
except TypeError: |
| 269 |
for i in range(len(new)): |
| 270 |
if not self.default_compare(modifiers, new[i], old[i], path + '/*', result): |
| 271 |
comparison_result = False |
| 272 |
else: |
| 273 |
new = sorted(new) |
| 274 |
old = sorted(old) |
| 275 |
for i in range(len(new)): |
| 276 |
if not self.default_compare(modifiers, new[i], old[i], path + '/*', result): |
| 277 |
comparison_result = False |
| 278 |
return comparison_result |
| 279 |
else: |
| 280 |
updatable = modifiers.get(path, {}).get('updatable', True) |
| 281 |
comparison = modifiers.get(path, {}).get('comparison', 'default') |
| 282 |
if comparison == 'ignore': |
| 283 |
return True |
| 284 |
elif comparison == 'default' or comparison == 'sensitive': |
| 285 |
if isinstance(old, str) and isinstance(new, str): |
| 286 |
new = new.lower() |
| 287 |
old = old.lower() |
| 288 |
elif comparison == 'location': |
| 289 |
if isinstance(old, str) and isinstance(new, str): |
| 290 |
new = new.replace(' ', '').lower() |
| 291 |
old = old.replace(' ', '').lower() |
| 292 |
if str(new) != str(old): |
| 293 |
result['compare'].append('changed [' + path + '] ' + str(new) + ' != ' + str(old) + ' - ' + str(comparison)) |
| 294 |
if updatable: |
| 295 |
return False |
| 296 |
else: |
| 297 |
self.module.warn("property '" + path + "' cannot be updated (" + str(old) + "->" + str(new) + ")") |
| 298 |
return True |
| 299 |
else: |
| 300 |
return True |
| 301 |
|
| 302 |
def update_single_managed_identity(self, curr_identity, new_identity, patch_support=False): |
| 303 |
|
| 304 |
new_identity = new_identity or dict() |
| 305 |
new_identity_converted = { |
| 306 |
'type': new_identity.get('type', 'None'), |
| 307 |
} |
| 308 |
user_assigned_identity = new_identity.get('user_assigned_identity', None) |
| 309 |
if user_assigned_identity is not None: |
| 310 |
new_identity_converted['user_assigned_identities'] = { |
| 311 |
'id': [user_assigned_identity] |
| 312 |
} |
| 313 |
return self.update_managed_identity(new_identity=new_identity_converted, |
| 314 |
curr_identity=curr_identity, |
| 315 |
allow_identities_append=False, patch_support=patch_support) |
| 316 |
|
| 317 |
def update_managed_identity(self, new_identity, curr_identity=None, |
| 318 |
allow_identities_append=True, patch_support=False): |
| 319 |
curr_identity = curr_identity or dict() |
| 320 |
curr_managed_type = curr_identity.get('type', 'None') |
| 321 |
new_managed_type = new_identity.get('type', 'None') |
| 322 |
|
| 323 |
if new_managed_type == 'None' and curr_managed_type == 'None': |
| 324 |
return False, None |
| 325 |
|
| 326 |
changed = False |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
if new_managed_type == 'None' or curr_managed_type != new_managed_type: |
| 331 |
changed = True |
| 332 |
|
| 333 |
curr_user_assigned_identities = set((curr_identity.get('user_assigned_identities') or {}).keys()) |
| 334 |
new_user_assigned_identities = set((new_identity.get('user_assigned_identities') or {}).get('id', [])) |
| 335 |
result_user_assigned_identities = new_user_assigned_identities |
| 336 |
clear_identities = [] |
| 337 |
|
| 338 |
result_identity = self.managed_identity['identity'](type=new_managed_type) |
| 339 |
|
| 340 |
|
| 341 |
if allow_identities_append and \ |
| 342 |
'UserAssigned' in new_managed_type and \ |
| 343 |
new_identity.get('user_assigned_identities', {}).get('append', True) is True: |
| 344 |
result_user_assigned_identities = new_user_assigned_identities.union(curr_user_assigned_identities) |
| 345 |
elif patch_support and 'UserAssigned' in new_managed_type: |
| 346 |
clear_identities = curr_user_assigned_identities.difference(result_user_assigned_identities) |
| 347 |
|
| 348 |
|
| 349 |
if result_user_assigned_identities.difference(curr_user_assigned_identities) != set(): |
| 350 |
changed = True |
| 351 |
|
| 352 |
|
| 353 |
if len(result_user_assigned_identities) > 0 or len(clear_identities) > 0: |
| 354 |
result_identity.user_assigned_identities = {} |
| 355 |
|
| 356 |
for identity in clear_identities: |
| 357 |
result_identity.user_assigned_identities[identity] = None |
| 358 |
|
| 359 |
for identity in result_user_assigned_identities: |
| 360 |
result_identity.user_assigned_identities[identity] = self.managed_identity['user_assigned']() |
| 361 |
|
| 362 |
return changed, result_identity |