SHARE
TWEET

Untitled

a guest Mar 20th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2.  
  3. require 'json'
  4. require 'pry'
  5.  
  6. def cf_curl(resource, parameter='')
  7.   response = JSON.parse `cf curl /v2/#{resource}?#{parameter}`
  8.   raise "pagination" if response["total_pages"] && response["total_pages"] > 1
  9.   return response
  10. end
  11.  
  12. def get_resources(resource, parameter='')
  13. #  puts "/v2/#{resource}?#{parameter}"
  14.   response = cf_curl(resource, parameter)
  15.   return response["resources"]
  16. end
  17.  
  18.  
  19. def get_space_name(space_guid)
  20.   unless @space_names[space_guid]
  21.     response = cf_curl("/spaces/#{space_guid}")
  22.     space_name =response['entity']['name']
  23.     org_guid = response['entity']['organization_guid']
  24.     org_name = get_org_name(org_guid)
  25.     @space_names[space_guid] = "#{org_name}/#{space_name}"
  26.   end
  27.   return @space_names[space_guid]
  28. end
  29.  
  30. def get_org_name(org_guid)
  31.   unless @org_names[org_guid]
  32.     @org_names[org_guid] = cf_curl("/organizations/#{org_guid}")['entity']['name']
  33.   end
  34.   return @org_names[org_guid]
  35. end
  36.  
  37. @space_names = {}
  38. @org_names = {}
  39.  
  40. if ARGV.length != 1
  41.   puts "Usage: apps-using-service <SERVICE_NAME>"
  42.   exit 1
  43. end
  44.  
  45. service_name = ARGV[0]
  46. services = get_resources('services').select {|v| v['entity']['label'] == service_name}
  47. if services.empty?
  48.   puts "can not find service #{service_name}"
  49.   exit 2
  50. end
  51. service_guid = services.first['metadata']['guid']
  52. puts "found service #{service_name} with guid #{service_guid}"
  53. service_plans = get_resources('service_plans', %Q|q=service_guid:#{service_guid}|)
  54. service_plans.each do |plan|
  55.   service_instances = []
  56.   service_bindings = []
  57.   sp_guid = plan['metadata']['guid']
  58.   puts "-- found plan #{plan['entity']['name']} with guid #{sp_guid} --"
  59.   service_instances.concat get_resources("service_plans/#{sp_guid}/service_instances")
  60.   service_instance_guids = service_instances.map { |i| i['metadata']['guid'] }
  61.   service_instance_guids.each do |si_guid|
  62.     service_bindings.concat get_resources("service_bindings", "q=service_instance_guid:#{si_guid}")
  63.   end
  64.   app_guids = service_bindings.map { |i| i['entity']['app_guid'] }
  65.   cfapps = []
  66.   app_guids.each do |app_guid|
  67.     cfapps.push cf_curl("apps/#{app_guid}/summary")
  68.   end
  69.   cfapps.each do |cfapp|
  70.     appname = cfapp['name']
  71.     appspace = get_space_name(cfapp['space_guid'])
  72.     puts "Name: #{appname} Space: #{appspace}"
  73.   end
  74. end
RAW Paste Data
Top