~ubuntu-branches/ubuntu/wily/aodh/wily

« back to all changes in this revision

Viewing changes to aodh/keystone_client.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2015-09-10 17:50:46 UTC
  • Revision ID: package-import@ubuntu.com-20150910175046-jb6cn5eo3s27um2p
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2015 eNovance <licensing@enovance.com>
 
3
#
 
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
# not use this file except in compliance with the License. You may obtain
 
6
# a copy of the License at
 
7
#
 
8
#      http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
# Unless required by applicable law or agreed to in writing, software
 
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
# License for the specific language governing permissions and limitations
 
14
# under the License.
 
15
 
 
16
 
 
17
from keystoneclient import discover as ks_discover
 
18
from keystoneclient import exceptions as ks_exception
 
19
from keystoneclient import session as ks_session
 
20
from keystoneclient.v2_0 import client as ks_client
 
21
from keystoneclient.v3 import client as ks_client_v3
 
22
 
 
23
 
 
24
def get_client(conf):
 
25
    return ks_client.Client(
 
26
        username=conf.service_credentials.os_username,
 
27
        password=conf.service_credentials.os_password,
 
28
        tenant_id=conf.service_credentials.os_tenant_id,
 
29
        tenant_name=conf.service_credentials.os_tenant_name,
 
30
        cacert=conf.service_credentials.os_cacert,
 
31
        auth_url=conf.service_credentials.os_auth_url,
 
32
        region_name=conf.service_credentials.os_region_name,
 
33
        insecure=conf.service_credentials.insecure,
 
34
        timeout=conf.http_timeout,)
 
35
 
 
36
 
 
37
def get_v3_client(conf, trust_id=None):
 
38
    """Return a client for keystone v3 endpoint, optionally using a trust."""
 
39
    auth_url = conf.service_credentials.os_auth_url
 
40
    try:
 
41
        auth_url_noneversion = auth_url.replace('/v2.0', '/')
 
42
        discover = ks_discover.Discover(auth_url=auth_url_noneversion)
 
43
        v3_auth_url = discover.url_for('3.0')
 
44
        if v3_auth_url:
 
45
            auth_url = v3_auth_url
 
46
        else:
 
47
            auth_url = auth_url
 
48
    except Exception:
 
49
        auth_url = auth_url.replace('/v2.0', '/v3')
 
50
    return ks_client_v3.Client(
 
51
        username=conf.service_credentials.os_username,
 
52
        password=conf.service_credentials.os_password,
 
53
        cacert=conf.service_credentials.os_cacert,
 
54
        auth_url=auth_url,
 
55
        region_name=conf.service_credentials.os_region_name,
 
56
        insecure=conf.service_credentials.insecure,
 
57
        timeout=conf.http_timeout,
 
58
        trust_id=trust_id)
 
59
 
 
60
 
 
61
def create_trust_id(conf, trustor_user_id, trustor_project_id,
 
62
                    roles, auth_plugin):
 
63
    """Create a new trust using the aodh service user."""
 
64
    admin_client = get_v3_client(conf)
 
65
 
 
66
    trustee_user_id = admin_client.auth_ref.user_id
 
67
 
 
68
    session = ks_session.Session.construct({
 
69
        'cacert': conf.service_credentials.os_cacert,
 
70
        'insecure': conf.service_credentials.insecure})
 
71
 
 
72
    client = ks_client_v3.Client(session=session, auth=auth_plugin)
 
73
 
 
74
    trust = client.trusts.create(trustor_user=trustor_user_id,
 
75
                                 trustee_user=trustee_user_id,
 
76
                                 project=trustor_project_id,
 
77
                                 impersonation=True,
 
78
                                 role_names=roles)
 
79
    return trust.id
 
80
 
 
81
 
 
82
def delete_trust_id(conf, trust_id, auth_plugin):
 
83
    """Delete a trust previously setup for the aodh user."""
 
84
    session = ks_session.Session.construct({
 
85
        'cacert': conf.service_credentials.os_cacert,
 
86
        'insecure': conf.service_credentials.insecure})
 
87
 
 
88
    client = ks_client_v3.Client(session=session, auth=auth_plugin)
 
89
    try:
 
90
        client.trusts.delete(trust_id)
 
91
    except ks_exception.NotFound:
 
92
        pass