1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -188,29 +188,29 @@ class GithubDeployKey:
def __init__(self, module):
self.module = module
self.github_url = self.module.params['github_url']
self.name = module.params['name']
self.key = module.params['key']
self.state = module.params['state']
self.read_only = module.params.get('read_only', True)
self.force = module.params.get('force', False)
self.username = module.params.get('username', None)
self.password = module.params.get('password', None)
self.token = module.params.get('token', None)
self.otp = module.params.get('otp', None)
self.github_url = self.module.params["github_url"]
self.name = module.params["name"]
self.key = module.params["key"]
self.state = module.params["state"]
self.read_only = module.params.get("read_only", True)
self.force = module.params.get("force", False)
self.username = module.params.get("username", None)
self.password = module.params.get("password", None)
self.token = module.params.get("token", None)
self.otp = module.params.get("otp", None)
@property
def url(self):
owner = self.module.params['owner']
repo = self.module.params['repo']
owner = self.module.params["owner"]
repo = self.module.params["repo"]
return f"{self.github_url}/repos/{owner}/{repo}/keys"
@property
def headers(self):
if self.username is not None and self.password is not None:
self.module.params['url_username'] = self.username
self.module.params['url_password'] = self.password
self.module.params['force_basic_auth'] = True
self.module.params["url_username"] = self.username
self.module.params["url_password"] = self.password
self.module.params["force_basic_auth"] = True
if self.otp is not None:
return {"X-GitHub-OTP": self.otp}
elif self.token is not None:
@ -226,10 +226,10 @@ class GithubDeployKey:
yield self.module.from_json(resp.read())
links = {}
for x, y in findall(r'<([^>]+)>;\s*rel="(\w+)"', info.get("link", '')):
for x, y in findall(r'<([^>]+)>;\s*rel="(\w+)"', info.get("link", "")):
links[y] = x
url = links.get('next')
url = links.get("next")
else:
self.handle_error(method="GET", info=info)
@ -240,7 +240,7 @@ class GithubDeployKey:
existing_key_id = str(i["id"])
if i["key"].split() == self.key.split()[:2]:
return existing_key_id
elif i['title'] == self.name and self.force:
elif i["title"] == self.name and self.force:
return existing_key_id
else:
return None
@ -248,7 +248,14 @@ class GithubDeployKey:
def add_new_key(self):
request_body = {"title": self.name, "key": self.key, "read_only": self.read_only}
resp, info = fetch_url(self.module, self.url, data=self.module.jsonify(request_body), headers=self.headers, method="POST", timeout=30)
resp, info = fetch_url(
self.module,
self.url,
data=self.module.jsonify(request_body),
headers=self.headers,
method="POST",
timeout=30,
)
status_code = info["status"]
@ -272,58 +279,59 @@ class GithubDeployKey:
status_code = info["status"]
if status_code == 204:
if self.state == 'absent':
if self.state == "absent":
self.module.exit_json(changed=True, msg="Deploy key successfully deleted", id=key_id)
else:
self.handle_error(method="DELETE", info=info, key_id=key_id)
def handle_error(self, method, info, key_id=None):
status_code = info['status']
body = info.get('body')
status_code = info["status"]
body = info.get("body")
if body:
err = self.module.from_json(body)['message']
err = self.module.from_json(body)["message"]
else:
err = None
if status_code == 401:
self.module.fail_json(msg=f"Failed to connect to {self.github_url} due to invalid credentials", http_status_code=status_code, error=err)
self.module.fail_json(
msg=f"Failed to connect to {self.github_url} due to invalid credentials",
http_status_code=status_code,
error=err,
)
elif status_code == 404:
self.module.fail_json(msg="GitHub repository does not exist", http_status_code=status_code, error=err)
else:
if method == "GET":
self.module.fail_json(msg="Failed to retrieve existing deploy keys", http_status_code=status_code, error=err)
self.module.fail_json(
msg="Failed to retrieve existing deploy keys", http_status_code=status_code, error=err
)
elif method == "POST":
self.module.fail_json(msg="Failed to add deploy key", http_status_code=status_code, error=err)
elif method == "DELETE":
self.module.fail_json(msg="Failed to delete existing deploy key", id=key_id, http_status_code=status_code, error=err)
self.module.fail_json(
msg="Failed to delete existing deploy key", id=key_id, http_status_code=status_code, error=err
)
def main():
module = AnsibleModule(
argument_spec=dict(
github_url=dict(type='str', default="https://api.github.com"),
owner=dict(required=True, type='str', aliases=['account', 'organization']),
repo=dict(required=True, type='str', aliases=['repository']),
name=dict(required=True, type='str', aliases=['title', 'label']),
key=dict(required=True, type='str', no_log=False),
read_only=dict(type='bool', default=True),
state=dict(default='present', choices=['present', 'absent']),
force=dict(type='bool', default=False),
username=dict(type='str'),
password=dict(type='str', no_log=True),
otp=dict(type='int', no_log=True),
token=dict(type='str', no_log=True)
github_url=dict(type="str", default="https://api.github.com"),
owner=dict(required=True, type="str", aliases=["account", "organization"]),
repo=dict(required=True, type="str", aliases=["repository"]),
name=dict(required=True, type="str", aliases=["title", "label"]),
key=dict(required=True, type="str", no_log=False),
read_only=dict(type="bool", default=True),
state=dict(default="present", choices=["present", "absent"]),
force=dict(type="bool", default=False),
username=dict(type="str"),
password=dict(type="str", no_log=True),
otp=dict(type="int", no_log=True),
token=dict(type="str", no_log=True),
),
mutually_exclusive=[
['password', 'token']
],
required_together=[
['username', 'password'],
['otp', 'username', 'password']
],
required_one_of=[
['username', 'token']
],
mutually_exclusive=[["password", "token"]],
required_together=[["username", "password"], ["otp", "username", "password"]],
required_one_of=[["username", "token"]],
supports_check_mode=True,
)
@ -337,17 +345,17 @@ def main():
module.exit_json(changed=False)
# to forcefully modify an existing key, the existing key must be deleted first
if deploy_key.state == 'absent' or deploy_key.force:
if deploy_key.state == "absent" or deploy_key.force:
key_id = deploy_key.get_existing_key()
if key_id is not None:
deploy_key.remove_existing_key(key_id)
elif deploy_key.state == 'absent':
elif deploy_key.state == "absent":
module.exit_json(changed=False, msg="Deploy key does not exist")
if deploy_key.state == "present":
deploy_key.add_new_key()
if __name__ == '__main__':
if __name__ == "__main__":
main()