Put the keystone auth in its own class

I did this so there is no need to reimplment this logen when using as
this as a python library.

Change-Id: I368312433a84be0d5f14089e06701a04b15e2300
This commit is contained in:
Tim Kuhlman 2014-11-26 14:42:57 -07:00
parent 56e55f7f3a
commit 1956915020
3 changed files with 108 additions and 74 deletions

View File

@ -333,17 +333,18 @@ to see the required and optional fields for each command.
Refer to this example in python-monascaclient/client_api_example.py::
from monascaclient import client
from monascaclient import ksclient
import monascaclient.exc as exc
import time
api_version = '2_0'
endpoint = 'http://192.168.10.4:8080/v2.0'
kwargs = {
'token': '12345678'
}
# Authenticate to Keystone
keystone_url = 'http://keystone:5000/v3'
ks = ksclient.KSClient(auth_url=keystone_url, username='user', password='password')
# construct the mon client
monasca_client = client.Client(api_version, endpoint, **kwargs)
monasca_client = client.Client(api_version, ks.monasca_url, token=ks.token)
# call the metric-create command
dimensions = {'instance_id': '12345', 'service': 'hello'}

98
monascaclient/ksclient.py Normal file
View File

@ -0,0 +1,98 @@
# 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.
"""
Wrapper around python keystone client to assist in getting a properly scoped token and the registered service
endpoint for Monasca.
"""
from keystoneclient.v3 import client
from monascaclient import exc
class KSClient(object):
def __init__(self, **kwargs):
"""Get an endpoint and auth token from Keystone.
:param username: name of user
:param password: user's password
:param project_id: unique identifier of project
:param project_name: name of project
:param domain_name: name of domain project is in
:param domain_id: id of domain project is in
:param auth_url: endpoint to authenticate against
:param token: token to use instead of username/password
"""
kc_args = {'auth_url': kwargs.get('auth_url'),
'insecure': kwargs.get('insecure')}
if kwargs.get('os_cacert'):
kc_args['cacert'] = kwargs.get('os_cacert')
if kwargs.get('project_id'):
kc_args['project_id'] = kwargs.get('project_id')
elif kwargs.get('project_name'):
kc_args['project_name'] = kwargs.get('project_name')
if kwargs.get('domain_name'):
kc_args['project_domain_name'] = kwargs.get('domain_name')
if kwargs.get('domain_id'):
kc_args['project_domain_id'] = kwargs.get('domain_id')
if kwargs.get('token'):
kc_args['token'] = kwargs.get('token')
else:
kc_args['username'] = kwargs.get('username')
kc_args['password'] = kwargs.get('password')
self._kwargs = kwargs
self._keystone = client.Client(**kc_args)
self._token = None
self._monasca_url = None
@property
def token(self):
"""Token property
Validate token is project scoped and return it if it is
project_id and auth_token were fetched when keystone client was created
"""
if self._token is None:
if self._keystone.project_id:
self._token = self._keystone.auth_token
else:
raise exc.CommandError("User does not have a default project. "
"You must provide a project id using "
"--os-project-id or via env[OS_PROJECT_ID], "
"or you must provide a project name using "
"--os-project-name or via env[OS_PROJECT_NAME] "
"and a domain using --os-domain-name, via "
"env[OS_DOMAIN_NAME], using --os-domain-id or "
"via env[OS_DOMAIN_ID]")
return self._token
@property
def monasca_url(self):
"""Return the monasca publicURL registered in keystone."""
if self._monasca_url is None:
if self._kwargs.get('region_name'):
self._monasca_url = self._keystone.service_catalog.url_for(
service_type=self._kwargs.get('service_type') or 'monitoring',
attr='region',
filter_value=self._kwargs.get('region_name'),
endpoint_type=self._kwargs.get('endpoint_type') or 'publicURL')
self._monasca_url = self._keystone.service_catalog.url_for(
service_type=self._kwargs.get('service_type') or 'monitoring',
endpoint_type=self._kwargs.get('endpoint_type') or 'publicURL')
return self._monasca_url

View File

@ -23,12 +23,11 @@ import argparse
import logging
import sys
from keystoneclient.v3 import client as ksclient
import monascaclient
from monascaclient import client as monasca_client
from monascaclient.common import utils
from monascaclient import exc
from monascaclient import ksclient
logger = logging.getLogger(__name__)
@ -247,70 +246,6 @@ class MonascaShell(object):
subparser.add_argument(*args, **kwargs)
subparser.set_defaults(func=callback)
def _get_ksclient(self, **kwargs):
"""Get an endpoint and auth token from Keystone.
:param username: name of user
:param password: user's password
:param project_id: unique identifier of project
:param project_name: name of project
:param domain_name: name of domain project is in
:param domain_id: id of domain project is in
:param auth_url: endpoint to authenticate against
:param token: token to use instead of username/password
"""
kc_args = {'auth_url': kwargs.get('auth_url'),
'insecure': kwargs.get('insecure')}
if kwargs.get('os_cacert'):
kc_args['cacert'] = kwargs.get('os_cacert')
if kwargs.get('project_id'):
kc_args['project_id'] = kwargs.get('project_id')
elif kwargs.get('project_name'):
kc_args['project_name'] = kwargs.get('project_name')
if kwargs.get('domain_name'):
kc_args['project_domain_name'] = kwargs.get('domain_name')
if kwargs.get('domain_id'):
kc_args['project_domain_id'] = kwargs.get('domain_id')
if kwargs.get('token'):
kc_args['token'] = kwargs.get('token')
else:
kc_args['username'] = kwargs.get('username')
kc_args['password'] = kwargs.get('password')
return ksclient.Client(**kc_args)
def _get_token(self, _ksclient):
"""Validate token is project scoped and return it if it is
project_id and auth_token were fetched when keystone client was created
:param _ksclient: keystone client
"""
if _ksclient.project_id:
return _ksclient.auth_token
raise exc.CommandError("User does not have a default project. "
"You must provide a project id using "
"--os-project-id or via env[OS_PROJECT_ID], "
"or you must provide a project name using "
"--os-project-name or via env[OS_PROJECT_NAME] "
"and a domain using --os-domain-name, via "
"env[OS_DOMAIN_NAME], using --os-domain-id or "
"via env[OS_DOMAIN_ID]")
def _get_endpoint(self, client, **kwargs):
"""Get an endpoint using the provided keystone client."""
if kwargs.get('region_name'):
return client.service_catalog.url_for(
service_type=kwargs.get('service_type') or 'monitoring',
attr='region',
filter_value=kwargs.get('region_name'),
endpoint_type=kwargs.get('endpoint_type') or 'publicURL')
return client.service_catalog.url_for(
service_type=kwargs.get('service_type') or 'monitoring',
endpoint_type=kwargs.get('endpoint_type') or 'publicURL')
def _setup_logging(self, debug):
log_lvl = logging.DEBUG if debug else logging.ERROR
logging.basicConfig(
@ -392,11 +327,11 @@ class MonascaShell(object):
endpoint = args.monasca_api_url
if not args.os_no_client_auth:
_ksclient = self._get_ksclient(**kwargs)
_ksclient = ksclient.KSClient(**kwargs)
if args.os_auth_token:
token = args.os_auth_token
else:
token = self._get_token(_ksclient)
token = _ksclient.token
kwargs = {
'token': token,
@ -415,7 +350,7 @@ class MonascaShell(object):
kwargs['region_name'] = args.os_region_name
if not endpoint:
endpoint = self._get_endpoint(_ksclient, **kwargs)
endpoint = _ksclient.monasca_url
client = monasca_client.Client(api_version, endpoint, **kwargs)