Fixed documentation errors, syntax mistakes, updated author email, and tested

This commit is contained in:
Austin Lucas Lake 2025-03-14 17:42:54 -07:00
parent eafe80c924
commit 4a535a22f3
No known key found for this signature in database
GPG key ID: 6A37FA54CFCFA4DB
3 changed files with 23 additions and 55 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2024, Austin Lucas Lake <53884490+austinlucaslake@users.noreply.github.com>
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
# Based on community.general.github_key module by Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
@ -14,7 +14,7 @@ DOCUMENTATION = '''
module: github_gpg_key
author: Austin Lucas Lake (@austinlucaslake)
short_description: Manage GitHub GPG keys
version_added: 9.0.0
version_added: 10.5.0
description:
- Creates or removes GitHub GPG keys for an authenticated user.
extends_documentation_fragment:
@ -48,7 +48,7 @@ options:
force:
description:
- The default is V(true), which will replace the existing remote key
if it is different than O(pubkey). If V(false), the key will only be
if it is different than O(armored_public_key). If V(false), the key will only be
set if no key with the given O(name) exists.
type: bool
default: true
@ -62,7 +62,7 @@ deleted_keys:
returned: When O(state=absent)
sample: [{
'id': 3,
'name': 'Octocat's GPG Key',
'name': "Octocat's GPG Key",
'primary_key_id': 2,
'key_id': '3262EFF25BA0D270',
'public_key': 'xsBNBFayYZ...',
@ -100,7 +100,7 @@ matching_keys:
returned: When O(state=present)
sample: [{
'id': 3,
'name': 'Octocat's GPG Key',
'name': "Octocat's GPG Key",
'primary_key_id': 2,
'key_id': '3262EFF25BA0D270',
'public_key': 'xsBNBFayYZ...',
@ -137,7 +137,7 @@ key:
returned: When O(state=present)
sample: {
'id': 3,
'name': 'Octocat's GPG Key',
'name': "Octocat's GPG Key",
'primary_key_id': 2,
'key_id': '3262EFF25BA0D270',
'public_key': 'xsBNBFayYZ...',
@ -239,39 +239,7 @@ def get_all_keys(session):
def create_key(session, name, armored_public_key, check_mode):
if check_mode:
now_t = now()
return {
'id': 3,
'name': name,
'primary_key_id': 2,
'key_id': '3262EFF25BA0D270',
'public_key': 'xsBNBFayYZ...',
'emails': [{
'email': 'octocat@users.noreply.github.com',
'verified': True
}],
'subkeys': [{
'id': 4,
'primary_key_id': 3,
'key_id': '4A595D4C72EE49C7',
'public_key': 'zsBNBFayYZ...',
'emails': [],
'can_sign': False,
'can_encrypt_comms': True,
'can_encrypt_storage': True,
'can_certify': False,
'created_at': now_t.strftime('%Y-%m-%dT%H:%M:%SZ'),
'expires_at': None,
'revoked': False
}],
'can_sign': True,
'can_encrypt_comms': False,
'can_encrypt_storage': False,
'can_certify': True,
'created_at': now_t.strftime('%Y-%m-%dT%H:%M:%SZ'),
'expires_at': None,
'revoked': False,
'raw_key': armored_public_key
}
return {}
else:
return session.request(
'POST',
@ -342,7 +310,7 @@ def run_module(module, params, check_mode):
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(type='str', default='present', choice=['present', 'absent']),
state=dict(type='str', default='present', choices=['present', 'absent']),
token=dict(type='str', required=True, no_log=True),
name=dict(type='str', required=True),
armored_public_key=dict(type='str', no_log=True),
@ -362,7 +330,7 @@ def main():
)
module.exit_json(**result)
except Exception as e:
module.fail_json(str(e))
module.fail_json(msg=str(e))
if __name__ == '__main__':

View file

@ -5,22 +5,22 @@
# Test code for the github_issue module.
#
# Copyright (c) 2024, Austin Lucas Lake <akasurde@redhat.com>
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Create GPG key
github_gpg_key:
token: {{ token }}
name: {{ name }}
armored_gpg_key: {{ armored_gpg_key }}
token: "{{ token }}"
name: "{{ name }}"
armored_gpg_key: "{{ armored_gpg_key }}"
register: created_gpg_key
- name: Delete GPG key
github_gpg_key:
state: absent
token: {{ token }}
name: {{ name }}
token: "{{ token }}"
name: "{{ name }}"
register: deleted_gpg_key
- assert:

View file

@ -1,4 +1,4 @@
# Copyright (c) 2024 Austin Lucas Lake
# Copyright (c) 2024-2025, Austin Lucas Lake, <git@austinlucaslake.com>
# Based on tests/unit/plugins/modules/test_github_repo.py by Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
@ -6,10 +6,10 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
import json
import sys
import datetime
from httmock import with_httmock, urlmatch, response
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.tests.unit.compat import unittest
from ansible_collections.community.general.plugins.modules import github_gpg_key
@ -99,7 +99,7 @@ def create_gpg_key_mock(url, request):
"can_encrypt_comms": False,
"can_encrypt_storage": False,
"can_certify": True,
"created_at": now_T.strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": now_t.strftime("%Y-%m-%dT%H:%M:%SZ"),
"expires_at": None,
"revoked": False,
"raw_key": gpg_key.get("armored_public_key")
@ -109,7 +109,7 @@ def create_gpg_key_mock(url, request):
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys/.*', method="delete")
def delete_repo_mock(url, request):
def delete_gpg_key_mock(url, request):
# https://docs.github.com/en/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
return response(204, None, None, None, 5, request)
@ -156,7 +156,7 @@ class TestGithubRepo(unittest.TestCase):
@with_httmock(delete_gpg_key_mock)
def test_delete_user_repo(self):
result = github_gpg_key.run_module(
module=self.module
module=self.module,
params=dict(
token="github_access_token",
name="GPG public key",
@ -168,10 +168,10 @@ class TestGithubRepo(unittest.TestCase):
self.assertEqual(result['changed'], True)
@with_httmock(list_gpg_keys_mock)
@with_httmock(delete_gpg_notfound_mock)
@with_httmock(delete_gpg_key_notfound_mock)
def test_delete_gpg_key_notfound(self):
result = github_gpg_key.run_module(
module=self.module
module=self.module,
params=dict(
token="github_access_token",
name="GPG public key",