3
# Copyright 2017 Canonical Ltd
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
9
# http://www.apache.org/licenses/LICENSE-2.0
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.
18
from charmhelpers.fetch import apt_install
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
27
return 'v2.0' if api_version == 2 else 'v3'
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
38
return '{}://{}:{}/{}/'.format(schema, addr, port,
39
get_api_suffix(api_version))
42
def get_keystone_manager(endpoint, api_version, **kwargs):
43
"""Return a keystonemanager for the correct API version
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
51
return KeystoneManager2(endpoint, **kwargs)
53
return KeystoneManager3(endpoint, **kwargs)
54
raise ValueError('No manager found for api version {}'.format(api_version))
57
class KeystoneManager(object):
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()]
65
if (name.lower() == s['name'].lower() and
66
service_type == s['type']):
69
if name.lower() == s['name'].lower():
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
78
class KeystoneManager2(KeystoneManager):
80
def __init__(self, endpoint, **kwargs):
82
from keystoneclient.v2_0 import client
83
from keystoneclient.auth.identity import v2
84
from keystoneclient import session
87
apt_install(["python-keystoneclient"], fatal=True)
89
apt_install(["python3-keystoneclient"], fatal=True)
91
from keystoneclient.v2_0 import client
92
from keystoneclient.auth.identity import v2
93
from keystoneclient import session
97
token = kwargs.get("token", None)
99
api = client.Client(endpoint=endpoint, token=token)
101
auth = v2.Password(username=kwargs.get("username"),
102
password=kwargs.get("password"),
103
tenant_name=kwargs.get("tenant_name"),
105
sess = session.Session(auth=auth)
106
api = client.Client(session=sess)
111
class KeystoneManager3(KeystoneManager):
113
def __init__(self, endpoint, **kwargs):
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
121
apt_install(["python-keystoneclient"], fatal=True)
123
apt_install(["python3-keystoneclient"], fatal=True)
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
132
token = kwargs.get("token", None)
134
auth = token_endpoint.Token(endpoint=endpoint,
136
sess = session.Session(auth=auth)
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)
144
self.api = client.Client(session=sess)