aws.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. require 'accounts'
  3. require 'regions'
  4. require 'fog'
  5. require 'neo4j'
  6. require 'neoinfra/config'
  7. # NeoInfra Account information
  8. module NeoInfra
  9. # Provide informations about the accounts available
  10. class Aws
  11. def regions
  12. @cfg = NeoInfra::Config.new
  13. account = @cfg.accounts.first
  14. base_conf = {
  15. provider: 'AWS',
  16. aws_access_key_id: account[:key],
  17. aws_secret_access_key: account[:secret]
  18. }
  19. conn = Fog::Compute.new(base_conf)
  20. conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
  21. end
  22. def azs(region)
  23. @cfg = NeoInfra::Config.new
  24. account = @cfg.accounts.first
  25. base_conf = {
  26. provider: 'AWS',
  27. aws_access_key_id: account[:key],
  28. aws_secret_access_key: account[:secret],
  29. region: region
  30. }
  31. conn = Fog::Compute.new(base_conf)
  32. conn.describe_availability_zones.data[:body]['availabilityZoneInfo'].collect { |x| x['zoneName'] }
  33. end
  34. def load_regions
  35. @cfg = NeoInfra::Config.new
  36. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  37. Neo4j::Session.open(:server_db, neo4j_url)
  38. self.regions.each do |region|
  39. next unless Region.where(region: region).empty?
  40. r = Region.new(
  41. region: region
  42. )
  43. r.save
  44. self.azs(region).each do |az|
  45. next unless Az.where(az: az).empty?
  46. a = Az.new(az: az)
  47. a.save
  48. AzRegion.create(from_node: a, to_node: Region.where(region: region).first )
  49. end
  50. end
  51. end
  52. end
  53. end