Skip to content

Commit

Permalink
Merge pull request #94 from nutanix/feat/floating_ips
Browse files Browse the repository at this point in the history
Feat/floating ips
  • Loading branch information
Gevorg-Khachatryan-97 authored Feb 15, 2022
2 parents 12abe4f + 79192b5 commit b84660e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
35 changes: 20 additions & 15 deletions plugins/modules/ntnx_floating_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
type: bool
required: false
default: True
floating_ip_uuid:
fip_uuid:
description: floating_ip UUID
type: str
external_subnet:
Expand Down Expand Up @@ -174,7 +174,7 @@
"uuid": "d34a85bc-67c5-4888-892c-76f51b1935fd"
}
spec:
description: An intentful representation of a VPC spec
description: An intentful representation of a Floating ip spec
returned: always
type: dict
sample: {
Expand Down Expand Up @@ -205,7 +205,7 @@
},
"state": "COMPLETE"
}
floating_ip_uuid:
fip_uuid:
description: The created Floating ip uuid
returned: always
type: str
Expand All @@ -228,6 +228,7 @@ def get_module_spec():
mutually_exclusive = [("name", "uuid")]
entity_by_spec = dict(name=dict(type="str"), uuid=dict(type="str"))
module_args = dict(
fip_uuid=dict(type="str", required=False),
external_subnet=dict(
type="dict", options=entity_by_spec, mutually_exclusive=mutually_exclusive
),
Expand All @@ -238,7 +239,6 @@ def get_module_spec():
type="dict", options=entity_by_spec, mutually_exclusive=mutually_exclusive
),
private_ip=dict(type="str"),
floating_ip_uuid=dict(type="str"),
)

return module_args
Expand All @@ -261,34 +261,34 @@ def create_floating_ip(module, result):
result["response"] = resp
module.fail_json(msg="Failed creating floating_ip", **result)

floating_ip_uuid = resp["metadata"]["uuid"]
fip_uuid = resp["metadata"]["uuid"]
result["changed"] = True
result["response"] = resp
result["floating_ip_uuid"] = floating_ip_uuid
result["fip_uuid"] = fip_uuid
result["task_uuid"] = resp["status"]["execution_context"]["task_uuid"]

if module.params.get("wait"):
wait_for_task_completion(module, result)
resp, tmp = floating_ip.read(floating_ip_uuid)
resp, tmp = floating_ip.read(fip_uuid)
result["response"] = resp


def delete_floating_ip(module, result):
floating_ip_uuid = module.params["floating_ip_uuid"]
if not floating_ip_uuid:
result["error"] = "Missing parameter floating_ip_uuid in playbook"
fip_uuid = module.params["fip_uuid"]
if not fip_uuid:
result["error"] = "Missing parameter fip_uuid in playbook"
module.fail_json(msg="Failed deleting floating_ip", **result)

floating_ip = FloatingIP(module)
resp, status = floating_ip.delete(floating_ip_uuid)
resp, status = floating_ip.delete(fip_uuid)
if status["error"]:
result["error"] = status["error"]
result["response"] = resp
module.fail_json(msg="Failed deleting floating_ip", **result)

result["changed"] = True
result["response"] = resp
result["floating_ip_uuid"] = floating_ip_uuid
result["fip_uuid"] = fip_uuid
result["task_uuid"] = resp["status"]["execution_context"]["task_uuid"]

if module.params.get("wait"):
Expand All @@ -310,18 +310,23 @@ def run_module():
module = BaseModule(
argument_spec=get_module_spec(),
supports_check_mode=True,
mutually_exclusive=[("vm", "vpc")],
mutually_exclusive=[
("vm", "vpc"),
("fip_uuid", "external_subnet"),
("fip_uuid", "vm"),
("fip_uuid", "vpc"),
],
required_if=[
("state", "present", ("external_subnet",)),
("state", "absent", ("floating_ip_uuid",)),
("state", "absent", ("fip_uuid",)),
],
)
remove_param_with_none_value(module.params)
result = {
"changed": False,
"error": None,
"response": None,
"floating_ip_uuid": None,
"fip_uuid": None,
"task_uuid": None,
}
state = module.params["state"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: create Floating IP with External Subnet Name
- name: create Floating IP with External Subnet uuid
ntnx_floating_ips:
validate_certs: False
state: present
Expand All @@ -10,8 +10,7 @@
uuid: "{{external_subnet.subnet_uuiid}}"
register: result
ignore_errors: True
- debug:
var: result

- name: Creation Status
assert:
that:
Expand All @@ -21,7 +20,7 @@
success_msg: " Floating ip created successfully "

- set_fact:
todelete: "{{ todelete + [ result.floating_ip_uuid ] }}"
todelete: "{{ todelete + [ result.fip_uuid ] }}"


- name: create Floating IP with vpc Name with external subnet uuid
Expand Down Expand Up @@ -55,10 +54,11 @@
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
validate_certs: false
floating_ip_uuid: "{{result.floating_ip_uuid}}"
fip_uuid: "{{result.fip_uuid}}"
register: result
ignore_errors: True


- name: create Floating IP with External Subnet with vm
ntnx_floating_ips:
validate_certs: False
Expand All @@ -83,7 +83,7 @@
success_msg: " Floating ip created successfully with vm "

- set_fact:
todelete: "{{ todelete + [ result.floating_ip_uuid ] }}"
todelete: "{{ todelete + [ result.fip_uuid ] }}"


- name: Delete all Created Floating ips
Expand All @@ -93,7 +93,7 @@
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
validate_certs: false
floating_ip_uuid: "{{ item }}"
fip_uuid: "{{ item }}"
register: result
loop: "{{ todelete }}"
ignore_errors: True
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ vm:
vm_uuid: 83c9c094-2b18-4e65-bf6f-234640e07bc0
private_ip: 192.168.1.153
todelete: []
vm_subnet_name: ext_102
vm_subnet_name: Prod-NAT

0 comments on commit b84660e

Please sign in to comment.