Skip to content

Commit 4cf8278

Browse files
authored
add lockdown state and mod (#199)
* add lockdown state and mod * run pre * add examples * remove def for name * fix docs * update docs * tests * reformat * fix conflict * fix whitespace * fix docs
1 parent 16bc3c2 commit 4cf8278

4 files changed

Lines changed: 269 additions & 0 deletions

File tree

src/saltext/vmware/modules/esxi.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,3 +2413,103 @@ def exit_maintenance_mode(host, timeout=0, catch_task_error=True, service_instan
24132413
mode = in_maintenance_mode(host_ref, service_instance)
24142414
mode["changes"] = mode["maintenanceMode"] == "normal"
24152415
return mode
2416+
2417+
2418+
def in_lockdown_mode(host, service_instance=None):
2419+
"""
2420+
Check if host is in lockdown mode.
2421+
2422+
host
2423+
Host IP or HostSystem/ManagedObjectReference (required).
2424+
2425+
service_instance
2426+
Use this vCenter service connection instance instead of creating a new one (optional).
2427+
2428+
.. code-block:: bash
2429+
2430+
salt '*' vmware_esxi.in_lockdown_mode '10.288.6.117'
2431+
"""
2432+
if isinstance(host, vim.HostSystem):
2433+
host_ref = host
2434+
else:
2435+
if service_instance is None:
2436+
service_instance = get_service_instance(opts=__opts__, pillar=__pillar__)
2437+
host_ref = utils_esxi.get_host(host, service_instance)
2438+
mode = "normal"
2439+
if host_ref.config.adminDisabled:
2440+
mode = "inLockdown"
2441+
return {"lockdownMode": mode}
2442+
2443+
2444+
def lockdown_mode(host, catch_task_error=True, service_instance=None):
2445+
"""
2446+
Put host into lockdown mode.
2447+
2448+
host
2449+
Host IP or HostSystem/ManagedObjectReference (required).
2450+
2451+
catch_task_error
2452+
If False and task failed then a salt exception will be thrown (optional).
2453+
2454+
service_instance
2455+
Use this vCenter service connection instance instead of creating a new one (optional).
2456+
2457+
.. code-block:: bash
2458+
2459+
salt '*' vmware_esxi.lockdown_mode '10.288.6.117'
2460+
"""
2461+
if isinstance(host, vim.HostSystem):
2462+
host_ref = host
2463+
else:
2464+
if service_instance is None:
2465+
service_instance = get_service_instance(opts=__opts__, pillar=__pillar__)
2466+
host_ref = utils_esxi.get_host(host, service_instance)
2467+
mode = in_lockdown_mode(host_ref)
2468+
if mode["lockdownMode"] == "inLockdown":
2469+
mode["changes"] = False
2470+
return mode
2471+
try:
2472+
host_ref.EnterLockdownMode()
2473+
except salt.exceptions.SaltException as exc:
2474+
if not catch_task_error:
2475+
raise exc
2476+
mode = in_lockdown_mode(host_ref, service_instance)
2477+
mode["changes"] = mode["lockdownMode"] == "inLockdown"
2478+
return mode
2479+
2480+
2481+
def exit_lockdown_mode(host, catch_task_error=True, service_instance=None):
2482+
"""
2483+
Put host out of lockdown mode.
2484+
2485+
host
2486+
Host IP or HostSystem/ManagedObjectReference (required).
2487+
2488+
catch_task_error
2489+
If False and task failed then a salt exception will be thrown (optional).
2490+
2491+
service_instance
2492+
Use this vCenter service connection instance instead of creating a new one (optional).
2493+
2494+
.. code-block:: bash
2495+
2496+
salt '*' vmware_esxi.exit_lockdown_mode '10.288.6.117'
2497+
"""
2498+
if isinstance(host, vim.HostSystem):
2499+
host_ref = host
2500+
else:
2501+
if service_instance is None:
2502+
service_instance = get_service_instance(opts=__opts__, pillar=__pillar__)
2503+
host_ref = utils_esxi.get_host(host, service_instance)
2504+
mode = in_lockdown_mode(host_ref)
2505+
if mode["lockdownMode"] == "normal":
2506+
mode["changes"] = False
2507+
return mode
2508+
try:
2509+
host_ref.ExitLockdownMode()
2510+
except salt.exceptions.SaltException as exc:
2511+
if not catch_task_error:
2512+
raise exc
2513+
mode = in_lockdown_mode(host_ref, service_instance)
2514+
mode["changes"] = mode["lockdownMode"] == "normal"
2515+
return mode

src/saltext/vmware/states/esxi.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,111 @@ def maintenance_mode(
739739
"comment"
740740
] = f"Failed to put host {str(name)} in {'Maintenance' if enter_maintenance_mode else 'Normal'} mode."
741741
return ret
742+
743+
744+
def lockdown_mode(
745+
name,
746+
enter_lockdown_mode,
747+
datacenter_name=None,
748+
cluster_name=None,
749+
get_all_hosts=False,
750+
service_instance=None,
751+
):
752+
"""
753+
Pust a hosts into or out of lockdown.
754+
755+
name
756+
IP of single host or list of host_names. If wanting to get a cluster just past an empty list (required).
757+
758+
enter_lockdown_mode
759+
If True, put host into lockdown mode.
760+
If False, put host out of lockdown mode (required)
761+
762+
datacenter_name
763+
The datacenter name. Default is None (optional).
764+
765+
host_names
766+
The host_names to be retrieved. Default is None (optional).
767+
768+
cluster_name
769+
The cluster name - used to restrict the hosts retrieved. Only used if
770+
the datacenter is set. This argument is optional (optional).
771+
772+
get_all_hosts
773+
Specifies whether to retrieve all hosts in the container.
774+
Default value is False (optional).
775+
776+
service_instance
777+
The Service Instance Object from which to obtain the hosts (optional).
778+
779+
.. code-block:: bash
780+
781+
salt '*' vmware_esxi.lockdown_mode '10.288.6.117'
782+
.. code-block:: yaml
783+
784+
Lockdown Mode:
785+
vmware_esxi.lockdown_mode:
786+
- host: '10.288.6.117'
787+
- enter_lockdown_mode: true
788+
"""
789+
ret = {"name": name, "changes": {}, "result": True, "comment": ""}
790+
if not isinstance(name, str):
791+
host_refs = utils_esxi.get_hosts(
792+
service_instance=service_instance,
793+
datacenter_name=datacenter_name,
794+
host_names=name,
795+
cluster_name=cluster_name,
796+
get_all_hosts=get_all_hosts,
797+
)
798+
else:
799+
if isinstance(name, vim.HostSystem):
800+
host_refs = (name,)
801+
else:
802+
if service_instance is None:
803+
service_instance = get_service_instance(opts=__opts__, pillar=__pillar__)
804+
host_refs = (utils_esxi.get_host(name, service_instance),)
805+
806+
for ref in host_refs:
807+
# check that host is not all ready in lock state.
808+
host_state = __salt__["vmware_esxi.in_lockdown_mode"](
809+
host=ref, service_instance=service_instance
810+
)
811+
if (host_state["lockdownMode"] == "inLockdown") == enter_lockdown_mode:
812+
ret[
813+
"comment"
814+
] += f"{ref.name} already in {'Lockdown' if enter_lockdown_mode else 'Normal'} mode.\n"
815+
continue
816+
817+
if __opts__["test"]:
818+
ret["result"] = None
819+
ret["changes"].setdefault("new", []).append(
820+
f"{ref.name} will enter {'Lockdown' if enter_lockdown_mode else 'Normal'} mode."
821+
)
822+
continue
823+
824+
if enter_lockdown_mode:
825+
host_state = __salt__["vmware_esxi.lockdown_mode"](
826+
host=ref,
827+
catch_task_error=True,
828+
service_instance=service_instance,
829+
)
830+
else:
831+
host_state = __salt__["vmware_esxi.exit_lockdown_mode"](
832+
host=ref, catch_task_error=True, service_instance=service_instance
833+
)
834+
ref_results = (host_state["lockdownMode"] == "inLockdown") == enter_lockdown_mode
835+
if ret["result"]:
836+
ret["result"] = ref_results
837+
if ref_results:
838+
ret["changes"].setdefault("new", []).append(
839+
f"{ref.name} entered {'Lockdown' if enter_lockdown_mode else 'Normal'} mode."
840+
)
841+
else:
842+
ret[
843+
"comment"
844+
] += f"Failed to put host {ref.name} in {'Lockdown' if enter_lockdown_mode else 'Normal'} mode.\n"
845+
if ret["result"]:
846+
ret["comment"] += f"Task was successfully!\n"
847+
elif ret["result"] is None:
848+
ret["comment"] += "These options are set to change."
849+
return ret

tests/integration/modules/test_esxi.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,29 @@ def test_maintenance_mode(service_instance):
684684

685685
ret = esxi.in_maintenance_mode(host, service_instance)
686686
assert ret == dict(maintenanceMode="normal")
687+
688+
689+
def test_lockdown_mode(service_instance):
690+
hosts = list(esxi.get(service_instance=service_instance))
691+
assert hosts
692+
host = hosts[0]
693+
ret = esxi.in_lockdown_mode(host, service_instance)
694+
assert ret == dict(lockdownMode="normal")
695+
696+
try:
697+
for i in range(3):
698+
ret = esxi.lockdown_mode(host, service_instance=service_instance)
699+
assert ret == dict(lockdownMode="inLockdown", changes=not i)
700+
except Exception as e:
701+
esxi.exit_lockdown_mode(host, service_instance=service_instance)
702+
raise e
703+
704+
ret = esxi.in_lockdown_mode(host, service_instance)
705+
assert ret == dict(lockdownMode="inLockdown")
706+
707+
for i in range(3):
708+
ret = esxi.exit_lockdown_mode(host, service_instance=service_instance)
709+
assert ret == dict(lockdownMode="normal", changes=not i)
710+
711+
ret = esxi.in_lockdown_mode(host, service_instance)
712+
assert ret == dict(lockdownMode="normal")

tests/integration/states/test_esxi.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,38 @@ def test_maintenance_mode_dry_run(service_instance, dry_run):
312312
assert ret["result"] is None
313313
assert ret["changes"]
314314
assert ret["comment"] == "These options are set to change."
315+
316+
317+
def test_lockdown_mode(service_instance):
318+
hosts = list(esxi_mod.get(service_instance=service_instance))
319+
assert hosts
320+
host = hosts[0]
321+
try:
322+
for i in range(3):
323+
ret = esxi.lockdown_mode(host, True, service_instance=service_instance)
324+
assert ret["result"]
325+
if not i:
326+
assert ret["changes"]
327+
else:
328+
assert not ret["changes"]
329+
except Exception as e:
330+
esxi.lockdown_mode(host, False, service_instance=service_instance)
331+
raise e
332+
333+
for i in range(3):
334+
ret = esxi.lockdown_mode(host, False, service_instance=service_instance)
335+
assert ret["result"]
336+
if not i:
337+
assert ret["changes"]
338+
else:
339+
assert not ret["changes"]
340+
341+
342+
def test_lockdown_mode_dry_run(service_instance, dry_run):
343+
hosts = list(esxi_mod.get(service_instance=service_instance))
344+
assert hosts
345+
host = hosts[0]
346+
ret = esxi.lockdown_mode(host, True, service_instance=service_instance)
347+
assert ret["result"] is None
348+
assert ret["changes"]
349+
assert ret["comment"] == "These options are set to change."

0 commit comments

Comments
 (0)