Files
matchbox/bootcfg/http/generic.go
Dalton Hubble 07751a4bba bootcfg/http: Add request query params to template variables
* Add query parameters to template variables, referenced by
 {{.request.query.param}}
* Render Ignition/Fuze, cloud-config, and generic templates and
metadata with collected variables
* Replace template variable {{.query}} with {{.request.raw_query}}
(breaking)
2016-07-19 16:25:01 -07:00

78 lines
2.0 KiB
Go

package http
import (
"bytes"
"net/http"
"strings"
"time"
"github.com/Sirupsen/logrus"
"golang.org/x/net/context"
"github.com/coreos/coreos-baremetal/bootcfg/server"
pb "github.com/coreos/coreos-baremetal/bootcfg/server/serverpb"
)
// genericHandler returns a handler that responds with the generic config
// matching the request.
func (s *Server) genericHandler(core server.Server) ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
group, err := groupFromContext(ctx)
if err != nil {
s.logger.WithFields(logrus.Fields{
"labels": labelsFromRequest(nil, req),
}).Infof("No matching group")
http.NotFound(w, req)
return
}
profile, err := core.ProfileGet(ctx, &pb.ProfileGetRequest{Id: group.Profile})
if err != nil {
s.logger.WithFields(logrus.Fields{
"labels": labelsFromRequest(nil, req),
"group": group.Id,
"group_name": group.Name,
}).Infof("No profile named: %s", group.Profile)
http.NotFound(w, req)
return
}
contents, err := core.GenericGet(ctx, profile.GenericId)
if err != nil {
s.logger.WithFields(logrus.Fields{
"labels": labelsFromRequest(nil, req),
"group": group.Id,
"group_name": group.Name,
"profile": group.Profile,
}).Infof("No generic template named: %s", profile.GenericId)
http.NotFound(w, req)
return
}
// match was successful
s.logger.WithFields(logrus.Fields{
"labels": labelsFromRequest(nil, req),
"group": group.Id,
"profile": profile.Id,
}).Debug("Matched a generic template")
// collect data for rendering
data, err := collectVariables(req, group)
if err != nil {
s.logger.Errorf("error collecting variables: %v", err)
http.NotFound(w, req)
return
}
// render the template of a generic config with data
var buf bytes.Buffer
err = s.renderTemplate(&buf, data, contents)
if err != nil {
http.NotFound(w, req)
return
}
config := buf.String()
http.ServeContent(w, req, "", time.Time{}, strings.NewReader(config))
}
return ContextHandlerFunc(fn)
}