Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for copying activation keys #157

Merged
merged 2 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions modules/katello_activation_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
- Set Auto-Attach on or off
default: true
type: bool
state:
description:
- State of the Activation Key
default: present
choices:
- present
- copied
new_name:
description:
- Name of the new activation key when state == copied
'''

EXAMPLES = '''
Expand Down Expand Up @@ -201,6 +211,25 @@ def activation_key(self, name, organization, lifecycle_environment=None, content

return updated

def activation_key_copy(self, name, organization, new_name):
updated = False
organization = self.find_organization(organization)

kwargs = {'name': name, 'organization': organization}
activation_key = self._entities.ActivationKey(self._server, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather find_activation_key was added to ansible_nailgun_cement.

response = activation_key.search({'name', 'organization'})

kwargs = {'name': new_name, 'organization': organization}
new_activation_key = self._entities.ActivationKey(self._server, **kwargs)
new_response = new_activation_key.search({'name', 'organization'})

if len(response) == 1 and len(new_response) == 0:
if not self._module.check_mode:
new_activation_key = response[0].copy(data={'new_name': new_name})
Copy link
Member

@sean797 sean797 Aug 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an elif state == 'copied': to

def naildown_entity(entity_class, entity_dict, entity, state, module):
and using that could be useful for content_views as well.

updated = True

return updated


def main():
module = AnsibleModule(
Expand All @@ -210,13 +239,18 @@ def main():
password=dict(required=True, no_log=True),
verify_ssl=dict(type='bool', default=True),
name=dict(required=True),
new_name=dict(),
organization=dict(required=True),
lifecycle_environment=dict(),
content_view=dict(),
subscriptions=dict(type='list'),
auto_attach=dict(type='bool', default=True),
state=dict(default='present', choices=['present', 'copied']),
),
supports_check_mode=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding required_if would be useful for new_name & state == copied

required_if=[
['state', 'copied', ['new_name']],
],
)

if has_import_error:
Expand All @@ -232,6 +266,8 @@ def main():
content_view = module.params['content_view']
subscriptions = module.params['subscriptions']
auto_attach = module.params['auto_attach']
new_name = module.params['new_name']
state = module.params['state']

server = ServerConfig(
url=server_url,
Expand All @@ -248,8 +284,11 @@ def main():
module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

try:
changed = ng.activation_key(name, organization, lifecycle_environment=lifecycle_environment, content_view=content_view, subscriptions=subscriptions,
auto_attach=auto_attach)
if state == 'copied':
changed = ng.activation_key_copy(name, organization, new_name)
else:
changed = ng.activation_key(name, organization, lifecycle_environment=lifecycle_environment, content_view=content_view,
subscriptions=subscriptions, auto_attach=auto_attach)
module.exit_json(changed=changed)
except Exception as e:
module.fail_json(msg=e)
Expand Down
3 changes: 3 additions & 0 deletions test/test_playbooks/activation_key.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
vars:
activation_key_auto_attach: True
expected_change: false
- include: tasks/activation_key_copy.yml
vars:
expected_change: true

- hosts: fixtures
gather_facts: false
Expand Down
Loading