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

This commit is contained in:
2024-09-16 10:50:05 +10:00
parent 57980a860a
commit 282459ccf8
4 changed files with 82 additions and 7 deletions

View File

@@ -22,6 +22,11 @@ INSERT INTO "Inventory" (
) )
RETURNING *; RETURNING *;
-- name: InventoryMarkDeleted :exec
UPDATE "Inventory"
SET "DeletionTime" = sqlc.arg('deletionTime')
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');
-- name: CreateUpdate :one -- name: CreateUpdate :one
INSERT INTO "Updates" ( INSERT INTO "Updates" (
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool" "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool"

View File

@@ -302,6 +302,23 @@ func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (In
return i, err return i, err
} }
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
UPDATE "Inventory"
SET "DeletionTime" = ?1
WHERE "VmId" = ?2 AND "Datacenter" = ?3
`
type InventoryMarkDeletedParams struct {
DeletionTime sql.NullInt64
VmId sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) InventoryMarkDeleted(ctx context.Context, arg InventoryMarkDeletedParams) error {
_, err := q.db.ExecContext(ctx, inventoryMarkDeleted, arg.DeletionTime, arg.VmId, arg.DatacenterName)
return err
}
const listEvents = `-- name: ListEvents :many const listEvents = `-- name: ListEvents :many
SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType FROM "Events" SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType FROM "Events"
ORDER BY "EventTime" ORDER BY "EventTime"

View File

@@ -1,21 +1,75 @@
package handler package handler
import ( import (
"context"
"database/sql"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"time"
"vctp/db/queries"
models "vctp/server/models"
) )
// 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) VmDelete(w http.ResponseWriter, r *http.Request) {
var (
deletedTimestamp int64
)
reqBody, err := io.ReadAll(r.Body) reqBody, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
h.Logger.Error("Invalid data received", "error", err)
fmt.Fprintf(w, "Invalid data received") fmt.Fprintf(w, "Invalid data received")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} else {
h.Logger.Debug("received input data", "length", len(reqBody))
} }
h.Logger.Debug("received delete request", "body", string(reqBody)) // Decode the JSON body into CloudEventReceived struct
w.WriteHeader(http.StatusOK) var event models.CloudEventReceived
fmt.Fprintf(w, "Delete Request (%d): %v\n", len(reqBody), string(reqBody)) if err := json.Unmarshal(reqBody, &event); err != nil {
h.Logger.Error("unable to decode json", "error", err)
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
} else {
h.Logger.Debug("successfully decoded JSON")
prettyPrint(event)
}
// Use the event CreatedTime to update the DeletionTime column in the VM inventory table
// Parse the datetime string to a time.Time object
eventTime, err := time.Parse(time.RFC3339, event.CloudEvent.Data.CreatedTime)
if err != nil {
h.Logger.Warn("unable to convert cloud event time to timestamp", "error", err)
deletedTimestamp = time.Now().Unix()
} else {
// Convert to Unix timestamp
deletedTimestamp = eventTime.Unix()
}
// create the database parameters
params := queries.InventoryMarkDeletedParams{
DeletionTime: sql.NullInt64{Int64: deletedTimestamp, Valid: deletedTimestamp > 0},
VmId: sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""},
DatacenterName: sql.NullString{String: event.CloudEvent.Data.Datacenter.Name, Valid: event.CloudEvent.Data.Datacenter.Name != ""},
}
h.Logger.Debug("database params", "params", params)
err = h.Database.Queries().InventoryMarkDeleted(context.Background(), params)
if err != nil {
h.Logger.Error("Error received marking VM as deleted", "error", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Delete Request unsuccessful %s\n", err)
} else {
w.WriteHeader(http.StatusOK)
// TODO - return some JSON
fmt.Fprintf(w, "Processed VM Deletion event successfully")
}
//h.Logger.Debug("received delete request", "body", string(reqBody))
//w.WriteHeader(http.StatusOK)
//fmt.Fprintf(w, "Delete Request (%d): %v\n", len(reqBody), string(reqBody))
} }

View File

@@ -2,7 +2,6 @@ package models
import ( import (
"encoding/json" "encoding/json"
"time"
) )
type CloudEventReceived struct { type CloudEventReceived struct {
@@ -11,7 +10,7 @@ type CloudEventReceived struct {
Specversion string `json:"specversion"` Specversion string `json:"specversion"`
Source string `json:"source"` Source string `json:"source"`
Type string `json:"type"` Type string `json:"type"`
Time string `json:"time"` Time string `json:"time"` // Modified from time.Time
Data struct { Data struct {
ChainID int `json:"ChainId"` ChainID int `json:"ChainId"`
ChangeTag string `json:"ChangeTag"` ChangeTag string `json:"ChangeTag"`
@@ -22,7 +21,7 @@ type CloudEventReceived struct {
} `json:"ComputeResource"` } `json:"ComputeResource"`
Name string `json:"Name"` Name string `json:"Name"`
} `json:"ComputeResource"` } `json:"ComputeResource"`
CreatedTime time.Time `json:"CreatedTime"` CreatedTime string `json:"CreatedTime"` // Modified from time.Time
Datacenter struct { Datacenter struct {
Datacenter struct { Datacenter struct {
Type string `json:"Type"` Type string `json:"Type"`
@@ -59,7 +58,7 @@ type CloudEventReceived struct {
} `json:"Vm"` } `json:"Vm"`
} `json:"Vm"` } `json:"Vm"`
ConfigSpec *json.RawMessage `json:"configSpec"` ConfigSpec *json.RawMessage `json:"configSpec"`
ConfigChanges *ConfigChangesReceived `json:"configChanges"` ConfigChanges *ConfigChangesReceived `json:"configChanges"` // Modified to separate struct
} `json:"data"` } `json:"data"`
} `json:"cloudEvent"` } `json:"cloudEvent"`
} }