store event type
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-26 14:21:07 +10:00
parent 00d474b937
commit 8931cb4891
7 changed files with 16 additions and 12 deletions

View File

@@ -47,9 +47,9 @@ RETURNING *;
-- name: CreateEvent :one -- name: CreateEvent :one
INSERT INTO "Events" ( INSERT INTO "Events" (
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventType", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
) VALUES( ) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
) )
RETURNING *; RETURNING *;

View File

@@ -28,9 +28,9 @@ func (q *Queries) CleanupUpdates(ctx context.Context, arg CleanupUpdatesParams)
const createEvent = `-- name: CreateEvent :one const createEvent = `-- name: CreateEvent :one
INSERT INTO "Events" ( INSERT INTO "Events" (
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventType", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
) VALUES( ) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
) )
RETURNING Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType RETURNING Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType
` `
@@ -42,6 +42,7 @@ type CreateEventParams struct {
ChainId string ChainId string
VmId sql.NullString VmId sql.NullString
VmName sql.NullString VmName sql.NullString
EventType sql.NullString
EventKey sql.NullString EventKey sql.NullString
DatacenterId sql.NullString DatacenterId sql.NullString
DatacenterName sql.NullString DatacenterName sql.NullString
@@ -58,6 +59,7 @@ func (q *Queries) CreateEvent(ctx context.Context, arg CreateEventParams) (Event
arg.ChainId, arg.ChainId,
arg.VmId, arg.VmId,
arg.VmName, arg.VmName,
arg.EventType,
arg.EventKey, arg.EventKey,
arg.DatacenterId, arg.DatacenterId,
arg.DatacenterName, arg.DatacenterName,

View File

@@ -68,6 +68,7 @@ func (h *Handler) VmCleanup(w http.ResponseWriter, r *http.Request) {
return return
} else { } else {
// Successful cleanup // Successful cleanup
h.Logger.Debug("VM successfully removed from inventory", "vm_name", vm.Name, "iid", vm.Iid, "vm_id", vmId, "datacenter_name", datacenterName)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{ json.NewEncoder(w).Encode(map[string]string{

View File

@@ -14,8 +14,8 @@ import (
models "vctp/server/models" models "vctp/server/models"
) )
// VmCreate receives the CloudEvent for a VM creation // VmCreateEvent receives the CloudEvent for a VM creation
func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) { func (h *Handler) VmCreateEvent(w http.ResponseWriter, r *http.Request) {
var ( var (
unixTimestamp int64 unixTimestamp int64
//numVcpus int32 //numVcpus int32
@@ -67,6 +67,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
Source: event.CloudEvent.Source, Source: event.CloudEvent.Source,
CloudId: event.CloudEvent.ID, CloudId: event.CloudEvent.ID,
EventTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}, EventTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
EventType: sql.NullString{String: event.CloudEvent.Type, Valid: event.CloudEvent.Type != ""},
ChainId: strconv.Itoa(e.ChainID), ChainId: strconv.Itoa(e.ChainID),
VmId: sql.NullString{String: e.VM.VM.Value, Valid: e.VM.VM.Value != ""}, VmId: sql.NullString{String: e.VM.VM.Value, Valid: e.VM.VM.Value != ""},
VmName: sql.NullString{String: e.VM.Name, Valid: e.VM.Name != ""}, VmName: sql.NullString{String: e.VM.Name, Valid: e.VM.Name != ""},

View File

@@ -13,7 +13,7 @@ import (
) )
// VmUpdate receives the CloudEvent for a VM modification or move // VmUpdate receives the CloudEvent for a VM modification or move
func (h *Handler) VmDelete(w http.ResponseWriter, r *http.Request) { func (h *Handler) VmDeleteEvent(w http.ResponseWriter, r *http.Request) {
var ( var (
deletedTimestamp int64 deletedTimestamp int64
) )

View File

@@ -18,8 +18,8 @@ import (
"github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/types"
) )
// VmModify receives the CloudEvent for a VM modification or move // VmModifyEvent receives the CloudEvent for a VM modification or move
func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) { func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
var configChanges []map[string]string var configChanges []map[string]string
params := queries.CreateUpdateParams{} params := queries.CreateUpdateParams{}
var unixTimestamp int64 var unixTimestamp int64

View File

@@ -22,9 +22,9 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
mux.Handle("/assets/", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir)))) mux.Handle("/assets/", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
mux.HandleFunc("/", h.Home) mux.HandleFunc("/", h.Home)
mux.HandleFunc("/api/event/vm/create", h.VmCreate) mux.HandleFunc("/api/event/vm/create", h.VmCreateEvent)
mux.HandleFunc("/api/event/vm/modify", h.VmModify) mux.HandleFunc("/api/event/vm/modify", h.VmModifyEvent)
mux.HandleFunc("/api/event/vm/delete", h.VmDelete) mux.HandleFunc("/api/event/vm/delete", h.VmDeleteEvent)
mux.HandleFunc("/api/import/vm", h.VmImport) mux.HandleFunc("/api/import/vm", h.VmImport)
// Use this when we need to manually remove a VM from the database to clean up // Use this when we need to manually remove a VM from the database to clean up
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup) mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)