~chad.smith/charm-helpers/retry-add-apt-repository

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/openstack/keystone.py

  • Committer: Jorge Niedbalski
  • Date: 2017-02-28 13:02:05 UTC
  • mfrom: (695.1.9 devel)
  • Revision ID: jorge.niedbalski@canonical.com-20170228130205-vb974hnz1tv5g9vl
[niedbalski,r=billy-olsen,shaner] Partial fix for LP: #1667478

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Copyright 2017 Canonical Ltd
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License");
 
6
# you may not use this file except in compliance with the License.
 
7
# You may obtain a copy of the License at
 
8
#
 
9
#  http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS,
 
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
# See the License for the specific language governing permissions and
 
15
# limitations under the License.
 
16
 
 
17
import six
 
18
from charmhelpers.fetch import apt_install
 
19
 
 
20
 
 
21
def get_api_suffix(api_version):
 
22
    """Return the formatted api suffix for the given version
 
23
    @param api_version: version of the keystone endpoint
 
24
    @returns the api suffix formatted according to the given api
 
25
    version
 
26
    """
 
27
    return 'v2.0' if api_version == 2 else 'v3'
 
28
 
 
29
 
 
30
def format_endpoint(schema, addr, port, api_version):
 
31
    """Return a formatted keystone endpoint
 
32
    @param schema: http or https
 
33
    @param addr: ipv4/ipv6 host of the keystone service
 
34
    @param port: port of the keystone service
 
35
    @param api_version: 2 or 3
 
36
    @returns a fully formatted keystone endpoint
 
37
    """
 
38
    return '{}://{}:{}/{}/'.format(schema, addr, port,
 
39
                                   get_api_suffix(api_version))
 
40
 
 
41
 
 
42
def get_keystone_manager(endpoint, api_version, **kwargs):
 
43
    """Return a keystonemanager for the correct API version
 
44
 
 
45
    @param endpoint: the keystone endpoint to point client at
 
46
    @param api_version: version of the keystone api the client should use
 
47
    @param kwargs: token or username/tenant/password information
 
48
    @returns keystonemanager class used for interrogating keystone
 
49
    """
 
50
    if api_version == 2:
 
51
        return KeystoneManager2(endpoint, **kwargs)
 
52
    if api_version == 3:
 
53
        return KeystoneManager3(endpoint, **kwargs)
 
54
    raise ValueError('No manager found for api version {}'.format(api_version))
 
55
 
 
56
 
 
57
class KeystoneManager(object):
 
58
 
 
59
    def resolve_service_id(self, name, service_type=None):
 
60
        """Find the service_id of a given service"""
 
61
        services = [s._info for s in self.api.services.list()]
 
62
 
 
63
        for s in services:
 
64
            if service_type:
 
65
                if (name.lower() == s['name'].lower() and
 
66
                        service_type == s['type']):
 
67
                    return s['id']
 
68
            else:
 
69
                if name.lower() == s['name'].lower():
 
70
                    return s['id']
 
71
        return None
 
72
 
 
73
    def service_exists(self, service_name, service_type=None):
 
74
        """Determine if the given service exists on the service list"""
 
75
        return self.resolve_service_id(service_name, service_type) is not None
 
76
 
 
77
 
 
78
class KeystoneManager2(KeystoneManager):
 
79
 
 
80
    def __init__(self, endpoint, **kwargs):
 
81
        try:
 
82
            from keystoneclient.v2_0 import client
 
83
            from keystoneclient.auth.identity import v2
 
84
            from keystoneclient import session
 
85
        except ImportError:
 
86
            if six.PY2:
 
87
                apt_install(["python-keystoneclient"], fatal=True)
 
88
            else:
 
89
                apt_install(["python3-keystoneclient"], fatal=True)
 
90
 
 
91
            from keystoneclient.v2_0 import client
 
92
            from keystoneclient.auth.identity import v2
 
93
            from keystoneclient import session
 
94
 
 
95
        self.api_version = 2
 
96
 
 
97
        token = kwargs.get("token", None)
 
98
        if token:
 
99
            api = client.Client(endpoint=endpoint, token=token)
 
100
        else:
 
101
            auth = v2.Password(username=kwargs.get("username"),
 
102
                               password=kwargs.get("password"),
 
103
                               tenant_name=kwargs.get("tenant_name"),
 
104
                               auth_url=endpoint)
 
105
            sess = session.Session(auth=auth)
 
106
            api = client.Client(session=sess)
 
107
 
 
108
        self.api = api
 
109
 
 
110
 
 
111
class KeystoneManager3(KeystoneManager):
 
112
 
 
113
    def __init__(self, endpoint, **kwargs):
 
114
        try:
 
115
            from keystoneclient.v3 import client
 
116
            from keystoneclient.auth import token_endpoint
 
117
            from keystoneclient import session
 
118
            from keystoneclient.auth.identity import v3
 
119
        except ImportError:
 
120
            if six.PY2:
 
121
                apt_install(["python-keystoneclient"], fatal=True)
 
122
            else:
 
123
                apt_install(["python3-keystoneclient"], fatal=True)
 
124
 
 
125
            from keystoneclient.v3 import client
 
126
            from keystoneclient.auth import token_endpoint
 
127
            from keystoneclient import session
 
128
            from keystoneclient.auth.identity import v3
 
129
 
 
130
        self.api_version = 3
 
131
 
 
132
        token = kwargs.get("token", None)
 
133
        if token:
 
134
            auth = token_endpoint.Token(endpoint=endpoint,
 
135
                                        token=token)
 
136
            sess = session.Session(auth=auth)
 
137
        else:
 
138
            auth = v3.Password(auth_url=endpoint,
 
139
                               user_id=kwargs.get("username"),
 
140
                               password=kwargs.get("password"),
 
141
                               project_id=kwargs.get("tenant_id"))
 
142
            sess = session.Session(auth=auth)
 
143
 
 
144
        self.api = client.Client(session=sess)