Skip to content

Instantly share code, notes, and snippets.

@m-richo
Last active December 12, 2017 22:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-richo/67fc0fae82c8fda484d3dfeabc5899be to your computer and use it in GitHub Desktop.
Save m-richo/67fc0fae82c8fda484d3dfeabc5899be to your computer and use it in GitHub Desktop.
query Cloud Foundry environment for all running applications and dump them into a CSV file
#!/bin/bash
set -eu
set -o pipefail
API_ENDPOINT=$(cf api |grep "endpoint" |awk -F "https://" '{print $2}')
rm -f $API_ENDPOINT.csv
echo "Running audit for $API_ENDPOINT"
echo "Output is in $API_ENDPOINT.csv"
echo "ORG_NAME,SPACE_NAME,APP_NAME,DEVELOPERS" > $API_ENDPOINT.csv
# List of all CF Orgs and then iterate through them
ORG_GUIDS=$(cf curl /v2/organizations | jq -r '.resources[] .metadata.guid')
for ORG_GUID in $ORG_GUIDS
do
ORG_NAME=$(cf curl /v2/organizations/$ORG_GUID |jq -r '.entity.name')
echo "CF ORG = $ORG_NAME"
# Iterate through all the CF spaces in this CF ORG
SPACE_GUIDS=$(cf curl /v2/organizations/$ORG_GUID/spaces |jq -r '.resources[] .metadata.guid')
for SPACE_GUID in $SPACE_GUIDS
do
SPACE_NAME=$(cf curl /v2/spaces/$SPACE_GUID |jq -r '.entity.name')
SPACE_APP_NUMBER=$(cf curl /v2/spaces/$SPACE_GUID/apps |jq -r '.total_results')
DEVELOPERS=$(cf curl /v2/spaces/$SPACE_GUID/developers | jq -r '.resources[] .entity.username' |tr '\n' ' ')
echo "CF SPACE = $SPACE_NAME - $SPACE_APP_NUMBER app(s)"
# Iterate through all the CF apps in this CF space
APP_GUIDS=$(cf curl /v2/spaces/$SPACE_GUID/apps | jq -r '.resources[] .metadata.guid')
for APP_GUID in $APP_GUIDS
do
APP_NAME=$(cf curl /v2/apps/$APP_GUID |jq -r '.entity.name')
echo "$ORG_NAME,$SPACE_NAME,$APP_NAME,$DEVELOPERS" >> $API_ENDPOINT.csv
done
done
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment