Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update support to ntnx_subnets #424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions plugins/modules/ntnx_subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
type: str
sample: "ae015bd7-eada-4e23-a225-4f02f9b2b21e"
"""

from ..module_utils import utils
from ..module_utils.base_module import BaseModule # noqa: E402
from ..module_utils.prism.subnets import Subnet # noqa: E402
from ..module_utils.prism.tasks import Task # noqa: E402
Expand Down Expand Up @@ -550,6 +550,46 @@ def get_module_spec():

return module_args

def update_subnet(module, result):
subnet = Subnet(module)
subnet_uuid = module.params.get("subnet_uuid")
result["subnet_uuid"] = subnet_uuid

# read the current state of subnet
resp = subnet.read(subnet_uuid)
utils.strip_extra_attrs(resp["status"], resp["spec"])
resp["spec"] = resp.pop("status")

# new spec for updating subnet
update_spec, error = subnet.get_spec(resp)
if error:
result["error"] = error
module.fail_json(msg="Failed generating Subnet update spec", **result)

# check for idempotency
if resp == update_spec:
result["skipped"] = True
module.exit_json(
msg="Nothing to change. Refer docs to check for fields which can be updated"
)

if module.check_mode:
result["response"] = update_spec
return

# update subnet
resp = subnet.update(update_spec, uuid=subnet_uuid)
task_uuid = resp["status"]["execution_context"]["task_uuid"]

# wait for subnet update to finish
if module.params.get("wait"):
task = Task(module)
task.wait_for_completion(task_uuid)
# get the subnet
resp = subnet.read(subnet_uuid)

result["changed"] = True
result["response"] = resp

def create_subnet(module, result):
subnet = Subnet(module)
Expand Down Expand Up @@ -618,7 +658,10 @@ def run_module():
}
state = module.params["state"]
if state == "present":
create_subnet(module, result)
if module.params.get("subnet_uuid"):
update_subnet(module, result)
else:
create_subnet(module, result)
elif state == "absent":
delete_subnet(module, result)

Expand Down