vpcs.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # frozen_string_literal: true
  2. require 'neoinfra'
  3. require 'vpc'
  4. require 'accounts'
  5. require 'fog-aws'
  6. require 'neo4j'
  7. # NeoInfra Account information
  8. module NeoInfra
  9. # Provide informations about the accounts available
  10. class Vpcs
  11. def initialize
  12. @cfg = NeoInfra::Config.new
  13. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  14. Neo4j::Session.open(:server_db, neo4j_url)
  15. end
  16. def non_default_vpc_count
  17. Vpc.all.collect(&:default).select { |y| y == 'false' }.length
  18. end
  19. def default_vpc_count
  20. Vpc.all.collect(&:default).select { |y| y == 'true' }.length
  21. end
  22. def list_vpcs
  23. node_counts = Hash.new(0)
  24. Node.all.each do |x|
  25. begin
  26. node_counts[x.subnet.subnet.name] += 1
  27. rescue
  28. foo = "noop"
  29. end
  30. end
  31. Vpc.all.collect { |x| { 'nodes' => node_counts[x.name], 'vpc_id' => x.vpc_id, 'name' => x.name, 'region' => x.region.region, 'owner' => x.owned.name, 'cidr' => x.cidr, 'default' => x.default } }.select { |y| y['default'] == 'false' }.sort_by { |h| h['nodes'] }.reverse
  32. end
  33. def load
  34. aws = NeoInfra::Aws.new
  35. @cfg.accounts.each do |account|
  36. base_conf = {
  37. provider: 'AWS',
  38. aws_access_key_id: account[:key],
  39. aws_secret_access_key: account[:secret]
  40. }
  41. aws.regions.each do |region|
  42. region_conf = { region: region }
  43. begin
  44. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  45. rescue StandardError
  46. puts "Error loading VPCs in region: #{region}"
  47. next
  48. end
  49. # Get VPCs
  50. new_conn.vpcs.all.each do |vpc|
  51. next unless Vpc.where(vpc_id: vpc.id).empty?
  52. vpc_name = if vpc.tags.empty?
  53. vpc.id
  54. elsif vpc.tags.key? 'Name'
  55. vpc.tags['Name']
  56. else
  57. vpc.id
  58. end
  59. vpc_id = Vpc.new(
  60. vpc_id: vpc.id,
  61. name: vpc_name,
  62. cidr: vpc.cidr_block,
  63. state: vpc.state,
  64. default: vpc.is_default.to_s
  65. )
  66. vpc_id.save
  67. AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
  68. VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first)
  69. end
  70. # Get all Subnets
  71. new_conn.subnets.all.each do |subnet|
  72. next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
  73. subnet_name = if subnet.tag_set.empty?
  74. subnet.subnet_id
  75. elsif subnet.tag_set.key? 'Name'
  76. subnet.tag_set['Name']
  77. else
  78. subnet.subnet_id
  79. end
  80. sn = Subnet.new(
  81. subnet_id: subnet.subnet_id,
  82. cidr: subnet.cidr_block,
  83. name: subnet_name,
  84. ip_count: subnet.available_ip_address_count,
  85. state: subnet.state
  86. )
  87. sn.save
  88. begin
  89. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first)
  90. SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first)
  91. rescue Exception => e
  92. # Handle the case of hanging subnets
  93. puts "Account #{account[:name]} couldn't load the following subnet: #{e.message}"
  94. p subnet
  95. end
  96. end
  97. end
  98. end
  99. end
  100. end
  101. end