Last active
May 16, 2018 16:47
-
-
Save bthelen/6eac28211fa616eba854c6aa2850035b to your computer and use it in GitHub Desktop.
create-ups-if-not-exist.sh
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
#! /usr/bin/env bash | |
set -e | |
readonly ARGS="$#" | |
readonly SERVICE_INSTANCE="$1" | |
readonly CREDENTIALS="$2" | |
readonly PROGNAME=$(basename $0) | |
usage() { | |
cat <<- EOF | |
usage: $PROGNAME service-instance credentials | |
Program creates a clound foundry user provided service in the currently | |
selected space if necessary. If a service already exists with the given | |
instance name, then the service will be updated | |
Examples: | |
Create a ups: | |
$PROGNAME my-ups '{"username":"admin","password":"password"}' | |
EOF | |
} | |
array_contains () { | |
local seeking=$1; shift | |
local in=false | |
for element; do | |
if [[ $element == $seeking ]]; then | |
in=true | |
break | |
fi | |
done | |
echo $in | |
} | |
does_service_exist() { | |
local services=$(cf services | awk '{print $1}' | tail -n +4) | |
echo $(array_contains $SERVICE_INSTANCE $services) | |
} | |
main() { | |
if [ $ARGS -ne 2 ]; then | |
usage | |
return 1 | |
fi | |
if [ "$(does_service_exist $SERVICE_INSTANCE)" = false ]; then | |
echo "Creating Service..." | |
cf create-user-provided-service $SERVICE_INSTANCE -p $CREDENTIALS | |
else | |
echo "Service $SERVICE_INSTANCE Already Exists, Skipping..." | |
cf update-user-provided-service $SERVICE_INSTANCE -p $CREDENTIALS | |
fi | |
} | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment