-
Notifications
You must be signed in to change notification settings - Fork 66
/
openstackcloud.py
8625 lines (7282 loc) · 347 KB
/
openstackcloud.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 3.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.
import base64
import collections
import functools
import hashlib
import ipaddress
import json
import jsonpatch
import keystoneauth1.session
import operator
import os
import os_client_config
import os_client_config.defaults
import six
import threading
import time
import warnings
import dogpile.cache
import munch
import requestsexceptions
from six.moves import urllib
import keystoneauth1.exceptions
import shade
from shade.exc import * # noqa
from shade import _adapter
from shade._heat import event_utils
from shade._heat import template_utils
from shade import _log
from shade import _legacy_clients
from shade import _normalize
from shade import meta
from shade import task_manager
from shade import _utils
OBJECT_MD5_KEY = 'x-object-meta-x-shade-md5'
OBJECT_SHA256_KEY = 'x-object-meta-x-shade-sha256'
IMAGE_MD5_KEY = 'owner_specified.shade.md5'
IMAGE_SHA256_KEY = 'owner_specified.shade.sha256'
IMAGE_OBJECT_KEY = 'owner_specified.shade.object'
# Rackspace returns this for intermittent import errors
IMAGE_ERROR_396 = "Image cannot be imported. Error code: '396'"
DEFAULT_OBJECT_SEGMENT_SIZE = 1073741824 # 1GB
# This halves the current default for Swift
DEFAULT_MAX_FILE_SIZE = (5 * 1024 * 1024 * 1024 + 2) / 2
DEFAULT_SERVER_AGE = 5
DEFAULT_PORT_AGE = 5
DEFAULT_FLOAT_AGE = 5
_OCC_DOC_URL = "https://docs.openstack.org/developer/os-client-config"
OBJECT_CONTAINER_ACLS = {
'public': '.r:*,.rlistings',
'private': '',
}
def _no_pending_volumes(volumes):
"""If there are any volumes not in a steady state, don't cache"""
for volume in volumes:
if volume['status'] not in ('available', 'error', 'in-use'):
return False
return True
def _no_pending_images(images):
"""If there are any images not in a steady state, don't cache"""
for image in images:
if image.status not in ('active', 'deleted', 'killed'):
return False
return True
def _no_pending_stacks(stacks):
"""If there are any stacks not in a steady state, don't cache"""
for stack in stacks:
status = stack['stack_status']
if '_COMPLETE' not in status and '_FAILED' not in status:
return False
return True
class OpenStackCloud(
_normalize.Normalizer,
_legacy_clients.LegacyClientFactoryMixin):
"""Represent a connection to an OpenStack Cloud.
OpenStackCloud is the entry point for all cloud operations, regardless
of which OpenStack service those operations may ultimately come from.
The operations on an OpenStackCloud are resource oriented rather than
REST API operation oriented. For instance, one will request a Floating IP
and that Floating IP will be actualized either via neutron or via nova
depending on how this particular cloud has decided to arrange itself.
:param TaskManager manager: Optional task manager to use for running
OpenStack API tasks. Unless you're doing
rate limiting client side, you almost
certainly don't need this. (optional)
:param bool log_inner_exceptions: Send wrapped exceptions to the error log.
Defaults to false, because there are a
number of wrapped exceptions that are
noise for normal usage. It's possible
that for a user that has python logging
configured properly, it's desirable to
have all of the wrapped exceptions be
emitted to the error log. This flag
will enable that behavior.
:param bool strict: Only return documented attributes for each resource
as per the shade Data Model contract. (Default False)
:param app_name: Name of the application to be appended to the user-agent
string. Optional, defaults to None.
:param app_version: Version of the application to be appended to the
user-agent string. Optional, defaults to None.
:param CloudConfig cloud_config: Cloud config object from os-client-config
In the future, this will be the only way
to pass in cloud configuration, but is
being phased in currently.
"""
def __init__(
self,
cloud_config=None,
manager=None, log_inner_exceptions=False,
strict=False,
app_name=None,
app_version=None,
use_direct_get=False,
**kwargs):
if log_inner_exceptions:
OpenStackCloudException.log_inner_exceptions = True
self.log = _log.setup_logging('shade')
if not cloud_config:
config = os_client_config.OpenStackConfig(
app_name=app_name, app_version=app_version)
cloud_config = config.get_one_cloud(**kwargs)
self.name = cloud_config.name
self.auth = cloud_config.get_auth_args()
self.region_name = cloud_config.region_name
self.default_interface = cloud_config.get_interface()
self.private = cloud_config.config.get('private', False)
self.api_timeout = cloud_config.config['api_timeout']
self.image_api_use_tasks = cloud_config.config['image_api_use_tasks']
self.secgroup_source = cloud_config.config['secgroup_source']
self.force_ipv4 = cloud_config.force_ipv4
self.strict_mode = strict
# TODO(mordred) When os-client-config adds a "get_client_settings()"
# method to CloudConfig - remove this.
self._extra_config = cloud_config._openstack_config.get_extra_config(
'shade', {
'get_flavor_extra_specs': True,
})
if manager is not None:
self.manager = manager
else:
self.manager = task_manager.TaskManager(
name=':'.join([self.name, self.region_name]), client=self)
self._external_ipv4_names = cloud_config.get_external_ipv4_networks()
self._internal_ipv4_names = cloud_config.get_internal_ipv4_networks()
self._external_ipv6_names = cloud_config.get_external_ipv6_networks()
self._internal_ipv6_names = cloud_config.get_internal_ipv6_networks()
self._nat_destination = cloud_config.get_nat_destination()
self._default_network = cloud_config.get_default_network()
self._floating_ip_source = cloud_config.config.get(
'floating_ip_source')
if self._floating_ip_source:
if self._floating_ip_source.lower() == 'none':
self._floating_ip_source = None
else:
self._floating_ip_source = self._floating_ip_source.lower()
self._use_external_network = cloud_config.config.get(
'use_external_network', True)
self._use_internal_network = cloud_config.config.get(
'use_internal_network', True)
# Work around older TaskManager objects that don't have submit_task
if not hasattr(self.manager, 'submit_task'):
self.manager.submit_task = self.manager.submitTask
(self.verify, self.cert) = cloud_config.get_requests_verify_args()
# Turn off urllib3 warnings about insecure certs if we have
# explicitly configured requests to tell it we do not want
# cert verification
if not self.verify:
self.log.debug(
"Turning off Insecure SSL warnings since verify=False")
category = requestsexceptions.InsecureRequestWarning
if category:
# InsecureRequestWarning references a Warning class or is None
warnings.filterwarnings('ignore', category=category)
self._disable_warnings = {}
self.use_direct_get = use_direct_get
self._servers = None
self._servers_time = 0
self._servers_lock = threading.Lock()
self._ports = None
self._ports_time = 0
self._ports_lock = threading.Lock()
self._floating_ips = None
self._floating_ips_time = 0
self._floating_ips_lock = threading.Lock()
self._floating_network_by_router = None
self._floating_network_by_router_run = False
self._floating_network_by_router_lock = threading.Lock()
self._networks_lock = threading.Lock()
self._reset_network_caches()
cache_expiration_time = int(cloud_config.get_cache_expiration_time())
cache_class = cloud_config.get_cache_class()
cache_arguments = cloud_config.get_cache_arguments()
self._resource_caches = {}
if cache_class != 'dogpile.cache.null':
self.cache_enabled = True
self._cache = self._make_cache(
cache_class, cache_expiration_time, cache_arguments)
expirations = cloud_config.get_cache_expiration()
for expire_key in expirations.keys():
# Only build caches for things we have list operations for
if getattr(
self, 'list_{0}'.format(expire_key), None):
self._resource_caches[expire_key] = self._make_cache(
cache_class, expirations[expire_key], cache_arguments)
self._SERVER_AGE = DEFAULT_SERVER_AGE
self._PORT_AGE = DEFAULT_PORT_AGE
self._FLOAT_AGE = DEFAULT_FLOAT_AGE
else:
self.cache_enabled = False
def _fake_invalidate(unused):
pass
class _FakeCache(object):
def invalidate(self):
pass
# Don't cache list_servers if we're not caching things.
# Replace this with a more specific cache configuration
# soon.
self._SERVER_AGE = 0
self._PORT_AGE = 0
self._FLOAT_AGE = 0
self._cache = _FakeCache()
# Undecorate cache decorated methods. Otherwise the call stacks
# wind up being stupidly long and hard to debug
for method in _utils._decorated_methods:
meth_obj = getattr(self, method, None)
if not meth_obj:
continue
if (hasattr(meth_obj, 'invalidate')
and hasattr(meth_obj, 'func')):
new_func = functools.partial(meth_obj.func, self)
new_func.invalidate = _fake_invalidate
setattr(self, method, new_func)
# If server expiration time is set explicitly, use that. Otherwise
# fall back to whatever it was before
self._SERVER_AGE = cloud_config.get_cache_resource_expiration(
'server', self._SERVER_AGE)
self._PORT_AGE = cloud_config.get_cache_resource_expiration(
'port', self._PORT_AGE)
self._FLOAT_AGE = cloud_config.get_cache_resource_expiration(
'floating_ip', self._FLOAT_AGE)
self._container_cache = dict()
self._file_hash_cache = dict()
self._keystone_session = None
self._legacy_clients = {}
self._raw_clients = {}
self._local_ipv6 = (
_utils.localhost_supports_ipv6() if not self.force_ipv4 else False)
self.cloud_config = cloud_config
def _make_cache(self, cache_class, expiration_time, arguments):
return dogpile.cache.make_region(
function_key_generator=self._make_cache_key
).configure(
cache_class,
expiration_time=expiration_time,
arguments=arguments)
def _make_cache_key(self, namespace, fn):
fname = fn.__name__
if namespace is None:
name_key = self.name
else:
name_key = '%s:%s' % (self.name, namespace)
def generate_key(*args, **kwargs):
arg_key = ','.join(args)
kw_keys = sorted(kwargs.keys())
kwargs_key = ','.join(
['%s:%s' % (k, kwargs[k]) for k in kw_keys if k != 'cache'])
ans = "_".join(
[str(name_key), fname, arg_key, kwargs_key])
return ans
return generate_key
def _get_cache(self, resource_name):
if resource_name and resource_name in self._resource_caches:
return self._resource_caches[resource_name]
else:
return self._cache
def _get_client(
self, service_key, client_class=None, interface_key=None,
pass_version_arg=True, **kwargs):
try:
client = self.cloud_config.get_legacy_client(
service_key=service_key, client_class=client_class,
interface_key=interface_key, pass_version_arg=pass_version_arg,
**kwargs)
except Exception:
self.log.debug(
"Couldn't construct %(service)s object",
{'service': service_key}, exc_info=True)
raise
if client is None:
raise OpenStackCloudException(
"Failed to instantiate {service} client."
" This could mean that your credentials are wrong.".format(
service=service_key))
return client
def _get_major_version_id(self, version):
if isinstance(version, int):
return version
elif isinstance(version, six.string_types + (tuple,)):
return int(version[0])
return version
def _get_versioned_client(
self, service_type, min_version=None, max_version=None):
config_version = self.cloud_config.get_api_version(service_type)
config_major = self._get_major_version_id(config_version)
max_major = self._get_major_version_id(max_version)
min_major = self._get_major_version_id(min_version)
# NOTE(mordred) The shade logic for versions is slightly different
# than the ksa Adapter constructor logic. shade knows the versions
# it knows, and uses them when it detects them. However, if a user
# requests a version, and it's not found, and a different one shade
# does know about it found, that's a warning in shade.
if config_version:
if min_major and config_major < min_major:
raise OpenStackCloudException(
"Version {config_version} requested for {service_type}"
" but shade understands a minimum of {min_version}".format(
config_version=config_version,
service_type=service_type,
min_version=min_version))
elif max_major and config_major > max_major:
raise OpenStackCloudException(
"Version {config_version} requested for {service_type}"
" but shade understands a maximum of {max_version}".format(
config_version=config_version,
service_type=service_type,
max_version=max_version))
request_min_version = config_version
request_max_version = '{version}.latest'.format(
version=config_major)
adapter = _adapter.ShadeAdapter(
session=self.keystone_session,
manager=self.manager,
service_type=self.cloud_config.get_service_type(service_type),
service_name=self.cloud_config.get_service_name(service_type),
interface=self.cloud_config.get_interface(service_type),
endpoint_override=self.cloud_config.get_endpoint(service_type),
region_name=self.cloud_config.region,
min_version=request_min_version,
max_version=request_max_version,
shade_logger=self.log)
if adapter.get_endpoint():
return adapter
adapter = _adapter.ShadeAdapter(
session=self.keystone_session,
manager=self.manager,
service_type=self.cloud_config.get_service_type(service_type),
service_name=self.cloud_config.get_service_name(service_type),
interface=self.cloud_config.get_interface(service_type),
endpoint_override=self.cloud_config.get_endpoint(service_type),
region_name=self.cloud_config.region,
min_version=min_version,
max_version=max_version,
shade_logger=self.log)
# data.api_version can be None if no version was detected, such
# as with neutron
api_version = adapter.get_api_major_version(
endpoint_override=self.cloud_config.get_endpoint(service_type))
api_major = self._get_major_version_id(api_version)
# If we detect a different version that was configured, warn the user.
# shade still knows what to do - but if the user gave us an explicit
# version and we couldn't find it, they may want to investigate.
if api_version and (api_major != config_major):
warning_msg = (
'{service_type} is configured for {config_version}'
' but only {api_version} is available. shade is happy'
' with this version, but if you were trying to force an'
' override, that did not happen. You may want to check'
' your cloud, or remove the version specification from'
' your config.'.format(
service_type=service_type,
config_version=config_version,
api_version='.'.join([str(f) for f in api_version])))
self.log.debug(warning_msg)
warnings.warn(warning_msg)
return adapter
def _get_raw_client(
self, service_type, api_version=None, endpoint_override=None):
return _adapter.ShadeAdapter(
session=self.keystone_session,
manager=self.manager,
service_type=self.cloud_config.get_service_type(service_type),
service_name=self.cloud_config.get_service_name(service_type),
interface=self.cloud_config.get_interface(service_type),
endpoint_override=self.cloud_config.get_endpoint(
service_type) or endpoint_override,
region_name=self.cloud_config.region,
shade_logger=self.log)
def _is_client_version(self, client, version):
client_name = '_{client}_client'.format(client=client)
client = getattr(self, client_name)
return client._version_matches(version)
@property
def _application_catalog_client(self):
if 'application-catalog' not in self._raw_clients:
self._raw_clients['application-catalog'] = self._get_raw_client(
'application-catalog')
return self._raw_clients['application-catalog']
@property
def _baremetal_client(self):
if 'baremetal' not in self._raw_clients:
client = self._get_raw_client('baremetal')
# Do this to force version discovery. We need to do that, because
# the endpoint-override trick we do for neutron because
# ironicclient just appends a /v1 won't work and will break
# keystoneauth - because ironic's versioned discovery endpoint
# is non-compliant and doesn't return an actual version dict.
client = self._get_versioned_client(
'baremetal', min_version=1, max_version='1.latest')
self._raw_clients['baremetal'] = client
return self._raw_clients['baremetal']
@property
def _container_infra_client(self):
if 'container-infra' not in self._raw_clients:
self._raw_clients['container-infra'] = self._get_raw_client(
'container-infra')
return self._raw_clients['container-infra']
@property
def _compute_client(self):
# TODO(mordred) Deal with microversions
if 'compute' not in self._raw_clients:
self._raw_clients['compute'] = self._get_raw_client('compute')
return self._raw_clients['compute']
@property
def _database_client(self):
if 'database' not in self._raw_clients:
self._raw_clients['database'] = self._get_raw_client('database')
return self._raw_clients['database']
@property
def _dns_client(self):
if 'dns' not in self._raw_clients:
dns_client = self._get_versioned_client(
'dns', min_version=2, max_version='2.latest')
self._raw_clients['dns'] = dns_client
return self._raw_clients['dns']
@property
def _identity_client(self):
if 'identity' not in self._raw_clients:
self._raw_clients['identity'] = self._get_versioned_client(
'identity', min_version=2, max_version='3.latest')
return self._raw_clients['identity']
@property
def _raw_image_client(self):
if 'raw-image' not in self._raw_clients:
image_client = self._get_raw_client('image')
self._raw_clients['raw-image'] = image_client
return self._raw_clients['raw-image']
@property
def _image_client(self):
if 'image' not in self._raw_clients:
self._raw_clients['image'] = self._get_versioned_client(
'image', min_version=1, max_version='2.latest')
return self._raw_clients['image']
@property
def _network_client(self):
if 'network' not in self._raw_clients:
client = self._get_raw_client('network')
# TODO(mordred) I don't care if this is what neutronclient does,
# fix this.
# Don't bother with version discovery - there is only one version
# of neutron. This is what neutronclient does, fwiw.
endpoint = client.get_endpoint()
if not endpoint.rstrip().rsplit('/')[1] == 'v2.0':
if not endpoint.endswith('/'):
endpoint += '/'
endpoint = urllib.parse.urljoin(
endpoint, 'v2.0')
client.endpoint_override = endpoint
self._raw_clients['network'] = client
return self._raw_clients['network']
@property
def _object_store_client(self):
if 'object-store' not in self._raw_clients:
raw_client = self._get_raw_client('object-store')
self._raw_clients['object-store'] = raw_client
return self._raw_clients['object-store']
@property
def _orchestration_client(self):
if 'orchestration' not in self._raw_clients:
raw_client = self._get_raw_client('orchestration')
self._raw_clients['orchestration'] = raw_client
return self._raw_clients['orchestration']
@property
def _volume_client(self):
if 'volume' not in self._raw_clients:
self._raw_clients['volume'] = self._get_raw_client('volume')
return self._raw_clients['volume']
def pprint(self, resource):
"""Wrapper aroud pprint that groks munch objects"""
# import late since this is a utility function
import pprint
new_resource = _utils._dictify_resource(resource)
pprint.pprint(new_resource)
def pformat(self, resource):
"""Wrapper aroud pformat that groks munch objects"""
# import late since this is a utility function
import pprint
new_resource = _utils._dictify_resource(resource)
return pprint.pformat(new_resource)
@property
def keystone_session(self):
if self._keystone_session is None:
try:
self._keystone_session = self.cloud_config.get_session()
if hasattr(self._keystone_session, 'additional_user_agent'):
self._keystone_session.additional_user_agent.append(
('shade', shade.__version__))
except Exception as e:
raise OpenStackCloudException(
"Error authenticating to keystone: %s " % str(e))
return self._keystone_session
@property
def _keystone_catalog(self):
return self.keystone_session.auth.get_access(
self.keystone_session).service_catalog
@property
def service_catalog(self):
return self._keystone_catalog.catalog
def endpoint_for(self, service_type, interface='public'):
return self._keystone_catalog.url_for(
service_type=service_type, interface=interface)
@property
def auth_token(self):
# Keystone's session will reuse a token if it is still valid.
# We don't need to track validity here, just get_token() each time.
return self.keystone_session.get_token()
@property
def current_project_id(self):
"""Get the current project ID.
Returns the project_id of the current token scope. None means that
the token is domain scoped or unscoped.
:raises keystoneauth1.exceptions.auth.AuthorizationFailure:
if a new token fetch fails.
:raises keystoneauth1.exceptions.auth_plugins.MissingAuthPlugin:
if a plugin is not available.
"""
return self.keystone_session.get_project_id()
@property
def current_project(self):
"""Return a ``munch.Munch`` describing the current project"""
return self._get_project_info()
def _get_project_info(self, project_id=None):
project_info = munch.Munch(
id=project_id,
name=None,
domain_id=None,
domain_name=None,
)
if not project_id or project_id == self.current_project_id:
# If we don't have a project_id parameter, it means a user is
# directly asking what the current state is.
# Alternately, if we have one, that means we're calling this
# from within a normalize function, which means the object has
# a project_id associated with it. If the project_id matches
# the project_id of our current token, that means we can supplement
# the info with human readable info about names if we have them.
# If they don't match, that means we're an admin who has pulled
# an object from a different project, so adding info from the
# current token would be wrong.
auth_args = self.cloud_config.config.get('auth', {})
project_info['id'] = self.current_project_id
project_info['name'] = auth_args.get('project_name')
project_info['domain_id'] = auth_args.get('project_domain_id')
project_info['domain_name'] = auth_args.get('project_domain_name')
return project_info
@property
def current_location(self):
"""Return a ``munch.Munch`` explaining the current cloud location."""
return self._get_current_location()
def _get_current_location(self, project_id=None, zone=None):
return munch.Munch(
cloud=self.name,
region_name=self.region_name,
zone=zone,
project=self._get_project_info(project_id),
)
def _get_identity_location(self):
'''Identity resources do not exist inside of projects.'''
return munch.Munch(
cloud=self.name,
region_name=None,
zone=None,
project=munch.Munch(
id=None,
name=None,
domain_id=None,
domain_name=None))
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 self._is_client_version('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 self._is_client_version('identity', 3):
if not domain_id:
raise OpenStackCloudException(
"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 range_search(self, data, filters):
"""Perform integer range searches across a list of dictionaries.
Given a list of dictionaries, search across the list using the given
dictionary keys and a range of integer values for each key. Only
dictionaries that match ALL search filters across the entire original
data set will be returned.
It is not a requirement that each dictionary contain the key used
for searching. Those without the key will be considered non-matching.
The range values must be string values and is either a set of digits
representing an integer for matching, or a range operator followed by
a set of digits representing an integer for matching. If a range
operator is not given, exact value matching will be used. Valid
operators are one of: <,>,<=,>=
:param list data: List of dictionaries to be searched.
:param filters: Dict describing the one or more range searches to
perform. If more than one search is given, the result will be the
members of the original data set that match ALL searches. An
example of filtering by multiple ranges::
{"vcpus": "<=5", "ram": "<=2048", "disk": "1"}
:returns: A list subset of the original data set.
:raises: OpenStackCloudException on invalid range expressions.
"""
filtered = []
for key, range_value in filters.items():
# We always want to operate on the full data set so that
# calculations for minimum and maximum are correct.
results = _utils.range_filter(data, key, range_value)
if not filtered:
# First set of results
filtered = results
else:
# The combination of all searches should be the intersection of
# all result sets from each search. So adjust the current set
# of filtered data by computing its intersection with the
# latest result set.
filtered = [r for r in results for f in filtered if r == f]
return filtered
def _get_and_munchify(self, key, data):
"""Wrapper around meta.get_and_munchify.
Some of the methods expect a `meta` attribute to be passed in as
part of the method signature. In those methods the meta param is
overriding the meta module making the call to meta.get_and_munchify
to fail.
"""
return meta.get_and_munchify(key, data)
@_utils.cache_on_arguments()
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: project name or ID.
:param filters: a dict containing additional filters to use
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: a list of ``munch.Munch`` containing the projects
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
kwargs = dict(
filters=filters,
domain_id=domain_id)
if self._is_client_version('identity', 3):
kwargs['obj_name'] = 'project'
pushdown, filters = _normalize._split_filters(**kwargs)
try:
if self._is_client_version('identity', 3):
key = 'projects'
else:
key = 'tenants'
data = self._identity_client.get(
'/{endpoint}'.format(endpoint=key), params=pushdown)
projects = self._normalize_projects(
self._get_and_munchify(key, data))
except Exception as e:
self.log.debug("Failed to list projects", exc_info=True)
raise OpenStackCloudException(str(e))
return _utils._filter_list(projects, name_or_id, filters)
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.
'''
return self.list_projects(
domain_id=domain_id, name_or_id=name_or_id, filters=filters)
def get_project(self, name_or_id, filters=None, domain_id=None):
"""Get exactly one project.
:param name_or_id: project name or ID.
:param filters: a dict containing additional filters to use.
:param domain_id: domain ID (identity v3 only).
:returns: a list of ``munch.Munch`` containing the project description.
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
return _utils._get_entity(self, 'project', name_or_id, filters,
domain_id=domain_id)
@_utils.valid_kwargs('description')
def update_project(self, name_or_id, enabled=None, domain_id=None,
**kwargs):
with _utils.shade_exceptions(
"Error in updating project {project}".format(
project=name_or_id)):
proj = self.get_project(name_or_id, domain_id=domain_id)
if not proj:
raise OpenStackCloudException(
"Project %s not found." % name_or_id)
if enabled is not None:
kwargs.update({'enabled': enabled})
# NOTE(samueldmq): Current code only allow updates of description
# or enabled fields.
if self._is_client_version('identity', 3):
data = self._identity_client.patch(
'/projects/' + proj['id'], json={'project': kwargs})
project = self._get_and_munchify('project', data)
else:
data = self._identity_client.post(
'/tenants/' + proj['id'], json={'tenant': kwargs})
project = self._get_and_munchify('tenant', data)
project = self._normalize_project(project)
self.list_projects.invalidate(self)
return project
def create_project(
self, name, description=None, domain_id=None, enabled=True):
"""Create a project."""
with _utils.shade_exceptions(
"Error in creating project {project}".format(project=name)):
project_ref = self._get_domain_id_param_dict(domain_id)
project_ref.update({'name': name,
'description': description,
'enabled': enabled})
endpoint, key = ('tenants', 'tenant')
if self._is_client_version('identity', 3):
endpoint, key = ('projects', 'project')
data = self._identity_client.post(
'/{endpoint}'.format(endpoint=endpoint),
json={key: project_ref})
project = self._normalize_project(
self._get_and_munchify(key, data))
self.list_projects.invalidate(self)
return project
def delete_project(self, name_or_id, domain_id=None):
"""Delete a project.
:param string name_or_id: Project name or ID.
:param string domain_id: Domain ID containing the project(identity v3
only).
:returns: True if delete succeeded, False if the project was not found.
:raises: ``OpenStackCloudException`` if something goes wrong during
the OpenStack API call
"""
with _utils.shade_exceptions(
"Error in deleting project {project}".format(
project=name_or_id)):
project = self.get_project(name_or_id, domain_id=domain_id)
if project is None:
self.log.debug(
"Project %s not found for deleting", name_or_id)
return False
if self._is_client_version('identity', 3):
self._identity_client.delete('/projects/' + project['id'])
else:
self._identity_client.delete('/tenants/' + project['id'])
return True
@_utils.valid_kwargs('domain_id')
@_utils.cache_on_arguments()
def list_users(self, **kwargs):
"""List users.
:param domain_id: Domain ID. (v3)
:returns: a list of ``munch.Munch`` containing the user description.
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
data = self._identity_client.get('/users', params=kwargs)
return _utils.normalize_users(
self._get_and_munchify('users', data))
@_utils.valid_kwargs('domain_id')
def search_users(self, name_or_id=None, filters=None, **kwargs):
"""Search users.
:param string name_or_id: user name or ID.
:param domain_id: Domain ID. (v3)
:param filters: a dict containing additional filters to use.
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: a list of ``munch.Munch`` containing the users
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
users = self.list_users(**kwargs)
return _utils._filter_list(users, name_or_id, filters)
@_utils.valid_kwargs('domain_id')
def get_user(self, name_or_id, filters=None, **kwargs):
"""Get exactly one user.
:param string name_or_id: user name or ID.
:param domain_id: Domain ID. (v3)
:param filters: a dict containing additional filters to use.
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: a single ``munch.Munch`` containing the user description.
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
return _utils._get_entity(self, 'user', name_or_id, filters, **kwargs)
def get_user_by_id(self, user_id, normalize=True):
"""Get a user by ID.
:param string user_id: user ID
:param bool normalize: Flag to control dict normalization
:returns: a single ``munch.Munch`` containing the user description
"""
data = self._identity_client.get(
'/users/{user}'.format(user=user_id),
error_message="Error getting user with ID {user_id}".format(
user_id=user_id))
user = self._get_and_munchify('user', data)
if user and normalize:
user = _utils.normalize_users(user)
return user
# NOTE(Shrews): Keystone v2 supports updating only name, email and enabled.
@_utils.valid_kwargs('name', 'email', 'enabled', 'domain_id', 'password',
'description', 'default_project')
def update_user(self, name_or_id, **kwargs):
self.list_users.invalidate(self)
user_kwargs = {}
if 'domain_id' in kwargs and kwargs['domain_id']: