database insert is working
Some checks failed
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-09-12 15:03:24 +10:00
parent 0d2f983eb3
commit 6b285e55b8
9 changed files with 165 additions and 28 deletions

View File

@@ -1,21 +1,73 @@
package handler
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
queries "vctp/db/queries"
models "vctp/server/models"
)
// VmCreate receives the CloudEvent for a VM creation
func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
var unixTimestamp int64
reqBody, err := io.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Invalid data received")
w.WriteHeader(http.StatusInternalServerError)
return
} else {
h.Logger.Debug("received input data", "length", len(reqBody))
}
// Decode the JSON body into vmModel struct
var vm models.CloudEventReceived
//if err := json.NewDecoder(r.Body).Decode(&vm); err != nil {
if err := json.Unmarshal(reqBody, &vm); err != nil {
h.Logger.Error("unable to decode json", "error", err)
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
// Convert vmModel to CreateInventoryParams using the utility function
//var params queries.CreateInventoryParams
//db.ConvertToSQLParams(&vm, &params)
// Parse the datetime string to a time.Time object
eventTime, err := time.Parse(time.RFC3339, vm.CloudEvent.Time)
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()
}
// Create an instance of CreateInventoryParams
params := queries.CreateInventoryParams{
Name: vm.CloudEvent.Data.VM.Name,
Vcenter: vm.CloudEvent.Source,
VmId: sql.NullString{String: "VirtualMachine-" + vm.CloudEvent.Data.VM.VM.Value, Valid: vm.CloudEvent.Data.VM.VM.Value != ""},
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
}
h.Logger.Debug("database params", "vm", vm)
// Insert the new inventory record into the database
result, err := h.Database.Queries().CreateInventory(context.Background(), 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
}
h.Logger.Debug("received create request", "body", string(reqBody))
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Create Request (%d): %v\n", len(reqBody), string(reqBody))
fmt.Fprintf(w, "Create Request : %v\n", result)
}

60
server/models/models.go Normal file
View File

@@ -0,0 +1,60 @@
package models
import "time"
type CloudEventReceived struct {
CloudEvent struct {
ID string `json:"id"`
Specversion string `json:"specversion"`
Source string `json:"source"`
Type string `json:"type"`
Time string `json:"time"`
Data struct {
ChainID int `json:"ChainId"`
ChangeTag string `json:"ChangeTag"`
ComputeResource struct {
ComputeResource struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"ComputeResource"`
Name string `json:"Name"`
} `json:"ComputeResource"`
CreatedTime time.Time `json:"CreatedTime"`
Datacenter struct {
Datacenter struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Datacenter"`
Name string `json:"Name"`
} `json:"Datacenter"`
Ds interface{} `json:"Ds"`
Dvs interface{} `json:"Dvs"`
FullFormattedMessage string `json:"FullFormattedMessage"`
Host struct {
Host struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Host"`
Name string `json:"Name"`
} `json:"Host"`
Key int `json:"Key"`
Net interface{} `json:"Net"`
SrcTemplate struct {
Name string `json:"Name"`
VM struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Vm"`
} `json:"SrcTemplate"`
Template bool `json:"Template"`
UserName string `json:"UserName"`
VM struct {
Name string `json:"Name"`
VM struct {
Type string `json:"Type"`
Value string `json:"Value"`
} `json:"Vm"`
} `json:"Vm"`
} `json:"data"`
} `json:"cloudEvent"`
}

View File

@@ -109,7 +109,7 @@ func (s *Server) Start() {
}
} else {
s.logger.Info("starting TLS server", "port", s.srv.Addr, "cert", s.tlsCertFilename, "key", s.tlsKeyFilename)
if err := s.srv.ListenAndServeTLS(s.tlsCertFilename, s.tlsKeyFilename); err != nil {
if err := s.srv.ListenAndServeTLS(s.tlsCertFilename, s.tlsKeyFilename); err != nil && err != http.ErrServerClosed {
s.logger.Warn("failed to start server", "error", err)
}
}