Allow separate parameters to TripleoInventory class

In the case of tripleoclient using this class there's no compatible
config object to pass, so it's easier and clearer if we break out the
individual parameters into the class constructor arguments.

The tripleo-validations code will be adjusted to suit then we can remove
the backwards compatibility code, but for now both old/new interfaces
are maintained.

Change-Id: I138ed6d07bd7ebb161b0a1dec3480de811e18e49
This commit is contained in:
Steven Hardy 2017-12-19 20:43:26 +00:00
parent 466386f513
commit c5358e58ad
2 changed files with 64 additions and 23 deletions

View File

@ -72,11 +72,28 @@ class StackOutputs(object):
class TripleoInventory(object):
def __init__(self, configs, session, hclient):
self.configs = configs
def __init__(self, configs=None, session=None, hclient=None,
plan_name=None, auth_url=None, project_name=None,
cacert=None, username=None, ansible_ssh_user=None):
self.session = session
self.hclient = hclient
self.stack_outputs = StackOutputs(self.configs.plan, self.hclient)
if configs is not None:
# FIXME(shardy) backwards compatibility until we switch
# tripleo-validations to pass the individual values
self.auth_url = configs.auth_url
self.cacert = configs.cacert
self.project_name = configs.project_name
self.username = configs.username
self.ansible_ssh_user = configs.ansible_ssh_user
self.plan_name = configs.plan
else:
self.auth_url = auth_url
self.cacert = cacert
self.project_name = project_name
self.username = username
self.ansible_ssh_user = ansible_ssh_user
self.plan_name = plan_name
self.stack_outputs = StackOutputs(self.plan_name, self.hclient)
@staticmethod
def get_roles_by_service(enabled_services):
@ -96,7 +113,7 @@ class TripleoInventory(object):
def get_overcloud_environment(self):
try:
environment = self.hclient.stacks.environment(self.configs.plan)
environment = self.hclient.stacks.environment(self.plan_name)
return environment
except HTTPNotFound:
return {}
@ -119,12 +136,12 @@ class TripleoInventory(object):
'hosts': ['localhost'],
'vars': {
'ansible_connection': 'local',
'auth_url': self.configs.auth_url,
'cacert': self.configs.cacert,
'auth_url': self.auth_url,
'cacert': self.cacert,
'os_auth_token': self.session.get_token(),
'plan': self.configs.plan,
'project_name': self.configs.project_name,
'username': self.configs.username,
'plan': self.plan_name,
'project_name': self.project_name,
'username': self.username,
},
}
})
@ -184,7 +201,7 @@ class TripleoInventory(object):
ret[role] = {
'children': sorted(shortnames),
'vars': {
'ansible_ssh_user': self.configs.ansible_ssh_user,
'ansible_ssh_user': self.ansible_ssh_user,
'bootstrap_server_id': role_node_id_map.get(
'bootstrap_server_id'),
'role_name': role,
@ -211,7 +228,7 @@ class TripleoInventory(object):
ret[service.lower()] = {
'children': service_children,
'vars': {
'ansible_ssh_user': self.configs.ansible_ssh_user
'ansible_ssh_user': self.ansible_ssh_user
}
}

View File

@ -99,21 +99,20 @@ class TestInventory(base.TestCase):
self.mock_stack.outputs = self.outputs_data['outputs']
self.hclient.stacks.get.return_value = self.mock_stack
self.configs = MagicMock()
self.configs.plan = self.plan_name
self.configs.auth_url = 'xyz://keystone.local'
self.configs.cacert = 'acacert'
self.configs.project_name = 'admin'
self.configs.username = 'admin'
self.configs.ansible_ssh_user = 'heat-admin'
self.session = MagicMock()
self.session.get_token.return_value = 'atoken'
self.session.get_endpoint.return_value = 'anendpoint'
self.outputs = StackOutputs('overcloud', self.hclient)
self.inventory = TripleoInventory(
self.configs, self.session, self.hclient)
session=self.session,
hclient=self.hclient,
plan_name=self.plan_name,
auth_url='xyz://keystone.local',
cacert='acacert',
project_name='admin',
username='admin',
ansible_ssh_user='heat-admin')
self.inventory.stack_outputs = self.outputs
def test_get_roles_by_service(self):
@ -160,6 +159,23 @@ class TestInventory(base.TestCase):
set([o for o in self.outputs]))
def test_inventory_list(self):
self._inventory_list(self.inventory)
def test_inventory_list_backwards_compat_configs(self):
# FIXME(shardy) backwards compatibility until we switch
# tripleo-validations to pass the individual values
configs = MagicMock()
configs.plan = self.plan_name
configs.auth_url = 'xyz://keystone.local'
configs.cacert = 'acacert'
configs.project_name = 'admin'
configs.username = 'admin'
configs.ansible_ssh_user = 'heat-admin'
inventory = TripleoInventory(
configs, self.session, self.hclient)
self._inventory_list(inventory)
def _inventory_list(self, inventory):
expected = {'c-0': {'hosts': ['x.x.x.1'],
'vars': {'deploy_server_id': 'a',
'ctlplane_ip': 'x.x.x.1',
@ -219,14 +235,22 @@ class TestInventory(base.TestCase):
'openstack-mistral-engine'],
'undercloud_swift_url': 'anendpoint',
'username': 'admin'}}}
inv_list = self.inventory.list()
inv_list = inventory.list()
for k in expected:
self.assertEqual(expected[k], inv_list[k])
def test_ansible_ssh_user(self):
self.configs.ansible_ssh_user = 'my-custom-admin'
self.inventory = TripleoInventory(
self.configs, self.session, self.hclient)
session=self.session,
hclient=self.hclient,
plan_name=self.plan_name,
auth_url='xyz://keystone.local',
project_name='admin',
username='admin',
cacert='acacert',
ansible_ssh_user='my-custom-admin')
self.inventory.stack_outputs = self.outputs
expected = {'c-0': {'hosts': ['x.x.x.1'],