mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-16 17:01:30 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -129,11 +129,18 @@ enable_shared_snat:
|
|||
# Imports
|
||||
###############################################################################
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.hwc_utils import (Config, HwcClientException,
|
||||
HwcClientException404, HwcModule,
|
||||
are_different_dicts, is_empty_value,
|
||||
wait_to_finish, get_region,
|
||||
build_path, navigate_value)
|
||||
from ansible_collections.community.general.plugins.module_utils.hwc_utils import (
|
||||
Config,
|
||||
HwcClientException,
|
||||
HwcClientException404,
|
||||
HwcModule,
|
||||
are_different_dicts,
|
||||
is_empty_value,
|
||||
wait_to_finish,
|
||||
get_region,
|
||||
build_path,
|
||||
navigate_value,
|
||||
)
|
||||
import re
|
||||
|
||||
###############################################################################
|
||||
|
|
@ -146,44 +153,47 @@ def main():
|
|||
|
||||
module = HwcModule(
|
||||
argument_spec=dict(
|
||||
state=dict(
|
||||
default='present', choices=['present', 'absent'], type='str'),
|
||||
timeouts=dict(type='dict', options=dict(
|
||||
create=dict(default='15m', type='str'),
|
||||
update=dict(default='15m', type='str'),
|
||||
delete=dict(default='15m', type='str'),
|
||||
), default=dict()),
|
||||
name=dict(required=True, type='str'),
|
||||
cidr=dict(required=True, type='str')
|
||||
state=dict(default="present", choices=["present", "absent"], type="str"),
|
||||
timeouts=dict(
|
||||
type="dict",
|
||||
options=dict(
|
||||
create=dict(default="15m", type="str"),
|
||||
update=dict(default="15m", type="str"),
|
||||
delete=dict(default="15m", type="str"),
|
||||
),
|
||||
default=dict(),
|
||||
),
|
||||
name=dict(required=True, type="str"),
|
||||
cidr=dict(required=True, type="str"),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
)
|
||||
config = Config(module, 'vpc')
|
||||
config = Config(module, "vpc")
|
||||
|
||||
state = module.params['state']
|
||||
state = module.params["state"]
|
||||
|
||||
if (not module.params.get("id")) and module.params.get("name"):
|
||||
module.params['id'] = get_id_by_name(config)
|
||||
module.params["id"] = get_id_by_name(config)
|
||||
|
||||
fetch = None
|
||||
link = self_link(module)
|
||||
# the link will include Nones if required format parameters are missed
|
||||
if not re.search('/None/|/None$', link):
|
||||
if not re.search("/None/|/None$", link):
|
||||
client = config.client(get_region(module), "vpc", "project")
|
||||
fetch = fetch_resource(module, client, link)
|
||||
if fetch:
|
||||
fetch = fetch.get('vpc')
|
||||
fetch = fetch.get("vpc")
|
||||
changed = False
|
||||
|
||||
if fetch:
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
expect = _get_editable_properties(module)
|
||||
current_state = response_to_hash(module, fetch)
|
||||
current = {"cidr": current_state["cidr"]}
|
||||
if are_different_dicts(expect, current):
|
||||
if not module.check_mode:
|
||||
fetch = update(config, self_link(module))
|
||||
fetch = response_to_hash(module, fetch.get('vpc'))
|
||||
fetch = response_to_hash(module, fetch.get("vpc"))
|
||||
changed = True
|
||||
else:
|
||||
fetch = current_state
|
||||
|
|
@ -193,15 +203,15 @@ def main():
|
|||
fetch = {}
|
||||
changed = True
|
||||
else:
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
if not module.check_mode:
|
||||
fetch = create(config, "vpcs")
|
||||
fetch = response_to_hash(module, fetch.get('vpc'))
|
||||
fetch = response_to_hash(module, fetch.get("vpc"))
|
||||
changed = True
|
||||
else:
|
||||
fetch = {}
|
||||
|
||||
fetch.update({'changed': changed})
|
||||
fetch.update({"changed": changed})
|
||||
|
||||
module.exit_json(**fetch)
|
||||
|
||||
|
|
@ -217,14 +227,14 @@ def create(config, link):
|
|||
msg = f"module(hwc_network_vpc): error creating resource, error: {ex}"
|
||||
module.fail_json(msg=msg)
|
||||
|
||||
wait_done = wait_for_operation(config, 'create', r)
|
||||
wait_done = wait_for_operation(config, "create", r)
|
||||
v = ""
|
||||
try:
|
||||
v = navigate_value(wait_done, ['vpc', 'id'])
|
||||
v = navigate_value(wait_done, ["vpc", "id"])
|
||||
except Exception as ex:
|
||||
module.fail_json(msg=str(ex))
|
||||
|
||||
url = build_path(module, 'vpcs/{op_id}', {'op_id': v})
|
||||
url = build_path(module, "vpcs/{op_id}", {"op_id": v})
|
||||
return fetch_resource(module, client, url)
|
||||
|
||||
|
||||
|
|
@ -239,7 +249,7 @@ def update(config, link):
|
|||
msg = f"module(hwc_network_vpc): error updating resource, error: {ex}"
|
||||
module.fail_json(msg=msg)
|
||||
|
||||
wait_for_operation(config, 'update', r)
|
||||
wait_for_operation(config, "update", r)
|
||||
|
||||
return fetch_resource(module, client, link)
|
||||
|
||||
|
|
@ -283,22 +293,18 @@ def get_id_by_name(config):
|
|||
pass
|
||||
if r is None:
|
||||
return None
|
||||
r = r.get('vpcs', [])
|
||||
ids = [
|
||||
i.get('id') for i in r if i.get('name', '') == name
|
||||
]
|
||||
r = r.get("vpcs", [])
|
||||
ids = [i.get("id") for i in r if i.get("name", "") == name]
|
||||
if not ids:
|
||||
return None
|
||||
elif len(ids) == 1:
|
||||
return ids[0]
|
||||
else:
|
||||
module.fail_json(
|
||||
msg="Multiple resources with same name are found.")
|
||||
module.fail_json(msg="Multiple resources with same name are found.")
|
||||
elif none_values:
|
||||
module.fail_json(
|
||||
msg="Can not find id by name because url includes None.")
|
||||
module.fail_json(msg="Can not find id by name because url includes None.")
|
||||
else:
|
||||
p = {'marker': ''}
|
||||
p = {"marker": ""}
|
||||
ids = set()
|
||||
while True:
|
||||
r = None
|
||||
|
|
@ -308,17 +314,16 @@ def get_id_by_name(config):
|
|||
pass
|
||||
if r is None:
|
||||
break
|
||||
r = r.get('vpcs', [])
|
||||
r = r.get("vpcs", [])
|
||||
if r == []:
|
||||
break
|
||||
for i in r:
|
||||
if i.get('name') == name:
|
||||
ids.add(i.get('id'))
|
||||
if i.get("name") == name:
|
||||
ids.add(i.get("id"))
|
||||
if len(ids) >= 2:
|
||||
module.fail_json(
|
||||
msg="Multiple resources with same name are found.")
|
||||
module.fail_json(msg="Multiple resources with same name are found.")
|
||||
|
||||
p['marker'] = r[-1].get('id')
|
||||
p["marker"] = r[-1].get("id")
|
||||
|
||||
return ids.pop() if ids else None
|
||||
|
||||
|
|
@ -330,11 +335,11 @@ def self_link(module):
|
|||
def resource_to_create(module):
|
||||
params = dict()
|
||||
|
||||
v = module.params.get('cidr')
|
||||
v = module.params.get("cidr")
|
||||
if not is_empty_value(v):
|
||||
params["cidr"] = v
|
||||
|
||||
v = module.params.get('name')
|
||||
v = module.params.get("name")
|
||||
if not is_empty_value(v):
|
||||
params["name"] = v
|
||||
|
||||
|
|
@ -349,7 +354,7 @@ def resource_to_create(module):
|
|||
def resource_to_update(module):
|
||||
params = dict()
|
||||
|
||||
v = module.params.get('cidr')
|
||||
v = module.params.get("cidr")
|
||||
if not is_empty_value(v):
|
||||
params["cidr"] = v
|
||||
|
||||
|
|
@ -368,17 +373,16 @@ def _get_editable_properties(module):
|
|||
|
||||
|
||||
def response_to_hash(module, response):
|
||||
""" Remove unnecessary properties from the response.
|
||||
This is for doing comparisons with Ansible's current parameters.
|
||||
"""Remove unnecessary properties from the response.
|
||||
This is for doing comparisons with Ansible's current parameters.
|
||||
"""
|
||||
return {
|
||||
'id': response.get('id'),
|
||||
'name': response.get('name'),
|
||||
'cidr': response.get('cidr'),
|
||||
'status': response.get('status'),
|
||||
'routes': VpcRoutesArray(
|
||||
response.get('routes', []), module).from_response(),
|
||||
'enable_shared_snat': response.get('enable_shared_snat')
|
||||
"id": response.get("id"),
|
||||
"name": response.get("name"),
|
||||
"cidr": response.get("cidr"),
|
||||
"status": response.get("status"),
|
||||
"routes": VpcRoutesArray(response.get("routes", []), module).from_response(),
|
||||
"enable_shared_snat": response.get("enable_shared_snat"),
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -386,29 +390,27 @@ def wait_for_operation(config, op_type, op_result):
|
|||
module = config.module
|
||||
op_id = ""
|
||||
try:
|
||||
op_id = navigate_value(op_result, ['vpc', 'id'])
|
||||
op_id = navigate_value(op_result, ["vpc", "id"])
|
||||
except Exception as ex:
|
||||
module.fail_json(msg=str(ex))
|
||||
|
||||
url = build_path(module, "vpcs/{op_id}", {'op_id': op_id})
|
||||
timeout = 60 * int(module.params['timeouts'][op_type].rstrip('m'))
|
||||
url = build_path(module, "vpcs/{op_id}", {"op_id": op_id})
|
||||
timeout = 60 * int(module.params["timeouts"][op_type].rstrip("m"))
|
||||
states = {
|
||||
'create': {
|
||||
'allowed': ['CREATING', 'DONW', 'OK'],
|
||||
'complete': ['OK'],
|
||||
"create": {
|
||||
"allowed": ["CREATING", "DONW", "OK"],
|
||||
"complete": ["OK"],
|
||||
},
|
||||
"update": {
|
||||
"allowed": ["PENDING_UPDATE", "DONW", "OK"],
|
||||
"complete": ["OK"],
|
||||
},
|
||||
'update': {
|
||||
'allowed': ['PENDING_UPDATE', 'DONW', 'OK'],
|
||||
'complete': ['OK'],
|
||||
}
|
||||
}
|
||||
|
||||
return wait_for_completion(url, timeout, states[op_type]['allowed'],
|
||||
states[op_type]['complete'], config)
|
||||
return wait_for_completion(url, timeout, states[op_type]["allowed"], states[op_type]["complete"], config)
|
||||
|
||||
|
||||
def wait_for_completion(op_uri, timeout, allowed_states,
|
||||
complete_states, config):
|
||||
def wait_for_completion(op_uri, timeout, allowed_states, complete_states, config):
|
||||
module = config.module
|
||||
client = config.client(get_region(module), "vpc", "project")
|
||||
|
||||
|
|
@ -421,21 +423,19 @@ def wait_for_completion(op_uri, timeout, allowed_states,
|
|||
|
||||
status = ""
|
||||
try:
|
||||
status = navigate_value(r, ['vpc', 'status'])
|
||||
status = navigate_value(r, ["vpc", "status"])
|
||||
except Exception:
|
||||
return None, ""
|
||||
|
||||
return r, status
|
||||
|
||||
try:
|
||||
return wait_to_finish(complete_states, allowed_states,
|
||||
_refresh_status, timeout)
|
||||
return wait_to_finish(complete_states, allowed_states, _refresh_status, timeout)
|
||||
except Exception as ex:
|
||||
module.fail_json(msg=str(ex))
|
||||
|
||||
|
||||
def wait_for_delete(module, client, link):
|
||||
|
||||
def _refresh_status():
|
||||
try:
|
||||
client.get(link)
|
||||
|
|
@ -447,7 +447,7 @@ def wait_for_delete(module, client, link):
|
|||
|
||||
return True, "Pending"
|
||||
|
||||
timeout = 60 * int(module.params['timeouts']['delete'].rstrip('m'))
|
||||
timeout = 60 * int(module.params["timeouts"]["delete"].rstrip("m"))
|
||||
try:
|
||||
return wait_to_finish(["Done"], ["Pending"], _refresh_status, timeout)
|
||||
except Exception as ex:
|
||||
|
|
@ -475,17 +475,11 @@ class VpcRoutesArray:
|
|||
return items
|
||||
|
||||
def _request_for_item(self, item):
|
||||
return {
|
||||
'destination': item.get('destination'),
|
||||
'nexthop': item.get('next_hop')
|
||||
}
|
||||
return {"destination": item.get("destination"), "nexthop": item.get("next_hop")}
|
||||
|
||||
def _response_from_item(self, item):
|
||||
return {
|
||||
'destination': item.get('destination'),
|
||||
'next_hop': item.get('nexthop')
|
||||
}
|
||||
return {"destination": item.get("destination"), "next_hop": item.get("nexthop")}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue