Refactor service client getters

This change removes duplication of service clients instantiations
throughout the code (especially the lookup plugins). It also
consolidates service client getter functions by placing them all
in the utils module.

Change-Id: Ia8ba252de3cebca9e75266f7bcbba373cc0f5008
This commit is contained in:
Ana Krivokapic 2018-06-26 17:02:06 +02:00
parent d96473bb3b
commit d860d9e335
12 changed files with 71 additions and 101 deletions

View File

@ -7,6 +7,7 @@ oslo.config>=5.2.0 # Apache-2.0
keystoneauth1>=3.4.0 # Apache-2.0
python-novaclient>=9.1.0 # Apache-2.0
python-heatclient>=1.10.0 # Apache-2.0
python-glanceclient>=2.9.1 # Apache-2.0
python-ironicclient>=2.3.0 # Apache-2.0
os-net-config>=7.1.0 # Apache-2.0
six>=1.10.0 # MIT

View File

@ -26,12 +26,11 @@ import sys
import json
import traceback
from heatclient import client as heat_client
from oslo_config import cfg
from six.moves import configparser
from tripleo_common.inventory import TripleoInventory
from tripleo_validations.utils import get_auth_session
from tripleo_validations import utils
opts = [
@ -121,16 +120,18 @@ def write_static_inventory(inventory_file_path, inventory):
def main():
configs = _parse_config()
session = get_auth_session(configs.auth_url,
configs.username,
configs.project_name,
configs.password,
configs.auth_token,
configs.cacert)
hclient = heat_client.Client('1', session=session)
auth_variables = {
'auth_url': configs.auth_url,
'username': configs.username,
'project_name': configs.project_name,
'os_auth_token': configs.auth_token,
'password': configs.password,
'cacert': configs.cacert
}
inventory = TripleoInventory(
session=session,
hclient=hclient,
session=utils.get_auth_session(auth_variables),
hclient=utils.get_heat_client(auth_variables),
auth_url=configs.auth_url,
cacert=configs.cacert,
project_name=configs.project_name,

View File

@ -20,14 +20,23 @@ from six import string_types
import collections
from glanceclient import client as glance_client
from heatclient import client as heat_client
from ironicclient import client as ironic_client
from keystoneauth1.identity import generic as ks_id
from keystoneauth1 import session
from keystoneauth1 import session as ks_session
from novaclient import client as nova_client
from swiftclient.client import Connection
def get_auth_session(auth_url, username, project_name, password=None,
auth_token=None, cacert=None):
def get_auth_session(auth_variables):
auth_url = auth_variables.get('auth_url')
username = auth_variables.get('username')
project_name = auth_variables.get('project_name')
auth_token = auth_variables.get('os_auth_token')
password = auth_variables.get('password')
cacert = auth_variables.get('cacert')
if auth_token:
auth = ks_id.Token(auth_url=auth_url,
token=auth_token,
@ -40,26 +49,38 @@ def get_auth_session(auth_url, username, project_name, password=None,
project_name=project_name,
user_domain_id='default',
project_domain_id='default')
return session.Session(auth=auth, verify=cacert)
return ks_session.Session(auth=auth, verify=cacert)
def get_swift_client(preauthurl, preauthtoken):
return Connection(preauthurl=preauthurl,
preauthtoken=preauthtoken,
def get_swift_client(auth_variables):
return Connection(preauthurl=auth_variables.get('undercloud_swift_url'),
preauthtoken=auth_variables.get('os_auth_token'),
retries=10,
starting_backoff=3,
max_backoff=120)
def get_nova_client(auth_variables):
auth_url = auth_variables.get('auth_url')
username = auth_variables.get('username')
project_name = auth_variables.get('project_name')
token = auth_variables.get('os_auth_token')
session = get_auth_session(auth_url, username, project_name,
auth_token=token)
return nova_client.Client(2, session=get_auth_session(auth_variables))
return nova_client.Client(2, session=session)
def get_glance_client(auth_variables):
return glance_client.Client(2, session=get_auth_session(auth_variables))
def get_heat_client(auth_variables):
return heat_client.Client('1', session=get_auth_session(auth_variables))
def get_ironic_client(auth_variables):
session = get_auth_session(auth_variables)
ironic_url = session.get_endpoint(service_type='baremetal',
interface='public')
return ironic_client.get_client(
1,
ironic_url=ironic_url,
os_auth_token=auth_variables.get('os_auth_token')
)
def filtered(obj):

View File

@ -16,10 +16,10 @@
# under the License.
from ansible.plugins.lookup import LookupBase
from glanceclient import client as glance_client
from glanceclient.exc import HTTPNotFound
from tripleo_validations.utils import get_auth_session
from tripleo_validations import utils
DOCUMENTATION = """
@ -57,13 +57,7 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns server information from nova."""
auth_url = variables.get('auth_url')
username = variables.get('username')
project_name = variables.get('project_name')
token = variables.get('os_auth_token')
session = get_auth_session(auth_url, username, project_name,
auth_token=token)
glance = glance_client.Client(2, session=session)
glance = utils.get_glance_client(variables)
images = []
if len(terms) > 0:

View File

@ -17,8 +17,7 @@
from ansible.plugins.lookup import LookupBase
from swiftclient.client import Connection
from tripleo_validations.utils import get_auth_session
from tripleo_validations import utils
class LookupModule(LookupBase):
@ -32,18 +31,13 @@ class LookupModule(LookupBase):
"""
ret = []
session = get_auth_session(kwargs.get('auth_url'),
"ironic",
"service",
kwargs.get('password'))
swift_client = Connection(session=session)
container = swift_client.get_container("ironic-inspector")
swift = utils.get_swift_client(variables)
container = swift.get_container("ironic-inspector")
for item in container[1]:
if item['name'].startswith('inspector_data') and \
not item['name'].endswith("UNPROCESSED"):
obj = swift_client.get_object("ironic-inspector", item['name'])
obj = swift.get_object("ironic-inspector", item['name'])
ret.append((item['name'], obj))
return ret

View File

@ -66,7 +66,6 @@ _raw:
"""
from ansible.plugins.lookup import LookupBase
from ironicclient import client as ironic_client
from tripleo_validations import utils
@ -75,16 +74,7 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns node information from ironic."""
auth_url = variables.get('auth_url')
username = variables.get('username')
project_name = variables.get('project_name')
token = variables.get('os_auth_token')
session = utils.get_auth_session(auth_url, username, project_name,
auth_token=token)
ironic_url = session.get_endpoint(service_type='baremetal',
interface='public')
ironic = ironic_client.get_client(1, ironic_url=ironic_url,
os_auth_token=token)
ironic = utils.get_ironic_client(variables)
if len(terms) > 0:
if terms[0] == 'id':

View File

@ -16,9 +16,8 @@
# under the License.
from ansible.plugins.lookup import LookupBase
from novaclient import client as nova_client
from tripleo_validations.utils import get_auth_session
from tripleo_validations import utils
DOCUMENTATION = """
@ -46,15 +45,7 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns server information from nova."""
auth_url = variables.get('auth_url')
username = variables.get('username')
project_name = variables.get('project_name')
token = variables.get('os_auth_token')
session = get_auth_session(auth_url, username, project_name,
auth_token=token)
nova = nova_client.Client(2, session=session)
nova = utils.get_nova_client(variables)
flavors = nova.flavors.list()
return {f.name: {'name': f.name, 'keys': f.get_keys()}
for f in flavors}

View File

@ -46,7 +46,5 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns server information from nova."""
nova = utils.get_nova_client(variables)
statistics = nova.hypervisor_stats.statistics()
return utils.filtered(statistics)

View File

@ -52,7 +52,6 @@ _raw:
"""
from ansible.plugins.lookup import LookupBase
from novaclient import client as nova_client
from novaclient.exceptions import NotFound
from tripleo_validations import utils
@ -62,13 +61,7 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns server information from nova."""
auth_url = variables.get('auth_url')
username = variables.get('username')
project_name = variables.get('project_name')
token = variables.get('os_auth_token')
session = utils.get_auth_session(auth_url, username, project_name,
auth_token=token)
nova = nova_client.Client(2, session=session)
nova = utils.get_nova_client(variables)
servers = []
if len(terms) > 0:

View File

@ -19,7 +19,7 @@ import yaml
from ansible.plugins.lookup import LookupBase
from tripleo_validations.utils import get_swift_client
from tripleo_validations import utils
DOCUMENTATION = """
@ -54,16 +54,10 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns server information from nova."""
swiftclient = get_swift_client(variables['undercloud_swift_url'],
variables['os_auth_token'])
swift = utils.get_swift_client(variables)
plan = variables.get('plan')
plan_env = self._get_object_yaml(
swiftclient, plan, 'plan-environment.yaml')
roles_data = self._get_object_yaml(
swiftclient, plan, 'roles_data.yaml')
plan_env = self._get_object_yaml(swift, plan, 'plan-environment.yaml')
roles_data = self._get_object_yaml(swift, plan, 'roles_data.yaml')
def default_role_data(role):
return {

View File

@ -17,9 +17,9 @@
from ansible.plugins.lookup import LookupBase
from heatclient import client as heat_client
from tripleo_validations.utils import get_auth_session
from tripleo_validations import utils
class LookupModule(LookupBase):
@ -30,14 +30,8 @@ class LookupModule(LookupBase):
:return: A list of dicts
"""
ret = []
session = get_auth_session(variables['auth_url'],
variables['username'],
variables['project_name'],
auth_token=variables['os_auth_token'],
cacert=variables['cacert'])
hclient = heat_client.Client('1', session=session)
resource_list = hclient.resources.list(variables['plan'])
heat = utils.get_heat_client(variables)
resource_list = heat.resources.list(variables['plan'])
for resource in resource_list:
ret.append(dict(
resource_name=resource.resource_name,

View File

@ -19,7 +19,7 @@ import os
from ansible.plugins.lookup import LookupBase
from tripleo_validations.utils import get_swift_client
from tripleo_validations import utils
EXCLUDED_EXT = (
@ -37,11 +37,10 @@ class LookupModule(LookupBase):
containing the template path and the template content.
"""
ret = []
swift_client = get_swift_client(variables['undercloud_swift_url'],
variables['os_auth_token'])
container = swift_client.get_container(variables['plan'])
swift = utils.get_swift_client(variables)
container = swift.get_container(variables['plan'])
for item in container[1]:
obj = swift_client.get_object(variables['plan'], item['name'])
obj = swift.get_object(variables['plan'], item['name'])
if os.path.splitext(item['name'])[-1] not in EXCLUDED_EXT:
ret.append((item['name'], obj))