-
Notifications
You must be signed in to change notification settings - Fork 0
/
interro-route
executable file
·63 lines (50 loc) · 2.53 KB
/
interro-route
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Exit on error.
set -e
# Display Commands/"demonstrator-mode."
# set -x
# Take host as an argument:
HOST=$1
# Set CF to use a seperate config file so as to avoid interfereing with normal login/target.
# NB: This also causes cf to create the ~/tmp/interroroute path if it doesn't already exist.
# This is useful in this case, but might be surprising if ~/tmp/ didn't previously exist.
export CF_HOME=~/tmp/interroroute
# Get target from environment.
USERNAME=$CF_USERNAME
PASSWORD=$CF_PASSWORD
API=$CF_API
# Login with credentials from env.
cf api $API > /dev/null
cf auth $USERNAME $PASSWORD > /dev/null
# Get routes data for the given host from cloud controller save it as a temp file.
cf curl "/v2/routes?q=host%3A${HOST}" > ~/tmp/interroroute/target-routes.json
### NOTE: Below is the section that needs to go inside an iterator in the case of multiple matches.
### Alternatively, could make multiple matches impossible by specifying domain, too.
### (Careful not to get too clever. "Domains" don't have to be top level.
### The URL bs.srsly.trrble.io can exist as domain "srsly.trrble.io"
### plus hostname "bs" OR as domain "bs.srsly.trrble.io" with no hostname.
### Or both! Though, it can't be both in the case of a shared domain,
### so the really stupid "both!" scenario is at least constrained to a single org.)
# Get a single entity.
jq '.resources | .[0].entity' ~/tmp/interroroute/target-routes.json > $CF_HOME/single-entity.json
# assign URLs for curling (and, you know, curl a little to accomplish that, in ORG_URL's case).
APPS_URL=$(jq '.apps_url' ~/tmp/interroroute/single-entity.json -r)
SPACE_URL=$(jq '.space_url' ~/tmp/interroroute/single-entity.json -r)
DOMAIN_URL=$(jq '.domain_url' ~/tmp/interroroute/single-entity.json -r)
ORG_URL=$(cf curl $SPACE_URL | jq '.entity | .organization_url' -r)
# Get the things' actual names:
APPS=$(cf curl $APPS_URL | jq '.resources | .[0] | .entity | .name' -r)
### Note: the above only yeilds the first such app, needs to be expanded to iterate.
SPACE=$(cf curl $SPACE_URL | jq '.entity | .name' -r)
ORG=$(cf curl $ORG_URL | jq '.entity | .name' -r)
DOMAIN=$(cf curl $DOMAIN_URL | sed '$d' | jq '.entity | .name' -r)
### Note: sed in above line to scrub deprecation warning, which otherwise provokes a failure from jq.
### of course, if the json is empty when it gets here, the sed ruins its validity.
### maybe there should be some error handling in here somewhere, right?
# output results
echo "App: $APPS"
echo "Org: $ORG"
echo "Space: $SPACE"
echo "Domain: $DOMAIN"
# logout
cf logout > /dev/null