mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-26 13:42:49 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -141,78 +141,89 @@ data:
|
|||
}
|
||||
"""
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.scaleway import SCALEWAY_LOCATION, scaleway_argument_spec, Scaleway, payload_from_object
|
||||
from ansible_collections.community.general.plugins.module_utils.scaleway import (
|
||||
SCALEWAY_LOCATION,
|
||||
scaleway_argument_spec,
|
||||
Scaleway,
|
||||
payload_from_object,
|
||||
)
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
def get_sgr_from_api(security_group_rules, security_group_rule):
|
||||
""" Check if a security_group_rule specs are present in security_group_rules
|
||||
Return None if no rules match the specs
|
||||
Return the rule if found
|
||||
"""Check if a security_group_rule specs are present in security_group_rules
|
||||
Return None if no rules match the specs
|
||||
Return the rule if found
|
||||
"""
|
||||
for sgr in security_group_rules:
|
||||
if (sgr['ip_range'] == security_group_rule['ip_range'] and sgr['dest_port_from'] == security_group_rule['dest_port_from'] and
|
||||
sgr['direction'] == security_group_rule['direction'] and sgr['action'] == security_group_rule['action'] and
|
||||
sgr['protocol'] == security_group_rule['protocol']):
|
||||
if (
|
||||
sgr["ip_range"] == security_group_rule["ip_range"]
|
||||
and sgr["dest_port_from"] == security_group_rule["dest_port_from"]
|
||||
and sgr["direction"] == security_group_rule["direction"]
|
||||
and sgr["action"] == security_group_rule["action"]
|
||||
and sgr["protocol"] == security_group_rule["protocol"]
|
||||
):
|
||||
return sgr
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def present_strategy(api, security_group_id, security_group_rule):
|
||||
ret = {'changed': False}
|
||||
ret = {"changed": False}
|
||||
|
||||
response = api.get(f'security_groups/{security_group_id}/rules')
|
||||
response = api.get(f"security_groups/{security_group_id}/rules")
|
||||
if not response.ok:
|
||||
api.module.fail_json(
|
||||
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
existing_rule = get_sgr_from_api(
|
||||
response.json['rules'], security_group_rule)
|
||||
existing_rule = get_sgr_from_api(response.json["rules"], security_group_rule)
|
||||
|
||||
if not existing_rule:
|
||||
ret['changed'] = True
|
||||
ret["changed"] = True
|
||||
if api.module.check_mode:
|
||||
return ret
|
||||
|
||||
# Create Security Group Rule
|
||||
response = api.post(f'/security_groups/{security_group_id}/rules',
|
||||
data=payload_from_object(security_group_rule))
|
||||
response = api.post(
|
||||
f"/security_groups/{security_group_id}/rules", data=payload_from_object(security_group_rule)
|
||||
)
|
||||
|
||||
if not response.ok:
|
||||
api.module.fail_json(
|
||||
msg=f'Error during security group rule creation: "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
ret['scaleway_security_group_rule'] = response.json['rule']
|
||||
msg=f'Error during security group rule creation: "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
ret["scaleway_security_group_rule"] = response.json["rule"]
|
||||
|
||||
else:
|
||||
ret['scaleway_security_group_rule'] = existing_rule
|
||||
ret["scaleway_security_group_rule"] = existing_rule
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def absent_strategy(api, security_group_id, security_group_rule):
|
||||
ret = {'changed': False}
|
||||
ret = {"changed": False}
|
||||
|
||||
response = api.get(f'security_groups/{security_group_id}/rules')
|
||||
response = api.get(f"security_groups/{security_group_id}/rules")
|
||||
if not response.ok:
|
||||
api.module.fail_json(
|
||||
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
existing_rule = get_sgr_from_api(
|
||||
response.json['rules'], security_group_rule)
|
||||
existing_rule = get_sgr_from_api(response.json["rules"], security_group_rule)
|
||||
|
||||
if not existing_rule:
|
||||
return ret
|
||||
|
||||
ret['changed'] = True
|
||||
ret["changed"] = True
|
||||
if api.module.check_mode:
|
||||
return ret
|
||||
|
||||
response = api.delete(
|
||||
f"/security_groups/{security_group_id}/rules/{existing_rule['id']}")
|
||||
response = api.delete(f"/security_groups/{security_group_id}/rules/{existing_rule['id']}")
|
||||
if not response.ok:
|
||||
api.module.fail_json(
|
||||
msg=f'Error deleting security group rule "{response.info["msg"]}": "{response.json["message"]}" ({response.json})')
|
||||
msg=f'Error deleting security group rule "{response.info["msg"]}": "{response.json["message"]}" ({response.json})'
|
||||
)
|
||||
|
||||
return ret
|
||||
|
||||
|
|
@ -221,40 +232,38 @@ def core(module):
|
|||
api = Scaleway(module=module)
|
||||
|
||||
security_group_rule = {
|
||||
'protocol': module.params['protocol'],
|
||||
'dest_port_from': module.params['port'],
|
||||
'ip_range': module.params['ip_range'],
|
||||
'direction': module.params['direction'],
|
||||
'action': module.params['action'],
|
||||
"protocol": module.params["protocol"],
|
||||
"dest_port_from": module.params["port"],
|
||||
"ip_range": module.params["ip_range"],
|
||||
"direction": module.params["direction"],
|
||||
"action": module.params["action"],
|
||||
}
|
||||
|
||||
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"]
|
||||
|
||||
if module.params['state'] == 'present':
|
||||
if module.params["state"] == "present":
|
||||
summary = present_strategy(
|
||||
api=api,
|
||||
security_group_id=module.params['security_group'],
|
||||
security_group_rule=security_group_rule)
|
||||
api=api, security_group_id=module.params["security_group"], security_group_rule=security_group_rule
|
||||
)
|
||||
else:
|
||||
summary = absent_strategy(
|
||||
api=api,
|
||||
security_group_id=module.params['security_group'],
|
||||
security_group_rule=security_group_rule)
|
||||
api=api, security_group_id=module.params["security_group"], security_group_rule=security_group_rule
|
||||
)
|
||||
module.exit_json(**summary)
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = scaleway_argument_spec()
|
||||
argument_spec.update(
|
||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||
region=dict(type='str', required=True, choices=list(SCALEWAY_LOCATION.keys())),
|
||||
protocol=dict(type='str', required=True, choices=['TCP', 'UDP', 'ICMP']),
|
||||
port=dict(type='int', required=True),
|
||||
ip_range=dict(type='str', default='0.0.0.0/0'),
|
||||
direction=dict(type='str', required=True, choices=['inbound', 'outbound']),
|
||||
action=dict(type='str', required=True, choices=['accept', 'drop']),
|
||||
security_group=dict(type='str', required=True),
|
||||
state=dict(type="str", default="present", choices=["absent", "present"]),
|
||||
region=dict(type="str", required=True, choices=list(SCALEWAY_LOCATION.keys())),
|
||||
protocol=dict(type="str", required=True, choices=["TCP", "UDP", "ICMP"]),
|
||||
port=dict(type="int", required=True),
|
||||
ip_range=dict(type="str", default="0.0.0.0/0"),
|
||||
direction=dict(type="str", required=True, choices=["inbound", "outbound"]),
|
||||
action=dict(type="str", required=True, choices=["accept", "drop"]),
|
||||
security_group=dict(type="str", required=True),
|
||||
)
|
||||
module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
|
|
@ -264,5 +273,5 @@ def main():
|
|||
core(module)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue