Remove ClientManager class.

Change-Id: Iff3c4eb4010d01a9148c200837722bdc31784b49
This commit is contained in:
Federico Ressi 2019-03-21 15:18:54 +01:00
parent 6746d08357
commit 481dc75fed
10 changed files with 31 additions and 96 deletions

View File

@ -19,7 +19,7 @@ import logging
import argparse import argparse
from oslo_log import log from oslo_log import log
from tobiko.common import clients
from tobiko.common.managers import stack from tobiko.common.managers import stack
from tobiko.common.managers import ansible from tobiko.common.managers import ansible
from tobiko import config from tobiko import config
@ -37,16 +37,13 @@ class TobikoCMD(object):
self.parser = self.get_parser() self.parser = self.get_parser()
self.args = (self.parser).parse_args() self.args = (self.parser).parse_args()
self.clientManager = clients.ClientManager()
curr_dir = os.path.dirname(__file__) curr_dir = os.path.dirname(__file__)
self.templates_dir = os.path.join(curr_dir, self.templates_dir = os.path.join(curr_dir,
"../tests/scenario/templates") "../tests/scenario/templates")
self.playbooks_dir = os.path.join(curr_dir, self.playbooks_dir = os.path.join(curr_dir,
"../tests/scenario/playbooks") "../tests/scenario/playbooks")
self.stackManager = stack.StackManager(self.clientManager, self.stackManager = stack.StackManager(self.templates_dir)
self.templates_dir) self.ansibleManager = ansible.AnsibleManager(self.playbooks_dir)
self.ansibleManager = ansible.AnsibleManager(self.clientManager,
self.playbooks_dir)
def get_parser(self): def get_parser(self):
parser = argparse.ArgumentParser(add_help=True) parser = argparse.ArgumentParser(add_help=True)
@ -64,3 +61,7 @@ class TobikoCMD(object):
for handler in root_logger.handlers: for handler in root_logger.handlers:
if isinstance(handler, logging.StreamHandler): if isinstance(handler, logging.StreamHandler):
handler.setLevel(level) handler.setLevel(level)
self.stackManager = stack.StackManager(
templates_dir=self.templates_dir)
self.ansibleManager = ansible.AnsibleManager(
playbooks_dir=self.playbooks_dir)

View File

@ -17,7 +17,6 @@ import argparse
import logging import logging
import sys import sys
from tobiko.common import clients
from tobiko.fault import executor from tobiko.fault import executor
@ -29,7 +28,6 @@ class FaultCMD(object):
def __init__(self): def __init__(self):
self.parser = self.get_parser() self.parser = self.get_parser()
self.args = self.parser.parse_args() self.args = self.parser.parse_args()
self.clients = clients.ClientManager()
def get_parser(self): def get_parser(self):
parser = argparse.ArgumentParser(add_help=True) parser = argparse.ArgumentParser(add_help=True)
@ -41,7 +39,7 @@ class FaultCMD(object):
def run(self): def run(self):
"""Run faults.""" """Run faults."""
fault_exec = executor.FaultExecutor(clients=self.clients) fault_exec = executor.FaultExecutor()
fault_exec.execute(self.args.fault) fault_exec.execute(self.args.fault)

View File

@ -1,60 +0,0 @@
# Copyright 2018 Red Hat
#
# 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 __future__ import absolute_import
from tobiko.openstack import keystone
from tobiko.openstack import heat
from tobiko.openstack import neutron
from tobiko.openstack import nova
class ClientManager(object):
"""Manages OpenStack official Python clients."""
_session = None
_heat_client = None
_neutron_client = None
_nova_client = None
def __init__(self, credentials=None):
self.credentials = credentials
@property
def session(self):
"""Returns keystone session."""
if self._session is None:
self._session = keystone.get_keystone_session(
credentials=self.credentials)
return self._session
@property
def heat_client(self):
if self._heat_client is None:
self._heat_client = heat.get_heat_client(session=self.session)
return self._heat_client
@property
def neutron_client(self):
"""Returns neutron client."""
if self._neutron_client is None:
self._neutron_client = neutron.get_neutron_client(
session=self.session)
return self._neutron_client
@property
def nova_client(self):
"""Returns nova client."""
if self._nova_client is None:
self._nova_client = nova.get_nova_client(session=self.session)
return self._nova_client

