Bladeren bron

load and display Lambda functions

Chris Mague 7 jaren geleden
bovenliggende
commit
ddd30802a5
6 gewijzigde bestanden met toevoegingen van 137 en 1 verwijderingen
  1. 51 0
      lib/neoinfra/aws.rb
  2. 34 0
      models/lambdas.rb
  3. 9 1
      tasks/load_data.rake
  4. 12 0
      web/controllers/views.rb
  5. 1 0
      web/views/base_layout.html.erb
  6. 30 0
      web/views/view_lambdas.html.erb

+ 51 - 0
lib/neoinfra/aws.rb

@@ -9,6 +9,7 @@ require 'date'
 require 'ipaddr'
 require 'neo4j'
 require 'rds'
+require 'lambdas'
 require 'neoinfra/config'
 require 'neoinfra/cloudwatch'
 
@@ -203,6 +204,56 @@ module NeoInfra
       end
     end
 
+    def list_lambdas
+      lambdas = []
+      Lambda.all.each do |l|
+        lambdas <<  {
+          'name'           => l.name,
+          'runtime'        => l.runtime,
+          'handler'        => l.handler,
+          'lambda_timeout' => l.lambda_timeout,
+          'memorysize'     => l.memorysize,
+          'last_modified'  => l.last_modified,
+          'region'         => l.region.region,
+          'owner'          => l.owner.name
+        }
+      end
+      return lambdas
+    end
+
+    def load_lambda
+      @cfg.accounts.each do |account|
+        base_conf = {
+          aws_access_key_id: account[:key],
+          aws_secret_access_key: account[:secret]
+        }
+        self.regions.each do |region|
+          region_conf = { region: region }
+          begin
+            lambdas = Fog::AWS::Lambda.new(region_conf.merge(base_conf))
+            lambdas.list_functions.data[:body]['Functions'].each do |f|
+              next unless Lambda.where(name: f['FunctionArn']).empty?
+              l = Lambda.new(
+                  name:             f['FunctionName'],
+                  runtime:          f['Runtime'],
+                  lambda_timeout:   f['Timeout'],
+                  handler:          f['Handler'],
+                  memorysize:       f['MemorySize'],
+                  arn:              f['FunctionArn'],
+                  codesize:         f['CodeSize'],
+                  last_modified:    f['LastModified'],
+              )
+              l.save
+              LambdaAccount.create(from_node: l, to_node: AwsAccount.where(name: account[:name]).first)
+              LambdaRegion.create(from_node: l, to_node: Region.where(region: region).first)
+            end
+          rescue Exception => e
+            puts "Error with #{region}: #{e.message}"
+            next
+          end
+        end
+      end
+    end
 
     def list_dynamos
       dynamos = []

+ 34 - 0
models/lambdas.rb

@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Information on Lambda
+class Lambda
+  include Neo4j::ActiveNode
+  property :name
+  property :runtime
+  property :lambda_timeout
+  property :handler
+  property :memorysize
+  property :arn
+  property :codesize
+  property :last_modified
+  has_one :out, :region, rel_class: :LambdaRegion
+  has_one :out, :owner, rel_class: :LambdaAccount
+end
+
+# Map Lambda to Region
+class LambdaRegion
+  include Neo4j::ActiveRel
+  from_class :Lambda
+  to_class :Region
+  type :region
+end
+
+# Map Lambda to Region
+class LambdaAccount
+  include Neo4j::ActiveRel
+  from_class :Lambda
+  to_class :AwsAccount
+  type :owner
+end

+ 9 - 1
tasks/load_data.rake

@@ -57,6 +57,14 @@ namespace :load_data do
     j.load_dynamo
   end
 
+
+  desc 'Load Lambdas'
+  task :lambda do
+    puts 'loading Lambdas'
+    j = NeoInfra::Aws.new
+    j.load_lambda
+  end
+
   desc 'Load Everything'
-  task all: %i[accounts regions vpcs buckets security_groups nodes rds dynamo]
+  task all: %i[accounts regions vpcs buckets security_groups nodes rds dynamo lambda]
 end

+ 12 - 0
web/controllers/views.rb

@@ -48,4 +48,16 @@ class Views < Sinatra::Base
     end
   end
 
+  get '/lambdas' do
+    j = NeoInfra::Aws.new
+    respond_to do |wants|
+      wants.html {
+        erb :view_lambdas,
+        :layout => :base_layout,
+        :locals => {:lambdas => j.list_lambdas}
+      }
+    end
+  end
+
+
 end

+ 1 - 0
web/views/base_layout.html.erb

@@ -35,6 +35,7 @@
               <a class="dropdown-item" href="/view/vpcs">VPCs</a>
               <a class="dropdown-item" href="/view/buckets">S3 Buckets</a>
               <a class="dropdown-item" href="/view/dynamos">Dyanmos</a>
+              <a class="dropdown-item" href="/view/lambdas">Labmda Functions</a>
               <a class="dropdown-item" href="/audit/tags">Tag Audit</a>
               <a class="dropdown-item" href="/load/all">Load Data</a>
             </div>

+ 30 - 0
web/views/view_lambdas.html.erb

@@ -0,0 +1,30 @@
+<h2 class="title"><br><br><center>Lambda Functions</center></h2>
+
+<div class="container">
+  <table class="table table-hover">
+    <thead>
+      <tr>
+        <th>Name</th>
+        <th>Owner</th>
+        <th>Region</th>
+        <th>Handler</th>
+        <th>Memory</th>
+        <th>Timeout</th>
+        <th>Modified</th>
+      </tr>
+    </thead>
+    <tbody>
+<% lambdas.each do |lambda| %>
+<tr>
+<td><%= lambda['name'] %></td>
+<td><%= lambda['owner'] %></td>
+<td><%= lambda['region'] %></td>
+<td><%= lambda['handler'] %></td>
+<td><%= lambda['memorysize'] %></td>
+<td><%= lambda['lambda_timeout']%></td>
+<td><%= lambda['last_modified'] %></td>
+</tr>
+<% end %>
+    </tbody>
+  </table>
+</div>