graph.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # frozen_string_literal: true
  2. require 'neoinfra'
  3. require 'vpc'
  4. require 'accounts'
  5. require 'fog-aws'
  6. require 'neo4j'
  7. require 'csv'
  8. require 'sinatra'
  9. require 's3'
  10. # NeoInfra Account information
  11. module NeoInfra
  12. # Provide informations about the accounts available
  13. class Graph
  14. def initialize
  15. @cfg = NeoInfra::Config.new
  16. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  17. Neo4j::Session.open(:server_db, neo4j_url)
  18. end
  19. def graph_vpcs
  20. csv_string = CSV.generate(force_quotes: false ) do |csv|
  21. csv << ['id,value']
  22. csv << ['aws,']
  23. @cfg = NeoInfra::Config.new
  24. @cfg.accounts.each do |account|
  25. csv << ["aws.#{account[:name]},"]
  26. Vpc.where(default: "false").select{|x| x.owned.name == account[:name]}.collect{|y| y.region.region}.uniq.each do |region|
  27. csv << ["aws.#{account[:name]}.#{region},"]
  28. end
  29. Vpc.where(default: "false").each do |vpc|
  30. if vpc.owned.name == account[:name]
  31. csv << ["aws.#{account[:name]}.#{vpc.region.region}.#{vpc.name},1"]
  32. end
  33. end
  34. end
  35. end
  36. return csv_string.gsub('"', '')
  37. end
  38. def graph_buckets
  39. csv_string = CSV.generate(force_quotes: false ) do |csv|
  40. csv << ['id,value']
  41. csv << ['aws,']
  42. @cfg = NeoInfra::Config.new
  43. @cfg.accounts.each do |account|
  44. csv << ["aws.#{account[:name]},"]
  45. Bucket.all.select{|x| x.owner.name == account[:name]}.collect{|y| y.region.region}.uniq.each do |region|
  46. csv << ["aws.#{account[:name]}.#{region},"]
  47. end
  48. Bucket.all.each do |bucket|
  49. if bucket.owner.name == account[:name]
  50. csv << ["aws.#{account[:name]}.#{bucket.region.region}.#{bucket.name.gsub("\.", ':')},1"]
  51. end
  52. end
  53. end
  54. end
  55. return csv_string.gsub('"', '')
  56. end
  57. def graph_queues
  58. csv_string = CSV.generate(force_quotes: false ) do |csv|
  59. csv << ['id,value']
  60. csv << ['aws,']
  61. @cfg = NeoInfra::Config.new
  62. @cfg.accounts.each do |account|
  63. csv << ["aws.#{account[:name]},"]
  64. SQSQueue.all.select{|x| x.owner.name == account[:name]}.collect{|y| y.region.region}.uniq.each do |region|
  65. csv << ["aws.#{account[:name]}.#{region},"]
  66. end
  67. SQSQueue.all.each do |q|
  68. if q.owner.name == account[:name]
  69. csv << ["aws.#{account[:name]}.#{q.region.region}.#{q.name},1"]
  70. end
  71. end
  72. end
  73. end
  74. return csv_string.gsub('"', '')
  75. end
  76. end
  77. end