vpcs.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # frozen_string_literal: true
  2. require 'vpc'
  3. require 'accounts'
  4. require 'fog'
  5. require 'neo4j'
  6. require 'neoinfra/aws'
  7. require 'neoinfra/config'
  8. # NeoInfra Account information
  9. module NeoInfra
  10. # Provide informations about the accounts available
  11. class Vpcs
  12. def load
  13. aws = NeoInfra::Aws.new
  14. @cfg = NeoInfra::Config.new
  15. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  16. Neo4j::Session.open(:server_db, neo4j_url)
  17. @cfg.accounts.each do |account|
  18. base_conf = {
  19. provider: 'AWS',
  20. aws_access_key_id: account[:key],
  21. aws_secret_access_key: account[:secret]
  22. }
  23. aws.regions.each do |region|
  24. region_conf = {:region => region['regionName'] }
  25. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  26. new_conn.vpcs.all.each do |vpc|
  27. if Vpc.where(vpc_id: vpc.id).length < 1
  28. if vpc.tags.empty?
  29. vpc_name = vpc.id
  30. else
  31. if vpc.tags.has_key? "Name"
  32. vpc_name = vpc.tags['Name']
  33. else
  34. vpc_name = vpc.id
  35. end
  36. end
  37. vpc_id = Vpc.new(:vpc_id => vpc.id, :name => vpc_name, :cidr => vpc.cidr_block)
  38. vpc_id.save
  39. AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first )
  40. end
  41. end
  42. end
  43. end
  44. end
  45. end
  46. end