2024-07-11 15:40:45 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import shutil
|
2024-07-15 14:23:14 +00:00
|
|
|
import xapian
|
2024-07-11 15:40:45 +00:00
|
|
|
import hashlib
|
2024-07-01 10:17:23 +00:00
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
from datetime import datetime
|
2024-07-15 14:23:14 +00:00
|
|
|
from snapshot.models import Snapshot, Annotation
|
|
|
|
from snapshot.xapian import search, indexer, database
|
2024-07-01 10:17:23 +00:00
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
HID_ALGO1 = [
|
2024-07-11 15:40:45 +00:00
|
|
|
"manufacturer",
|
|
|
|
"model",
|
|
|
|
"chassis",
|
|
|
|
"serialNumber",
|
|
|
|
"sku"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class Build:
|
|
|
|
def __init__(self, snapshot_json, user):
|
2024-07-01 10:17:23 +00:00
|
|
|
self.json = snapshot_json
|
2024-07-11 15:40:45 +00:00
|
|
|
self.user = user
|
|
|
|
self.hid = None
|
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
self.index()
|
|
|
|
self.create_annotation()
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
def index(self):
|
|
|
|
matches = search(self.json['uuid'], limit=1)
|
|
|
|
if matches.size() > 0:
|
2024-07-11 15:40:45 +00:00
|
|
|
return
|
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
snap = json.dumps(self.json)
|
|
|
|
doc = xapian.Document()
|
|
|
|
doc.set_data(snap)
|
|
|
|
|
|
|
|
indexer.set_document(doc)
|
|
|
|
indexer.index_text(snap)
|
|
|
|
|
|
|
|
# Add the document to the database.
|
|
|
|
database.add_document(doc)
|
|
|
|
|
|
|
|
def get_hid_14(self):
|
|
|
|
device = self.json['device']
|
|
|
|
manufacturer = device.get("manufacturer", '')
|
|
|
|
model = device.get("model", '')
|
|
|
|
chassis = device.get("chassis", '')
|
|
|
|
serial_number = device.get("serialNumber", '')
|
|
|
|
sku = device.get("sku", '')
|
|
|
|
hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}"
|
|
|
|
return hashlib.sha3_256(hid.encode()).hexdigest()
|
|
|
|
|
|
|
|
def create_annotation(self):
|
|
|
|
uuid = self.json['uuid']
|
|
|
|
owner = self.user
|
|
|
|
key = 'hidalgo1'
|
|
|
|
value = self.get_hid_14()
|
|
|
|
Annotation.objects.create(
|
|
|
|
uuid=uuid,
|
|
|
|
owner=owner,
|
|
|
|
key=key,
|
|
|
|
value=value
|
2024-07-11 15:40:45 +00:00
|
|
|
)
|
|
|
|
|