Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit ae859f8

Browse files
committed
Added Gene's project
1 parent 3b1ba60 commit ae859f8

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed

students/gene/project/node_app.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
3+
from node_db import Node, NodeStorage
4+
5+
6+
def main_menu_usage():
7+
print ("\n\n"
8+
"------------------------------------------------------\n"
9+
"| Divice Database |\n"
10+
"------------------------------------------------------"
11+
"\n\nSelect from one of these options:\n\n"
12+
"(1) Add Device\n"
13+
"(2) Remove Device\n"
14+
"(3) Create device inventory report\n"
15+
"(4) Quit\n")
16+
17+
18+
def main_menu():
19+
20+
while True:
21+
main_menu_usage()
22+
try:
23+
answer = int(input("Option: "))
24+
print("\nOption selected: {}\n\n".format(answer))
25+
except ValueError:
26+
answer = 0
27+
print("\n\n****ERROR: Please choose a numeric option****\n\n")
28+
continue
29+
30+
if answer == 4:
31+
print("\nQuiting.....")
32+
break
33+
34+
elif answer == 1:
35+
node_id = input("Device name: ").strip().upper()
36+
site_id = input("Site: ").strip().upper()
37+
management_ip = input("Management IP: ").strip()
38+
console_id = input("Console server: ").strip().upper()
39+
vendor_id = input("Vendor: ").strip().upper()
40+
platform_id = input("Platform: ").strip().upper()
41+
os_id = input("SW Version: ").strip()
42+
#import pdb; pdb.set_trace()
43+
device = ds.find_node(node_id)
44+
if device is None:
45+
device = Node(node_id, site_id, management_ip, console_id, vendor_id, platform_id, os_id)
46+
print("--------------------------")
47+
print(device.description)
48+
print("--------------------------")
49+
print("\n\n Please review and confirm above device information\n")
50+
confirmation = input(str("\ny/n: ").strip())
51+
if confirmation == "y":
52+
add_device = ds.add_node(device)
53+
# device = ds.add_node(node_id, site_id, management_ip, console_id, vendor_id, platform_id, os_id)
54+
print("\n\nDevice has been added to database\n\n")
55+
else:
56+
continue
57+
58+
else:
59+
print("\n\nDevice {} already exist in databse with below information\n\n".format(device.node_id))
60+
print("--------------------------")
61+
print(device.description)
62+
print("--------------------------")
63+
# print("\nAdding Device to Database...\n\n\n")
64+
# print("Generating Thank you note...\n\n\n")
65+
66+
67+
elif answer == 2:
68+
node_id = input("Device name: ").strip().upper()
69+
rm_device = ds.find_node(node_id)
70+
if rm_device is None:
71+
print("\n\nDevice {} does not exist...".format(node_id))
72+
else:
73+
rm_device = ds.remove_node(node_id)
74+
print("\n\nDevice {} has been removed".format(node_id))
75+
76+
elif answer == 3:
77+
ds.generate_report()
78+
79+
else:
80+
print("\n\n****ERROR: Invalid selection*******\n\n")
81+
82+
83+
84+
85+
if __name__ == "__main__":
86+
print("\n\nProgram starting... \n\n")
87+
ds = NodeStorage()
88+
main_menu()
89+

