-
Notifications
You must be signed in to change notification settings - Fork 197
/
_identity.py
1520 lines (1281 loc) · 54.1 KB
/
_identity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from openstack.cloud import _utils
from openstack import exceptions
from openstack.identity.v3._proxy import Proxy
from openstack import utils
class IdentityCloudMixin:
identity: Proxy
def _get_project_id_param_dict(self, name_or_id):
if name_or_id:
project = self.get_project(name_or_id)
if not project:
return {}
if utils.supports_version(self.identity, '3'):
return {'default_project_id': project['id']}
else:
return {'tenant_id': project['id']}
else:
return {}
def _get_domain_id_param_dict(self, domain_id):
"""Get a useable domain."""
# Keystone v3 requires domains for user and project creation. v2 does
# not. However, keystone v2 does not allow user creation by non-admin
# users, so we can throw an error to the user that does not need to
# mention api versions
if utils.supports_version(self.identity, '3'):
if not domain_id:
raise exceptions.SDKException(
"User or project creation requires an explicit domain_id "
"argument."
)
else:
return {'domain_id': domain_id}
else:
return {}
def _get_identity_params(self, domain_id=None, project=None):
"""Get the domain and project/tenant parameters if needed.
keystone v2 and v3 are divergent enough that we need to pass or not
pass project or tenant_id or domain or nothing in a sane manner.
"""
ret = {}
ret.update(self._get_domain_id_param_dict(domain_id))
ret.update(self._get_project_id_param_dict(project))
return ret
def list_projects(self, domain_id=None, name_or_id=None, filters=None):
"""List projects.
With no parameters, returns a full listing of all visible projects.
:param domain_id: Domain ID to scope the searched projects.
:param name_or_id: Name or ID of the project(s).
:param filters: A dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of identity ``Project`` objects.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
if not filters:
filters = {}
query = dict(**filters)
if name_or_id:
query['name'] = name_or_id
if domain_id:
query['domain_id'] = domain_id
return list(self.identity.projects(**query))
def search_projects(self, name_or_id=None, filters=None, domain_id=None):
"""Backwards compatibility method for search_projects
search_projects originally had a parameter list that was name_or_id,
filters and list had domain_id first. This method exists in this form
to allow code written with positional parameter to still work. But
really, use keyword arguments.
:param name_or_id: Name or ID of the project(s).
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:param domain_id: Domain ID to scope the searched projects.
:returns: A list of identity ``Project`` objects.
"""
projects = self.list_projects(domain_id=domain_id, filters=filters)
return _utils._filter_list(projects, name_or_id, filters)
def get_project(self, name_or_id, filters=None, domain_id=None):
"""Get exactly one project.
:param name_or_id: Name or unique ID of the project.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:param domain_id: Domain ID to scope the retrieved project.
:returns: An identity ``Project`` object.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return _utils._get_entity(
self, 'project', name_or_id, filters, domain_id=domain_id
)
def update_project(
self,
name_or_id,
enabled=None,
domain_id=None,
**kwargs,
):
"""Update a project
:param name_or_id: Name or unique ID of the project.
:param enabled: Whether the project is enabled or not.
:param domain_id: Domain ID to scope the retrieved project.
:returns: An identity ``Project`` object.
"""
project = self.identity.find_project(
name_or_id=name_or_id,
domain_id=domain_id,
)
if not project:
raise exceptions.SDKException("Project %s not found." % name_or_id)
if enabled is not None:
kwargs.update({'enabled': enabled})
project = self.identity.update_project(project, **kwargs)
return project
def create_project(
self,
name,
domain_id,
description=None,
enabled=True,
**kwargs,
):
"""Create a project.
:param name:
:param domain_id:
:param description:
:param enabled:
:returns: An identity ``Project`` object.
"""
attrs = dict(
name=name,
description=description,
domain_id=domain_id,
is_enabled=enabled,
)
if kwargs:
attrs.update(kwargs)
return self.identity.create_project(**attrs)
def delete_project(self, name_or_id, domain_id=None):
"""Delete a project.
:param name_or_id: Name or unique ID of the project.
:param domain_id: Domain ID to scope the retrieved project.
:returns: True if delete succeeded, False if the project was not found.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
try:
project = self.identity.find_project(
name_or_id=name_or_id, ignore_missing=True, domain_id=domain_id
)
if not project:
self.log.debug("Project %s not found for deleting", name_or_id)
return False
self.identity.delete_project(project)
return True
except exceptions.SDKException:
self.log.exception(
"Error in deleting project {project}".format(
project=name_or_id
)
)
return False
@_utils.valid_kwargs('domain_id', 'name')
def list_users(self, **kwargs):
"""List users.
:param name:
:param domain_id: Domain ID to scope the retrieved users.
:returns: A list of identity ``User`` objects.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.identity.users(**kwargs))
def search_users(self, name_or_id=None, filters=None, domain_id=None):
"""Search users.
:param name_or_id: Name or ID of the user(s).
:param domain_id: Domain ID to scope the retrieved users.
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of identity ``User`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
kwargs = {}
# NOTE(jdwidari) if name_or_id isn't UUID like then make use of server-
# side filter for user name https://bit.ly/2qh0Ijk
# especially important when using LDAP and using page to limit results
if name_or_id and not _utils._is_uuid_like(name_or_id):
kwargs['name'] = name_or_id
if domain_id:
kwargs['domain_id'] = domain_id
users = self.list_users(**kwargs)
return _utils._filter_list(users, name_or_id, filters)
# TODO(stephenfin): Remove 'filters' in a future major version
# TODO(stephenfin): Remove 'kwargs' since it doesn't do anything
@_utils.valid_kwargs('domain_id')
def get_user(self, name_or_id, filters=None, **kwargs):
"""Get exactly one user.
:param name_or_id: Name or unique ID of the user.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: an identity ``User`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return _utils._get_entity(self, 'user', name_or_id, filters, **kwargs)
# TODO(stephenfin): Remove normalize since it doesn't do anything
def get_user_by_id(self, user_id, normalize=True):
"""Get a user by ID.
:param string user_id: user ID
:returns: an identity ``User`` object
"""
return self.identity.get_user(user_id)
@_utils.valid_kwargs(
'name',
'email',
'enabled',
'domain_id',
'password',
'description',
'default_project',
)
def update_user(self, name_or_id, **kwargs):
user_kwargs = {}
if 'domain_id' in kwargs and kwargs['domain_id']:
user_kwargs['domain_id'] = kwargs['domain_id']
user = self.get_user(name_or_id, **user_kwargs)
# TODO(mordred) When this changes to REST, force interface=admin
# in the adapter call if it's an admin force call (and figure out how
# to make that disctinction)
# NOTE(samueldmq): now this is a REST call and domain_id is dropped
# if None. keystoneclient drops keys with None values.
if 'domain_id' in kwargs and kwargs['domain_id'] is None:
del kwargs['domain_id']
user = self.identity.update_user(user, **kwargs)
return user
def create_user(
self,
name,
password=None,
email=None,
default_project=None,
enabled=True,
domain_id=None,
description=None,
):
"""Create a user."""
params = self._get_identity_params(domain_id, default_project)
params.update({'name': name, 'email': email, 'enabled': enabled})
if password is not None:
params['password'] = password
if description is not None:
params['description'] = description
user = self.identity.create_user(**params)
return user
@_utils.valid_kwargs('domain_id')
def delete_user(self, name_or_id, **kwargs):
try:
user = self.get_user(name_or_id, **kwargs)
if not user:
self.log.debug(
"User {0} not found for deleting".format(name_or_id)
)
return False
self.identity.delete_user(user)
return True
except exceptions.SDKException:
self.log.exception(
"Error in deleting user {user}".format(user=name_or_id)
)
return False
def _get_user_and_group(self, user_name_or_id, group_name_or_id):
user = self.get_user(user_name_or_id)
if not user:
raise exceptions.SDKException(
'User {user} not found'.format(user=user_name_or_id)
)
group = self.get_group(group_name_or_id)
if not group:
raise exceptions.SDKException(
'Group {user} not found'.format(user=group_name_or_id)
)
return (user, group)
def add_user_to_group(self, name_or_id, group_name_or_id):
"""Add a user to a group.
:param name_or_id: Name or unique ID of the user.
:param group_name_or_id: Group name or ID
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
user, group = self._get_user_and_group(name_or_id, group_name_or_id)
self.identity.add_user_to_group(user, group)
def is_user_in_group(self, name_or_id, group_name_or_id):
"""Check to see if a user is in a group.
:param name_or_id: Name or unique ID of the user.
:param group_name_or_id: Group name or ID
:returns: True if user is in the group, False otherwise
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
user, group = self._get_user_and_group(name_or_id, group_name_or_id)
return self.identity.check_user_in_group(user, group)
def remove_user_from_group(self, name_or_id, group_name_or_id):
"""Remove a user from a group.
:param name_or_id: Name or unique ID of the user.
:param group_name_or_id: Group name or ID
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
user, group = self._get_user_and_group(name_or_id, group_name_or_id)
self.identity.remove_user_from_group(user, group)
@_utils.valid_kwargs('type', 'service_type', 'description')
def create_service(self, name, enabled=True, **kwargs):
"""Create a service.
:param name: Service name.
:param type: Service type. (type or service_type required.)
:param service_type: Service type. (type or service_type required.)
:param description: Service description (optional).
:param enabled: Whether the service is enabled (v3 only)
:returns: an identity ``Service`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
type_ = kwargs.pop('type', None)
service_type = kwargs.pop('service_type', None)
# TODO(mordred) When this changes to REST, force interface=admin
# in the adapter call
kwargs['type'] = type_ or service_type
kwargs['is_enabled'] = enabled
kwargs['name'] = name
return self.identity.create_service(**kwargs)
@_utils.valid_kwargs(
'name', 'enabled', 'type', 'service_type', 'description'
)
def update_service(self, name_or_id, **kwargs):
# NOTE(SamYaple): Keystone v3 only accepts 'type' but shade accepts
# both 'type' and 'service_type' with a preference
# towards 'type'
type_ = kwargs.pop('type', None)
service_type = kwargs.pop('service_type', None)
if type_ or service_type:
kwargs['type'] = type_ or service_type
service = self.get_service(name_or_id)
return self.identity.update_service(service, **kwargs)
def list_services(self):
"""List all Keystone services.
:returns: A list of identity ``Service`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.identity.services())
def search_services(self, name_or_id=None, filters=None):
"""Search Keystone services.
:param name_or_id: Name or ID of the service(s).
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: a list of identity ``Service`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
services = self.list_services()
return _utils._filter_list(services, name_or_id, filters)
# TODO(stephenfin): Remove 'filters' since it's a noop
def get_service(self, name_or_id, filters=None):
"""Get exactly one Keystone service.
:param name_or_id: Name or unique ID of the service.
:returns: an identity ``Service`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call or if multiple matches are
found.
"""
return _utils._get_entity(self, 'service', name_or_id, filters)
def delete_service(self, name_or_id):
"""Delete a Keystone service.
:param name_or_id: Name or unique ID of the service.
:returns: True if delete succeeded, False otherwise.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
service = self.get_service(name_or_id=name_or_id)
if service is None:
self.log.debug("Service %s not found for deleting", name_or_id)
return False
try:
self.identity.delete_service(service)
return True
except exceptions.SDKException:
self.log.exception(
'Failed to delete service {id}'.format(id=service['id'])
)
return False
@_utils.valid_kwargs('public_url', 'internal_url', 'admin_url')
def create_endpoint(
self,
service_name_or_id,
url=None,
interface=None,
region=None,
enabled=True,
**kwargs,
):
"""Create a Keystone endpoint.
:param service_name_or_id: Service name or id for this endpoint.
:param url: URL of the endpoint
:param interface: Interface type of the endpoint
:param public_url: Endpoint public URL.
:param internal_url: Endpoint internal URL.
:param admin_url: Endpoint admin URL.
:param region: Endpoint region.
:param enabled: Whether the endpoint is enabled
:returns: A list of identity ``Endpoint`` objects
:raises: :class:`~openstack.exceptions.SDKException` if the service
cannot be found or if something goes wrong during the OpenStack API
call.
"""
public_url = kwargs.pop('public_url', None)
internal_url = kwargs.pop('internal_url', None)
admin_url = kwargs.pop('admin_url', None)
if (url or interface) and (public_url or internal_url or admin_url):
raise exceptions.SDKException(
"create_endpoint takes either url and interface OR "
"public_url, internal_url, admin_url"
)
service = self.get_service(name_or_id=service_name_or_id)
if service is None:
raise exceptions.SDKException(
"service {service} not found".format(
service=service_name_or_id
)
)
endpoints_args = []
if url:
# v3 in use, v3-like arguments, one endpoint created
endpoints_args.append(
{
'url': url,
'interface': interface,
'service_id': service['id'],
'enabled': enabled,
'region_id': region,
}
)
else:
# v3 in use, v2.0-like arguments, one endpoint created for each
# interface url provided
endpoint_args = {
'region_id': region,
'enabled': enabled,
'service_id': service['id'],
}
if public_url:
endpoint_args.update(
{'url': public_url, 'interface': 'public'}
)
endpoints_args.append(endpoint_args.copy())
if internal_url:
endpoint_args.update(
{'url': internal_url, 'interface': 'internal'}
)
endpoints_args.append(endpoint_args.copy())
if admin_url:
endpoint_args.update({'url': admin_url, 'interface': 'admin'})
endpoints_args.append(endpoint_args.copy())
endpoints = []
for args in endpoints_args:
endpoints.append(self.identity.create_endpoint(**args))
return endpoints
@_utils.valid_kwargs(
'enabled', 'service_name_or_id', 'url', 'interface', 'region'
)
def update_endpoint(self, endpoint_id, **kwargs):
service_name_or_id = kwargs.pop('service_name_or_id', None)
if service_name_or_id is not None:
kwargs['service_id'] = service_name_or_id
if 'region' in kwargs:
kwargs['region_id'] = kwargs.pop('region')
return self.identity.update_endpoint(endpoint_id, **kwargs)
def list_endpoints(self):
"""List Keystone endpoints.
:returns: A list of identity ``Endpoint`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.identity.endpoints())
def search_endpoints(self, id=None, filters=None):
"""List Keystone endpoints.
:param id: ID of endpoint(s).
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of identity ``Endpoint`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
# NOTE(SamYaple): With keystone v3 we can filter directly via the
# the keystone api, but since the return of all the endpoints even in
# large environments is small, we can continue to filter in shade just
# like the v2 api.
endpoints = self.list_endpoints()
return _utils._filter_list(endpoints, id, filters)
# TODO(stephenfin): Remove 'filters' since it's a noop
def get_endpoint(self, id, filters=None):
"""Get exactly one Keystone endpoint.
:param id: ID of endpoint.
:returns: An identity ``Endpoint`` object
"""
return _utils._get_entity(self, 'endpoint', id, filters)
def delete_endpoint(self, id):
"""Delete a Keystone endpoint.
:param id: ID of the endpoint to delete.
:returns: True if delete succeeded, False otherwise.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
endpoint = self.get_endpoint(id=id)
if endpoint is None:
self.log.debug("Endpoint %s not found for deleting", id)
return False
try:
self.identity.delete_endpoint(id)
return True
except exceptions.SDKException:
self.log.exception("Failed to delete endpoint {id}".format(id=id))
return False
def create_domain(self, name, description=None, enabled=True):
"""Create a domain.
:param name: The name of the domain.
:param description: A description of the domain.
:param enabled: Is the domain enabled or not (default True).
:returns: The created identity ``Endpoint`` object.
:raises: :class:`~openstack.exceptions.SDKException` if the domain
cannot be created.
"""
domain_ref = {'name': name, 'enabled': enabled}
if description is not None:
domain_ref['description'] = description
return self.identity.create_domain(**domain_ref)
# TODO(stephenfin): domain_id and name_or_id are the same thing now;
# deprecate one of them
def update_domain(
self,
domain_id=None,
name=None,
description=None,
enabled=None,
name_or_id=None,
):
"""Update a Keystone domain
:param domain_id:
:param name:
:param description:
:param enabled:
:param name_or_id: Name or unique ID of the domain.
:returns: The updated identity ``Domain`` object.
:raises: :class:`~openstack.exceptions.SDKException` if the domain
cannot be updated
"""
if domain_id is None:
if name_or_id is None:
raise exceptions.SDKException(
"You must pass either domain_id or name_or_id value"
)
dom = self.get_domain(None, name_or_id)
if dom is None:
raise exceptions.SDKException(
"Domain {0} not found for updating".format(name_or_id)
)
domain_id = dom['id']
domain_ref = {}
domain_ref.update({'name': name} if name else {})
domain_ref.update({'description': description} if description else {})
domain_ref.update({'enabled': enabled} if enabled is not None else {})
return self.identity.update_domain(domain_id, **domain_ref)
# TODO(stephenfin): domain_id and name_or_id are the same thing now;
# deprecate one of them
def delete_domain(self, domain_id=None, name_or_id=None):
"""Delete a Keystone domain.
:param domain_id: ID of the domain to delete.
:param name_or_id: Name or unique ID of the domain.
:returns: True if delete succeeded, False otherwise.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
try:
if domain_id is None:
if name_or_id is None:
raise exceptions.SDKException(
"You must pass either domain_id or name_or_id value"
)
dom = self.get_domain(name_or_id=name_or_id)
if dom is None:
self.log.debug(
"Domain %s not found for deleting", name_or_id
)
return False
domain_id = dom['id']
# A domain must be disabled before deleting
self.identity.update_domain(domain_id, is_enabled=False)
self.identity.delete_domain(domain_id, ignore_missing=False)
return True
except exceptions.SDKException:
self.log.exception("Failed to delete domain %s" % domain_id)
raise
def list_domains(self, **filters):
"""List Keystone domains.
:returns: A list of identity ``Domain`` objects.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.identity.domains(**filters))
# TODO(stephenfin): These arguments are backwards from everything else.
def search_domains(self, filters=None, name_or_id=None):
"""Search Keystone domains.
:param name_or_id: Name or ID of the domain(s).
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: a list of identity ``Domain`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
if filters is None:
filters = {}
if name_or_id is not None:
domains = self.list_domains()
return _utils._filter_list(domains, name_or_id, filters)
else:
return self.list_domains(**filters)
# TODO(stephenfin): domain_id and name_or_id are the same thing now;
# deprecate one of them
# TODO(stephenfin): Remove 'filters' in a future major version
def get_domain(self, domain_id=None, name_or_id=None, filters=None):
"""Get exactly one Keystone domain.
:param domain_id: ID of the domain.
:param name_or_id: Name or unique ID of the domain.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: an identity ``Domain`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
if domain_id is None:
return self.identity.find_domain(name_or_id)
else:
return self.identity.get_domain(domain_id)
@_utils.valid_kwargs('domain_id')
def list_groups(self, **kwargs):
"""List Keystone groups.
:param domain_id: Domain ID.
:returns: A list of identity ``Group`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.identity.groups(**kwargs))
@_utils.valid_kwargs('domain_id')
def search_groups(self, name_or_id=None, filters=None, **kwargs):
"""Search Keystone groups.
:param name_or_id: Name or ID of the group(s).
:param filters: dictionary of meta data to use for further filtering.
Elements of this dictionary may, themselves, be dictionaries.
Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:param domain_id: domain id.
:returns: A list of identity ``Group`` objects
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
groups = self.list_groups(**kwargs)
return _utils._filter_list(groups, name_or_id, filters)
# TODO(stephenfin): Remove filters since it's a noop
# TODO(stephenfin): Remove kwargs since it's a noop
@_utils.valid_kwargs('domain_id')
def get_group(self, name_or_id, filters=None, **kwargs):
"""Get exactly one Keystone group.
:param name_or_id: Name or unique ID of the group(s).
:returns: An identity ``Group`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return _utils._get_entity(self, 'group', name_or_id, filters, **kwargs)
def create_group(self, name, description, domain=None):
"""Create a group.
:param string name: Group name.
:param string description: Group description.
:param string domain: Domain name or ID for the group.
:returns: An identity ``Group`` object
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
group_ref = {'name': name}
if description:
group_ref['description'] = description
if domain:
dom = self.get_domain(domain)
if not dom:
raise exceptions.SDKException(
"Creating group {group} failed: Invalid domain "
"{domain}".format(group=name, domain=domain)
)
group_ref['domain_id'] = dom['id']
group = self.identity.create_group(**group_ref)
return group
def update_group(
self,
name_or_id,
name=None,
description=None,
**kwargs,
):
"""Update an existing group
:param name_or_id: Name or unique ID of the group.
:param name: New group name.
:param description: New group description.