vpcs.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 { |x| node_counts[x.subnet.subnet.name] += 1 }
  25. 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
  26. end
  27. def load
  28. aws = NeoInfra::Aws.new
  29. @cfg.accounts.each do |account|
  30. base_conf = {
  31. provider: 'AWS',
  32. aws_access_key_id: account[:key],
  33. aws_secret_access_key: account[:secret]
  34. }
  35. aws.regions.each do |region|
  36. region_conf = { region: region }
  37. begin
  38. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  39. rescue StandardError
  40. puts "Error loading VPCs in region: #{region}"
  41. next
  42. end
  43. # Get VPCs
  44. new_conn.vpcs.all.each do |vpc|
  45. next unless Vpc.where(vpc_id: vpc.id).empty?
  46. vpc_name = if vpc.tags.empty?
  47. vpc.id
  48. elsif vpc.tags.key? 'Name'
  49. vpc.tags['Name']
  50. else
  51. vpc.id
  52. end
  53. vpc_id = Vpc.new(
  54. vpc_id: vpc.id,
  55. name: vpc_name,
  56. cidr: vpc.cidr_block,
  57. state: vpc.state,
  58. default: vpc.is_default.to_s
  59. )
  60. vpc_id.save
  61. AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
  62. VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first)
  63. end
  64. # Get all Subnets
  65. new_conn.subnets.all.each do |subnet|
  66. next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
  67. subnet_name = if subnet.tag_set.empty?
  68. subnet.subnet_id
  69. elsif subnet.tag_set.key? 'Name'
  70. subnet.tag_set['Name']
  71. else
  72. subnet.subnet_id
  73. end
  74. sn = Subnet.new(
  75. subnet_id: subnet.subnet_id,
  76. cidr: subnet.cidr_block,
  77. name: subnet_name,
  78. ip_count: subnet.available_ip_address_count,
  79. state: subnet.state
  80. )
  81. sn.save
  82. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first)
  83. begin
  84. SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first)
  85. rescue Exception => e
  86. # Handle the case of hanging subnets
  87. puts "Account #{account[:name]} couldn't load the following subnet: #{e.message}"
  88. p subnet
  89. end
  90. end
  91. end
  92. end
  93. end
  94. end
  95. end