+def action_vm(action_type, server, vimId, tenantId):
+ param = {}
+ if action_type == ACTION_TYPE.START:
+ param = {
+ "os-start": None,
+ }
+ elif action_type == ACTION_TYPE.STOP:
+ param = {
+ "os-stop": None,
+ }
+ elif action_type == ACTION_TYPE.REBOOT:
+ param = {
+ "reboot": {}
+ }
+ if server["status"] == "ACTIVE":
+ param["reboot"]["type"] = "SOFT"
+ else:
+ param["reboot"]["type"] = "HARD"
+ res_id = server["id"]
+ api.action_vm(vimId, tenantId, res_id, param)
+
+
+# TODO Have to check if the resources should be started and stopped in some order.
+def operate_vim_res(data, changeStateTo, stopType, gracefulStopTimeout, do_notify_op):
+ for res in ignore_case_get(data, "vm"):
+ try:
+ if changeStateTo == "STARTED":
+ action_vm(ACTION_TYPE.START, res, res["vim_id"], res["tenant_id"])
+ do_notify_op("ACTIVE", res["id"])
+ elif changeStateTo == "STOPPED":
+ if stopType == "GRACEFUL":
+ if gracefulStopTimeout > 60:
+ gracefulStopTimeout = 60
+ time.sleep(gracefulStopTimeout)
+ action_vm(ACTION_TYPE.STOP, res, res["vim_id"], res["tenant_id"])
+ # TODO check if the we should poll getvm to get the status or the action_vm api
+ # successful return should suffice to mark vm as Active/Inactive
+ do_notify_op("INACTIVE", res["id"])
+ except VimException as e:
+ # TODO Have to update database appropriately on failure
+ logger.error("Failed to Heal %s(%s)", RES_VM, res["res_id"])
+ logger.error("%s:%s", e.http_code, e.message)
+
+