add temp cleanup function
This commit is contained in:
@@ -22,6 +22,10 @@ WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');
|
|||||||
SELECT * FROM "Inventory"
|
SELECT * FROM "Inventory"
|
||||||
WHERE "VmUuid" = sqlc.arg('vmUuid') AND "Datacenter" = sqlc.arg('datacenterName');
|
WHERE "VmUuid" = sqlc.arg('vmUuid') AND "Datacenter" = sqlc.arg('datacenterName');
|
||||||
|
|
||||||
|
-- name: GetInventoryVcUrl :many
|
||||||
|
SELECT * FROM "Inventory"
|
||||||
|
WHERE "Vcenter" = sqlc.arg('vc');
|
||||||
|
|
||||||
-- name: GetInventoryEventId :one
|
-- name: GetInventoryEventId :one
|
||||||
SELECT * FROM "Inventory"
|
SELECT * FROM "Inventory"
|
||||||
WHERE "CloudId" = ? LIMIT 1;
|
WHERE "CloudId" = ? LIMIT 1;
|
||||||
@@ -44,6 +48,11 @@ DELETE FROM "Inventory"
|
|||||||
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName')
|
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName')
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: InventoryCleanupVcenter :exec
|
||||||
|
DELETE FROM "Inventory"
|
||||||
|
WHERE "Vcenter" = sqlc.arg('vc')
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
-- name: CreateUpdate :one
|
-- name: CreateUpdate :one
|
||||||
INSERT INTO "Updates" (
|
INSERT INTO "Updates" (
|
||||||
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName"
|
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName"
|
||||||
|
@@ -348,6 +348,55 @@ func (q *Queries) GetInventoryEventId(ctx context.Context, cloudid sql.NullStrin
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getInventoryVcUrl = `-- name: GetInventoryVcUrl :many
|
||||||
|
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
|
||||||
|
WHERE "Vcenter" = ?1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetInventoryVcUrl(ctx context.Context, vc string) ([]Inventory, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getInventoryVcUrl, vc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []Inventory
|
||||||
|
for rows.Next() {
|
||||||
|
var i Inventory
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Iid,
|
||||||
|
&i.Name,
|
||||||
|
&i.Vcenter,
|
||||||
|
&i.VmId,
|
||||||
|
&i.EventKey,
|
||||||
|
&i.CloudId,
|
||||||
|
&i.CreationTime,
|
||||||
|
&i.DeletionTime,
|
||||||
|
&i.ResourcePool,
|
||||||
|
&i.VmType,
|
||||||
|
&i.Datacenter,
|
||||||
|
&i.Cluster,
|
||||||
|
&i.Folder,
|
||||||
|
&i.ProvisionedDisk,
|
||||||
|
&i.InitialVcpus,
|
||||||
|
&i.InitialRam,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PoweredOn,
|
||||||
|
&i.SrmPlaceholder,
|
||||||
|
&i.VmUuid,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const getInventoryVmId = `-- name: GetInventoryVmId :one
|
const getInventoryVmId = `-- name: GetInventoryVmId :one
|
||||||
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
|
SELECT Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid FROM "Inventory"
|
||||||
WHERE "VmId" = ?1 AND "Datacenter" = ?2
|
WHERE "VmId" = ?1 AND "Datacenter" = ?2
|
||||||
@@ -529,6 +578,17 @@ func (q *Queries) InventoryCleanup(ctx context.Context, arg InventoryCleanupPara
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const inventoryCleanupVcenter = `-- name: InventoryCleanupVcenter :exec
|
||||||
|
DELETE FROM "Inventory"
|
||||||
|
WHERE "Vcenter" = ?1
|
||||||
|
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, IsTemplate, PoweredOn, SrmPlaceholder, VmUuid
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) InventoryCleanupVcenter(ctx context.Context, vc string) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, inventoryCleanupVcenter, vc)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
|
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
|
||||||
UPDATE "Inventory"
|
UPDATE "Inventory"
|
||||||
SET "DeletionTime" = ?1
|
SET "DeletionTime" = ?1
|
||||||
|
76
server/handler/vcCleanup.go
Normal file
76
server/handler/vcCleanup.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Remove a specified VM from the inventory
|
||||||
|
func (h *Handler) VcCleanup(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Get the parameters
|
||||||
|
vcUrl := r.URL.Query().Get("vc_url")
|
||||||
|
if vcUrl != "" {
|
||||||
|
h.Logger.Debug("Checking inventory table for vCenter", "url", vcUrl)
|
||||||
|
_, err := h.Database.Queries().GetInventoryVcUrl(ctx, vcUrl)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
h.Logger.Error("No VMs found for vcenter", "url", vcUrl)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("No match to vcenter details specified. vc_url: '%s'", vcUrl),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
h.Logger.Error("Error checking for vcenter to cleanup", "error", err, "url", vcUrl)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Error checking for vcenter to cleanup. error: '%s'", err),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// delete the VMs
|
||||||
|
err = h.Database.Queries().InventoryCleanupVcenter(ctx, vcUrl)
|
||||||
|
if err != nil {
|
||||||
|
h.Logger.Error("Error cleaning up VMs from Inventory table", "error", err, "url", vcUrl)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Error cleaning up VMs from Inventory table. error: '%s'", err),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// Successful cleanup
|
||||||
|
h.Logger.Debug("VMs successfully removed from inventory for vcenter", "url", vcUrl)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "OK",
|
||||||
|
"message": fmt.Sprintf("Removed VMs from Inventory table for vcenter '%s'", vcUrl),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
h.Logger.Error("Parameters not correctly specified", "url", vcUrl)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Parameters not correctly specified. vc_url: '%s'", vcUrl),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
@@ -36,6 +36,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
|
|||||||
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
|
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
|
||||||
// temporary endpoint
|
// temporary endpoint
|
||||||
//mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
//mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
||||||
|
mux.HandleFunc("/api/cleanup/vcenter", h.VcCleanup)
|
||||||
|
|
||||||
mux.HandleFunc("/api/report/inventory", h.InventoryReportDownload)
|
mux.HandleFunc("/api/report/inventory", h.InventoryReportDownload)
|
||||||
mux.HandleFunc("/api/report/updates", h.UpdateReportDownload)
|
mux.HandleFunc("/api/report/updates", h.UpdateReportDownload)
|
||||||
|
Reference in New Issue
Block a user