Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# Usage:
# $ sh ./set-vm-power-state.sh ./prism-login.conf vm01 OFF
# $ sh ./set-vm-power-state.sh ./prism-login.conf vm01 ON
PRISM_LOGIN_FILE=$1
VM_NAME=$2
VM_POWER_STATE=$3
. ${PRISM_LOGIN_FILE}
VM_JSON=vm-${VM_NAME}.json
VM_UUID=`curl -k -s -X POST \
-H "Content-Type: application/json" \
-u "${PRISM_USER}:${PRISM_PASS}" \
-d "{}" \
"https://${PRISM_ADDR}:9440/api/nutanix/v3/vms/list" | jq --monochrome-output -C '.entities[] | select(.spec.name == "'${VM_NAME}'") | .metadata.uuid'`
VM_UUID=`echo $VM_UUID | sed s/\"//g`
echo "VM_UUID: $VM_UUID"
curl -k -s -X GET \
-H "Content-Type: application/json" \
-u "${PRISM_USER}:${PRISM_PASS}" \
"https://${PRISM_ADDR}:9440/api/nutanix/v3/vms/${VM_UUID}" | jq --monochrome-output -C '{spec: .spec, metadata: .metadata}' > ${VM_JSON}
cat ${VM_JSON} | jq --monochrome-output '.spec.resources.power_state |= "'${VM_POWER_STATE}'"' > ${VM_JSON}.power-state
curl -k -s -X PUT \
-H "Content-Type: application/json" \
-u "${PRISM_USER}:${PRISM_PASS}" \
-d @${VM_JSON}.power-state \
"https://${PRISM_ADDR}:9440/api/nutanix/v3/vms/${VM_UUID}" | jq --monochrome-output -C '.spec.resources.power_state'
@gowatana

This comment has been minimized.

Show comment
Hide comment
@gowatana

gowatana Jul 31, 2018

下記の投稿むけ。

Nutanix REST API v3 で VM を停止 / 起動してみる。
http://blog.ntnx.jp/entry/2018/08/01/074640

Owner

gowatana commented Jul 31, 2018

下記の投稿むけ。

Nutanix REST API v3 で VM を停止 / 起動してみる。
http://blog.ntnx.jp/entry/2018/08/01/074640

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment