add vm name and placeholder change to updates table
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
-- +goose StatementBegin
|
-- +goose StatementBegin
|
||||||
ALTER TABLE "Updates" ADD COLUMN PlaceholderChange TEXT;
|
ALTER TABLE "Updates" ADD COLUMN PlaceholderChange TEXT;
|
||||||
|
ALTER TABLE "Updates" ADD COLUMN Name TEXT;
|
||||||
-- +goose StatementEnd
|
-- +goose StatementEnd
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
-- +goose StatementBegin
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE "Updates" DROP COLUMN Name;
|
||||||
ALTER TABLE "Updates" DROP COLUMN PlaceholderChange;
|
ALTER TABLE "Updates" DROP COLUMN PlaceholderChange;
|
||||||
-- +goose StatementEnd
|
-- +goose StatementEnd
|
||||||
|
@@ -73,4 +73,5 @@ type Updates struct {
|
|||||||
NewProvisionedDisk sql.NullFloat64
|
NewProvisionedDisk sql.NullFloat64
|
||||||
UserName sql.NullString
|
UserName sql.NullString
|
||||||
PlaceholderChange sql.NullString
|
PlaceholderChange sql.NullString
|
||||||
|
Name sql.NullString
|
||||||
}
|
}
|
||||||
|
@@ -60,9 +60,9 @@ RETURNING *;
|
|||||||
|
|
||||||
-- name: CreateUpdate :one
|
-- name: CreateUpdate :one
|
||||||
INSERT INTO "Updates" (
|
INSERT INTO "Updates" (
|
||||||
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange"
|
"InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange"
|
||||||
) VALUES(
|
) VALUES(
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
)
|
)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
|
@@ -13,7 +13,7 @@ import (
|
|||||||
const cleanupUpdates = `-- name: CleanupUpdates :exec
|
const cleanupUpdates = `-- name: CleanupUpdates :exec
|
||||||
DELETE FROM "Updates"
|
DELETE FROM "Updates"
|
||||||
WHERE "UpdateType" = ?1 AND "UpdateTime" <= ?2
|
WHERE "UpdateType" = ?1 AND "UpdateTime" <= ?2
|
||||||
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange
|
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name
|
||||||
`
|
`
|
||||||
|
|
||||||
type CleanupUpdatesParams struct {
|
type CleanupUpdatesParams struct {
|
||||||
@@ -210,15 +210,16 @@ func (q *Queries) CreateInventoryHistory(ctx context.Context, arg CreateInventor
|
|||||||
|
|
||||||
const createUpdate = `-- name: CreateUpdate :one
|
const createUpdate = `-- name: CreateUpdate :one
|
||||||
INSERT INTO "Updates" (
|
INSERT INTO "Updates" (
|
||||||
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange"
|
"InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange"
|
||||||
) VALUES(
|
) VALUES(
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
)
|
)
|
||||||
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange
|
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateUpdateParams struct {
|
type CreateUpdateParams struct {
|
||||||
InventoryId sql.NullInt64
|
InventoryId sql.NullInt64
|
||||||
|
Name sql.NullString
|
||||||
EventKey sql.NullString
|
EventKey sql.NullString
|
||||||
EventId sql.NullString
|
EventId sql.NullString
|
||||||
UpdateTime sql.NullInt64
|
UpdateTime sql.NullInt64
|
||||||
@@ -234,6 +235,7 @@ type CreateUpdateParams struct {
|
|||||||
func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) {
|
func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) {
|
||||||
row := q.db.QueryRowContext(ctx, createUpdate,
|
row := q.db.QueryRowContext(ctx, createUpdate,
|
||||||
arg.InventoryId,
|
arg.InventoryId,
|
||||||
|
arg.Name,
|
||||||
arg.EventKey,
|
arg.EventKey,
|
||||||
arg.EventId,
|
arg.EventId,
|
||||||
arg.UpdateTime,
|
arg.UpdateTime,
|
||||||
@@ -259,6 +261,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd
|
|||||||
&i.NewProvisionedDisk,
|
&i.NewProvisionedDisk,
|
||||||
&i.UserName,
|
&i.UserName,
|
||||||
&i.PlaceholderChange,
|
&i.PlaceholderChange,
|
||||||
|
&i.Name,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -569,7 +572,7 @@ func (q *Queries) GetReportInventory(ctx context.Context) ([]Inventory, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getReportUpdates = `-- name: GetReportUpdates :many
|
const getReportUpdates = `-- name: GetReportUpdates :many
|
||||||
SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange FROM "Updates"
|
SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name FROM "Updates"
|
||||||
ORDER BY "UpdateTime"
|
ORDER BY "UpdateTime"
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -595,6 +598,7 @@ func (q *Queries) GetReportUpdates(ctx context.Context) ([]Updates, error) {
|
|||||||
&i.NewProvisionedDisk,
|
&i.NewProvisionedDisk,
|
||||||
&i.UserName,
|
&i.UserName,
|
||||||
&i.PlaceholderChange,
|
&i.PlaceholderChange,
|
||||||
|
&i.Name,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@@ -191,7 +191,7 @@ func main() {
|
|||||||
logger.Debug("Created event processing cron job", "job", job.ID())
|
logger.Debug("Created event processing cron job", "job", job.ID())
|
||||||
|
|
||||||
// start background checks of vcenter inventory
|
// start background checks of vcenter inventory
|
||||||
startsAt2 := time.Now().Add(time.Second * 30)
|
startsAt2 := time.Now().Add(time.Second * 300)
|
||||||
job2, err := c.NewJob(
|
job2, err := c.NewJob(
|
||||||
gocron.DurationJob(cronInvFrequency),
|
gocron.DurationJob(cronInvFrequency),
|
||||||
gocron.NewTask(func() {
|
gocron.NewTask(func() {
|
||||||
|
@@ -104,8 +104,17 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
|
params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
|
||||||
params.UpdateType = "reconfigure"
|
params.UpdateType = "reconfigure"
|
||||||
}
|
}
|
||||||
case "config.managedBy":
|
case "config.managedBy": // This changes when a VM becomes a placeholder or vice versa
|
||||||
// This changes when a VM becomes a placeholder or vice versa
|
found = true
|
||||||
|
params.UpdateType = "srm"
|
||||||
|
|
||||||
|
if change["newValue"] == "(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\")" {
|
||||||
|
params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true}
|
||||||
|
} else if change["newValue"] == "<unset>" {
|
||||||
|
params.PlaceholderChange = sql.NullString{String: "Vm", Valid: true}
|
||||||
|
} else {
|
||||||
|
h.Logger.Error("Unexpected value for managedBy configuration", "new_value", change["newValue"])
|
||||||
|
}
|
||||||
|
|
||||||
// map[newValue:(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\") type:config.managedBy]
|
// map[newValue:(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\") type:config.managedBy]
|
||||||
// map[newValue:\"testVm\" type:config.managedBy.type]
|
// map[newValue:\"testVm\" type:config.managedBy.type]
|
||||||
@@ -187,12 +196,14 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// populate other parameters for the Update database record
|
// populate other parameters for the Update database record
|
||||||
|
params.Name = sql.NullString{String: event.CloudEvent.Data.VM.Name, Valid: event.CloudEvent.Data.VM.Name != ""}
|
||||||
params.EventId = sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""}
|
params.EventId = sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""}
|
||||||
params.EventKey = sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: event.CloudEvent.Data.Key > 0}
|
params.EventKey = sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: event.CloudEvent.Data.Key > 0}
|
||||||
params.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}
|
params.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}
|
||||||
params.UserName = sql.NullString{String: event.CloudEvent.Data.UserName, Valid: event.CloudEvent.Data.UserName != ""}
|
params.UserName = sql.NullString{String: event.CloudEvent.Data.UserName, Valid: event.CloudEvent.Data.UserName != ""}
|
||||||
|
|
||||||
// Create the Update database record
|
// Create the Update database record
|
||||||
|
h.Logger.Debug("Adding Update record", "params", params)
|
||||||
result, err := h.Database.Queries().CreateUpdate(ctx, params)
|
result, err := h.Database.Queries().CreateUpdate(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.Logger.Error("unable to perform database insert", "error", err)
|
h.Logger.Error("unable to perform database insert", "error", err)
|
||||||
|
Reference in New Issue
Block a user