Last active
November 10, 2021 20:54
-
-
Save f9n/f497dd54b215b504b95119b1325b8b80 to your computer and use it in GitHub Desktop.
Get consul services with filter
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 | |
# export CONSUL_HTTP_ADDR=http://127.0.0.1:8500 | |
# export CONSUL_HTTP_TOKEN=... | |
service=$1 | |
filter_expression=$2 | |
mode=${3:-'health'} | |
if [ -z $CONSUL_HTTP_ADDR ]; then | |
echo -e "Make sure you define CONSUL_HTTP_ADDR environment variable:\nexport CONSUL_HTTP_ADDR=http://127.0.0.1:8500" | |
exit 1; | |
fi | |
CONSUL_ARGS="" | |
if [ ! -z $CONSUL_HTTP_TOKEN ]; then | |
CONSUL_ARGS+=" --header 'X-Consul-Token: ${CONSUL_HTTP_TOKEN}'" | |
fi | |
URL=$CONSUL_HTTP_ADDR/v1/$mode/service/$service | |
curl -s -G $URL $CONSUL_ARGS --data-urlencode "filter=${filter_expression}" | |
### Usage | |
# $ consul-service-filter.sh <service_name> <filter_exporession> [mode] | |
# $ consul-service-filter.sh elasticsearch 'Service.Tags contains "_cluster=abc-discovery-search-01"' | |
# $ consul-service-filter.sh elasticsearch 'Service.Tags contains "_cluster=abc-discovery-search-01"' | jq . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment