#!/bin/bash usage() { echo "$0" } # clientId= # clientSecret= # LOOKER_DEV_HOST= # LOOKER_DEV_URL=https://$LOOKER_DEV_HOST # CREATE_LOOKS=true # CREATE_DASHBOARDS=true # EXCLUDE_DIRS=dev_only # CREATE_FOLDERS=true CURL="curl -k -sS" suffix=" " exportAllDashboards() { EXP_DIR=$1 echo "gzr space export 1 --dir $EXP_DIR --host $LOOKER_DEV_HOST --client-id $clientId --client-secret $clientSecret --port=443 " } makeAllSubFolders(){ ## $1: parent id [[ $CREATE_LOOKS == true ]] && createLooks $1 [[ $CREATE_DASHBOARDS == true ]] && createDashboards $1 find . -maxdepth 1 -mindepth 1 -type d | cut -d"/" -f2 | \ while read DIR; do NEWDIR=$DIR$suffix [[ $EXCLUDE_DIRS =~ ,$DIR, ]] && echo "Skipping $DIR..." && continue # simple format for folder creation (only requires two fields) [[ $CREATE_FOLDERS == true ]] && createFolder "{\"name\": \"$NEWDIR\", \"parent_id\": $1}" FOLDER_ID=$OBJECT_ID if [[ $FOLDER_ID != "null" ]]; then cd "$DIR" makeAllSubFolders $FOLDER_ID fi cd .. done } createFolder(){ ## $1: JSON representation of the object to create ## sets global var OBJECT_ID to the new/existing object ## change the literal % symbols to URL encoded percent symbols objJson=$(echo "$1" | sed 's/%"/%25"/g') objName=$(echo "$objJson" | jq -r '.name // .title // .view') objParent=$(echo "$objJson" | jq -r '.parent_id // .folder_id') ## does it already exist? OBJECT_ID=$(${CURL} ${LOOKER_DEV_URL}:443/api/4.0/folders/search \ --header "Accept: application/json" \ --header "Authorization: token ${API_TOKEN}" \ --request GET \ --data "name=$objName&parent_id=$objParent" | jq '.[0].id // empty' 2>/dev/null) ## if not found then create it, otherwise skip it... if [[ $OBJECT_ID == "" ]]; then resp=$( ${CURL} ${LOOKER_DEV_URL}:443/api/4.0/folders \ --header "Content-Type: application/x-www-form-urlencoded" \ --request POST \ --header "Authorization: token ${API_TOKEN}" \ --data "$objJson") OBJECT_ID=$(echo "$resp" | jq .id) echo "Object Id: $OBJECT_ID" err=$(echo "$resp" | jq -r '.errors[]?.message // .message' 2>/dev/null) [[ $err == "" || $err == "null" ]] && echo "Created folders \"$objName\" ($OBJECT_ID)" || echo "ERROR: $err ($objName)" else #Commented below line for temporary purpose echo "" # echo "Folder already exists at given location. Skipping creation of $objName" fi } transformDashboard() { dashboardJSONFileName=$1 dashboardId=`echo "$dashboardJSONFileName" | cut -f2 -d_` modelName=`cat "$dashboardJSONFileName" | grep -m 1 "model" | cut -f2 -d: | cut -f2 -d\"` dashboardTitle=`cat "$dashboardJSONFileName" | grep -m 1 "title" | cut -f2 -d: | cut -f2 -d\"` transformFile=`mktemp` echo "{\"dashboard_elements\": [],\"replacements\": [{ \"\\\"model\\\": \\\"$modelName\\\",\": \"\\\"model\\\": \\\"gcp_$modelName\\\",\" }, { \"\\\"title\\\": \\\"$dashboardTitle\\\",\": \"\\\"title\\\": \\\"$dashboardTitle$suffix\\\",\" } ]}" > $transformFile # cat "$transformFile" # echo "gzr dashboard cat $dashboardId --host $LOOKER_DEV_HOST --port 443 --dir . --transform $transformFile" gzr dashboard cat $dashboardId --host $LOOKER_DEV_HOST --port 443 --dir . --transform $transformFile } createDashboards() { ls Dashboard_*.json 2>/dev/null | \ while read x; do [[ $x == "" ]] && continue # Transform dashboard transformDashboard "$x" # echo " Create dashboard: $DIR/$x" echo "cd `pwd`" echo "gzr dashboard import \"$x\" $1 --host $LOOKER_DEV_HOST --client-id $clientId --client-secret $clientSecret --port 443 " echo "" gzr dashboard import "$x" $1 --host $LOOKER_DEV_HOST --client-id $clientId --client-secret $clientSecret --port 443 --force --debug done } transformLook() { lookJSONFileName="$1" lookId=`echo "$lookJSONFileName" | cut -f2 -d_` modelName=`cat "$lookJSONFileName" | grep -m 1 "model" | cut -f2 -d: | cut -f2 -d\"` lookTitle=`cat "$lookJSONFileName" | grep -m 1 "title" | cut -f2 -d: | cut -f2 -d\"` sed -i "s/\"model\": \"$modelName\",/\"model\": \"gcp_$modelName\",/g" "$1" sed -i "s/\"title\": \"$lookTitle\",/\"title\": \"$lookTitle$suffix\",/g" "$1" } createLooks(){ ## $1: parent id [[ $CREATE_LOOKS != true ]] && echo "Skipping look creation" && return 0 ls -1 Look_*.json 2>/dev/null | \ while read x; do [[ $x == "" ]] && continue transformLook "$x" # echo " Create look: $DIR/$x" echo "cd `pwd`" echo "gzr look import \"$x\" $1 --host $LOOKER_DEV_HOST --client-id $clientId --client-secret $clientSecret --port 443 " #gzr look import "$x" $1 --host $LOOKER_DEV_HOST --client-id $clientId --client-secret $clientSecret --port 443 done } exportAllDashboards $1 #Get API Token to make Curl commands API_TOKEN=$( ${CURL} $LOOKER_DEV_URL:443/api/4.0/login \ --header "Content-Type: application/x-www-form-urlencoded" \ --request POST \ --data "client_id=$clientId&client_secret=$clientSecret" -k | jq -r .access_token) # echo $API_TOKEN cd $1 makeAllSubFolders 174