Browse Source

collect information on the s3 buckets

Chris Mague 7 years ago
parent
commit
2f106f5489
4 changed files with 58 additions and 1 deletions
  1. 1 0
      Gemfile
  2. 8 1
      Rakefile
  3. 22 0
      lib/neoinfra/aws.rb
  4. 27 0
      models/s3.rb

+ 1 - 0
Gemfile

@@ -5,6 +5,7 @@ source 'http://rubygems.org'
 gem 'fog'
 gem 'neo4j', '7.2.0'
 gem 'rspec'
+gem 'mime-types'
 gem 'rubytree'
 
 group :development do

+ 8 - 1
Rakefile

@@ -44,6 +44,13 @@ task :load_regions do
   j.load_regions
 end
 
+desc 'Load S3 Buckets'
+task :load_buckets do
+  j = NeoInfra::Aws.new
+  j.load_buckets
+end
+
+
 desc 'Load Everything'
-task load_all: %i[load_accounts load_regions load_vpcs]
+task load_all: %i[load_accounts load_regions load_vpcs load_buckets]
 task full_test: %i[rubocop spec]

+ 22 - 0
lib/neoinfra/aws.rb

@@ -2,7 +2,9 @@
 
 require 'accounts'
 require 'regions'
+require 'mime-types'
 require 'fog'
+require 's3'
 require 'neo4j'
 require 'neoinfra/config'
 
@@ -54,5 +56,25 @@ module NeoInfra
       end
     end
 
+    def load_buckets
+      @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]
+        }
+        s = Fog::Storage.new(base_conf)
+        s.directories.each do |bucket|
+          next unless Bucket.where(name: bucket.key).empty?
+          b = Bucket.new(name: bucket.key)
+          b.save
+          BucketRegion.create(from_node: b, to_node: Region.where(region: bucket.location).first )
+          BucketAccount.create(from_node: b, to_node: AwsAccount.where(name: account[:name]).first )
+        end
+      end
+    end
   end
 end

+ 27 - 0
models/s3.rb

@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Information on Bucket
+class Bucket
+  include Neo4j::ActiveNode
+  property :name, constraint: :unique
+  has_one :out, :region, rel_class: :BucketRegion
+  has_one :out, :owner, rel_class: :BucketAccount
+end
+
+# Map Bucket to Region
+class BucketRegion
+  include Neo4j::ActiveRel
+  from_class :Bucket
+  to_class :Region
+  type :region
+end
+
+# Map Bucket to Region
+class BucketAccount
+  include Neo4j::ActiveRel
+  from_class :Bucket
+  to_class :AwsAccount
+  type :owner
+end