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:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -135,88 +135,93 @@ data:
|
|||
}
|
||||
"""
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.scaleway import SCALEWAY_LOCATION, scaleway_argument_spec, Scaleway
|
||||
from ansible_collections.community.general.plugins.module_utils.scaleway import (
|
||||
SCALEWAY_LOCATION,
|
||||
scaleway_argument_spec,
|
||||
Scaleway,
|
||||
)
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def payload_from_security_group(security_group):
|
||||
return {
|
||||
k: v
|
||||
for k, v in security_group.items()
|
||||
if k != 'id' and v is not None
|
||||
}
|
||||
return {k: v for k, v in security_group.items() if k != "id" and v is not None}
|
||||
|
||||
|
||||
def present_strategy(api, security_group):
|
||||
ret = {'changed': False}
|
||||
ret = {"changed": False}
|
||||
|
||||
response = api.get('security_groups')
|
||||
response = api.get("security_groups")
|
||||
if not response.ok:
|
||||
api.module.fail_json(msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
api.module.fail_json(
|
||||
msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
security_group_lookup = {sg['name']: sg for sg in response.json['security_groups']}
|
||||
security_group_lookup = {sg["name"]: sg for sg in response.json["security_groups"]}
|
||||
|
||||
if security_group['name'] not in security_group_lookup.keys():
|
||||
ret['changed'] = True
|
||||
if security_group["name"] not in security_group_lookup.keys():
|
||||
ret["changed"] = True
|
||||
if api.module.check_mode:
|
||||
# Help user when check mode is enabled by defining id key
|
||||
ret['scaleway_security_group'] = {'id': str(uuid4())}
|
||||
ret["scaleway_security_group"] = {"id": str(uuid4())}
|
||||
return ret
|
||||
|
||||
# Create Security Group
|
||||
response = api.post('/security_groups',
|
||||
data=payload_from_security_group(security_group))
|
||||
response = api.post("/security_groups", data=payload_from_security_group(security_group))
|
||||
|
||||
if not response.ok:
|
||||
msg = f'Error during security group creation: "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
api.module.fail_json(msg=msg)
|
||||
ret['scaleway_security_group'] = response.json['security_group']
|
||||
ret["scaleway_security_group"] = response.json["security_group"]
|
||||
|
||||
else:
|
||||
ret['scaleway_security_group'] = security_group_lookup[security_group['name']]
|
||||
ret["scaleway_security_group"] = security_group_lookup[security_group["name"]]
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def absent_strategy(api, security_group):
|
||||
response = api.get('security_groups')
|
||||
ret = {'changed': False}
|
||||
response = api.get("security_groups")
|
||||
ret = {"changed": False}
|
||||
|
||||
if not response.ok:
|
||||
api.module.fail_json(msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
api.module.fail_json(
|
||||
msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
security_group_lookup = {sg['name']: sg for sg in response.json['security_groups']}
|
||||
if security_group['name'] not in security_group_lookup.keys():
|
||||
security_group_lookup = {sg["name"]: sg for sg in response.json["security_groups"]}
|
||||
if security_group["name"] not in security_group_lookup.keys():
|
||||
return ret
|
||||
|
||||
ret['changed'] = True
|
||||
ret["changed"] = True
|
||||
if api.module.check_mode:
|
||||
return ret
|
||||
|
||||
response = api.delete(f"/security_groups/{security_group_lookup[security_group['name']]['id']}")
|
||||
if not response.ok:
|
||||
api.module.fail_json(msg=f'Error deleting security group "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
api.module.fail_json(
|
||||
msg=f'Error deleting security group "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def core(module):
|
||||
security_group = {
|
||||
'organization': module.params['organization'],
|
||||
'name': module.params['name'],
|
||||
'description': module.params['description'],
|
||||
'stateful': module.params['stateful'],
|
||||
'inbound_default_policy': module.params['inbound_default_policy'],
|
||||
'outbound_default_policy': module.params['outbound_default_policy'],
|
||||
'organization_default': module.params['organization_default'],
|
||||
"organization": module.params["organization"],
|
||||
"name": module.params["name"],
|
||||
"description": module.params["description"],
|
||||
"stateful": module.params["stateful"],
|
||||
"inbound_default_policy": module.params["inbound_default_policy"],
|
||||
"outbound_default_policy": module.params["outbound_default_policy"],
|
||||
"organization_default": module.params["organization_default"],
|
||||
}
|
||||
|
||||
region = module.params['region']
|
||||
module.params['api_url'] = SCALEWAY_LOCATION[region]['api_endpoint']
|
||||
region = module.params["region"]
|
||||
module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"]
|
||||
|
||||
api = Scaleway(module=module)
|
||||
if module.params['state'] == 'present':
|
||||
if module.params["state"] == "present":
|
||||
summary = present_strategy(api=api, security_group=security_group)
|
||||
else:
|
||||
summary = absent_strategy(api=api, security_group=security_group)
|
||||
|
|
@ -225,25 +230,27 @@ def core(module):
|
|||
|
||||
def main():
|
||||
argument_spec = scaleway_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||
organization=dict(type='str', required=True),
|
||||
name=dict(type='str', required=True),
|
||||
description=dict(type='str'),
|
||||
region=dict(type='str', required=True, choices=list(SCALEWAY_LOCATION.keys())),
|
||||
stateful=dict(type='bool', required=True),
|
||||
inbound_default_policy=dict(type='str', choices=['accept', 'drop']),
|
||||
outbound_default_policy=dict(type='str', choices=['accept', 'drop']),
|
||||
organization_default=dict(type='bool'),
|
||||
))
|
||||
argument_spec.update(
|
||||
dict(
|
||||
state=dict(type="str", default="present", choices=["absent", "present"]),
|
||||
organization=dict(type="str", required=True),
|
||||
name=dict(type="str", required=True),
|
||||
description=dict(type="str"),
|
||||
region=dict(type="str", required=True, choices=list(SCALEWAY_LOCATION.keys())),
|
||||
stateful=dict(type="bool", required=True),
|
||||
inbound_default_policy=dict(type="str", choices=["accept", "drop"]),
|
||||
outbound_default_policy=dict(type="str", choices=["accept", "drop"]),
|
||||
organization_default=dict(type="bool"),
|
||||
)
|
||||
)
|
||||
module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
supports_check_mode=True,
|
||||
required_if=[['stateful', True, ['inbound_default_policy', 'outbound_default_policy']]]
|
||||
required_if=[["stateful", True, ["inbound_default_policy", "outbound_default_policy"]]],
|
||||
)
|
||||
|
||||
core(module)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue