Skip to content

Commit

Permalink
VI/JSON Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
William Lam committed Nov 17, 2023
1 parent 5a8ff77 commit ab9846d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
28 changes: 28 additions & 0 deletions shell/create_snapshot_for_vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
VC='vcsa.primp-industries.local'
VC_USERNAME='[email protected]'
VC_PASSWORD='VMware1!'
VC_API_RELEASE='8.0.1.0'
VM_NAME="esx-1.0"

# vCenter REST API
VCREST_API_SESSION_ID=$(curl -s -u "${VC_USERNAME}:${VC_PASSWORD}" -X POST "https://${VC}/api/session" | jq -j)
VM_MOREF=$(curl -s -H "vmware-api-session-id: ${VCREST_API_SESSION_ID}" -X GET "https://${VC}/api/vcenter/vm?names=${VM_NAME}" | jq -r '.[0].vm')

# vCenter VI JSON API
SESSION_MANAGER_MOID=$(curl -s https://$VC/sdk/vim25/${VC_API_RELEASE}/ServiceInstance/ServiceInstance/content | jq -j .sessionManager.value)
VIJSON_API_SESSION_ID=$(curl -s -o /dev/null -D - "https://$VC/sdk/vim25/${VC_API_RELEASE}/SessionManager/$SESSION_MANAGER_MOID/Login" -H 'Content-Type: application/json' -d "{\"userName\":\"${VC_USERNAME}\", \"password\": \"${VC_PASSWORD}\"}" | awk 'BEGIN {FS=": "}/^vmware-api-session-id/{print $2}')

# Create Snapshot spec
cat > snapshot_spec.json <<EOF
{
"description": "Test Snapshot",
"memory": false,
"name": "test-snapshot-1"
}
EOF

# Create Snapshot
curl -s -H "vmware-api-session-id: ${VIJSON_API_SESSION_ID}" -H "Content-Type: application/json" -X POST "https://$VC/sdk/vim25/${VC_API_RELEASE}/VirtualMachine/${VM_MOREF}/CreateSnapshotEx_Task" -d@snapshot_spec.json

# List Snapshots
curl -s -H "vmware-api-session-id: ${VIJSON_API_SESSION_ID}" -H "Content-Type: application/json" -X GET "https://$VC/sdk/vim25/${VC_API_RELEASE}/VirtualMachine/${VM_MOREF}/snapshot"
18 changes: 18 additions & 0 deletions shell/list_advanced_settings_for_vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
VC='vcsa.primp-industries.local'
VC_USERNAME='[email protected]'
VC_PASSWORD='VMware1!'
VC_API_RELEASE='8.0.1.0'
VM_NAME="esx-1.0"

# vCenter REST API

VCREST_API_SESSION_ID=$(curl -s -u "${VC_USERNAME}:${VC_PASSWORD}" -X POST "https://${VC}/api/session" | jq -j)
VM_MOREF=$(curl -s -H "vmware-api-session-id: ${VCREST_API_SESSION_ID}" -X GET "https://${VC}/api/vcenter/vm?names=${VM_NAME}" | jq -r '.[0].vm')

# vCenter VI JSON API

SESSION_MANAGER_MOID=$(curl -s https://$VC/sdk/vim25/${VC_API_RELEASE}/ServiceInstance/ServiceInstance/content | jq -j .sessionManager.value)
VIJSON_API_SESSION_ID=$(curl -s -o /dev/null -D - "https://$VC/sdk/vim25/${VC_API_RELEASE}/SessionManager/$SESSION_MANAGER_MOID/Login" -H 'Content-Type: application/json' -d "{\"userName\":\"${VC_USERNAME}\", \"password\": \"${VC_PASSWORD}\"}" | awk 'BEGIN {FS=": "}/^vmware-api-session-id/{print $2}')

# List VM Advanced Setings
curl -s -H "vmware-api-session-id: ${VIJSON_API_SESSION_ID}" -X GET "https://$VC/sdk/vim25/${VC_API_RELEASE}/VirtualMachine/${VM_MOREF}/config" | jq -r .extraConfig
22 changes: 22 additions & 0 deletions shell/list_custom_attributes_for_vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
VC='vcsa.primp-industries.local'
VC_USERNAME='[email protected]'
VC_PASSWORD='VMware1!'
VC_API_RELEASE='8.0.1.0'
VM_NAME="esx-1.0"

# vCenter REST API

VCREST_API_SESSION_ID=$(curl -s -u "${VC_USERNAME}:${VC_PASSWORD}" -X POST "https://${VC}/api/session" | jq -j)
VM_MOREF=$(curl -s -H "vmware-api-session-id: ${VCREST_API_SESSION_ID}" -X GET "https://${VC}/api/vcenter/vm?names=${VM_NAME}" | jq -r '.[0].vm')

# vCenter VI JSON API

SESSION_MANAGER_MOID=$(curl -s https://$VC/sdk/vim25/${VC_API_RELEASE}/ServiceInstance/ServiceInstance/content | jq -j .sessionManager.value)
VIJSON_API_SESSION_ID=$(curl -s -o /dev/null -D - "https://$VC/sdk/vim25/${VC_API_RELEASE}/SessionManager/$SESSION_MANAGER_MOID/Login" -H 'Content-Type: application/json' -d "{\"userName\":\"${VC_USERNAME}\", \"password\": \"${VC_PASSWORD}\"}" | awk 'BEGIN {FS=": "}/^vmware-api-session-id/{print $2}')

CUSTOM_FIELDS_MANAGER_MOID=$(curl -s https://$VC/sdk/vim25/${VC_API_RELEASE}/ServiceInstance/ServiceInstance/content | jq -j .customFieldsManager.value)

# Retrieve Custom Field Key/Name for VirtualMachine definitions
curl -s -H "vmware-api-session-id: ${VIJSON_API_SESSION_ID}" -X GET "https://$VC/sdk/vim25/${VC_API_RELEASE}/CustomFieldsManager/${CUSTOM_FIELDS_MANAGER_MOID}/field" | jq -r '.[] | select(.managedObjectType == "VirtualMachine") | {key, name}'

curl -s -H "vmware-api-session-id: ${VIJSON_API_SESSION_ID}" -X GET "https://$VC/sdk/vim25/${VC_API_RELEASE}/VirtualMachine/${VM_MOREF}/value"

0 comments on commit ab9846d

Please sign in to comment.