Skip to content

Get a list of VMware vmServers #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions snippets/curl/get_nb_vmservers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/sh

#####################n#####################################################

# This script demonstrates the usage of netbackup REST API for listing
# the vmservers

# This script requires jq command-line JSON parser
# if your system does not have jq installed, this will not work
# jq can be downloaded from here: https://github.com/stedolan/jq/releases

###########################################################################

port=1556
master_server=""
username=""
password=""
domainname=""
domaintype=""

showHelp()
{
echo ""
echo "Invalid command parameters"
echo "Usage:"
echo "./get_nb_vmservers.sh -nbmaster <master_server> -username <username> -password <password> -domainname <dname> -domaintype <unixpwd/nt>"
echo ""
exit 1
}

parseArguments()
{
if [ $# -lt 6 ]; then
showHelp
fi

while [ "$1" != "" ]; do
case $1 in
-nbmaster)
master_server=$2
;;
-username)
username=$2
;;
-password)
password=$2
;;
-domainname)
domainname=$2
;;
-domaintype)
domaintype=$2
;;
*)
showHelp
;;
esac
shift 2
done

if [ -z "$master_server" ] || [ -z "$username" ] || [ -z "$password" ] || [ -z "$domainname" ] || [ -z "$domaintype" ]; then
showhelp
fi

if [ "${domaintype^^}" = "WINDOWS" ] || [ "${domaintype^^}" = "NT" ]; then
domaintype="nt"
fi
}

uriencode()
{
jq -nr --arg v "$1" '$v|@uri';
}
###############main############

parseArguments "$@"

master_server=$master_server.$domainname
basepath="https://$master_server:$port/netbackup"
content_header='content-type:application/json'

##############login#############

uri="$basepath/login"
echo $uri

data=$(jq --arg name "$username" --arg pass "$password" --arg dname "$domainname" --arg dtype "$domaintype" \
--null-input '{userName: $name, password: $pass}')

jwt=$(curl -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token')

### To use filter page[limit] in URI, The key 'page[limit]' must be url encoded already. ###
### Curl --data-urlencode encodes only the content part of the data of the form 'name=content' ###
param1="$(uriencode 'page[limit]')=10" #op: page%5Blimit%5D=10
param2="$(uriencode 'page[offset]')=0"


##############jobs##############
auth_header="authorization:$jwt"
uri="$basepath/config/servers/vmservers"

curl --insecure --request GET --globoff --get $uri -H $content_header -H $auth_header \
--data-urlencode "$param1" \
--data-urlencode "$param2" \
| \
jq '[.data[]|{Type: .type, ID: .id,
ServerName: .attributes.vmServer.serverName,
VmType: .attributes.vmServer.vmType,
UserId: .attributes.vmServer.userId}]'

exit 0