From 5cf2512cf1fed4905ab3654c144df200ee867fed Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Wed, 1 Mar 2017 12:56:15 -0500 Subject: [PATCH] new decorator to combine UJT options --- lib/tower_cli/resources/node.py | 18 ++++++--- lib/tower_cli/utils/resource_decorators.py | 47 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 lib/tower_cli/utils/resource_decorators.py diff --git a/lib/tower_cli/resources/node.py b/lib/tower_cli/resources/node.py index b77aab2..2168ac6 100644 --- a/lib/tower_cli/resources/node.py +++ b/lib/tower_cli/resources/node.py @@ -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) diff --git a/lib/tower_cli/utils/resource_decorators.py b/lib/tower_cli/utils/resource_decorators.py new file mode 100644 index 0000000..8c1aea8 --- /dev/null +++ b/lib/tower_cli/utils/resource_decorators.py @@ -0,0 +1,47 @@ +# Copyright 2017 Ansible by Red Hat +# Alan Rominger +# +# 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