handle moves of VMs not in inventory
This commit is contained in:
@@ -106,16 +106,18 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
params.UpdateType = "reconfigure"
|
params.UpdateType = "reconfigure"
|
||||||
}
|
}
|
||||||
case "config.managedBy": // This changes when a VM becomes a placeholder or vice versa
|
case "config.managedBy": // This changes when a VM becomes a placeholder or vice versa
|
||||||
|
|
||||||
if change["newValue"] == "(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\")" {
|
if change["newValue"] == "(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\")" {
|
||||||
params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true}
|
params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true}
|
||||||
|
h.Logger.Debug("placeholderVm")
|
||||||
changeFound = true
|
changeFound = true
|
||||||
params.UpdateType = "srm"
|
params.UpdateType = "srm"
|
||||||
} else if change["newValue"] == "<unset>" {
|
} else if change["newValue"] == "<unset>" {
|
||||||
params.PlaceholderChange = sql.NullString{String: "Vm", Valid: true}
|
params.PlaceholderChange = sql.NullString{String: "Vm", Valid: true}
|
||||||
|
h.Logger.Debug("vm")
|
||||||
changeFound = true
|
changeFound = true
|
||||||
params.UpdateType = "srm"
|
params.UpdateType = "srm"
|
||||||
} else if change["newValue"] == "testVm" {
|
} else if change["newValue"] == "testVm" {
|
||||||
|
h.Logger.Debug("testVm")
|
||||||
params.PlaceholderChange = sql.NullString{String: "testVm", Valid: true}
|
params.PlaceholderChange = sql.NullString{String: "testVm", Valid: true}
|
||||||
changeFound = true
|
changeFound = true
|
||||||
params.UpdateType = "srm"
|
params.UpdateType = "srm"
|
||||||
@@ -217,15 +219,15 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
// TODO Add a record to the inventory table for this VM
|
// TODO Add a record to the inventory table for this VM
|
||||||
h.Logger.Info("Received VM modify event for a VM not currently in the inventory. Adding to inventory")
|
h.Logger.Info("Received VM modify event for a VM not currently in the inventory. Adding to inventory")
|
||||||
|
|
||||||
iid, err := h.AddVmToInventory(event, ctx, unixTimestamp)
|
iid, err2 := h.AddVmToInventory(event, ctx, unixTimestamp)
|
||||||
if err != nil {
|
if err2 != nil {
|
||||||
h.Logger.Error("Received error adding VM to inventory", "error", err)
|
h.Logger.Error("Received error adding VM to inventory", "error", err2)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
"status": "ERROR",
|
"status": "ERROR",
|
||||||
"message": fmt.Sprintf("Valid request but experienced error adding vm id '%s' in datacenter name '%s' to inventory table : %s",
|
"message": fmt.Sprintf("Valid request but experienced error adding vm id '%s' in datacenter name '%s' to inventory table : %s",
|
||||||
event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name, err),
|
event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name, err2),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -66,8 +67,39 @@ func (h *Handler) VmMoveEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
invResult, err := h.Database.Queries().GetInventoryVmId(ctx, invParams)
|
invResult, err := h.Database.Queries().GetInventoryVmId(ctx, invParams)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// If a VM is being moved it must exist, so lets add an inventory record for this VM
|
||||||
h.Logger.Error("unable to find existing inventory record for this VM", "error", err)
|
h.Logger.Error("unable to find existing inventory record for this VM", "error", err)
|
||||||
// TODO - how to handle?
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
// Add a record to the inventory table for this VM
|
||||||
|
h.Logger.Info("Received VM modify event for a VM not currently in the inventory. Adding to inventory")
|
||||||
|
|
||||||
|
iid, err2 := h.AddVmToInventory(event, ctx, unixTimestamp)
|
||||||
|
if err2 != nil {
|
||||||
|
h.Logger.Error("Received error adding VM to inventory", "error", err2)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Valid request but experienced error adding vm id '%s' in datacenter name '%s' to inventory table : %s",
|
||||||
|
event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name, err2),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if iid > 0 {
|
||||||
|
params.InventoryId = sql.NullInt64{Int64: iid, Valid: iid > 0}
|
||||||
|
} else {
|
||||||
|
h.Logger.Error("Received zero for inventory id when adding VM to inventory")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ERROR",
|
||||||
|
"message": fmt.Sprintf("Valid request but received zero result when adding vm id '%s' in datacenter name '%s' to inventory table",
|
||||||
|
event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0}
|
params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user