package handler import ( "context" "database/sql" "encoding/json" "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 found = 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 { found = 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 { found = 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 // 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] // TODO - track when this happens, maybe need a new database column? } // 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")) { // TODO - query current disk size from Inventory table and only create an update if the size is now changed 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") found = true params.UpdateType = "diskchange" diskSize := h.calculateNewDiskSize(event) params.NewProvisionedDisk = sql.NullFloat64{Float64: diskSize, Valid: diskSize > 0} } else { h.Logger.Debug("This disk belongs to a different VM, don't record this config change") } } } } } } } // Only create a database record if we found one of the config changes we were interested in if found { // 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 { 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} } // 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() } // populate other parameters for the Update database record 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 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 }