
While not strictly necessary for files containing only ASCII characters, adding a line with "coding: utf-8" can guard against future SyntaxError's in case someone inserts a Unicode literal. This commit adds such lines to all .py files. The syntax used by this commit works with Python (of course). It also works with Emacs, which will recognize the special "-*-" marker and use the "coding" variable to correctly decode the file, even in an environment where UTF-8 is not the default file encoding. Existing coding lines were normalized to match the new lines added. Partial-bug: #1325193 Change-Id: I58bf93fea711fd25890356a397e594bd820c99e3
111 lines
4.2 KiB
Python
111 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# 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.
|
|
|
|
from keystoneclient.v2_0 import client as ksclient
|
|
|
|
from ironicclient.common import utils
|
|
from ironicclient import exc
|
|
from ironicclient.openstack.common import gettextutils
|
|
|
|
gettextutils.install('ironicclient')
|
|
|
|
|
|
def _get_ksclient(**kwargs):
|
|
"""Get an endpoint and auth token from Keystone.
|
|
|
|
:param kwargs: keyword args containing credentials:
|
|
* username: name of user
|
|
* password: user's password
|
|
* auth_url: endpoint to authenticate against
|
|
* insecure: allow insecure SSL (no cert verification)
|
|
* tenant_{name|id}: name or ID of tenant
|
|
"""
|
|
return ksclient.Client(username=kwargs.get('username'),
|
|
password=kwargs.get('password'),
|
|
tenant_id=kwargs.get('tenant_id'),
|
|
tenant_name=kwargs.get('tenant_name'),
|
|
auth_url=kwargs.get('auth_url'),
|
|
insecure=kwargs.get('insecure'))
|
|
|
|
|
|
def _get_endpoint(client, **kwargs):
|
|
"""Get an endpoint using the provided keystone client."""
|
|
return client.service_catalog.url_for(
|
|
service_type=kwargs.get('service_type') or 'baremetal',
|
|
endpoint_type=kwargs.get('endpoint_type') or 'publicURL')
|
|
|
|
|
|
def get_client(api_version, **kwargs):
|
|
"""Get an authenticated client, based on the credentials
|
|
in the keyword args.
|
|
|
|
:param api_version: the API version to use. Valid value: '1'.
|
|
:param kwargs: keyword args containing credentials, either:
|
|
* os_auth_token: pre-existing token to re-use
|
|
* ironic_url: ironic API endpoint
|
|
or:
|
|
* os_username: name of user
|
|
* os_password: user's password
|
|
* os_auth_url: endpoint to authenticate against
|
|
* insecure: allow insecure SSL (no cert verification)
|
|
* os_tenant_{name|id}: name or ID of tenant
|
|
"""
|
|
|
|
if kwargs.get('os_auth_token') and kwargs.get('ironic_url'):
|
|
token = kwargs.get('os_auth_token')
|
|
endpoint = kwargs.get('ironic_url')
|
|
elif (kwargs.get('os_username') and
|
|
kwargs.get('os_password') and
|
|
kwargs.get('os_auth_url') and
|
|
(kwargs.get('os_tenant_id') or kwargs.get('os_tenant_name'))):
|
|
|
|
ks_kwargs = {
|
|
'username': kwargs.get('os_username'),
|
|
'password': kwargs.get('os_password'),
|
|
'tenant_id': kwargs.get('os_tenant_id'),
|
|
'tenant_name': kwargs.get('os_tenant_name'),
|
|
'auth_url': kwargs.get('os_auth_url'),
|
|
'service_type': kwargs.get('os_service_type'),
|
|
'endpoint_type': kwargs.get('os_endpoint_type'),
|
|
'insecure': kwargs.get('insecure'),
|
|
}
|
|
_ksclient = _get_ksclient(**ks_kwargs)
|
|
token = (kwargs.get('os_auth_token')
|
|
if kwargs.get('os_auth_token')
|
|
else _ksclient.auth_token)
|
|
|
|
endpoint = kwargs.get('ironic_url') or \
|
|
_get_endpoint(_ksclient, **ks_kwargs)
|
|
else:
|
|
e = (_('Must provide Keystone credentials or user-defined endpoint '
|
|
'and token'))
|
|
raise exc.AmbigiousAuthSystem(e)
|
|
|
|
cli_kwargs = {
|
|
'token': token,
|
|
'insecure': kwargs.get('insecure'),
|
|
'timeout': kwargs.get('timeout'),
|
|
'ca_file': kwargs.get('ca_file'),
|
|
'cert_file': kwargs.get('cert_file'),
|
|
'key_file': kwargs.get('key_file'),
|
|
}
|
|
|
|
return Client(api_version, endpoint, **cli_kwargs)
|
|
|
|
|
|
def Client(version, *args, **kwargs):
|
|
module = utils.import_versioned_module(version, 'client')
|
|
client_class = getattr(module, 'Client')
|
|
return client_class(*args, **kwargs)
|