vpcs.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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{|x| x.default}.select{|y| y == "false"}.length
  18. end
  19. def default_vpc_count
  20. Vpc.all.collect{|x| x.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. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  38. # Get VPCs
  39. new_conn.vpcs.all.each do |vpc|
  40. next unless Vpc.where(vpc_id: vpc.id).empty?
  41. vpc_name = if vpc.tags.empty?
  42. vpc.id
  43. elsif vpc.tags.key? 'Name'
  44. vpc.tags['Name']
  45. else
  46. vpc.id
  47. end
  48. vpc_id = Vpc.new(
  49. vpc_id: vpc.id,
  50. name: vpc_name,
  51. cidr: vpc.cidr_block,
  52. state: vpc.state,
  53. default: vpc.is_default.to_s
  54. )
  55. vpc_id.save
  56. AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
  57. VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first)
  58. end
  59. # Get all Subnets
  60. new_conn.subnets.all.each do |subnet|
  61. next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
  62. subnet_name = if subnet.tag_set.empty?
  63. subnet.subnet_id
  64. elsif subnet.tag_set.key? 'Name'
  65. subnet.tag_set['Name']
  66. else
  67. subnet.subnet_id
  68. end
  69. sn = Subnet.new(
  70. subnet_id: subnet.subnet_id,
  71. cidr: subnet.cidr_block,
  72. name: subnet_name,
  73. ip_count: subnet.available_ip_address_count,
  74. state: subnet.state
  75. )
  76. sn.save
  77. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first)
  78. begin
  79. SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first)
  80. rescue
  81. # Handle the case of hanging subnets
  82. puts "Account #{account[:name]} couldn't load the following subnet:"
  83. p subnet
  84. end
  85. end
  86. end
  87. end
  88. end
  89. end
  90. end