Skip to content

Commit 1be9247

Browse files
cURL sample to list NSX-T Policy VM/Tags in VMC
1 parent 9f86f43 commit 1be9247

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

shell/list_vmc_nsxt_vm_tags.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# Author: William Lam (@lamw)
3+
# Description: Using cURL to list all NSX-T VM/Tags in VMC
4+
5+
if [ ${#} -ne 3 ]; then
6+
echo -e "Usage: \n\t$0 [REFRESH_TOKEN] [ORGID] [SDDCID]\n"
7+
exit 1
8+
fi
9+
10+
type jq > /dev/null 2&>1
11+
if [ $? -eq 1 ]; then
12+
echo "It does not look like you have jq installed. This script uses jq to parse the JSON output"
13+
exit 1
14+
fi
15+
16+
REFRESH_TOKEN=$1
17+
ORGID=$2
18+
SDDCID=$3
19+
20+
RESULTS=$(curl -s -X POST -H "application/x-www-form-urlencoded" "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize" -d "refresh_token=$REFRESH_TOKEN")
21+
CSP_ACCESS_TOKEN=$(echo $RESULTS | jq -r .access_token)
22+
23+
curl -s -X GET -H "Content-Type: application/json" -H "csp-auth-token: ${CSP_ACCESS_TOKEN}" -o SDDC_RESULTS "https://vmc.vmware.com/vmc/api/orgs/${ORGID}/sddcs/${SDDCID}"
24+
25+
NSXT_PROXY_URL=$(cat SDDC_RESULTS|jq -r .resource_config.nsx_api_public_endpoint_url)
26+
NSXT_TAGS_URL="${NSXT_PROXY_URL}/policy/api/v1/infra/realized-state/enforcement-points/vmc-enforcementpoint/virtual-machines?page_size=50&sort_by=display_name&sort_ascending=true&query="
27+
28+
curl -s -X GET -H "Content-Type: application/json" -H "csp-auth-token: ${CSP_ACCESS_TOKEN}" "${NSXT_TAGS_URL}" | jq '.results[] | .display_name,.external_id,.tags'

0 commit comments

Comments
 (0)