Skip to content

Instantly share code, notes, and snippets.

@suhlig

suhlig/cf-which-org

Created Jul 12, 2019
Embed
What would you like to do?
Prints the space and org names for a given route, filtered down to the hostname
#!/usr/bin/env ruby
# Prints the space and org names for a given route, filtered down to the hostname
require 'json'
require 'shellwords'
def curl(url)
while url
JSON.parse(`cf curl #{Shellwords.escape(url)}`).tap do |result|
raise "Error #{result['error_code']}: #{result['description']}" if result['error_code']
yield result # streaming: handle each page as we get it
url = result['next_url']
end
end
end
if ARGV.size != 1
warn "Missing hostname."
exit 1
end
host_name = ARGV.first
curl("/v2/routes?q=host:#{host_name}") do |route|
route_mappings_url = route['resources'].first['entity']['route_mappings_url'] # there can only be one space owning a route
space_url = route['resources'].first['entity']['space_url']
space_name = nil
org_name = nil
curl(space_url) do |space|
space_name = space['entity']['name']
org_url = space['entity']['organization_url']
curl(org_url) do |org|
org_name = org['entity']['name']
end
end
curl(route_mappings_url) do |rm|
rm['resources'].each do |app|
app_url = app['entity']['app_url']
curl(app_url) do |app|
app_name = app['entity']['name']
puts "Route: #{host_name}"
puts "App: #{app_name}"
puts "Space: #{space_name}"
puts "Org: #{org_name}"
warn ''
warn 'Retrieve the recent events for this app:'
warn ''
warn " cf target -o #{org_name} -s #{space_name}"
warn " cf events #{app_name}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment