Forráskód Böngészése

start search functionality

Chris Mague 7 éve
szülő
commit
876d240ecf

+ 4 - 0
config.ru

@@ -19,3 +19,7 @@ end
 map '/view' do
   run Views
 end
+
+map '/search' do
+  run Search
+end

+ 32 - 2
lib/neoinfra/nodes.rb

@@ -11,11 +11,41 @@ require 'fog-aws'
 module NeoInfra
   # Provide informations about the accounts available
   class Nodes
-    def load_nodes
-      aws = NeoInfra::Aws.new
+
+    def initialize
       @cfg = NeoInfra::Config.new
       neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
       Neo4j::Session.open(:server_db, neo4j_url)
+    end
+
+    def display_node(node_id)
+      n = Node.where(node_id: node_id).first
+      return {
+        "Name"          => n.name,
+        "IP"            => n.ip,
+        "State"         => n.state,
+        "AMI"           => n.ami,
+        "Public_IP"     => n.public_ip,
+        "AZ"            => n.az.az,
+        "Account"       => n.account.name,
+        "Size"          => n.size,
+        "Subnet"        => n.subnet.name,
+        "VPC"           => n.subnet.subnet.name,
+        "SSH-Key"       => n.sshkey.name,
+        "SecurityGroup" => n.node_sg.name,
+      }
+    end
+
+    def search_nodes_by_ip(ip)
+      display_node(Node.where(ip: ip).first.node_id)
+    end
+
+    def search_nodes_by_node_id(node_id)
+      display_node(Node.where(node_id: node_id).first.node_id)
+    end
+
+    def load_nodes
+      aws = NeoInfra::Aws.new
 
       @cfg.accounts.each do |account|
         base_conf = {

+ 40 - 0
web/controllers/search.rb

@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+lib_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'lib')
+$LOAD_PATH.unshift(lib_dir) unless
+  $LOAD_PATH.include?(lib_dir) || $LOAD_PATH.include?(lib_dir)
+
+require 'json'
+require 'neoinfra'
+require 'sinatra'
+require 'sinatra/base'
+require 'sinatra/respond_to'
+
+# Handle loading data into the graph db
+class Search < Sinatra::Base
+  register Sinatra::RespondTo
+  set :views, File.join(File.dirname(__FILE__), '..', '/views')
+
+  post '/all' do
+    puts params.to_s
+    if params['search'] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
+      n = NeoInfra::Nodes.new
+      respond_to do |wants|
+        wants.html {
+          erb :view_node,
+          :layout => :base_layout,
+          :locals => {:node => n.search_nodes_by_ip(params['search'])}
+        }
+    end
+    elsif params['search'] =~ /i-[a-f0-9]{6,20}/
+      n = NeoInfra::Nodes.new
+      respond_to do |wants|
+        wants.html {
+          erb :view_node,
+          :layout => :base_layout,
+          :locals => {:node => n.search_nodes_by_node_id(params['search'])}
+        }
+    end
+    end
+  end
+end

+ 2 - 2
web/views/base_layout.html.erb

@@ -39,8 +39,8 @@
             </div>
           </li>
         </ul>
-        <form class="form-inline my-2 my-lg-0">
-          <input class="form-control mr-sm-2" type="text" placeholder="Search">
+        <form class="form-inline my-2 my-lg-0" action="/search/all" method="POST">
+          <input class="form-control mr-sm-2" type="text" name="search" placeholder="Search">
           <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
         </form>
       </div>

+ 38 - 0
web/views/view_node.html.erb

@@ -0,0 +1,38 @@
+<h2 class="title"><br><br><center>Node Information</center></h2>
+<div class="container">
+ <table class="table table-hover">
+    <thead>
+      <tr>
+        <th>Name</th>
+        <th>IP</th>
+        <th>Public IP</th>
+        <th>Account</th>
+        <th>Size</th>
+        <th>Subnet</th>
+        <th>AZ</th>
+        <th>VPC</th>
+        <th>SSH Key</th>
+        <th>AMI</th>
+        <th>State</th>
+        <th>Security Group</th>
+      </tr>
+    </thead>
+    <tbody>
+<tr>
+<td><%= node['Name'] %></td>
+<td><%= node['IP'] %></td>
+<td><%= node['Public_IP'] %></td>
+<td><%= node['Account'] %></td>
+<td><%= node['Size'] %></td>
+<td><%= node['Subnet'] %></td>
+<td><%= node['AZ'] %></td>
+<td><%= node['VPC'] %></td>
+<td><%= node['SSH-Key'] %></td>
+<td><%= node['AMI'] %></td>
+<td><%= node['State'] %></td>
+<td><%= node['SecurityGroup'] %></td>
+</tr>
+    </tbody>
+  </table>
+
+</div>