Browse Source

get vpcs/subnets all in shape

Chris Mague 7 years ago
parent
commit
a064707799
3 changed files with 69 additions and 29 deletions
  1. 5 7
      lib/neoinfra/aws.rb
  2. 41 22
      lib/neoinfra/vpcs.rb
  3. 23 0
      models/vpc.rb

+ 5 - 7
lib/neoinfra/aws.rb

@@ -9,18 +9,16 @@ module NeoInfra
   # Provide informations about the accounts available
   class Aws
     def regions
-
       @cfg = NeoInfra::Config.new
       account = @cfg.accounts.first
       base_conf = {
-        :provider              => 'AWS',
-        :aws_access_key_id     => account[:key],
-        :aws_secret_access_key => account[:secret]
+        provider: 'AWS',
+        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']}
-
+      conn = Fog::Compute.new(base_conf)
+      conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
     end
   end
 end

+ 41 - 22
lib/neoinfra/vpcs.rb

@@ -25,32 +25,51 @@ module NeoInfra
           aws_secret_access_key: account[:secret]
         }
         aws.regions.each do |region|
-          region_conf = {:region => region['regionName'] }
+          region_conf = { region: region['regionName'] }
           new_conn = Fog::Compute.new(region_conf.merge(base_conf))
           # Get VPCs
           new_conn.vpcs.all.each do |vpc|
-            if Vpc.where(vpc_id: vpc.id).length < 1
-              if vpc.tags.empty?
-                vpc_name = vpc.id
-              else
-                if vpc.tags.has_key? "Name"
-                  vpc_name = vpc.tags['Name']
-                else
-                  vpc_name = vpc.id
-                end
-              end
-              vpc_id = Vpc.new(
-                :vpc_id => vpc.id,
-                :name => vpc_name,
-                :cidr => vpc.cidr_block,
-                :state => vpc.state,
-                :default => vpc.is_default.to_s
-              )
-              vpc_id.save
-              AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first )
-            end
+            next unless Vpc.where(vpc_id: vpc.id).empty?
+            vpc_name = if vpc.tags.empty?
+                         vpc.id
+                       else
+                         if vpc.tags.key? 'Name'
+                           vpc.tags['Name']
+                         else
+                           vpc.id
+                                   end
+                           end
+            vpc_id = Vpc.new(
+              vpc_id: vpc.id,
+              name: vpc_name,
+              cidr: vpc.cidr_block,
+              state: vpc.state,
+              default: vpc.is_default.to_s
+            )
+            vpc_id.save
+            AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
+          end
+          # Get all Subnets
+          new_conn.subnets.all.each do |subnet|
+            next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
+            subnet_name = if subnet.tag_set.empty?
+                            subnet.subnet_id
+                          else
+                            if subnet.tag_set.key? 'Name'
+                              subnet.tag_set['Name']
+                            else
+                              subnet.subnet_id
+                            end
+                          end
+            sn = Subnet.new(
+              subnet_id: subnet.subnet_id,
+              cidr: subnet.cidr_block,
+              ip_count: subnet.available_ip_address_count,
+              state: subnet.state
+            )
+            sn.save
+            VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first )
           end
-        # Get all Subnets
         end
       end
     end

+ 23 - 0
models/vpc.rb

@@ -15,6 +15,21 @@ class Vpc
   has_many :out, :az, rel_class: :VpcAz
 end
 
+# Provide the subnet information
+class Subnet
+  include Neo4j::ActiveNode
+  property :subnet_id, constraint: :unique
+  property :name
+  property :cidr
+  property :ip_count
+  property :state
+  has_one :out, :az, rel_class: :SubnetAz
+  has_one :out, :subnet, rel_class: :VpcSubnet
+end
+
+###############################################################################
+# Relationships go below here
+###############################################################################
 # Provide Neo4J Model for VPC Owners
 class AccountVpc
   include Neo4j::ActiveRel
@@ -22,3 +37,11 @@ class AccountVpc
   to_class :AwsAccount
   type :owned
 end
+
+# Relationship between Subnet and VPC
+class VpcSubnet
+  include Neo4j::ActiveRel
+  from_class :Subnet
+  to_class :Vpc
+  type :subnet
+end