Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | |
| 4 | def __virtual__(): |
| 5 | return 'keystonev3' if 'keystonev3.endpoint_list' in __salt__ else False # noqa |
| 6 | |
| 7 | |
| 8 | log = logging.getLogger(__name__) |
| 9 | |
| 10 | |
| 11 | def _keystonev3_call(fname, *args, **kwargs): |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 12 | if __opts__.get('test') and not (fname.find('_list') > 0 or fname.find('_get_') > 0): |
| 13 | return {'changes':'keystonev3 state {} with options {} {} will be executed'.format(fname, args, kwargs)} |
| 14 | else: |
| 15 | return __salt__['keystonev3.{}'.format(fname)](*args, **kwargs) # noqa |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def endpoint_present(name, url, interface, service_id, cloud_name, **kwargs): |
| 19 | |
Vasyl Saienko | a8fc6ee | 2019-04-25 07:48:08 +0000 | [diff] [blame] | 20 | ekwargs = {} |
| 21 | if 'region_id' in kwargs: |
| 22 | ekwargs['region_id'] = kwargs['region_id'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 23 | service_id = _keystonev3_call( |
| 24 | 'service_get_details', service_id, |
| 25 | cloud_name=cloud_name)['service']['id'] |
| 26 | |
| 27 | endpoints = _keystonev3_call( |
| 28 | 'endpoint_list', name=name, service_id=service_id, interface=interface, |
Vasyl Saienko | a8fc6ee | 2019-04-25 07:48:08 +0000 | [diff] [blame] | 29 | cloud_name=cloud_name, **ekwargs)['endpoints'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 30 | |
| 31 | if not endpoints: |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 32 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 33 | try: |
| 34 | resp = _keystonev3_call( |
| 35 | 'endpoint_create', url=url, interface=interface, |
| 36 | service_id=service_id, cloud_name=cloud_name, **kwargs |
| 37 | ) |
| 38 | except Exception as e: |
| 39 | log.error('Keystone endpoint create failed with {}'.format(e)) |
| 40 | return _create_failed(name, 'endpoint') |
| 41 | return _created(name, 'endpoint', resp) |
| 42 | elif len(endpoints) == 1: |
| 43 | exact_endpoint = endpoints[0] |
| 44 | endpoint_id = exact_endpoint['id'] |
| 45 | changable = ( |
| 46 | 'url', 'region', 'interface', 'service_id' |
| 47 | ) |
| 48 | to_update = {} |
| 49 | to_check = {'url': url} |
| 50 | to_check.update(kwargs) |
| 51 | |
| 52 | for key in to_check: |
| 53 | if (key in changable and (key not in exact_endpoint or |
| 54 | to_check[key] != exact_endpoint[key])): |
| 55 | to_update[key] = to_check[key] |
| 56 | if to_update: |
| 57 | try: |
| 58 | resp = _keystonev3_call( |
| 59 | 'endpoint_update', endpoint_id=endpoint_id, |
| 60 | cloud_name=cloud_name, **to_update |
| 61 | ) |
| 62 | except Exception as e: |
| 63 | log.error('Keystone endpoint update failed with {}'.format(e)) |
| 64 | return _update_failed(name, 'endpoint') |
| 65 | return _updated(name, 'endpoint', resp) |
| 66 | else: |
| 67 | return _no_changes(name, 'endpoint') |
| 68 | else: |
| 69 | return _find_failed(name, 'endpoint') |
| 70 | |
| 71 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 72 | def endpoint_absent(name, service_id, interface, cloud_name): |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 73 | |
| 74 | try: |
| 75 | service_id = _keystonev3_call( |
| 76 | 'service_get_details', service_id, |
| 77 | cloud_name=cloud_name)['service']['id'] |
| 78 | |
| 79 | except Exception as e: |
| 80 | if 'ResourceNotFound' in repr(e): |
| 81 | return _absent(name, 'service') |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 82 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 83 | endpoints = _keystonev3_call( |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 84 | 'endpoint_list', name=name, service_id=service_id, interface=interface, |
| 85 | cloud_name=cloud_name)['endpoints'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 86 | if not endpoints: |
| 87 | return _absent(name, 'endpoint') |
| 88 | elif len(endpoints) == 1: |
| 89 | try: |
| 90 | _keystonev3_call( |
| 91 | 'endpoint_delete', endpoints[0]['id'], cloud_name=cloud_name |
| 92 | ) |
| 93 | except Exception as e: |
| 94 | log.error('Keystone delete endpoint failed with {}'.format(e)) |
| 95 | return _delete_failed(name, 'endpoint') |
| 96 | return _deleted(name, 'endpoint') |
| 97 | else: |
| 98 | return _find_failed(name, 'endpoint') |
| 99 | |
| 100 | |
| 101 | def service_present(name, type, cloud_name, **kwargs): |
| 102 | |
| 103 | service_id = '' |
| 104 | |
| 105 | try: |
| 106 | exact_service = _keystonev3_call( |
| 107 | 'service_get_details', name, |
| 108 | cloud_name=cloud_name)['service'] |
| 109 | service_id = exact_service['id'] |
| 110 | except Exception as e: |
| 111 | if 'ResourceNotFound' in repr(e): |
| 112 | pass |
| 113 | else: |
| 114 | log.error('Failed to get service {}'.format(e)) |
| 115 | return _create_failed(name, 'service') |
| 116 | |
| 117 | if not service_id: |
| 118 | try: |
| 119 | resp = _keystonev3_call( |
| 120 | 'service_create', name=name, type=type, |
| 121 | cloud_name=cloud_name, **kwargs |
| 122 | ) |
| 123 | except Exception as e: |
| 124 | log.error('Keystone service create failed with {}'.format(e)) |
| 125 | return _create_failed(name, 'service') |
| 126 | return _created(name, 'service', resp) |
| 127 | |
| 128 | else: |
| 129 | changable = ('type', 'enabled', 'description') |
| 130 | to_update = {} |
| 131 | to_check = {'type': type} |
| 132 | to_check.update(kwargs) |
| 133 | |
| 134 | for key in to_check: |
| 135 | if (key in changable and (key not in exact_service or |
| 136 | to_check[key] != exact_service[key])): |
| 137 | to_update[key] = to_check[key] |
| 138 | if to_update: |
| 139 | try: |
| 140 | resp = _keystonev3_call( |
| 141 | 'service_update', service_id=service_id, |
| 142 | cloud_name=cloud_name, **to_update |
| 143 | ) |
| 144 | except Exception as e: |
| 145 | log.error('Keystone service update failed with {}'.format(e)) |
| 146 | return _update_failed(name, 'service') |
| 147 | return _updated(name, 'service', resp) |
| 148 | else: |
| 149 | return _no_changes(name, 'service') |
| 150 | return _find_failed(name, 'service') |
| 151 | |
| 152 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 153 | def service_absent(name, cloud_name): |
| 154 | try: |
| 155 | _keystonev3_call( |
| 156 | 'service_get_details', name, |
| 157 | cloud_name=cloud_name)['service'] |
| 158 | except Exception as e: |
| 159 | if 'ResourceNotFound' in repr(e): |
| 160 | return _absent(name, 'service') |
| 161 | else: |
| 162 | log.error('Failed to get service {}'.format(e)) |
| 163 | return _find_failed(name, 'service') |
| 164 | try: |
| 165 | _keystonev3_call('service_delete', name, cloud_name=cloud_name) |
| 166 | except Exception: |
| 167 | return _delete_failed(name, 'service') |
| 168 | return _deleted(name, 'service') |
| 169 | |
| 170 | |
| 171 | |
| 172 | def project_present(name, domain_id, cloud_name, **kwargs): |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 173 | |
| 174 | projects = _keystonev3_call( |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 175 | 'project_list', name=name, domain_id=domain_id, cloud_name=cloud_name |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 176 | )['projects'] |
| 177 | |
| 178 | if not projects: |
| 179 | try: |
| 180 | resp = _keystonev3_call( |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 181 | 'project_create', domain_id=domain_id, name=name, |
| 182 | cloud_name=cloud_name, **kwargs |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 183 | ) |
| 184 | except Exception as e: |
| 185 | log.error('Keystone project create failed with {}'.format(e)) |
| 186 | return _create_failed(name, 'project') |
| 187 | return _created(name, 'project', resp) |
| 188 | elif len(projects) == 1: |
| 189 | exact_project = projects[0] |
| 190 | project_id = exact_project['id'] |
| 191 | changable = ( |
| 192 | 'is_domain', 'description', 'domain_id', 'enabled', |
| 193 | 'parent_id', 'tags' |
| 194 | ) |
| 195 | to_update = {} |
| 196 | |
| 197 | for key in kwargs: |
| 198 | if (key in changable and (key not in exact_project or |
| 199 | kwargs[key] != exact_project[key])): |
| 200 | to_update[key] = kwargs[key] |
| 201 | |
| 202 | if to_update: |
| 203 | try: |
| 204 | resp = _keystonev3_call( |
| 205 | 'project_update', project_id=project_id, |
| 206 | cloud_name=cloud_name, **to_update |
| 207 | ) |
| 208 | except Exception as e: |
| 209 | log.error('Keystone project update failed with {}'.format(e)) |
| 210 | return _update_failed(name, 'project') |
| 211 | return _updated(name, 'project', resp) |
| 212 | else: |
| 213 | return _no_changes(name, 'project') |
| 214 | else: |
| 215 | return _find_failed(name, 'project') |
| 216 | |
| 217 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 218 | def project_absent(name, cloud_name): |
| 219 | try: |
| 220 | _keystonev3_call('project_get_details', |
| 221 | project_id=name, cloud_name=cloud_name) |
| 222 | except Exception as e: |
| 223 | if 'ResourceNotFound' in repr(e): |
| 224 | return _absent(name, 'project') |
| 225 | else: |
| 226 | log.error('Failed to get project {}'.format(e)) |
| 227 | return _find_failed(name, 'project') |
| 228 | try: |
| 229 | _keystonev3_call('project_delete', project_id=name, |
| 230 | cloud_name=cloud_name) |
| 231 | except Exception: |
| 232 | return _delete_failed(name, 'project') |
| 233 | return _deleted(name, 'project') |
| 234 | |
| 235 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 236 | def user_present(name, cloud_name, password_reset=False, **kwargs): |
| 237 | |
| 238 | users = _keystonev3_call( |
| 239 | 'user_list', name=name, cloud_name=cloud_name |
| 240 | )['users'] |
| 241 | |
| 242 | if 'default_project_id' in kwargs: |
| 243 | kwargs['default_project_id'] = _keystonev3_call( |
| 244 | 'project_get_details', kwargs['default_project_id'], |
| 245 | cloud_name=cloud_name)['project']['id'] |
| 246 | |
Michael Polenchuk | 4de57c5 | 2019-01-22 12:43:12 +0400 | [diff] [blame] | 247 | if 'domain_id' in kwargs: |
| 248 | kwargs['domain_id'] = _keystonev3_call( |
| 249 | 'domain_get_details', kwargs['domain_id'], |
| 250 | cloud_name=cloud_name)['domain']['id'] |
| 251 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 252 | if not users: |
| 253 | try: |
| 254 | resp = _keystonev3_call( |
| 255 | 'user_create', name=name, cloud_name=cloud_name, **kwargs |
| 256 | ) |
| 257 | except Exception as e: |
Oleksandr Shyshko | daaa1ae | 2019-04-18 13:16:31 +0000 | [diff] [blame] | 258 | log.error('Keystone user create failed with: {}'.format(e)) |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 259 | return _create_failed(name, 'user') |
| 260 | return _created(name, 'user', resp) |
| 261 | |
| 262 | elif len(users) == 1: |
| 263 | exact_user = users[0] |
| 264 | user_id = exact_user['id'] |
| 265 | changable = ( |
Oleksandr Shyshko | a0b79e2 | 2019-02-27 15:02:30 +0000 | [diff] [blame] | 266 | 'default_project_id', 'domain_id', 'enabled', 'email', 'options' |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 267 | ) |
| 268 | if password_reset: |
| 269 | changable += ('password',) |
| 270 | to_update = {} |
| 271 | |
| 272 | for key in kwargs: |
Oleksandr Shyshko | a0b79e2 | 2019-02-27 15:02:30 +0000 | [diff] [blame] | 273 | if key in changable: |
| 274 | if key == 'options': |
| 275 | to_update['options'] = {option: value for option, value in kwargs['options'].items() |
| 276 | if (option not in exact_user['options']) |
| 277 | or (value != exact_user['options'][option])} |
| 278 | |
| 279 | if not len(to_update['options']): |
| 280 | del to_update['options'] |
| 281 | |
| 282 | elif key not in exact_user or kwargs[key] != exact_user[key]: |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 283 | to_update[key] = kwargs[key] |
| 284 | |
| 285 | if to_update: |
| 286 | log.info('Updating keystone user {} with: {}'.format(user_id, |
| 287 | to_update)) |
| 288 | try: |
| 289 | resp = _keystonev3_call( |
| 290 | 'user_update', user_id=user_id, |
| 291 | cloud_name=cloud_name, **to_update |
| 292 | ) |
| 293 | except Exception as e: |
| 294 | log.error('Keystone user update failed with {}'.format(e)) |
| 295 | return _update_failed(name, 'user') |
| 296 | return _updated(name, 'user', resp) |
| 297 | else: |
| 298 | return _no_changes(name, 'user') |
| 299 | else: |
| 300 | return _find_failed(name, 'user') |
| 301 | |
| 302 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 303 | def user_absent(name, cloud_name): |
| 304 | try: |
| 305 | _keystonev3_call('user_get_details', user_id=name, |
| 306 | cloud_name=cloud_name) |
| 307 | except Exception as e: |
| 308 | if 'ResourceNotFound' in repr(e): |
| 309 | return _absent(name, 'user') |
| 310 | else: |
| 311 | log.error('Failed to get user {}'.format(e)) |
| 312 | return _find_failed(name, 'user') |
| 313 | try: |
| 314 | _keystonev3_call('user_delete', user_id=name, cloud_name=cloud_name) |
| 315 | except Exception: |
| 316 | return _delete_failed(name, 'user') |
| 317 | return _deleted(name, 'user') |
| 318 | |
| 319 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 320 | def user_role_assigned(name, role_id, cloud_name, project_id=None, |
| 321 | domain_id=None, role_domain_id=None, **kwargs): |
| 322 | |
| 323 | user_id = _keystonev3_call( |
| 324 | 'user_get_details', name, |
| 325 | cloud_name=cloud_name)['user']['id'] |
| 326 | |
| 327 | if project_id: |
| 328 | project_id = _keystonev3_call( |
| 329 | 'project_get_details', project_id, |
| 330 | cloud_name=cloud_name)['project']['id'] |
| 331 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 332 | if domain_id: |
| 333 | domain_id = _keystonev3_call( |
| 334 | 'domain_get_details', domain_id, |
| 335 | cloud_name=cloud_name)['domain']['id'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 336 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 337 | |
| 338 | if (project_id and domain_id) or (not project_id and not domain_id): |
| 339 | return { |
| 340 | 'name': name, |
| 341 | 'changes': {}, |
| 342 | 'result': False, |
| 343 | 'comment': 'Use project_id or domain_id (only one of them)' |
| 344 | } |
| 345 | |
| 346 | |
| 347 | if role_domain_id: |
| 348 | role_domain_id = _keystonev3_call( |
| 349 | 'domain_get_details', role_domain_id, |
| 350 | cloud_name=cloud_name)['domain']['id'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 351 | |
| 352 | if role_id: |
| 353 | role_id = _keystonev3_call( |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 354 | 'role_get_details', role_id, domain_id=role_domain_id, |
| 355 | cloud_name=cloud_name)['role']['id'] |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 356 | |
| 357 | req_kwargs = {'role.id': role_id, 'user.id': user_id, |
| 358 | 'cloud_name': cloud_name} |
| 359 | if domain_id: |
| 360 | req_kwargs['domain_id'] = domain_id |
| 361 | if project_id: |
| 362 | req_kwargs['project_id'] = project_id |
| 363 | |
| 364 | role_assignments = _keystonev3_call( |
| 365 | 'role_assignment_list', **req_kwargs)['role_assignments'] |
| 366 | |
| 367 | req_kwargs = {'cloud_name': cloud_name, 'user_id': user_id, |
| 368 | 'role_id': role_id} |
| 369 | if domain_id: |
| 370 | req_kwargs['domain_id'] = domain_id |
| 371 | if project_id: |
| 372 | req_kwargs['project_id'] = project_id |
| 373 | |
| 374 | if not role_assignments: |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 375 | method_type = 'project' if project_id else 'domain' |
| 376 | method = 'role_assign_for_user_on_{}'.format(method_type) |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 377 | try: |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 378 | resp = _keystonev3_call(method, **req_kwargs) |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 379 | except Exception as e: |
| 380 | log.error('Keystone user role assignment with {}'.format(e)) |
| 381 | return _create_failed(name, 'user_role_assignment') |
| 382 | # We check for exact assignment when did role_assignment_list |
| 383 | # on this stage we already just assigned role if it was missed. |
| 384 | return _created(name, 'user_role_assignment', resp) |
| 385 | return _no_changes(name, 'user_role_assignment') |
| 386 | |
| 387 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 388 | def user_role_unassign(name, role_id, cloud_name, project_id=None, |
| 389 | domain_id=None, role_domain_id=None): |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 390 | |
| 391 | try: |
| 392 | user_id = _keystonev3_call( |
| 393 | 'user_get_details', name, |
| 394 | cloud_name=cloud_name)['user']['id'] |
| 395 | |
| 396 | except Exception as e: |
| 397 | if 'ResourceNotFound' in repr(e): |
| 398 | return _no_changes(name, 'user') |
| 399 | else: |
| 400 | log.error('Failed to get user {}'.format(e)) |
| 401 | return _find_failed(name, 'user') |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 402 | |
| 403 | if project_id: |
| 404 | project_id = _keystonev3_call( |
| 405 | 'project_get_details', project_id, |
| 406 | cloud_name=cloud_name)['project']['id'] |
| 407 | |
| 408 | if domain_id: |
| 409 | domain_id = _keystonev3_call( |
| 410 | 'domain_get_details', domain_id, |
| 411 | cloud_name=cloud_name)['domain']['id'] |
| 412 | |
| 413 | if (project_id and domain_id) or (not project_id and not domain_id): |
| 414 | return { |
| 415 | 'name': name, |
| 416 | 'changes': {}, |
| 417 | 'result': False, |
| 418 | 'comment': 'Use project_id or domain_id (only one of them)' |
| 419 | } |
| 420 | |
| 421 | if role_domain_id: |
| 422 | role_domain_id = _keystonev3_call( |
| 423 | 'domain_get_details', role_domain_id, |
| 424 | cloud_name=cloud_name)['domain']['id'] |
| 425 | |
| 426 | if role_id: |
| 427 | role_id = _keystonev3_call( |
| 428 | 'role_get_details', role_id, domain_id=role_domain_id, |
| 429 | cloud_name=cloud_name)['role']['id'] |
| 430 | |
| 431 | req_kwargs = {'role.id': role_id, 'user.id': user_id, |
| 432 | 'cloud_name': cloud_name} |
| 433 | if domain_id: |
| 434 | req_kwargs['domain_id'] = domain_id |
| 435 | if project_id: |
| 436 | req_kwargs['project_id'] = project_id |
| 437 | |
| 438 | role_assignments = _keystonev3_call( |
| 439 | 'role_assignment_list', **req_kwargs)['role_assignments'] |
| 440 | |
| 441 | req_kwargs = {'cloud_name': cloud_name, 'user_id': user_id, |
| 442 | 'role_id': role_id} |
| 443 | if domain_id: |
| 444 | req_kwargs['domain_id'] = domain_id |
| 445 | if project_id: |
| 446 | req_kwargs['project_id'] = project_id |
| 447 | |
| 448 | if not role_assignments: |
| 449 | return _absent(name, 'user_role_assignment') |
| 450 | else: |
| 451 | method_type = 'project' if project_id else 'domain' |
| 452 | method = 'role_unassign_for_user_on_{}'.format(method_type) |
| 453 | try: |
| 454 | _keystonev3_call(method, **req_kwargs) |
| 455 | except Exception: |
| 456 | return _delete_failed(name, 'user_role_assignment') |
| 457 | return _deleted(name, 'user_role_assignment') |
| 458 | |
| 459 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 460 | def role_present(name, cloud_name, **kwargs): |
| 461 | |
| 462 | roles = _keystonev3_call( |
| 463 | 'role_list', name=name, cloud_name=cloud_name |
| 464 | )['roles'] |
| 465 | |
| 466 | if 'domain_id' in kwargs: |
| 467 | kwargs['domain_id'] = _keystonev3_call( |
| 468 | 'domain_get_details', kwargs['domain_id'], |
| 469 | cloud_name=cloud_name)['domains'] |
| 470 | |
| 471 | if not roles: |
| 472 | try: |
| 473 | resp = _keystonev3_call( |
| 474 | 'role_create', name=name, cloud_name=cloud_name, **kwargs |
| 475 | ) |
| 476 | except Exception as e: |
| 477 | log.error('Keystone role create failed with {}'.format(e)) |
| 478 | return _create_failed(name, 'role') |
| 479 | return _created(name, 'role', resp) |
| 480 | elif len(roles) == 1: |
| 481 | exact_role = roles[0] |
| 482 | role_id = exact_role['id'] |
| 483 | changable = ('domain_id') |
| 484 | to_update = {} |
| 485 | |
| 486 | for key in kwargs: |
| 487 | if (key in changable and (key not in exact_role or |
| 488 | kwargs[key] != exact_role[key])): |
| 489 | to_update[key] = kwargs[key] |
| 490 | |
| 491 | if to_update: |
| 492 | try: |
| 493 | resp = _keystonev3_call( |
| 494 | 'role_update', role_id=role_id, |
| 495 | cloud_name=cloud_name, **to_update |
| 496 | ) |
| 497 | except Exception as e: |
| 498 | log.error('Keystone role update failed with {}'.format(e)) |
| 499 | return _update_failed(name, 'role') |
| 500 | return _updated(name, 'role', resp) |
| 501 | else: |
| 502 | return _no_changes(name, 'role') |
| 503 | else: |
| 504 | return _find_failed(name, 'role') |
| 505 | |
| 506 | |
Oleksiy Petrenko | e03e2c7 | 2018-08-10 13:24:32 +0300 | [diff] [blame] | 507 | def role_absent(name, cloud_name): |
| 508 | try: |
| 509 | _keystonev3_call('role_get_details', role_id=name, |
| 510 | cloud_name=cloud_name) |
| 511 | except Exception as e: |
| 512 | if 'ResourceNotFound' in repr(e): |
| 513 | return _absent(name, 'role') |
| 514 | else: |
| 515 | log.error('Failed to get role {}'.format(e)) |
| 516 | return _find_failed(name, 'role') |
| 517 | try: |
| 518 | _keystonev3_call('role_delete', role_id=name, cloud_name=cloud_name) |
| 519 | except Exception: |
| 520 | return _delete_failed(name, 'role') |
| 521 | return _deleted(name, 'role') |
| 522 | |
| 523 | |
Michael Polenchuk | 89e1edb | 2018-11-27 13:06:49 +0400 | [diff] [blame] | 524 | def domain_present(name, cloud_name, **kwargs): |
| 525 | |
| 526 | domains = _keystonev3_call( |
| 527 | 'domain_list', name=name, cloud_name=cloud_name)['domains'] |
| 528 | |
| 529 | if not domains: |
| 530 | try: |
| 531 | resp = _keystonev3_call( |
| 532 | 'domain_create', name=name, cloud_name=cloud_name, **kwargs |
| 533 | ) |
| 534 | except Exception as e: |
| 535 | log.error('Keystone domain create failed with {}'.format(e)) |
| 536 | return _create_failed(name, 'domain') |
| 537 | return _created(name, 'domain', resp) |
| 538 | elif len(domains) == 1: |
| 539 | exact_domain = domains[0] |
| 540 | domain_id = exact_domain['id'] |
| 541 | changable = ('tags', 'enabled', 'description') |
| 542 | to_update = {} |
| 543 | |
| 544 | for key in kwargs: |
| 545 | if (key in changable and (key not in exact_domain or |
| 546 | kwargs[key] != exact_domain[key])): |
| 547 | to_update[key] = kwargs[key] |
| 548 | |
| 549 | if to_update: |
| 550 | try: |
| 551 | resp = _keystonev3_call( |
| 552 | 'domain_update', domain_id=domain_id, |
| 553 | cloud_name=cloud_name, **to_update |
| 554 | ) |
| 555 | except Exception as e: |
| 556 | log.error('Keystone domain update failed with {}'.format(e)) |
| 557 | return _update_failed(name, 'domain') |
| 558 | return _updated(name, 'domain', resp) |
| 559 | else: |
| 560 | return _no_changes(name, 'domain') |
| 561 | else: |
| 562 | return _find_failed(name, 'domain') |
| 563 | |
| 564 | |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 565 | def domain_absent(name, cloud_name, force_delete=False): |
Michael Polenchuk | 89e1edb | 2018-11-27 13:06:49 +0400 | [diff] [blame] | 566 | try: |
| 567 | _keystonev3_call('domain_get_details', |
| 568 | domain_id=name, cloud_name=cloud_name) |
| 569 | except Exception as e: |
| 570 | if 'ResourceNotFound' in repr(e): |
| 571 | return _absent(name, 'domain') |
| 572 | else: |
| 573 | log.error('Failed to get a domain {}'.format(e)) |
| 574 | return _find_failed(name, 'domain') |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 575 | |
Michael Polenchuk | 89e1edb | 2018-11-27 13:06:49 +0400 | [diff] [blame] | 576 | try: |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 577 | if force_delete: |
| 578 | _keystonev3_call( |
| 579 | 'domain_update', domain_id=name, |
| 580 | enabled=False, cloud_name=cloud_name |
| 581 | ) |
| 582 | |
Michael Polenchuk | 89e1edb | 2018-11-27 13:06:49 +0400 | [diff] [blame] | 583 | _keystonev3_call('domain_delete', domain_id=name, |
| 584 | cloud_name=cloud_name) |
| 585 | except Exception: |
| 586 | return _delete_failed(name, 'domain') |
| 587 | return _deleted(name, 'domain') |
| 588 | |
| 589 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 590 | def _created(name, resource, resource_definition): |
| 591 | changes_dict = { |
| 592 | 'name': name, |
| 593 | 'changes': resource_definition, |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 594 | 'result': _check_test_mode(), |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 595 | 'comment': '{}{} created'.format(resource, name) |
| 596 | } |
| 597 | return changes_dict |
| 598 | |
| 599 | |
| 600 | def _updated(name, resource, resource_definition): |
| 601 | changes_dict = { |
| 602 | 'name': name, |
| 603 | 'changes': resource_definition, |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 604 | 'result': _check_test_mode(), |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 605 | 'comment': '{}{} updated'.format(resource, name) |
| 606 | } |
| 607 | return changes_dict |
| 608 | |
| 609 | |
| 610 | def _no_changes(name, resource): |
| 611 | changes_dict = { |
| 612 | 'name': name, |
| 613 | 'changes': {}, |
| 614 | 'result': True, |
| 615 | 'comment': '{}{} is in desired state'.format(resource, name) |
| 616 | } |
| 617 | return changes_dict |
| 618 | |
| 619 | |
| 620 | def _deleted(name, resource): |
| 621 | changes_dict = { |
| 622 | 'name': name, |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 623 | 'changes': {"resource": resource, "name": name, "status": 'deleted'}, |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 624 | 'result': _check_test_mode(), |
Oleksandr Shyshko | c0a50e4 | 2018-12-19 19:20:40 +0200 | [diff] [blame] | 625 | 'comment': '{} {} removed'.format(resource, name) |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 626 | } |
| 627 | return changes_dict |
| 628 | |
| 629 | |
| 630 | def _absent(name, resource): |
| 631 | changes_dict = {'name': name, |
| 632 | 'changes': {}, |
| 633 | 'comment': '{0} {1} not present'.format(resource, name), |
| 634 | 'result': True} |
| 635 | return changes_dict |
| 636 | |
| 637 | |
| 638 | def _delete_failed(name, resource): |
| 639 | changes_dict = {'name': name, |
| 640 | 'changes': {}, |
| 641 | 'comment': '{0} {1} failed to delete'.format(resource, |
| 642 | name), |
| 643 | 'result': False} |
| 644 | return changes_dict |
| 645 | |
| 646 | |
| 647 | def _create_failed(name, resource): |
| 648 | changes_dict = {'name': name, |
| 649 | 'changes': {}, |
| 650 | 'comment': '{0} {1} failed to create'.format(resource, |
| 651 | name), |
| 652 | 'result': False} |
| 653 | return changes_dict |
| 654 | |
| 655 | |
| 656 | def _update_failed(name, resource): |
| 657 | changes_dict = {'name': name, |
| 658 | 'changes': {}, |
| 659 | 'comment': '{0} {1} failed to update'.format(resource, |
| 660 | name), |
| 661 | 'result': False} |
| 662 | return changes_dict |
| 663 | |
| 664 | |
Sergey Galkin | 408c180 | 2020-02-10 15:56:19 +0400 | [diff] [blame] | 665 | def _check_test_mode(): |
| 666 | if __opts__.get('test'): |
| 667 | return None |
| 668 | else: |
| 669 | return True |
| 670 | |
| 671 | |
Vasyl Saienko | 4eda4f2 | 2018-04-26 19:30:39 +0300 | [diff] [blame] | 672 | def _find_failed(name, resource): |
| 673 | changes_dict = { |
| 674 | 'name': name, |
| 675 | 'changes': {}, |
| 676 | 'comment': '{0} {1} found multiple {0}'.format(resource, name), |
| 677 | 'result': False, |
| 678 | } |
| 679 | return changes_dict |