Add a tripleo-common module util

This change will co-locate all tripleo-common utility code which we use
within modules. By creating a module util for tripleo-common we'll save
duplication and ensure functionality across all modules that leverage
tripleo-common.

The ansible-auto-doc plugin has been updated to support a new module-util.

Change-Id: Ic85c45a64e160223a012b77f698c2224da5d09d7
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2020-04-13 07:38:09 -05:00
parent 02a9baa9f7
commit 2adb49cf63
No known key found for this signature in database
GPG Key ID: CE94BD890A47B20A
12 changed files with 273 additions and 219 deletions

View File

@ -88,7 +88,8 @@ bug_project = 'tripleo'
bug_tag = 'documentation'
needed_module_utils = [
'baremetal_deploy'
'baremetal_deploy',
'tripleo_common_utils'
]
# load our custom module_utils so that modules can be imported for
# generating docs

View File

@ -0,0 +1,233 @@
#!/usr/bin/python
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from glanceclient import client as glanceclient
from heatclient.v1 import client as heatclient
from ironicclient import client as ironicclient
from novaclient import client as novaclient
from swiftclient import client as swift_client
from tripleo_common.actions import baremetal
from tripleo_common.utils import nodes
from tripleo_common.utils import parameters
import ironic_inspector_client
class TripleOCommon(object):
def __init__(self, session):
self.sess = session
self.client_cache = dict()
def get_ironic_inspector_client(self):
"""Return the ironic inspector client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'ironic_inspector_client' in self.client_cache:
return self.client_cache['ironic_inspector_client']
else:
self.client_cache['ironic_inspector_client'] = \
ironic_inspector_client.ClientV1(session=self.sess)
return self.client_cache['ironic_inspector_client']
def get_orchestration_client(self):
"""Return the orchestration (heat) client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'heatclient' in self.client_cache:
return self.client_cache['heatclient']
else:
self.client_cache['heatclient'] = \
heatclient.Client(session=self.sess)
return self.client_cache['heatclient']
def get_compute_client(self):
"""Return the compute (nova) client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'novaclient' in self.client_cache:
return self.client_cache['novaclient']
else:
self.client_cache['novaclient'] = \
novaclient.Client(version=2, session=self.sess)
return self.client_cache['novaclient']
def get_baremetal_client(self):
"""Return the baremetal (ironic) client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'ironicclient' in self.client_cache:
return self.client_cache['ironicclient']
else:
self.client_cache['ironicclient'] = \
ironicclient.Client(
1,
session=self.sess,
os_ironic_api_version='1.36'
)
return self.client_cache['ironicclient']
def get_image_client(self):
"""Return the image (glance) client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'glanceclient' in self.client_cache:
return self.client_cache['glanceclient']
else:
self.client_cache['glanceclient'] = \
glanceclient.Client(
2,
session=self.sess
)
return self.client_cache['glanceclient']
def get_object_client(self):
"""Return the object (swift) client.
This method will return a client object using the legacy library. Upon
the creation of a successful client creation, the client object will
be stored in the `self.client_cache object`, should this method be
called more than once, the cached object will automatically return,
resulting in fewer authentications and faster API interactions.
:returns: Object
"""
if 'swift_client' in self.client_cache:
return self.client_cache['swift_client']
else:
self.client_cache['swift_client'] = \
swift_client.Connection(
session=self.sess,
retries=10,
starting_backoff=3,
max_backoff=120
)
return self.client_cache['swift_client']
def baremetal_configure_boot(self, kwargs):
"""Run the action configure boot, and return data.
:param kwargs: options to pass into the ConfigureBootAction
:type kwargs: Dictionary
:returns: Object
"""
action = baremetal.ConfigureBootAction(**kwargs)
baremetal_client = ironicclient.Client(
1,
session=self.sess
)
image_client = glanceclient.Client(2, session=self.sess)
return action.configure_boot(
baremetal_client,
image_client
)
def baremetal_configure_root_device(self, kwargs):
"""Run the action configure root device, and return data.
:param kwargs: options to pass into the ConfigureRootDeviceAction
:type kwargs: Dictionary
:returns: Object
"""
action = baremetal.ConfigureRootDeviceAction(**kwargs)
baremetal_client = ironicclient.Client(
1,
session=self.sess
)
inspector_client = self.get_ironic_inspector_client()
if not action.root_device:
return
else:
return action.configure_root_device(
baremetal_client,
inspector_client
)
def return_baremetal_data(self, node_id):
"""Return baremetal data from the ironic inspector.
:param node_id: Node UUID
:type node_id: String
:returns: Object
"""
client = self.get_ironic_inspector_client()
return client.get_data(node_id=node_id)
def return_flavor_profile(self, flavor_name):
"""Return flavor profile information.
:param flavor_name: Flavor name
:type flavor_name: String
:returns: Object
"""
return parameters.get_profile_of_flavor(
flavor_name=flavor_name,
compute_client=self.get_compute_client()
)

View File

@ -21,18 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE(cloudnull): This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from glanceclient import client as glanceclient
from ironicclient import client as ironicclient
from tripleo_common.utils import nodes
ANSIBLE_METADATA = {
@ -73,21 +67,6 @@ author:
'''
def _get_baremetal_client(session):
return ironicclient.Client(
1,
session=session,
os_ironic_api_version='1.36'
)
def _get_image_client(session):
return glanceclient.Client(
2,
session=session
)
def run_module():
result = dict(
success=False,
@ -106,7 +85,7 @@ def run_module():
)
_, conn = openstack_cloud_from_module(module)
session = conn.session
tripleo = tc.TripleOCommon(session=conn.session)
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
@ -126,8 +105,8 @@ def run_module():
module.params['instance_boot_option'])
node['capabilities'] = nodes.dict_to_capabilities(caps)
baremetal_client = _get_baremetal_client(session)
image_client = _get_image_client(session)
baremetal_client = tripleo.get_baremetal_client()
image_client = tripleo.get_image_client()
try:
registered_nodes = nodes.register_all_nodes(

View File

@ -339,7 +339,6 @@ EXAMPLES = '''
- os_baremetal_node_info:
cloud: undercloud
auth: password
filters:
is_maintenance: true
'''
@ -362,22 +361,16 @@ def _choose_id_value(module):
def main():
argument_spec = openstack_full_argument_spec(
**yaml.safe_load(DOCUMENTATION)['options'])
**yaml.safe_load(DOCUMENTATION)['options']
)
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs)
if (module.params['auth_type'] in [None, 'None'] and
module.params['ironic_url'] is None):
module.fail_json(msg="Authentication appears to be disabled, "
"Please define an ironic_url parameter")
if (module.params['ironic_url'] and
module.params['auth_type'] in [None, 'None']):
module.params['auth'] = dict(
endpoint=module.params['ironic_url']
)
module = AnsibleModule(
argument_spec,
supports_check_mode=False,
**module_kwargs
)
sdk, cloud = openstack_cloud_from_module(module)
try:
if module.params['name'] or module.params['uuid']:
result = cloud.get_machine(_choose_id_value(module))

View File

@ -14,6 +14,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
@ -72,48 +73,6 @@ import os
import yaml
# NOTE(cloudnull): This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from tripleo_common.actions import baremetal
from glanceclient import client as glanceclient
from ironicclient import client as ironicclient
import ironic_inspector_client
class TripleOCommon(object):
def __init__(self, session):
self.sess = session
def baremetal_configure_boot(self, kwargs):
action = baremetal.ConfigureBootAction(**kwargs)
baremetal_client = ironicclient.Client(
1,
session=self.sess
)
image_client = glanceclient.Client(2, session=self.sess)
return action.configure_boot(
baremetal_client,
image_client
)
def baremetal_configure_root_device(self, kwargs):
action = baremetal.ConfigureRootDeviceAction(**kwargs)
baremetal_client = ironicclient.Client(
1,
session=self.sess
)
inspector_client = ironic_inspector_client.ClientV1(session=self.sess)
if not action.root_device:
return
else:
return action.configure_root_device(
baremetal_client,
inspector_client
)
def main():
argument_spec = openstack_full_argument_spec(
@ -125,7 +84,7 @@ def main():
)
_, conn = openstack_cloud_from_module(module)
tripleo = TripleOCommon(session=conn.session)
tripleo = tc.TripleOCommon(session=conn.session)
if hasattr(tripleo, module.params["action"]):
action = getattr(tripleo, module.params["action"])

View File

@ -21,17 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
import swiftclient
from tripleo_common.utils import plan as plan_utils
ANSIBLE_METADATA = {
@ -109,24 +104,17 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swiftclient.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
try:
container = module.params.get('container')
_, conn = openstack_cloud_from_module(module)
session = conn.session
tripleo = tc.TripleOCommon(session=conn.session)
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
module.exit_json(**result)
swift = get_object_client(session)
swift = tripleo.get_object_client()
fernet_keys = plan_utils.update_plan_rotate_fernet_keys(swift, container)
result['success'] = True
result['fernet_keys'] = fernet_keys

View File

@ -21,18 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from swiftclient import client as swift_client
from tripleo_common.utils import plan as plan_utils
ANSIBLE_METADATA = {
@ -92,20 +86,12 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swift_client.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
try:
container = module.params.get('container')
with_roledata = module.params.get('with_roledata')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
plan_utils.update_plan_environment_with_image_parameters(
swift, container, with_roledata=with_roledata)
result['success'] = True

View File

@ -21,20 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from heatclient.v1 import client as heatclient
from mistralclient.api import client as mistral_client
from swiftclient import client as swift_client
from tripleo_common.utils import plan as plan_utils
ANSIBLE_METADATA = {
@ -108,26 +100,14 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swift_client.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
def get_orchestration_client(session):
return heatclient.Client(
session=session)
try:
container = module.params.get('container')
rotate_passwords = module.params.get('rotate_passwords')
password_list = module.params.get('password_list')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
heat = get_orchestration_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
heat = tripleo.get_orchestration_client()
rotated_passwords = plan_utils.generate_passwords(
swift, heat, container=container,
rotate_passwords=rotate_passwords,

View File

@ -21,19 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from heatclient.v1 import client as heatclient
from swiftclient import client as swift_client
from tripleo_common.utils import stack as stack_utils
ANSIBLE_METADATA = {
@ -100,26 +93,14 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swift_client.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
def get_orchestration_client(session):
return heatclient.Client(
session=session)
try:
container = module.params.get('container')
skip_deploy_identifier = module.params.get('skip_deploy_identifier')
timeout_mins = module.params.get('timeout_mins')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
heat = get_orchestration_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
heat = tripleo.get_orchestration_client()
stack_utils.deploy_stack(
swift, heat,
container=container,

View File

@ -21,19 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from heatclient.v1 import client as heatclient
from swiftclient import client as swift_client
from tripleo_common.utils import stack_parameters as stack_param_utils
ANSIBLE_METADATA = {
@ -106,27 +99,15 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swift_client.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
def get_orchestration_client(session):
return heatclient.Client(
session=session)
try:
container = module.params.get('container')
parameters = module.params.get('parameters')
parameter_key = module.params.get('parameter_key')
validate = module.params.get('validate')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
heat = get_orchestration_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
heat = tripleo.get_orchestration_client()
stack_param_utils.update_parameters(
swift, heat, parameters,
container=container,

View File

@ -21,17 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
import swiftclient
from tripleo_common.utils import swift as swift_utils
ANSIBLE_METADATA = {
@ -108,21 +103,13 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swiftclient.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
try:
container = module.params.get('container')
obj = module.params.get('object')
method = module.params.get('method')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
tempurl = swift_utils.get_temp_url(swift, container, obj, method)
result['success'] = True
result['changed'] = True

View File

@ -21,18 +21,12 @@ from __future__ import print_function
import yaml
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
# NOTE: This is still using the legacy clients. We've not
# changed to using the OpenStackSDK fully because
# tripleo-common expects the legacy clients. Once
# we've updated tripleo-common to use the SDK we
# should revise this.
from swiftclient import client as swift_client
from tripleo_common.utils import template as template_utils
ANSIBLE_METADATA = {
@ -92,20 +86,12 @@ def run_module():
**openstack_module_kwargs()
)
def get_object_client(session):
return swift_client.Connection(
session=session,
retries=10,
starting_backoff=3,
max_backoff=120)
try:
container = module.params.get('container')
templates_dir = module.params.get('templates_dir')
_, conn = openstack_cloud_from_module(module)
session = conn.session
swift = get_object_client(session)
tripleo = tc.TripleOCommon(session=conn.session)
swift = tripleo.get_object_client()
template_utils.upload_templates_as_tarball(
swift, container, templates_dir)
result['success'] = True