summaryrefslogtreecommitdiff
path: root/os_client_config/cloud_config.py
blob: 5ac061208538e723fee54d1da1e325bd0e9938a4 (plain)
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
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# 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.

import warnings


class CloudConfig(object):
    def __init__(self, name, region, config, prefer_ipv6=False):
        self.name = name
        self.region = region
        self.config = config
        self._prefer_ipv6 = prefer_ipv6

    def __getattr__(self, key):
        """Return arbitrary attributes."""

        if key.startswith('os_'):
            key = key[3:]

        if key in [attr.replace('-', '_') for attr in self.config]:
            return self.config[key]
        else:
            return None

    def __iter__(self):
        return self.config.__iter__()

    def __eq__(self, other):
        return (self.name == other.name and self.region == other.region
                and self.config == other.config)

    def __ne__(self, other):
        return not self == other

    def get_requests_verify_args(self):
        """Return the verify and cert values for the requests library."""
        if self.config['verify'] and self.config['cacert']:
            verify = self.config['cacert']
        else:
            verify = self.config['verify']
            if self.config['cacert']:
                warnings.warn(
                    "You are specifying a cacert for the cloud {0} but "
                    "also to ignore the host verification. The host SSL cert "
                    "will not be verified.".format(self.name))

        cert = self.config.get('cert', None)
        if cert:
            if self.config['key']:
                cert = (cert, self.config['key'])
        return (verify, cert)

    def get_services(self):
        """Return a list of service types we know something about."""
        services = []
        for key, val in self.config.items():
            if (key.endswith('api_version')
                    or key.endswith('service_type')
                    or key.endswith('service_name')):
                services.append("_".join(key.split('_')[:-2]))
        return list(set(services))

    def get_auth_args(self):
        return self.config['auth']

    def get_endpoint_type(self, service_type=None):
        if not service_type:
            return self.config['endpoint_type']
        key = '{service_type}_endpoint_type'.format(service_type=service_type)
        return self.config.get(key, self.config['endpoint_type'])

    def get_region_name(self, service_type=None):
        if not service_type:
            return self.region
        key = '{service_type}_region_name'.format(service_type=service_type)
        return self.config.get(key, self.region)

    def get_api_version(self, service_type):
        key = '{service_type}_api_version'.format(service_type=service_type)
        return self.config.get(key, None)

    def get_service_type(self, service_type):
        key = '{service_type}_service_type'.format(service_type=service_type)
        return self.config.get(key, service_type)

    def get_service_name(self, service_type):
        key = '{service_type}_service_name'.format(service_type=service_type)
        return self.config.get(key, None)

    @property
    def prefer_ipv6(self):
        return self._prefer_ipv6