Move keypair module to proxy layer

Update keypair module to new openstacksdk

Change-Id: I8f29d983b176e83fbca1919be34a86fe4756aa5e
This commit is contained in:
Arx Cruz 2022-05-10 11:23:51 +02:00 committed by Rafael Castillo
parent 81a81ad759
commit 6d8c965333
2 changed files with 56 additions and 24 deletions

View File

@ -6,6 +6,12 @@
state: present
register: keypair
- name: Assert fields
assert:
that:
- item in keypair.keypair
loop: "{{ expected_fields }}"
- name: Get list of all keypairs
openstack.cloud.keypair_info:
cloud: "{{ cloud }}"
@ -32,7 +38,7 @@
- name: Ensure public key is returned
assert:
that:
- keypair.key.public_key is defined and keypair.key.public_key
- keypair.keypair.public_key is defined and keypair.keypair.public_key
- name: Create another keypair
openstack.cloud.keypair:

View File

@ -61,23 +61,49 @@ EXAMPLES = '''
'''
RETURN = '''
id:
description: Unique UUID.
returned: success
type: str
name:
description: Name given to the keypair.
returned: success
type: str
public_key:
description: The public key value for the keypair.
returned: success
type: str
private_key:
description: The private key value for the keypair.
returned: Only when a keypair is generated for the user (e.g., when creating one
and a public key is not specified).
type: str
keypair:
description: Dictionary describing the keypair.
returned: On success when I(state) is 'present'
type: dict
contains:
created_at:
description: Date the keypair was created
returned: success
type: str
fingerprint:
description: The short fingerprint associated with the public_key
for this keypair.
returned: success
type: str
id:
description: Unique UUID.
returned: success
type: str
is_deleted:
description: Whether the keypair is deleted or not
returned: success
type: bool
name:
description: Name given to the keypair.
returned: success
type: str
private_key:
description: The private key value for the keypair.
returned: Only when a keypair is generated for the user (e.g., when
creating one and a public key is not specified).
type: str
public_key:
description: The public key value for the keypair.
returned: success
type: str
type:
description: The type of keypair
returned: success
type: str
user_id:
description: The user id for a keypair
returned: success
type: str
'''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (
@ -115,11 +141,12 @@ class KeyPairModule(OpenStackModule):
with open(self.params['public_key_file']) as public_key_fh:
public_key = public_key_fh.read().rstrip()
keypair = self.conn.get_keypair(name)
keypair = self.conn.compute.find_keypair(name)
if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change(keypair))
changed = False
if state in ('present', 'replace'):
if keypair and keypair['name'] == name:
if public_key and (public_key != keypair['public_key']):
@ -129,20 +156,19 @@ class KeyPairModule(OpenStackModule):
" as offered. Delete key first." % name
)
else:
self.conn.delete_keypair(name)
self.conn.compute.delete_keypair(keypair)
keypair = self.conn.create_keypair(name, public_key)
changed = True
else:
changed = False
else:
keypair = self.conn.create_keypair(name, public_key)
changed = True
self.exit_json(changed=changed, key=keypair, id=keypair['id'])
self.exit_json(
changed=changed, keypair=keypair.to_dict(computed=False))
elif state == 'absent':
if keypair:
self.conn.delete_keypair(name)
self.conn.compute.delete_keypair(keypair)
self.exit_json(changed=True)
self.exit_json(changed=False)