mirror of
https://github.com/ansible/tower-cli.git
synced 2026-02-06 00:48:50 +01:00
new decorator to combine UJT options
This commit is contained in:
@@ -15,8 +15,11 @@
|
||||
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from tower_cli import models
|
||||
from tower_cli import models, resources
|
||||
from tower_cli.utils import types
|
||||
from tower_cli.utils.resource_decorators import unified_job_template_options
|
||||
|
||||
import click
|
||||
|
||||
|
||||
class Resource(models.Resource):
|
||||
@@ -26,8 +29,6 @@ class Resource(models.Resource):
|
||||
|
||||
workflow_job_template = models.Field(
|
||||
key='-W', type=types.Related('workflow'))
|
||||
unified_job_template = models.Field(
|
||||
key='-J', type=types.Related('job_template'))
|
||||
inventory = models.Field(
|
||||
type=types.Related('inventory'), required=False, display=False)
|
||||
credential = models.Field(
|
||||
@@ -36,6 +37,11 @@ class Resource(models.Resource):
|
||||
job_tags = models.Field(required=False, display=False)
|
||||
skip_tags = models.Field(required=False, display=False)
|
||||
limit = models.Field(required=False, display=False)
|
||||
fail_on_job_failure = models.Field(
|
||||
type=bool, required=False, display=False,
|
||||
help_text='Set workflow to fail if connected job fails.')
|
||||
|
||||
@resources.command(use_fields_as_options=False)
|
||||
@unified_job_template_options
|
||||
@click.argument('parent', type=types.Related('node'))
|
||||
@click.argument('child', type=types.Related('node'))
|
||||
def associate_success_node(self, parent, child=None, **kwargs):
|
||||
"""Add a node to run on success."""
|
||||
return self._assoc('success_nodes', parent, child)
|
||||
|
||||
47
lib/tower_cli/utils/resource_decorators.py
Normal file
47
lib/tower_cli/utils/resource_decorators.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Copyright 2017 Ansible by Red Hat
|
||||
# Alan Rominger <arominge@redhat.com>
|
||||
#
|
||||
# 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.
|
||||
|
||||
import functools
|
||||
from tower_cli.utils import types
|
||||
|
||||
import click
|
||||
|
||||
|
||||
def unified_job_template_options(method):
|
||||
"""
|
||||
Adds the decorators for all types of unified job templates,
|
||||
and if the non-unified type is specified, converts it into the
|
||||
unified_job_template kwarg.
|
||||
"""
|
||||
jt_dec = click.option(
|
||||
'--job-template', type=types.Related('job_template'))
|
||||
prj_dec = click.option(
|
||||
'--project', type=types.Related('project'))
|
||||
inv_src_dec = click.option(
|
||||
'--inventory-source', type=types.Related('inventory_source'))
|
||||
|
||||
def ujt_translation(_method):
|
||||
def _ujt_translation(*args, **kwargs):
|
||||
for fd in ['job_template', 'project', 'inventory_source']:
|
||||
if fd in kwargs and kwargs[fd] is not None:
|
||||
kwargs['unified_job_template'] = kwargs.pop(fd)
|
||||
return _method(*args, **kwargs)
|
||||
return functools.wraps(_method)(_ujt_translation)
|
||||
|
||||
method = jt_dec(method)
|
||||
method = prj_dec(method)
|
||||
method = inv_src_dec(method)
|
||||
method = ujt_translation(method)
|
||||
return method
|
||||
Reference in New Issue
Block a user