Created
October 13, 2015 08:48
-
-
Save bahamat/884bda7da7dc7027090d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
# Copyright (c) 2015, Brian Bennett <[email protected]> | |
cn=$1; shift | |
action=$1; shift | |
set -o errexit | |
get_server_uuid () { | |
if (( ${#cn} == 36 )); then | |
echo "$cn" | |
else | |
sdc-cnapi "/servers?hostname=$cn" -f | json -Ha uuid | |
if (( $? > 0 )); then | |
usage ; exit 1 | |
fi | |
fi | |
} | |
do_delete_comment () { | |
sdc-cnapi "/servers/$uuid" -f -X POST -d '{"comments":""}' >/dev/null | |
} | |
do_set_comment() { | |
if [[ -z $1 ]]; then | |
echo "Cannot set empty comment. Use delete instead." | |
usage; exit 1 | |
fi | |
json=$(json -o json-0 -e "this.comments=\"$*\"" <<< '{}') | |
sdc-cnapi "/servers/$uuid" -f -X POST -d "$json" >/dev/null | |
do_get_comment | |
} | |
do_get_comment () { | |
printf "%s: " "$cn" | |
sdc-cnapi "/servers/$uuid" -f | json -Ha comments | |
} | |
usage () { | |
printf "Usage:\n" | |
printf "\t%s get CN\n" "${0/.*\/}" | |
printf "\t%s set CN comment\n" "${0/.*\/}" | |
printf "\t%s delete CN\n" "${0/.*\/}" | |
printf "\nNote: comment should not be quoted.\n" | |
} | |
if [[ -z $cn ]]; then | |
echo "No CN provided" | |
usage ; exit 1 | |
fi | |
uuid=$(get_server_uuid) | |
if [[ -z $uuid ]]; then | |
printf "Invalid CN: %s\n" "$cn" | |
usage ; exit 1 | |
fi | |
case "${action:=get}" in | |
del|delete) do_delete_comment;; | |
set) do_set_comment "$*";; | |
get) do_get_comment;; | |
*) usage; exit 1;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment