add copy to modules

This commit is contained in:
sean-m-sullivan
2021-02-21 19:49:14 -06:00
parent 74a5247d9d
commit 9a7dd38cbb
15 changed files with 536 additions and 28 deletions

View File

@@ -32,6 +32,14 @@ options:
- Setting this option will change the existing name (looked up via the name field.
required: False
type: str
copy_from:
description:
- Name or id to copy the credential from.
- This will copy an existing credential and change any parameters supplied.
- The new credential name will be the one provided in the name parameter.
- The organization parameter is not used in this, to facilitate copy from one organization to another.
- Provide the id or use the lookup plugin to provide the id if multiple credentials share the same name.
type: str
description:
description:
- The description to use for the credential.
@@ -274,6 +282,12 @@ EXAMPLES = '''
vault_password: 'new_password'
vault_id: 'My ID'
- name: Copy Credential
tower_credential:
name: Copy password
copy_from: Example password
credential_type: Vault
organization: Foo
'''
from ..module_utils.tower_api import TowerAPIModule
@@ -318,6 +332,7 @@ def main():
argument_spec = dict(
name=dict(required=True),
new_name=dict(),
copy_from=dict(),
description=dict(),
organization=dict(),
credential_type=dict(),
@@ -356,6 +371,7 @@ def main():
# Extract our parameters
name = module.params.get('name')
new_name = module.params.get('new_name')
copy_from = module.params.get('copy_from')
description = module.params.get('description')
organization = module.params.get('organization')
credential_type = module.params.get('credential_type')
@@ -385,6 +401,28 @@ def main():
if organization:
lookup_data['organization'] = org_id
# Attempt to look up credential to copy based on the provided name
if copy_from:
# Check if credential exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
if credential is not None:
module.fail_json(msg="A credential with the name {0} already exists.".format(name))
else:
# Lookup existing credential.
copy_lookup_data = {
'credential_type': cred_type_id,
}
copy_from_lookup = module.get_one('credentials', name_or_id=copy_from, **{'data': copy_lookup_data})
if copy_from_lookup is None:
module.fail_json(msg="A credential with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
lookup_data['organization'] = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='credential'
)
credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data})
if state == 'absent':