Skip to content

Commit

Permalink
Add /bundle/connections path (#83)
Browse files Browse the repository at this point in the history
A GET will return the bundles current connections
A POST with all connections will write the conns to the connections.auto
file
  • Loading branch information
dramich authored Sep 27, 2023
1 parent 9676ff3 commit 3f585ec
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
90 changes: 86 additions & 4 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bundle
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"path"
Expand All @@ -13,9 +14,15 @@ import (
"sigs.k8s.io/yaml"
)

const (
ParamsFile = "_params.auto.tfvars.json"
ConnsFile = "_connections.auto.tfvars.json"
)

type Handler struct {
Bundle Bundle
fs afero.Fs
Bundle Bundle
fs afero.Fs
bundleDir string
}
type Step struct {
Path string `json:"path" yaml:"path"`
Expand Down Expand Up @@ -157,10 +164,10 @@ func NewHandler(dir string) (*Handler, error) {
return nil, err
}
ApplyAppBlockDefaults(bundle)
return &Handler{Bundle: *bundle, fs: fs}, nil
return &Handler{Bundle: *bundle, fs: fs, bundleDir: dir}, nil
}

func (b *Handler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
func (b *Handler) GetSecrets(w http.ResponseWriter, _ *http.Request) {
out, err := json.Marshal(b.Bundle.AppSpec.Secrets)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -172,3 +179,78 @@ func (b *Handler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
slog.Error(err.Error())
}
}

func (b *Handler) Connections(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
b.getConnections(w)
case http.MethodPost:
b.postConnections(w, r)
default:
w.Header().Add("Allow", "GET, POST")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

out, err := json.Marshal(b.Bundle.AppSpec.Secrets)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
slog.Error(err.Error())
}
}

func (b *Handler) getConnections(w http.ResponseWriter) {
f, err := afero.ReadFile(b.fs, path.Join(b.bundleDir, "src", ConnsFile))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}

w.Header().Set("Content-Type", "application/json")
_, err = w.Write(f)
if err != nil {
slog.Error(err.Error())
}
}

func (b *Handler) postConnections(w http.ResponseWriter, r *http.Request) {
conns, err := io.ReadAll(r.Body)
if err != nil {
slog.Debug("Error reading payload", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer r.Body.Close()

connMap := make(map[string]any)

// We have to go through the unmarshal/marshal dance to ensure
// we keep the formatting in the final file. If the json payload
// is a single line that would end up back in the file and make
// it unreadable.
err = json.Unmarshal(conns, &connMap)
if err != nil {
slog.Debug("Error unmarshalling payload", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}

bytes, err := json.MarshalIndent(connMap, "", " ")
if err != nil {
slog.Debug("Error marshalling payload", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}

err = afero.WriteFile(b.fs, path.Join(b.bundleDir, "src", ConnsFile), bytes, 0755)
if err != nil {
slog.Debug("Error writing file", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func RegisterServerHandler(dir string) {
os.Exit(1)
}

http.Handle("/bundle/secrets", originHeaderMiddleware(bundleHandler))
http.Handle("/bundle/secrets", originHeaderMiddleware(http.HandlerFunc(bundleHandler.GetSecrets)))
http.Handle("/bundle/connections", originHeaderMiddleware(http.HandlerFunc(bundleHandler.Connections)))

configHandler, err := config.NewHandler()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/terraform/generate_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func GenerateFiles(buildPath, stepPath string, b *bundle.Bundle, fs afero.Fs) er
return err
}

devParamPath := path.Join(buildPath, stepPath, "_params.auto.tfvars.json")
devParamPath := path.Join(buildPath, stepPath, bundle.ParamsFile)

err = transpileAndWriteDevParams(devParamPath, b, fs)

if err != nil {
return fmt.Errorf("error compiling dev params: %w", err)
}

err = transpileConnectionVarFile(path.Join(buildPath, stepPath, "_connections.auto.tfvars.json"), b, fs)
err = transpileConnectionVarFile(path.Join(buildPath, stepPath, bundle.ConnsFile), b, fs)

if err != nil {
return err
Expand Down

0 comments on commit 3f585ec

Please sign in to comment.