package handler import ( "context" "database/sql" "encoding/json" "errors" "fmt" "io" "net/http" "regexp" "strconv" "strings" "time" "vctp/db/queries" "vctp/internal/vcenter" models "vctp/server/models" "github.com/vmware/govmomi/vim25/types" ) // VmModifyEvent receives the CloudEvent for a VM modification or move func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) { var configChanges []map[string]string params := queries.CreateUpdateParams{} var unixTimestamp int64 re := regexp.MustCompile(`/([^/]+)/[^/]+\.vmdk$`) ctx := context.Background() reqBody, err := io.ReadAll(r.Body) if err != nil { h.Logger.Error("Invalid data received", "length", len(reqBody), "error", err) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(map[string]string{ "status": "ERROR", "message": fmt.Sprintf("Invalid data received: '%s'", err), }) return } // Decode the JSON body into CloudEventReceived struct var event models.CloudEventReceived if err := json.Unmarshal(reqBody, &event); err != nil { h.Logger.Error("Unable to decode json request body", "length", len(reqBody), "error", err) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(map[string]string{ "status": "ERROR", "message": fmt.Sprintf("Unable to decode json request body: '%s'", err), }) return } else { //h.Logger.Debug("successfully decoded JSON") //prettyPrint(event) } if event.CloudEvent.Data.ConfigChanges == nil { h.Logger.Warn("Received event contains no config change") prettyPrint(event) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(map[string]string{ "status": "OK", "message": fmt.Sprintf("Received update event successfully but no config changes were found"), }) return } else { h.Logger.Debug("Received event contains config change info", "source", event.CloudEvent.Source, "id", event.CloudEvent.ID, "vm", event.CloudEvent.Data.VM.Name, "user_name", event.CloudEvent.Data.UserName) // Try to decode the config changes data var testConfig models.ConfigSpec if err := json.Unmarshal(*event.CloudEvent.Data.ConfigSpec, &testConfig); err != nil { h.Logger.Warn("unable to decode ConfigSpec json", "error", err) } else { h.Logger.Debug("successfully decoded ConfigSpec JSON") } configChanges = h.processConfigChanges(event.CloudEvent.Data.ConfigChanges.Modified) //prettyPrint(configChanges) var changeFound = false // Only interested in vCPU or ram changes currently for _, change := range configChanges { //fmt.Printf("Type: %s, New Value: %s\n", change["type"], change["newValue"]) switch change["type"] { case "config.hardware.numCPU": i, err := strconv.ParseInt(change["newValue"], 10, 64) if err != nil { h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"]) } else { changeFound = true params.NewVcpus = sql.NullInt64{Int64: i, Valid: i > 0} params.UpdateType = "reconfigure" } case "config.hardware.memoryMB": i, err := strconv.ParseInt(change["newValue"], 10, 64) if err != nil { h.Logger.Error("Unable to convert new value to int64", "new_value", change["newValue"]) } else { changeFound = true params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0} params.UpdateType = "reconfigure" } case "config.managedBy": // This changes when a VM becomes a placeholder or vice versa if change["newValue"] == "(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\")" { params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true} h.Logger.Debug("placeholderVm") changeFound = true params.UpdateType = "srm" } else if change["newValue"] == "" { params.PlaceholderChange = sql.NullString{String: "Vm", Valid: true} h.Logger.Debug("vm") changeFound = true params.UpdateType = "srm" } else if change["newValue"] == "testVm" { h.Logger.Debug("testVm") params.PlaceholderChange = sql.NullString{String: "testVm", Valid: true} changeFound = true params.UpdateType = "srm" } 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:\"testVm\" type:config.managedBy.type] // [map[newValue:\"placeholderVm\" type:config.managedBy.type] // map[newValue: type:config.managedBy] // config.managedBy.type: "testVm" -> "placeholderVm" // TODO - track when this happens, maybe need a new database column? case "config.managedBy.type": h.Logger.Debug("config.managedBy.type") if change["newValue"] == "testVm" { h.Logger.Debug("testVm") params.PlaceholderChange = sql.NullString{String: "testVm", Valid: true} changeFound = true params.UpdateType = "srm" } else if change["newValue"] == "\\\"placeholderVm\\\"" { h.Logger.Debug("placeholderVm") params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true} changeFound = true params.UpdateType = "srm" } } // Check if a disk was added (or maybe removed?) if strings.Contains(change["type"], "config.hardware.device") && (strings.Contains(event.CloudEvent.Data.FullFormattedMessage, ".vmdk") || strings.Contains(event.CloudEvent.Data.FullFormattedMessage, "capacityInKB")) { var diskChangeFound = false if testConfig.DeviceChange != nil { for i := range testConfig.DeviceChange { if testConfig.DeviceChange[i].Device.Backing != nil { h.Logger.Debug("Found backing in configspec", "backing", testConfig.DeviceChange[i].Device.Backing) // Find the match backingFile := testConfig.DeviceChange[i].Device.Backing.FileName matches := re.FindStringSubmatch(backingFile) if len(matches) < 2 { h.Logger.Warn("unable to match regex", "backing_filename", backingFile, "match_count", len(matches)) } else { h.Logger.Debug("Matched regex", "disk_owner", matches[1]) if strings.ToLower(matches[1]) == strings.ToLower(event.CloudEvent.Data.VM.Name) { h.Logger.Debug("This disk belongs to this VM") changeFound = true diskChangeFound = true // don't need to keep searching through the rest of the backing devices in this VM break } else { h.Logger.Debug("This disk belongs to a different VM, don't record this config change") } } } } } // If we found a disk change belonging to this VM then recalculate the disk size if diskChangeFound { params.UpdateType = "diskchange" diskSize := h.calculateNewDiskSize(event) params.NewProvisionedDisk = sql.NullFloat64{Float64: diskSize, Valid: diskSize > 0} } } } // Only create a database record if we found one of the config changes we were interested in if changeFound { // 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) unixTimestamp = time.Now().Unix() } else { // Convert to Unix timestamp unixTimestamp = eventTime.Unix() } // lookup Iid from Inventory table for this VM // also figure out what to do if we didn't find an entry for this VM in the Inventory table. Create one? h.Logger.Debug("Checking inventory table for VM record") invParams := queries.GetInventoryVmIdParams{ 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 != ""}, } invResult, err := h.Database.Queries().GetInventoryVmId(ctx, invParams) if err != nil { if errors.Is(err, sql.ErrNoRows) { // 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") 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 { h.Logger.Error("unable to find existing inventory record for this VM", "error", err) 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 could not locate vm id '%s' and datacenter name '%s' within inventory table : %s", event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name, err), }) return } } else { params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0} } // Check current disk size from Inventory table and don't create an update if the size is still the same if params.UpdateType == "diskChange" && invResult.ProvisionedDisk.Float64 == params.NewProvisionedDisk.Float64 { h.Logger.Info("VM update type was for disk size but current size of VM matches inventory record, no need for update record", "vm_name", invResult.Name, "db_value", invResult.ProvisionedDisk.Float64, "new_value", params.NewProvisionedDisk.Float64) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{ "status": "OK", "message": fmt.Sprintf("Successfully processed vm modify event"), }) return } // 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.RawChangeString = []byte(event.CloudEvent.Data.ConfigChanges.Modified) 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.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0} params.UserName = sql.NullString{String: event.CloudEvent.Data.UserName, Valid: event.CloudEvent.Data.UserName != ""} // Create the Update database record h.Logger.Debug("Adding Update record", "params", params) result, err := h.Database.Queries().CreateUpdate(ctx, params) if err != nil { h.Logger.Error("unable to perform database insert", "error", err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error : %v\n", err) return } else { h.Logger.Debug("created database record", "insert_result", result) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{ "status": "OK", "message": fmt.Sprintf("Successfully processed vm modify event"), }) return } } else { h.Logger.Debug("Didn't find any configuration changes of interest", "id", event.CloudEvent.ID, "vm", event.CloudEvent.Data.VM.Name, "config_changes", configChanges) w.WriteHeader(http.StatusAccepted) fmt.Fprintf(w, "Processed update event but no config changes were of interest\n") //prettyPrint(event.CloudEvent.Data.ConfigSpec) } } } func (h *Handler) processConfigChanges(configChanges string) []map[string]string { // Split the string on one or more consecutive newline characters changes := regexp.MustCompile(`\n+`).Split(configChanges, -1) // Regular expression to match config type and the new value after '->' or '<-' // examples: // "config.memoryHotAddEnabled: true -\u003e false; \n\nconfig.cpuHotAddEnabled: true -\u003e false; \n\n" // "config.hardware.device(1000).device: (2000, 2001, 2002) -> (2000, 2001, 2002, 2003);" // "config.hardware.numCPU: 2 -\u003e 1; \n\nconfig.hardware.memoryMB: 4096 -\u003e 3072;" // "config.hardware.device(4000).deviceInfo.summary: \"nsx.LogicalSwitch: 618884fd-7e8f-4c02-9a0d-2af36b5296a1\" -> \"DVSwitch: 50 18 92 03 a1 54 8f 8c-f2 b1 87 0f 97 5b d3 17\";" //re := regexp.MustCompile(`(?P[^\s]+): [^-]+-[><] (?P[^;]+);`) re := regexp.MustCompile(`(?P[^\s]+): .*?-[><] (?P[^;]+);`) // Result will hold a list of changes with type and new value var result []map[string]string matchFound := false for _, change := range changes { // Trim any extra spaces and skip empty lines change = strings.TrimSpace(change) //h.Logger.Debug("Processing config change element", "substring", change) if change == "" { continue } // Find the matches using the regex match := re.FindStringSubmatch(change) if len(match) > 0 { matchFound = true // Create a map with 'type' and 'newValue' changeMap := map[string]string{ "type": match[1], // config type "newValue": match[2], // new value after -> or <- } //h.Logger.Debug("Adding new entry to output", "map", changeMap) result = append(result, changeMap) } else { h.Logger.Warn("No regex matches for string", "input", change) } } if !matchFound { h.Logger.Info("No matches found for config change string", "input", configChanges) } return result } func (h *Handler) calculateNewDiskSize(event models.CloudEventReceived) float64 { var diskSize float64 var totalDiskBytes int64 h.Logger.Debug("connecting to vcenter") vc := vcenter.New(h.Logger, h.VcCreds) vc.Login(event.CloudEvent.Source) vmObject, err := vc.FindVMByIDWithDatacenter(event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Datacenter.Value) if err != nil { h.Logger.Error("Can't locate vm in vCenter", "vmID", event.CloudEvent.Data.VM.VM.Value, "error", err) } else { if vmObject.Config != nil { h.Logger.Debug("Found VM with config, calculating new total disk size", "vmID", event.CloudEvent.Data.VM.VM.Value) // Calculate the total disk allocated in GB for _, device := range vmObject.Config.Hardware.Device { if disk, ok := device.(*types.VirtualDisk); ok { // Print the filename of the backing device if backing, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok { h.Logger.Debug("Adding disk", "size_bytes", disk.CapacityInBytes, "backing_file", backing.FileName) } else { h.Logger.Debug("Adding disk, unknown backing type", "size_bytes", disk.CapacityInBytes) } //diskSize += float64(disk.CapacityInBytes / 1024 / 1024 / 1024) // Convert from bytes to GB totalDiskBytes += disk.CapacityInBytes } } diskSize = float64(totalDiskBytes / 1024 / 1024 / 1024) h.Logger.Debug("Converted total disk size", "bytes", totalDiskBytes, "GB", diskSize) } } err = vc.Logout() if err != nil { h.Logger.Error("unable to logout of vcenter", "error", err) } h.Logger.Debug("Calculated new disk size", "value", diskSize) return diskSize } // AddVmToInventory adds a vm from a received cloudevent and returns the inventoryid and any error message func (h *Handler) AddVmToInventory(evt models.CloudEventReceived, ctx context.Context, unixTimestamp int64) (int64, error) { var ( numVcpus int32 numRam int32 totalDiskGB float64 srmPlaceholder string foundVm bool isTemplate string poweredOn string folderPath string rpName string vmUuid string ) //c.Logger.Debug("connecting to vcenter") vc := vcenter.New(h.Logger, h.VcCreds) vc.Login(evt.CloudEvent.Source) //datacenter = evt.DatacenterName.String vmObject, err := vc.FindVMByIDWithDatacenter(evt.CloudEvent.Data.VM.VM.Value, evt.CloudEvent.Data.Datacenter.Datacenter.Value) if err != nil { h.Logger.Error("Can't locate vm in vCenter", "vmID", evt.CloudEvent.Data.VM.VM.Value, "error", err) return 0, err } else if vmObject == nil { h.Logger.Debug("didn't find VM", "vm_id", evt.CloudEvent.Data.VM.VM.Value) return 0, nil } //c.Logger.Debug("found VM") srmPlaceholder = "FALSE" // Default assumption //prettyPrint(vmObject) // calculate VM properties we want to store if vmObject.Config != nil { numRam = vmObject.Config.Hardware.MemoryMB numVcpus = vmObject.Config.Hardware.NumCPU vmUuid = vmObject.Config.Uuid var totalDiskBytes int64 // Calculate the total disk allocated in GB for _, device := range vmObject.Config.Hardware.Device { if disk, ok := device.(*types.VirtualDisk); ok { // Print the filename of the backing device if _, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok { //c.Logger.Debug("Adding disk", "size_bytes", disk.CapacityInBytes, "backing_file", backing.FileName) } else { //c.Logger.Debug("Adding disk, unknown backing type", "size_bytes", disk.CapacityInBytes) } totalDiskBytes += disk.CapacityInBytes //totalDiskGB += float64(disk.CapacityInBytes / 1024 / 1024 / 1024) // Convert from bytes to GB } } totalDiskGB = float64(totalDiskBytes / 1024 / 1024 / 1024) h.Logger.Debug("Converted total disk size", "bytes", totalDiskBytes, "GB", totalDiskGB) // Determine if the VM is a normal VM or an SRM placeholder if vmObject.Config.ManagedBy != nil && vmObject.Config.ManagedBy.ExtensionKey == "com.vmware.vcDr" { if vmObject.Config.ManagedBy.Type == "placeholderVm" { h.Logger.Debug("VM is a placeholder") srmPlaceholder = "TRUE" } else { h.Logger.Debug("VM is managed by SRM but not a placeholder", "details", vmObject.Config.ManagedBy) } } if vmObject.Config.Template { isTemplate = "TRUE" } else { isTemplate = "FALSE" } // Retrieve the full folder path of the VM folderPath, err = vc.GetVMFolderPath(*vmObject) if err != nil { h.Logger.Error("failed to get vm folder path", "error", err) folderPath = "" } else { h.Logger.Debug("Found vm folder path", "folder_path", folderPath) } // Retrieve the resource pool of the VM rpName, _ = vc.GetVmResourcePool(*vmObject) foundVm = true } else { h.Logger.Error("Empty VM config") } //c.Logger.Debug("VM has runtime data", "power_state", vmObject.Runtime.PowerState) if vmObject.Runtime.PowerState == "poweredOff" { poweredOn = "FALSE" } else { poweredOn = "TRUE" } err = vc.Logout() if err != nil { h.Logger.Error("unable to logout of vcenter", "error", err) } if foundVm { e := evt.CloudEvent h.Logger.Debug("Adding to Inventory table", "vm_name", e.Data.VM.Name, "vcpus", numVcpus, "ram", numRam, "dc", e.Data.Datacenter.Datacenter.Value) insertParams := queries.CreateInventoryParams{ Name: e.Data.VM.Name, Vcenter: evt.CloudEvent.Source, CloudId: sql.NullString{String: e.ID, Valid: e.ID != ""}, EventKey: sql.NullString{String: strconv.Itoa(e.Data.Key), Valid: e.Data.Key > 0}, VmId: sql.NullString{String: e.Data.VM.VM.Value, Valid: e.Data.VM.VM.Value != ""}, Datacenter: sql.NullString{String: e.Data.Datacenter.Name, Valid: e.Data.Datacenter.Name != ""}, Cluster: sql.NullString{String: e.Data.ComputeResource.Name, Valid: e.Data.ComputeResource.Name != ""}, CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0}, InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0}, InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0}, ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0}, Folder: sql.NullString{String: folderPath, Valid: folderPath != ""}, ResourcePool: sql.NullString{String: rpName, Valid: rpName != ""}, VmUuid: sql.NullString{String: vmUuid, Valid: vmUuid != ""}, SrmPlaceholder: srmPlaceholder, IsTemplate: isTemplate, PoweredOn: poweredOn, } /* params := queries.CreateInventoryParams{ Name: vmObject.Name, Vcenter: evt.Source, CloudId: sql.NullString{String: evt.CloudId, Valid: evt.CloudId != ""}, EventKey: sql.NullString{String: evt.EventKey.String, Valid: evt.EventKey.Valid}, VmId: sql.NullString{String: evt.VmId.String, Valid: evt.VmId.Valid}, Datacenter: sql.NullString{String: evt.DatacenterName.String, Valid: evt.DatacenterName.Valid}, Cluster: sql.NullString{String: evt.ComputeResourceName.String, Valid: evt.ComputeResourceName.Valid}, CreationTime: sql.NullInt64{Int64: evt.EventTime.Int64, Valid: evt.EventTime.Valid}, InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0}, InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0}, ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0}, Folder: sql.NullString{String: folderPath, Valid: folderPath != ""}, ResourcePool: sql.NullString{String: rpName, Valid: rpName != ""}, VmUuid: sql.NullString{String: vmUuid, Valid: vmUuid != ""}, SrmPlaceholder: srmPlaceholder, IsTemplate: isTemplate, PoweredOn: poweredOn, } */ //c.Logger.Debug("database params", "params", params) // Insert the new inventory record into the database record, err := h.Database.Queries().CreateInventory(ctx, insertParams) if err != nil { h.Logger.Error("unable to perform database insert", "error", err) return 0, err } else { //c.Logger.Debug("created database record", "insert_result", result) return record.Iid, nil } } else { h.Logger.Debug("Not adding to Inventory due to missing vcenter config property", "vm_name", evt.CloudEvent.Data.VM.Name) } return 0, nil }