add vm power state and template
This commit is contained in:
@@ -7,7 +7,7 @@ templ Header() {
|
|||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<meta name="description" content="Hello world"/>
|
<meta name="description" content="Hello world"/>
|
||||||
<title>Test Page</title>
|
<title>vCTP API</title>
|
||||||
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
|
<script src="/assets/js/htmx@v2.0.2.min.js"></script>
|
||||||
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
|
<link href={ "/assets/css/output@" + version.Value + ".css" } rel="stylesheet"/>
|
||||||
</head>
|
</head>
|
||||||
|
11
db/migrations/20240915232747_extend_inventory.sql
Normal file
11
db/migrations/20240915232747_extend_inventory.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE "Inventory" ADD COLUMN IsTemplate INTEGER;
|
||||||
|
ALTER TABLE "Inventory" ADD COLUMN PowerState INTEGER;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE "Updates" DROP COLUMN PowerState;
|
||||||
|
ALTER TABLE "Updates" DROP COLUMN IsTemplate;
|
||||||
|
-- +goose StatementEnd
|
@@ -23,6 +23,7 @@ type Events struct {
|
|||||||
DatacenterId sql.NullString
|
DatacenterId sql.NullString
|
||||||
ComputeResourceId sql.NullString
|
ComputeResourceId sql.NullString
|
||||||
VmName sql.NullString
|
VmName sql.NullString
|
||||||
|
EventType sql.NullString
|
||||||
}
|
}
|
||||||
|
|
||||||
type Inventory struct {
|
type Inventory struct {
|
||||||
@@ -43,6 +44,8 @@ type Inventory struct {
|
|||||||
InitialVcpus sql.NullInt64
|
InitialVcpus sql.NullInt64
|
||||||
InitialRam sql.NullInt64
|
InitialRam sql.NullInt64
|
||||||
SrmPlaceholder sql.NullInt64
|
SrmPlaceholder sql.NullInt64
|
||||||
|
IsTemplate sql.NullInt64
|
||||||
|
PowerState sql.NullInt64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Updates struct {
|
type Updates struct {
|
||||||
|
@@ -16,9 +16,9 @@ WHERE "EventId" = ? LIMIT 1;
|
|||||||
|
|
||||||
-- name: CreateInventory :one
|
-- name: CreateInventory :one
|
||||||
INSERT INTO "Inventory" (
|
INSERT INTO "Inventory" (
|
||||||
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder"
|
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "IsTemplate", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder", "PowerState"
|
||||||
) VALUES(
|
) VALUES(
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
)
|
)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
|
@@ -16,7 +16,7 @@ INSERT INTO "Events" (
|
|||||||
) VALUES(
|
) VALUES(
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
)
|
)
|
||||||
RETURNING Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName
|
RETURNING Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateEventParams struct {
|
type CreateEventParams struct {
|
||||||
@@ -65,17 +65,18 @@ func (q *Queries) CreateEvent(ctx context.Context, arg CreateEventParams) (Event
|
|||||||
&i.DatacenterId,
|
&i.DatacenterId,
|
||||||
&i.ComputeResourceId,
|
&i.ComputeResourceId,
|
||||||
&i.VmName,
|
&i.VmName,
|
||||||
|
&i.EventType,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const createInventory = `-- name: CreateInventory :one
|
const createInventory = `-- name: CreateInventory :one
|
||||||
INSERT INTO "Inventory" (
|
INSERT INTO "Inventory" (
|
||||||
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder"
|
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "IsTemplate", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder", "PowerState"
|
||||||
) VALUES(
|
) VALUES(
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
)
|
)
|
||||||
RETURNING Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder
|
RETURNING Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateInventoryParams struct {
|
type CreateInventoryParams struct {
|
||||||
@@ -87,6 +88,7 @@ type CreateInventoryParams struct {
|
|||||||
CreationTime sql.NullInt64
|
CreationTime sql.NullInt64
|
||||||
ResourcePool sql.NullString
|
ResourcePool sql.NullString
|
||||||
VmType sql.NullString
|
VmType sql.NullString
|
||||||
|
IsTemplate sql.NullInt64
|
||||||
Datacenter sql.NullString
|
Datacenter sql.NullString
|
||||||
Cluster sql.NullString
|
Cluster sql.NullString
|
||||||
Folder sql.NullString
|
Folder sql.NullString
|
||||||
@@ -94,6 +96,7 @@ type CreateInventoryParams struct {
|
|||||||
InitialVcpus sql.NullInt64
|
InitialVcpus sql.NullInt64
|
||||||
InitialRam sql.NullInt64
|
InitialRam sql.NullInt64
|
||||||
SrmPlaceholder sql.NullInt64
|
SrmPlaceholder sql.NullInt64
|
||||||
|
PowerState sql.NullInt64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams) (Inventory, error) {
|
func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams) (Inventory, error) {
|
||||||
@@ -106,6 +109,7 @@ func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams
|
|||||||
arg.CreationTime,
|
arg.CreationTime,
|
||||||
arg.ResourcePool,
|
arg.ResourcePool,
|
||||||
arg.VmType,
|
arg.VmType,
|
||||||
|
arg.IsTemplate,
|
||||||
arg.Datacenter,
|
arg.Datacenter,
|
||||||
arg.Cluster,
|
arg.Cluster,
|
||||||
arg.Folder,
|
arg.Folder,
|
||||||
@@ -113,6 +117,7 @@ func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams
|
|||||||
arg.InitialVcpus,
|
arg.InitialVcpus,
|
||||||
arg.InitialRam,
|
arg.InitialRam,
|
||||||
arg.SrmPlaceholder,
|
arg.SrmPlaceholder,
|
||||||
|
arg.PowerState,
|
||||||
)
|
)
|
||||||
var i Inventory
|
var i Inventory
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
@@ -133,6 +138,8 @@ func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams
|
|||||||
&i.InitialVcpus,
|
&i.InitialVcpus,
|
||||||
&i.InitialRam,
|
&i.InitialRam,
|
||||||
&i.SrmPlaceholder,
|
&i.SrmPlaceholder,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PowerState,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -184,7 +191,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getInventoryByName = `-- name: GetInventoryByName :many
|
const getInventoryByName = `-- name: GetInventoryByName :many
|
||||||
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
|
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState FROM "Inventory"
|
||||||
WHERE "Name" = ?
|
WHERE "Name" = ?
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -215,6 +222,8 @@ func (q *Queries) GetInventoryByName(ctx context.Context, name string) ([]Invent
|
|||||||
&i.InitialVcpus,
|
&i.InitialVcpus,
|
||||||
&i.InitialRam,
|
&i.InitialRam,
|
||||||
&i.SrmPlaceholder,
|
&i.SrmPlaceholder,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PowerState,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -230,7 +239,7 @@ func (q *Queries) GetInventoryByName(ctx context.Context, name string) ([]Invent
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getInventoryEventId = `-- name: GetInventoryEventId :one
|
const getInventoryEventId = `-- name: GetInventoryEventId :one
|
||||||
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
|
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState FROM "Inventory"
|
||||||
WHERE "EventId" = ? LIMIT 1
|
WHERE "EventId" = ? LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -255,12 +264,14 @@ func (q *Queries) GetInventoryEventId(ctx context.Context, eventid sql.NullStrin
|
|||||||
&i.InitialVcpus,
|
&i.InitialVcpus,
|
||||||
&i.InitialRam,
|
&i.InitialRam,
|
||||||
&i.SrmPlaceholder,
|
&i.SrmPlaceholder,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PowerState,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getInventoryVmId = `-- name: GetInventoryVmId :one
|
const getInventoryVmId = `-- name: GetInventoryVmId :one
|
||||||
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
|
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState FROM "Inventory"
|
||||||
WHERE "VmId" = ? LIMIT 1
|
WHERE "VmId" = ? LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -285,12 +296,14 @@ func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (In
|
|||||||
&i.InitialVcpus,
|
&i.InitialVcpus,
|
||||||
&i.InitialRam,
|
&i.InitialRam,
|
||||||
&i.SrmPlaceholder,
|
&i.SrmPlaceholder,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PowerState,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, 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 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"
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -318,6 +331,7 @@ func (q *Queries) ListEvents(ctx context.Context) ([]Events, error) {
|
|||||||
&i.DatacenterId,
|
&i.DatacenterId,
|
||||||
&i.ComputeResourceId,
|
&i.ComputeResourceId,
|
||||||
&i.VmName,
|
&i.VmName,
|
||||||
|
&i.EventType,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -333,7 +347,7 @@ func (q *Queries) ListEvents(ctx context.Context) ([]Events, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listInventory = `-- name: ListInventory :many
|
const listInventory = `-- name: ListInventory :many
|
||||||
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
|
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState FROM "Inventory"
|
||||||
ORDER BY "Name"
|
ORDER BY "Name"
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -364,6 +378,8 @@ func (q *Queries) ListInventory(ctx context.Context) ([]Inventory, error) {
|
|||||||
&i.InitialVcpus,
|
&i.InitialVcpus,
|
||||||
&i.InitialRam,
|
&i.InitialRam,
|
||||||
&i.SrmPlaceholder,
|
&i.SrmPlaceholder,
|
||||||
|
&i.IsTemplate,
|
||||||
|
&i.PowerState,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -379,7 +395,7 @@ func (q *Queries) ListInventory(ctx context.Context) ([]Inventory, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listUnprocessedEvents = `-- name: ListUnprocessedEvents :many
|
const listUnprocessedEvents = `-- name: ListUnprocessedEvents :many
|
||||||
SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName FROM "Events"
|
SELECT Eid, CloudId, Source, EventTime, ChainId, VmId, EventKey, DatacenterName, ComputeResourceName, UserName, Processed, DatacenterId, ComputeResourceId, VmName, EventType FROM "Events"
|
||||||
WHERE "Processed" = 0
|
WHERE "Processed" = 0
|
||||||
ORDER BY "EventTime"
|
ORDER BY "EventTime"
|
||||||
`
|
`
|
||||||
@@ -408,6 +424,7 @@ func (q *Queries) ListUnprocessedEvents(ctx context.Context) ([]Events, error) {
|
|||||||
&i.DatacenterId,
|
&i.DatacenterId,
|
||||||
&i.ComputeResourceId,
|
&i.ComputeResourceId,
|
||||||
&i.VmName,
|
&i.VmName,
|
||||||
|
&i.EventType,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,8 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
|
|||||||
totalDiskGB float64
|
totalDiskGB float64
|
||||||
srmPlaceholder int
|
srmPlaceholder int
|
||||||
foundVm bool
|
foundVm bool
|
||||||
|
isTemplate int
|
||||||
|
poweredOn int
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.Debug("Started Events processing", "time", time.Now())
|
logger.Debug("Started Events processing", "time", time.Now())
|
||||||
@@ -57,6 +59,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
|
|||||||
numRam = 0
|
numRam = 0
|
||||||
numVcpus = 0
|
numVcpus = 0
|
||||||
totalDiskGB = 0
|
totalDiskGB = 0
|
||||||
|
isTemplate = 0
|
||||||
} else {
|
} else {
|
||||||
c.Logger.Debug("found VM")
|
c.Logger.Debug("found VM")
|
||||||
srmPlaceholder = 0 // Default assumption
|
srmPlaceholder = 0 // Default assumption
|
||||||
@@ -80,11 +83,27 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
|
|||||||
srmPlaceholder = 1
|
srmPlaceholder = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if vmObject.Vm.Config.Template {
|
||||||
|
isTemplate = 1
|
||||||
|
} else {
|
||||||
|
isTemplate = 0
|
||||||
|
}
|
||||||
|
|
||||||
foundVm = true
|
foundVm = true
|
||||||
} else {
|
} else {
|
||||||
c.Logger.Error("Empty VM config")
|
c.Logger.Error("Empty VM config")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if (types.VirtualMachineRuntimeInfo{}) != vmObject.Vm.Runtime {
|
||||||
|
//if runtime, ok := vmObject.Vm.Runtime.(types.VirtualMachineRuntimeInfo); ok {
|
||||||
|
c.Logger.Debug("VM has runtime data", "power_state", vmObject.Vm.Runtime.PowerState)
|
||||||
|
if vmObject.Vm.Runtime.PowerState == "" {
|
||||||
|
poweredOn = 0
|
||||||
|
} else {
|
||||||
|
poweredOn = 1
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
|
||||||
}
|
}
|
||||||
err = vc.Logout()
|
err = vc.Logout()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -109,6 +128,8 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
|
|||||||
Folder: sql.NullString{String: vmObject.FolderPath, Valid: vmObject.FolderPath != ""},
|
Folder: sql.NullString{String: vmObject.FolderPath, Valid: vmObject.FolderPath != ""},
|
||||||
ResourcePool: sql.NullString{String: vmObject.ResourcePool, Valid: vmObject.ResourcePool != ""},
|
ResourcePool: sql.NullString{String: vmObject.ResourcePool, Valid: vmObject.ResourcePool != ""},
|
||||||
SrmPlaceholder: sql.NullInt64{Int64: int64(srmPlaceholder), Valid: true},
|
SrmPlaceholder: sql.NullInt64{Int64: int64(srmPlaceholder), Valid: true},
|
||||||
|
IsTemplate: sql.NullInt64{Int64: int64(isTemplate), Valid: true},
|
||||||
|
PowerState: sql.NullInt64{Int64: int64(poweredOn), Valid: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Logger.Debug("database params", "params", params)
|
c.Logger.Debug("database params", "params", params)
|
||||||
|
Reference in New Issue
Block a user