View File

@ -13,9 +13,8 @@
# under the License. # under the License.
from __future__ import absolute_import from __future__ import absolute_import
import os
from collections import namedtuple from collections import namedtuple
import os
from ansible.executor import playbook_executor from ansible.executor import playbook_executor
from ansible.inventory.manager import InventoryManager from ansible.inventory.manager import InventoryManager
@ -24,6 +23,7 @@ from ansible.vars.manager import VariableManager
from oslo_log import log from oslo_log import log
from tobiko.common import constants from tobiko.common import constants
from tobiko.openstack import keystone
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -32,8 +32,7 @@ LOG = log.getLogger(__name__)
class AnsibleManager(object): class AnsibleManager(object):
"""Manages Ansible entities.""" """Manages Ansible entities."""
def __init__(self, client_manager, playbooks_dir): def __init__(self, playbooks_dir):
self.client_manager = client_manager
self.playbooks_dir = playbooks_dir self.playbooks_dir = playbooks_dir
self.loader = DataLoader() self.loader = DataLoader()
self.inventory = InventoryManager(loader=self.loader, self.inventory = InventoryManager(loader=self.loader,
@ -73,14 +72,14 @@ class AnsibleManager(object):
"""Executes given playbook.""" """Executes given playbook."""
playbook_path = self.playbooks_dir + '/' + playbook playbook_path = self.playbooks_dir + '/' + playbook
credentials = keystone.default_keystone_credentials()
extra_vars = {'mode': mode, extra_vars = {'mode': mode,
'auth_url': self.client_manager.credentials['auth_url'], 'auth_url': credentials.auth_url,
'username': self.client_manager.credentials['username'], 'username': credentials.username,
'project_name': self.client_manager.credentials[ 'project_name': credentials.project_name,
'project_name'], 'password': credentials.project_name.password,
'image': constants.DEFAULT_PARAMS['image'], 'image': constants.DEFAULT_PARAMS['image'],
'flavor': constants.DEFAULT_PARAMS['flavor'], 'flavor': constants.DEFAULT_PARAMS['flavor']}
'password': self.client_manager.credentials['password']}
self.variable_manager.extra_vars = extra_vars self.variable_manager.extra_vars = extra_vars

View File

@ -13,19 +13,18 @@
# under the License. # under the License.
from __future__ import absolute_import from __future__ import absolute_import
from tobiko.openstack import neutron
class NetworkManager(object): class NetworkManager(object):
"""Manages Neutron Resources.""" """Manages Neutron Resources."""
def __init__(self, client_manager):
self._client_manager = client_manager
_client = None _client = None
@property @property
def client(self): def client(self):
if not self._client: if not self._client:
self._client = self._client_manager.network_client self._client = neutron.get_neutron_client()
return self._client return self._client
def create_sg_rules(self, rules, sg_id): def create_sg_rules(self, rules, sg_id):

View File

@ -23,6 +23,7 @@ import yaml
from tobiko.common import constants from tobiko.common import constants
from tobiko.common import exceptions from tobiko.common import exceptions
from tobiko.openstack import heat
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -40,8 +41,7 @@ DELETE_FAILED = 'DELETE_FAILED'
class StackManager(object): class StackManager(object):
"""Manages Heat stacks.""" """Manages Heat stacks."""
def __init__(self, client_manager, templates_dir, wait_interval=5): def __init__(self, templates_dir, wait_interval=5):
self._client_manager = client_manager
self.templates_dir = templates_dir self.templates_dir = templates_dir
self.wait_interval = wait_interval self.wait_interval = wait_interval
@ -50,7 +50,7 @@ class StackManager(object):
@property @property
def client(self): def client(self):
if not self._client: if not self._client:
self._client = self._client_manager.heat_client self._client = heat.get_heat_client()
return self._client return self._client
def load_template(self, template_path): def load_template(self, template_path):

View File

@ -21,6 +21,7 @@ from oslo_log import log
from tobiko.fault import constants as fault_const from tobiko.fault import constants as fault_const
from tobiko.common.utils import file as file_utils from tobiko.common.utils import file as file_utils
from tobiko.openstack import nova
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -33,10 +34,9 @@ class FaultConfig(object):
DEFAULT_CONF_NAME = "os-faults.yml" DEFAULT_CONF_NAME = "os-faults.yml"
DEFAULT_CONF_FILE = os.path.join(DEFAULT_CONF_PATH, DEFAULT_CONF_NAME) DEFAULT_CONF_FILE = os.path.join(DEFAULT_CONF_PATH, DEFAULT_CONF_NAME)
def __init__(self, conf_file, clients): def __init__(self, conf_file):
self.templates_dir = os.path.join(os.path.dirname(__file__), self.templates_dir = os.path.join(os.path.dirname(__file__),
'templates') 'templates')
self.clients = clients
if conf_file: if conf_file:
self.conf_file = conf_file self.conf_file = conf_file
else: else:
@ -68,6 +68,7 @@ class FaultConfig(object):
def get_nodes(self): def get_nodes(self):
"""Returns a list of dictonaries with nodes name and address.""" """Returns a list of dictonaries with nodes name and address."""
client = nova.get_nova_client()
return [{'name': server.name, return [{'name': server.name,
'address': server.addresses['ctlplane'][0]['addr']} 'address': server.addresses['ctlplane'][0]['addr']}
for server in self.clients.nova_client.servers.list()] for server in client.servers.list()]

View File

@ -25,8 +25,8 @@ LOG = log.getLogger(__name__)
class FaultExecutor(object): class FaultExecutor(object):
"""Responsible for executing faults.""" """Responsible for executing faults."""
def __init__(self, conf_file=None, clients=None): def __init__(self, conf_file=None):
self.config = FaultConfig(conf_file=conf_file, clients=clients) self.config = FaultConfig(conf_file=conf_file)
try: try:
self.connect() self.connect()
LOG.info("os-faults connected.") LOG.info("os-faults connected.")

View File

@ -28,7 +28,6 @@ class TobikoCMDTest(test_base.OpenstackTest):
def test_init(self, argv=None): def test_init(self, argv=None):
self.patch_argv(argv=argv) self.patch_argv(argv=argv)
cmd = self.command_class() cmd = self.command_class()
self.assertIsNotNone(cmd.clientManager)
self.assertTrue(os.path.isdir(cmd.templates_dir)) self.assertTrue(os.path.isdir(cmd.templates_dir))
self.assertIsNotNone(cmd.stackManager) self.assertIsNotNone(cmd.stackManager)
return cmd return cmd

View File

@ -19,18 +19,16 @@ import os
from tobiko.tests import base from tobiko.tests import base
from tobiko.common.managers import stack as stack_manager from tobiko.common.managers import stack as stack_manager
from tobiko.common.managers import network from tobiko.common.managers import network
from tobiko.common import clients
from tobiko.common import constants from tobiko.common import constants
class ScenarioTestsBase(base.TobikoTest): class ScenarioTestsBase(base.TobikoTest):
"""All scenario tests inherit from this scenario base class.""" """All scenario tests inherit from this scenario base class."""
clients = clients.ClientManager()
default_params = constants.DEFAULT_PARAMS default_params = constants.DEFAULT_PARAMS
networks = network.NetworkManager(clients) networks = network.NetworkManager()
templates_dir = os.path.join(os.path.dirname(__file__), 'templates') templates_dir = os.path.join(os.path.dirname(__file__), 'templates')
stacks = stack_manager.StackManager(clients, templates_dir) stacks = stack_manager.StackManager(templates_dir=templates_dir)
stack = None stack = None
fault = None fault = None