students/gene/project/node_db.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
import sqlite3
3+
from sqlite3 import Error
4+
5+
6+
class Node:
7+
8+
def __init__(self, node_id, site_id='', management_ip='', console_id='', vendor_id='', platform_id='', os_id=''):
9+
self.node_id = node_id
10+
self.site_id = site_id
11+
self.management_ip = management_ip
12+
self.console_id = console_id
13+
self.vendor_id = vendor_id
14+
self.platform_id = platform_id
15+
self.os_id = os_id
16+
17+
18+
@property
19+
def description(self):
20+
return ("""Node: {}
21+
Site: {}
22+
Management IP: {}
23+
Console: {}
24+
Vendor: {}
25+
Platform: {}
26+
SW Version: {}""".format(self.node_id, self.site_id, self.management_ip,
27+
self.console_id, self.vendor_id, self.platform_id,
28+
self.os_id))
29+
30+
31+
class NodeStorage:
32+
"""
33+
To test in database in memory use ':memory:'
34+
To use database use 'test.db'
35+
36+
"""
37+
38+
def __init__(self, database='test.db'):
39+
self.db = database
40+
self.conn = sqlite3.connect(self.db)
41+
self.create_table()
42+
43+
44+
def create_connection(self):
45+
try:
46+
conn = sqlite3.connect(self.db)
47+
return conn
48+
except Error as e:
49+
print(e)
50+
51+
return None
52+
53+
54+
def create_table(self):
55+
with self.conn:
56+
# import pdb;pdb.set_trace()
57+
c = self.conn.cursor()
58+
c.execute(""" CREATE TABLE IF NOT EXISTS node_db (
59+
node_id text,
60+
site_id text,
61+
management_ip text,
62+
console_id text,
63+
vendor_id text,
64+
platform_id text,
65+
os_id text
66+
);""")
67+
68+
69+
def add_node(self, node_info):
70+
"""
71+
node_info is an object of Node() class
72+
73+
"""
74+
# node_info = Node(node_id, site_id, management_ip, console_id, vendor_id, platform_id, os_id)
75+
# print(node_info.description())
76+
with self.conn:
77+
c = self.conn.cursor()
78+
c.execute("INSERT INTO node_db VALUES (:node_id, :site_id, :management_ip, :console_id,"
79+
" :vendor_id, :platform_id, :os_id)",
80+
{'node_id': node_info.node_id, 'site_id': node_info.site_id, 'management_ip': node_info.management_ip,
81+
'console_id': node_info.console_id, 'vendor_id': node_info.vendor_id, 'platform_id': node_info.platform_id,
82+
'os_id': node_info.os_id})
83+
84+
85+
def remove_node(self, node_id):
86+
with self.conn:
87+
c = self.conn.cursor()
88+
c.execute("DELETE FROM node_db WHERE node_id = :node_id", {'node_id': node_id})
89+
return node_id
90+
91+
92+
def find_node(self, node_id):
93+
with self.conn:
94+
c = self.conn.cursor()
95+
c.execute("SELECT * FROM node_db WHERE node_id = :node_id", {'node_id': node_id})
96+
node = c.fetchone()
97+
98+
if node:
99+
return Node(*node)
100+
else:
101+
return None
102+
103+
104+
def generate_report(self):
105+
with self.conn:
106+
c = self.conn.cursor()
107+
c.execute("SELECT * FROM node_db")
108+
all_devices = c.fetchall()
109+
110+
print("\n\n"
111+
"--------------------------------------------------------------------\n"
112+
"Node name | Site | Management Ip | Vendor | Platform | OS version \n"
113+
"--------------------------------------------------------------------")
114+
for node in all_devices:
115+
# print(donor)
116+
node_id = node[0]
117+
site_id = node[1]
118+
management_ip = node[2]
119+
vendor_id = node[3]
120+
platform_id = node[4]
121+
os_id = node[5]
122+
123+
print("{:12}{:7}{:16}{:10}{:11}{:11}".format(node_id, site_id, management_ip, vendor_id, platform_id, os_id))
124+
125+
print("--------------------------------------------------------------------\n")
126+
print("Total Devices: {}".format(len(all_devices)))
127+
print("\n\n\n\n")
128+
129+
130+
131+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from node_db import Node, NodeStorage
2+
import pytest
3+
4+
5+
6+
7+
@pytest.fixture
8+
def sample_data():
9+
10+
ds = NodeStorage()
11+
n1 = Node(node_id='TRNSOY30', site_id='SYO', management_ip='1.2.3.4.5', console_id='CACSYO123',
12+
vendor_id='Cisco', platform_id='IOS', os_id='5.2')
13+
n2 = Node(node_id='TRCAUS30', site_id='AUS', management_ip='1.2.3.4.5', console_id='CACSYO123',
14+
vendor_id='Cisco', platform_id='IOS', os_id='5.3')
15+
n3 = Node(node_id='TRADAL30', site_id='DAL', management_ip='1.2.3.4.5', console_id='CACSYO123',
16+
vendor_id='Cisco', platform_id='IOS', os_id='5.4')
17+
# print(donor1)
18+
ds.add_node(n1)
19+
ds.add_node(n2)
20+
ds.add_node(n3)
21+
return ds
22+
23+
24+
def test_find_node(sample_data):
25+
device = sample_data.find_node('TRNSOY30')
26+
assert device.node_id == 'TRNSOY30'
27+
28+
29+
def test_find_node_notthere(sample_data):
30+
device = sample_data.find_node('TGRIRV30')
31+
assert device is None
32+
33+
34+
def test_remove_node(sample_data):
35+
device = sample_data.remove_node('TRNSOY30')
36+
device2 = sample_data.find_node('TRNSOY30')
37+
assert device2 is None
38+

0 commit comments

Comments
 (0)