Created
January 29, 2018 09:25
-
-
Save lrivallain/498a2cedabc1e1aed3a48b27dbb85f94 to your computer and use it in GitHub Desktop.
Import content package in vRealize Automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function helper { | |
echo "To use the script, ensure to configure environment variables with the credentials for your vRA account: | |
export VRA_SERVER='vra-server.domain.lcl' | |
export VRA_USERNAME='configurationadmin@vsphere.local' | |
export VRA_PASSWORD='VMware1!' | |
export VRA_TENANT='vsphere.local'" | |
} | |
# Help and env setup | |
[ "$1" == '-h' ] && helper && exit 0; | |
[ -z "$VRA_SERVER" ] && helper && exit -1; | |
[ -z "$VRA_USERNAME" ] && helper && exit -1; | |
[ -z "$VRA_PASSWORD" ] && helper && exit -1; | |
[ -z "$VRA_TENANT" ] && helper && exit -1; | |
# Mandatory parameter. | |
if [ "$#" -ne 1 ]; then | |
echo "Missing parameter. Please use the script with ./$0 filename.zip" | |
helper | |
exit -1 | |
fi | |
# If jq is installed, use this tool, instead use python | |
JSON_PRETTY_CMD="python -m json.tool" | |
if [ -x "$(command -v jq)" ]; then | |
JSON_PRETTY_CMD="jq" | |
fi | |
echo "Authenticating..." | |
BEARER_TOKEN=$(curl -sk -X POST \ | |
-H "Accept: application/json" \ | |
-H "Content-Type: application/json" \ | |
-H "Cache-Control: no-cache" \ | |
-d "{ | |
\"username\": \"$VRA_USERNAME\", | |
\"password\": \"$VRA_PASSWORD\", | |
\"tenant\" : \"$VRA_TENANT\" | |
}" \ | |
"https://$VRA_SERVER/identity/api/tokens" | \ | |
python -c "import sys, json; print json.load(sys.stdin)['id']" 2> /dev/null) | |
# Test auth result | |
[ -z "$BEARER_TOKEN" ] && echo "Error during auth process !!! Check credentials settings." && exit -1; | |
# Validation of package | |
echo "Pushing package file for validation..." | |
curl -sk -X POST \ | |
"https://$VRA_SERVER/content-management-service/api/packages/validate" \ | |
-H "Content-Type: multipart/form-data" \ | |
-H "authorization: Bearer $BEARER_TOKEN" \ | |
-F "file=@$(pwd)/$1" | $JSON_PRETTY_CMD | |
# confirmation from user | |
read -p "Do you whant to proceed upload (y/n) ? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
# if ok, we push the package on vRA | |
echo "Pushing package file for import with resolutionMode=OVERWRITE..." | |
curl -sk -X POST \ | |
"https://$VRA_SERVER/content-management-service/api/packages?resolutionMode=OVERWRITE" \ | |
-H "Content-Type: multipart/form-data" \ | |
-H "authorization: Bearer $BEARER_TOKEN" \ | |
-F "file=@$(pwd)/$1" | $JSON_PRETTY_CMD | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment