1# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import json
16import os
17import threading
18
19_json_path = os.path.join(
20    os.path.dirname(os.path.realpath(__file__)), 'defaults.json')
21_defaults = None
22_defaults_lock = threading.Lock()
23
24
25# json_path argument is there for os-client-config
26def get_defaults(json_path=_json_path):
27    global _defaults
28    if _defaults is not None:
29        return _defaults.copy()
30    with _defaults_lock:
31        if _defaults is not None:
32            # Did someone else just finish filling it?
33            return _defaults.copy()
34        # Python language specific defaults
35        # These are defaults related to use of python libraries, they are
36        # not qualities of a cloud.
37        #
38        # NOTE(harlowja): update a in-memory dict, before updating
39        # the global one so that other callers of get_defaults do not
40        # see the partially filled one.
41        tmp_defaults = dict(
42            api_timeout=None,
43            verify=True,
44            cacert=None,
45            cert=None,
46            key=None,
47        )
48        with open(json_path, 'r') as json_file:
49            updates = json.load(json_file)
50            if updates is not None:
51                tmp_defaults.update(updates)
52        _defaults = tmp_defaults
53        return tmp_defaults.copy()
54