Last active
January 6, 2022 15:16
-
-
Save prydin/c4d284bd8f7345ca5ef9a46148626211 to your computer and use it in GitHub Desktop.
Prevent power down on change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def handler(context, inputs): | |
# We only care about updates | |
if inputs["eventType"] != "UPDATE_DEPLOYMENT": | |
return inputs | |
result = context.request("/blueprint/api/blueprint-requests/%s/resources-plan?expand=true" % inputs["id"], "GET", "") | |
if result["status"] != 200: | |
raise Exception(result["content"]) | |
envelope = json.loads(result["content"]) | |
print(envelope) | |
for resource in envelope["resources"]: | |
# We only care about machines | |
if not resource["resourceType"].endswith(".Machine"): | |
continue | |
new_props = resource["newProperties"] | |
old_props = resource["oldProperties"] | |
print("Old cpu: %d, new cpu %d" % (old_props["cpuCount"], new_props["cpuCount"])) | |
# If the machine is already powered off, we're fine | |
if new_props["powerState"] == "OFF": | |
continue | |
# If the VM doesn't support hot add AND we're changing the number of CPUs, we need to stop this | |
# deployment, since it would power down the VM. | |
if new_props.get("__cpuHotAddEnabled", "false") != "true" and old_props.get("cpuCount", -1) != new_props.get("cpuCount", -1): | |
raise Exception("Changing number of CPUs on a powered on machine isn't allowed") | |
# If the VM doesn't support hot add AND we're changing the amount of memory, we need to stop this | |
# deployment, since it would power down the VM. | |
if new_props.get("__memoryHotAddEnabled", "false") != "true" and old_props.get("totalMemoryMB", -1) != new_props.get("totalMemoryMB", -1): | |
raise Exception("Changing the amound of memory on a powered on machine isn't allowed") | |
return inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment