Browse Source

load up vpcs

Chris Mague 7 years ago
parent
commit
4e1aeb8e33
5 changed files with 108 additions and 0 deletions
  1. 8 0
      Rakefile
  2. 2 0
      lib/neoinfra.rb
  3. 26 0
      lib/neoinfra/aws.rb
  4. 50 0
      lib/neoinfra/vpcs.rb
  5. 22 0
      models/vpc.rb

+ 8 - 0
Rakefile

@@ -14,6 +14,7 @@ require 'rubocop/rake_task'
 require 'rake'
 require 'rspec/core/rake_task'
 require 'neoinfra/accounts'
+require 'neoinfra/vpcs'
 
 RuboCop::RakeTask.new(:rubocop) do |t|
   t.options = ['--display-cop-names']
@@ -30,4 +31,11 @@ task :load_accounts do
   j.load
 end
 
+desc 'Load accounts into the neo4j container'
+task :load_vpcs do
+  j = NeoInfra::Vpcs.new
+  j.load
+end
+
+task load_all: %i[load_accounts load_vpcs]
 task full_test: %i[rubocop spec]

+ 2 - 0
lib/neoinfra.rb

@@ -4,4 +4,6 @@
 module NeoInfra
   require 'neoinfra/config'
   require 'neoinfra/accounts'
+  require 'neoinfra/aws'
+  require 'neoinfra/vpcs'
 end

+ 26 - 0
lib/neoinfra/aws.rb

@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+require 'accounts'
+require 'fog'
+require 'neoinfra/config'
+
+# NeoInfra Account information
+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]
+      }
+
+      conn  = Fog::Compute.new(base_conf)
+      conn.describe_regions.data[:body]['regionInfo'].collect{ |x| x['regionName']}
+
+    end
+  end
+end

+ 50 - 0
lib/neoinfra/vpcs.rb

@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+require 'vpc'
+require 'accounts'
+require 'fog'
+require 'neo4j'
+require 'neoinfra/aws'
+require 'neoinfra/config'
+
+# NeoInfra Account information
+module NeoInfra
+  # Provide informations about the accounts available
+  class Vpcs
+    def load
+      aws = NeoInfra::Aws.new
+      @cfg = NeoInfra::Config.new
+
+      neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
+      Neo4j::Session.open(:server_db, neo4j_url)
+
+      @cfg.accounts.each do |account|
+        base_conf = {
+          provider: 'AWS',
+          aws_access_key_id: account[:key],
+          aws_secret_access_key: account[:secret]
+        }
+        aws.regions.each do |region|
+          region_conf = {:region => region['regionName'] }
+          new_conn = Fog::Compute.new(region_conf.merge(base_conf))
+          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)
+              vpc_id.save
+              AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first )
+            end
+          end
+        end
+      end
+    end
+  end
+end

+ 22 - 0
models/vpc.rb

@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Provide Neo4J Model for VPCs
+class Vpc
+  include Neo4j::ActiveNode
+  property :vpc_id, constraint: :unique
+  property :name
+  property :cidr
+  has_one :out, :region, rel_class: :VpcRegion
+  has_one :out, :owned, rel_class: :AccountVpc
+  has_many :out, :az, rel_class: :VpcAz
+end
+
+# Provide Neo4J Model for VPC Owners
+class AccountVpc
+  include Neo4j::ActiveRel
+  from_class :Vpc
+  to_class :AwsAccount
+  type :owned
+end