Skip to content

Commit

Permalink
split connection into submodules based on params
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Bacho committed Oct 2, 2023
1 parent bc7c09b commit d4ca718
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
25 changes: 22 additions & 3 deletions plugins/module_utils/metal/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ def get_routes(mpc):
equinix_metal.VLANsApi(mpc).find_virtual_networks,
{'id': 'project_id'},
),
('metal_connection', action.LIST): spec_types.Specs(
('metal_connection_project', action.LIST): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).project_list_interconnections,
{'id': 'project_id'},
),
('metal_connection_organization', action.LIST): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).organization_list_interconnections,
{'id': 'organization_id'},
),

# DELETERS
('metal_device', action.DELETE): spec_types.Specs(
Expand Down Expand Up @@ -185,10 +189,25 @@ def get_routes(mpc):
{'id': 'project_id'},
equinix_metal.VirtualNetworkCreateInput,
),
('metal_connection', action.CREATE): spec_types.Specs(
('metal_connection_organization_dedicated', action.CREATE): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).create_organization_interconnection,
{'id': 'organization_id'},
equinix_metal.DedicatedPortCreateInput,
),
('metal_connection_project_dedicated', action.CREATE): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).create_project_interconnection,
{'id': 'project_id'},
equinix_metal.DedicatedPortCreateInput,
),
('metal_connection_project_vlanfabric', action.CREATE): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).create_project_interconnection,
{'id': 'project_id'},
equinix_metal.VlanFabricVcCreateInput,
),
('metal_connection_project_vrf', action.CREATE): spec_types.Specs(
equinix_metal.InterconnectionsApi(mpc).create_project_interconnection,
{'id': 'project_id'},
equinix_metal.InterconnectionCreateInput,
equinix_metal.VrfFabricVcCreateInput,
),

# UPDATERS
Expand Down
2 changes: 1 addition & 1 deletion plugins/module_utils/metal/metal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def get_attribute_mapper(resource_type):
ssh_key_resources = set(['metal_ssh_key', 'metal_project_ssh_key'])
hardware_reservation_resources = set(['metal_project_hardware_reservation', 'metal_hardware_reservation'])
vlan_resources = set(["metal_vlan"])
connection_resources = set(['metal_connection'])
connection_resources = set(['metal_connection', 'metal_connection_project', 'metal_connection_organization'])
if resource_type in device_resources:
return METAL_DEVICE_RESPONSE_ATTRIBUTE_MAP
elif resource_type in project_resources:
Expand Down
26 changes: 24 additions & 2 deletions plugins/modules/metal_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
(10 * GIGA, "10Gbps"),
]
MODULE_NAME = "metal_connection"
DEDICATED_ORG_IC_SUBMODULE = "metal_connection_organization_dedicated"
DEDICATED_PROJECT_IC_SUBMODULE = "metal_connection_project_dedicated"
VLAN_PROJECT_IC_SUBMODULE = "metal_connection_project_vlanfabric"
VRF_PROJECT_IC_SUBMODULE = "metal_connection_project_vrf"

module_spec = dict(
id=SpecField(
Expand Down Expand Up @@ -359,8 +363,9 @@ def main():
tolerate_not_found = state == "absent"
fetched = module.get_by_id(MODULE_NAME, tolerate_not_found)
else:
connection_list_submodule = "metal_connection_project" if module.params.get("project_id") else "metal_connection_organization"
fetched = module.get_one_from_list(
MODULE_NAME,
connection_list_submodule,
["name"],
)

Expand All @@ -377,7 +382,8 @@ def main():
changed = True
else:
if state == "present":
fetched = module.create(MODULE_NAME)
connection_submodule = determine_ic_submodule(module)
fetched = module.create(connection_submodule)
if "id" not in fetched:
module.fail_json(msg=f"UUID not found in {MODULE_NAME} creation response")
changed = True
Expand All @@ -400,5 +406,21 @@ def speed_str_to_int(module):
raise module.fail_json(msg=f"Speed value invalid, allowed values are {[s[1] for s in allowed_speeds]}")


def determine_ic_submodule(module):
type = module.params["type"]
is_project = "project_id" in module.params
vlans = module.params.get("vlans")
vrfs = module.params.get("vrfs")

if is_project:
if type == "dedicated":
return DEDICATED_PROJECT_IC_SUBMODULE
elif vlans:
return VLAN_PROJECT_IC_SUBMODULE
elif vrfs:
return VRF_PROJECT_IC_SUBMODULE
else:
return DEDICATED_ORG_IC_SUBMODULE

if __name__ == "__main__":
main()
12 changes: 9 additions & 3 deletions plugins/modules/metal_connection_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@
),
project_id=SpecField(
type=FieldType.string,
description=["UUID of parent project the connection is scoped to."],
),
description=["ID of the project where the connection is scoped to."],
),
organization_id=SpecField(
type=FieldType.string,
description=["ID of the organization where the connection is scoped to."],
),
)

specdoc_examples = [
Expand Down Expand Up @@ -111,10 +115,12 @@ def main():
module = EquinixModule(
argument_spec=SPECDOC_META.ansible_spec,
is_info=True,
required_one_of=[("project_id", "organization_id")],
)
try:
module.params_syntax_check()
return_value = {"resources": module.get_list("metal_connection")}
connection_submodule = "metal_connection_project" if module.params.get("project_id") else "metal_connection_organization"
return_value = {"resources": module.get_list(connection_submodule)}
except Exception as e:
tr = traceback.format_exc()
module.fail_json(msg=to_native(e), exception=tr)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/inventory
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[testgroup]
testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/usr/bin/python3"
testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/home/sani/code/pythonEnv/ans/bin/python"
1 change: 0 additions & 1 deletion tests/integration/targets/metal_connection/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
- name: list test connections
equinix.cloud.metal_connection_info:
project_id: "{{ project.id }}"
name: "{{ test_name }}"
register: test_connections_list

- assert:
Expand Down

0 comments on commit d4ca718

Please sign in to comment.