Browse Source

add subnet and az information

Chris Mague 7 years ago
parent
commit
fd674f37f6
5 changed files with 85 additions and 2 deletions
  1. 8 1
      Rakefile
  2. 35 1
      lib/neoinfra/aws.rb
  3. 2 0
      lib/neoinfra/vpcs.rb
  4. 24 0
      models/regions.rb
  5. 16 0
      models/vpc.rb

+ 8 - 1
Rakefile

@@ -15,6 +15,7 @@ require 'rake'
 require 'rspec/core/rake_task'
 require 'neoinfra/accounts'
 require 'neoinfra/vpcs'
+require 'neoinfra/aws'
 
 RuboCop::RakeTask.new(:rubocop) do |t|
   t.options = ['--display-cop-names']
@@ -37,6 +38,12 @@ task :load_vpcs do
   j.load
 end
 
+desc 'Load Region and Availability Zone information'
+task :load_regions do
+  j = NeoInfra::Aws.new
+  j.load_regions
+end
+
 desc 'Load Everything'
-task load_all: %i[load_accounts load_vpcs]
+task load_all: %i[load_accounts load_regions load_vpcs]
 task full_test: %i[rubocop spec]

+ 35 - 1
lib/neoinfra/aws.rb

@@ -1,7 +1,9 @@
 # frozen_string_literal: true
 
 require 'accounts'
+require 'regions'
 require 'fog'
+require 'neo4j'
 require 'neoinfra/config'
 
 # NeoInfra Account information
@@ -16,9 +18,41 @@ module NeoInfra
         aws_access_key_id: account[:key],
         aws_secret_access_key: account[:secret]
       }
-
       conn = Fog::Compute.new(base_conf)
       conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
     end
+
+    def azs(region)
+      @cfg = NeoInfra::Config.new
+      account = @cfg.accounts.first
+      base_conf = {
+        provider: 'AWS',
+        aws_access_key_id: account[:key],
+        aws_secret_access_key: account[:secret],
+        region: region
+      }
+      conn = Fog::Compute.new(base_conf)
+      conn.describe_availability_zones.data[:body]['availabilityZoneInfo'].collect { |x| x['zoneName'] }
+    end
+
+    def load_regions
+      @cfg = NeoInfra::Config.new
+      neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
+      Neo4j::Session.open(:server_db, neo4j_url)
+      self.regions.each do |region|
+        next unless Region.where(region: region).empty?
+        r = Region.new(
+          region: region
+        )
+        r.save
+        self.azs(region).each do |az|
+          next unless Az.where(az: az).empty?
+          a = Az.new(az: az)
+          a.save
+          AzRegion.create(from_node: a, to_node: Region.where(region: region).first )
+        end
+      end
+    end
+
   end
 end

+ 2 - 0
lib/neoinfra/vpcs.rb

@@ -48,6 +48,7 @@ module NeoInfra
             )
             vpc_id.save
             AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
+            VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first )
           end
           # Get all Subnets
           new_conn.subnets.all.each do |subnet|
@@ -70,6 +71,7 @@ module NeoInfra
             )
             sn.save
             VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first )
+            SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first )
           end
         end
       end

+ 24 - 0
models/regions.rb

@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Information on Region
+class Region
+  include Neo4j::ActiveNode
+  property :region, constraint: :unique
+end
+
+# Information on Availability Zones
+class Az
+  include Neo4j::ActiveNode
+  property :az, constraint: :unique
+  has_one :out, :region, rel_class: :AzRegion
+end
+
+# Map AZs to regions
+class AzRegion
+  include Neo4j::ActiveRel
+  from_class :Az
+  to_class :Region
+  type :region
+end

+ 16 - 0
models/vpc.rb

@@ -45,3 +45,19 @@ class VpcSubnet
   to_class :Vpc
   type :subnet
 end
+
+# Relationship between the VPC and the Region
+class VpcRegion
+  include Neo4j::ActiveRel
+  from_class :Vpc
+  to_class :Region
+  type :region
+end
+
+# Relationship between the Subnet and the AZ
+class SubnetAz
+  include Neo4j::ActiveRel
+  from_class :Subnet
+  to_class :Az
+  type :az
+end