server.garden privileged automation agent (mirror of https://git.sequentialread.com/forest/rootsystem)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
1.6 KiB

package main
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
)
type terraformStateHandler struct{}
func (handler terraformStateHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if len(request.URL.Path) == 0 {
response.WriteHeader(400)
response.Write([]byte("file path is required "))
return
}
if strings.Contains(request.URL.Path, ".") {
response.WriteHeader(400)
response.Write([]byte("file path cannot contain ."))
return
}
objectStoragePath := filepath.Join("/rootsystem/terraform-state", request.URL.Path, "terraform.tfstate")
if request.Method == "GET" {
file, notFound, err := global.storage.Get(objectStoragePath)
if notFound {
response.WriteHeader(404)
response.Write([]byte("Not Found"))
return
}
if err != nil {
response.WriteHeader(500)
fmt.Fprintf(response, "error getting terraform state: %s", err.Error())
return
}
response.WriteHeader(200)
response.Write(file.Content)
}
if request.Method == "PUT" || request.Method == "POST" {
bytes, err := ioutil.ReadAll(request.Body)
if err != nil {
response.WriteHeader(500)
response.Write([]byte("could not read request body"))
return
}
err = global.storage.Put(objectStoragePath, bytes)
if err != nil {
response.WriteHeader(500)
fmt.Fprintf(response, "error saving terraform state: %s", err.Error())
return
}
response.WriteHeader(200)
}
if request.Method == "DELETE" {
err := global.storage.Delete(objectStoragePath)
if err != nil {
response.WriteHeader(500)
fmt.Fprintf(response, "error deleting terraform state: %s", err.Error())
return
}
response.WriteHeader(200)
}
}