more file structure
This commit is contained in:
43
api/resource/common/handler.go
Normal file
43
api/resource/common/handler.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Display some information about this API for the default page
|
||||
// TODO - static fileserver with docs
|
||||
func HomeLink(w http.ResponseWriter, r *http.Request) {
|
||||
//w.WriteHeader(http.StatusNotImplemented)
|
||||
fmt.Fprintf(w, "VM Chargeback Tracking Program. API interface only. See Nathan Coad (nathan.coad@dell.com) for further details. ")
|
||||
}
|
||||
|
||||
// Display error message for invalid requests
|
||||
func HandleNotFound(w http.ResponseWriter, r *http.Request) {
|
||||
ip, err := IPFromRequest(r)
|
||||
if err != nil {
|
||||
fmt.Println("Error", err)
|
||||
http.Error(w, "VM Chargeback Tracking Program.. Invalid Path Specified.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Request from IP %s\n", ip.String())
|
||||
http.Error(w, "VM Chargeback Tracking Program. Invalid Path Specified.", http.StatusNotFound)
|
||||
// TODO - investigate rate limiting for invalid requests
|
||||
}
|
||||
|
||||
// IPFromRequest extracts the user IP address from req, if present.
|
||||
// @see https://blog.golang.org/context/userip/userip.go
|
||||
func IPFromRequest(req *http.Request) (net.IP, error) {
|
||||
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("userip: %q is not IP:port", req.RemoteAddr)
|
||||
}
|
||||
|
||||
userIP := net.ParseIP(ip)
|
||||
if userIP == nil {
|
||||
return nil, fmt.Errorf("userip: %q is not IP:port", req.RemoteAddr)
|
||||
}
|
||||
return userIP, nil
|
||||
}
|
32
api/resource/vm/handler.go
Normal file
32
api/resource/vm/handler.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// TODO godoc
|
||||
func VmCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "Invalid data received")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
slog.Debug("received create request", "body", string(reqBody))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "Create Request (%d): %v\n", len(reqBody), string(reqBody))
|
||||
}
|
||||
|
||||
// TODO godoc
|
||||
func VmRemoveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "Remove Request: %v\n", vars)
|
||||
}
|
58
api/resource/vm/model.go
Normal file
58
api/resource/vm/model.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package vm
|
||||
|
||||
import "time"
|
||||
|
||||
type CloudEventReceived struct {
|
||||
ID string `json:"id"`
|
||||
Specversion string `json:"specversion"`
|
||||
Source string `json:"source"`
|
||||
Type string `json:"type"`
|
||||
Time time.Time `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"`
|
||||
}
|
25
api/router/router.go
Normal file
25
api/router/router.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"vm-ctp/api/resource/common"
|
||||
"vm-ctp/api/resource/vm"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func New() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
|
||||
// If nothing more specific is requested then just display some version information
|
||||
r.HandleFunc("/", common.HomeLink)
|
||||
|
||||
s := r.PathPrefix("/api").Subrouter()
|
||||
s.HandleFunc("/event/vm/create", vm.VmCreateHandler).Methods("POST") // receive VM creation event from Direktiv
|
||||
s.HandleFunc("/event/vm/remove", vm.VmRemoveHandler).Methods("POST") // receive VM creation event from Direktiv
|
||||
|
||||
// Not found handler
|
||||
r.NotFoundHandler = http.HandlerFunc(common.HandleNotFound)
|
||||
|
||||
return r
|
||||
}
|
Reference in New Issue
Block a user