From 0bfeefc25b31e6d02d130392b3bb63dd7d714702 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 20 Mar 2017 06:51:01 -0500 Subject: [PATCH] Start to consume os-client-config There are two libraries - os-client-config and shade - that already exist for the purposes of what these utility libs are doing. os-client-config's job is to collect client config information, whether via environment variables, command line arguments or clouds.yaml files and construct relevant objects from that. For instance, it will very happily make novaclient.client.Client objects for user - and it'll handle all of the arguments possible. shade's job is to contain business logic. So a good amount of the API calls in NovaClient are probably just things that need to live in shade. That's a few more steps than this is, but since shade is also driven by os-client-config, moving to os-client-config as a first step seems fairly sane. --- common/lib/authentication.py | 50 ------------------- common/lib/keystone_client.py | 10 ++-- common/lib/nova_client.py | 9 ++-- .../antiaffinity_check/antiaffinity_check.py | 14 ++++-- 4 files changed, 19 insertions(+), 64 deletions(-) delete mode 100644 common/lib/authentication.py diff --git a/common/lib/authentication.py b/common/lib/authentication.py deleted file mode 100644 index 284ea9b..0000000 --- a/common/lib/authentication.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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. -# -# This tool is used to check if instances within the same anti-affinity -# group are violating the group's policy or not. It can be also used to -# list the instances in the group id -# specified in the argument. -# -# - -from keystoneclient.auth import identity -from keystoneclient import session -import os -import sys - - -class Authentication(object): - """ - Authentication base class - """ - - def __init__(self): - """ - Constructor for authenticating openrc credentials - """ - try: - credentials = {} - credentials['project_domain_name'] =\ - os.environ['OS_PROJECT_DOMAIN_NAME'] - credentials['user_domain_name'] = os.environ['OS_USER_DOMAIN_NAME'] - credentials['project_name'] = os.environ['OS_PROJECT_NAME'] - credentials['username'] = os.environ['OS_USERNAME'] - credentials['password'] = os.environ['OS_PASSWORD'] - credentials['auth_url'] = os.environ['OS_AUTH_URL'] - auth = identity.v3.Password(**credentials) - self.sess = session.Session(auth=auth) - self.authenticated = True - except KeyError as k: - self.authenticated = False - print "Please source the openrc file" - sys.exit(1) diff --git a/common/lib/keystone_client.py b/common/lib/keystone_client.py index 9b571f6..c9ea2e5 100644 --- a/common/lib/keystone_client.py +++ b/common/lib/keystone_client.py @@ -15,21 +15,19 @@ # list the instances in the group id specified in the argument. # -from keystoneauth1.identity import v3 -from keystoneauth1 import session -from keystoneclient.v3 import client - class KeystoneClient(object): """ Keystone connection base class """ - def __init__(self, auth): + def __init__(self, config): """ Constructor for creating a keystone object + + :param config: `os_client_config.cloud_config.CloudConfig` object """ - self.keystone = client.Client(session=auth.sess) + self.keystone = config.get_legacy_client('identity') def project_list(self): """ diff --git a/common/lib/nova_client.py b/common/lib/nova_client.py index 8349b7a..5a3697e 100644 --- a/common/lib/nova_client.py +++ b/common/lib/nova_client.py @@ -14,9 +14,6 @@ # group are violating the group's policy or not. It can be also used to # list the instances in the group id specified in the argument. # -from novaclient import client as nova_client -from keystoneclient.auth import identity -from keystoneclient import session import os from collections import Counter @@ -26,11 +23,13 @@ class NovaClient(object): Nova connection base class """ - def __init__(self, auth): + def __init__(self, config): """ Constructor for creating a nova object + + :param config: `os_client_config.cloud_config.CloudConfig` object """ - self.nova = nova_client.Client('2', session=auth.sess) + self.nova = config.get_legacy_client('compute') def nova_vm_list(self): """ diff --git a/tools/antiaffinity_check/antiaffinity_check.py b/tools/antiaffinity_check/antiaffinity_check.py index dd84e47..c113762 100644 --- a/tools/antiaffinity_check/antiaffinity_check.py +++ b/tools/antiaffinity_check/antiaffinity_check.py @@ -109,9 +109,17 @@ def setup_clients(self): A method to seting up authentication and necessary clients for the tool - Called by Constructor """ - auth = Authentication() - self.novaclient = NovaClient(auth) - self.keystoneclient = KeystoneClient(auth) + # This dance can be slightly a bit better, but for now, let's just + # keep it operating like it was before. This will get a configuration + # based on envvars. If the user has a clouds.yaml file with more than + # one cloud defined, the user will need to define OS_CLOUD and maybe + # OS_REGION_NAME to indicate which one + config = os_client_config.OpenStackConfig() + cloud_config = config.get_one_cloud() + # As a further TODO - we should get rid of NovaClient and + # KeystoneClient altogether and add whatever they don't have to shade. + self.novaclient = NovaClient(cloud_config) + self.keystoneclient = KeystoneClient(cloud_config) def create_table(self, fields): """