Skip to content

Commit

Permalink
v2: Restore changes to Render (#278)
Browse files Browse the repository at this point in the history
As part of #277 the
changes to `Render` (originally introduced in
#252) got reverted to
allow tracking `[email protected]` which didn't contain the necessary
changes to `Render`.

After backporting the changes to LXD's `stable-5.21` with
canonical/lxd#14409, we can restore the original
changes using the latest version of LXD in `stable-5.21`.
  • Loading branch information
tomponline authored Nov 6, 2024
2 parents beb4f0b + 66a77d8 commit fddf663
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.7

require (
github.com/canonical/go-dqlite/v2 v2.0.0
github.com/canonical/lxd v0.0.0-20241105095730-921967798cb1
github.com/canonical/lxd v0.0.0-20241106123323-f0be5c8f36a6
github.com/fsnotify/fsnotify v1.8.0
github.com/google/renameio v1.0.1
github.com/gorilla/mux v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/canonical/go-dqlite/v2 v2.0.0 h1:RNFcFVhHMh70muKKErbW35rSzqmAFswheHdAgxW0Ddw=
github.com/canonical/go-dqlite/v2 v2.0.0/go.mod h1:IaIC8u4Z1UmPjuAqPzA2r83YMaMHRLoKZdHKI5uHCJI=
github.com/canonical/lxd v0.0.0-20241105095730-921967798cb1 h1:kJTKpOZlC3dDx0YAMGMlV4D7i/7YLz9p4uEVuMXk1uM=
github.com/canonical/lxd v0.0.0-20241105095730-921967798cb1/go.mod h1:ORoLmeSj+tQmUgh10kLA9eMtRnbBZPT6X10V2I4cTGs=
github.com/canonical/lxd v0.0.0-20241106123323-f0be5c8f36a6 h1:dr4LtQbdutkhfxVCGH/jcVpaLxK6067dNCE6tp3Isyw=
github.com/canonical/lxd v0.0.0-20241106123323-f0be5c8f36a6/go.mod h1:ORoLmeSj+tQmUgh10kLA9eMtRnbBZPT6X10V2I4cTGs=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 2 additions & 2 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (d *Daemon) initServer(resources ...rest.Resources) *http.Server {

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := response.SyncResponse(true, []string{"/1.0"}).Render(w)
err := response.SyncResponse(true, []string{"/1.0"}).Render(w, r)
if err != nil {
logger.Error("Failed to write HTTP response", logger.Ctx{"url": r.URL, "err": err})
}
Expand All @@ -476,7 +476,7 @@ func (d *Daemon) initServer(resources ...rest.Resources) *http.Server {
mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Info("Sending top level 404", logger.Ctx{"url": r.URL})
w.Header().Set("Content-Type", "application/json")
err := response.NotFound(nil).Render(w)
err := response.NotFound(nil).Render(w, r)
if err != nil {
logger.Error("Failed to write HTTP response", logger.Ctx{"url": r.URL, "err": err})
}
Expand Down
6 changes: 3 additions & 3 deletions internal/rest/resources/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func clusterMemberPut(s state.State, r *http.Request) response.Response {
go reExec()

return response.ManualResponse(func(w http.ResponseWriter) error {
err := response.EmptySyncResponse.Render(w)
err := response.EmptySyncResponse.Render(w, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -448,7 +448,7 @@ func clusterMemberDelete(s state.State, r *http.Request) response.Response {
}

return response.ManualResponse(func(w http.ResponseWriter) error {
err := response.EmptySyncResponse.Render(w)
err := response.EmptySyncResponse.Render(w, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -566,7 +566,7 @@ func clusterMemberDelete(s state.State, r *http.Request) response.Response {
}

return response.ManualResponse(func(w http.ResponseWriter) error {
err := response.EmptySyncResponse.Render(w)
err := response.EmptySyncResponse.Render(w, r)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions internal/rest/resources/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

Expand Down Expand Up @@ -116,7 +117,10 @@ func (t *hooksSuite) Test_hooks() {
ranHook = ""
isForce = false
expectForce := false
req := &http.Request{}
req := &http.Request{
// Set an URL for response.Render to not cause any panic.
URL: &url.URL{},
}
payload, ok := c.req.(internalTypes.HookRemoveMemberOptions)
if !ok {
payload, ok := c.req.(internalTypes.HookNewMemberOptions)
Expand All @@ -131,7 +135,7 @@ func (t *hooksSuite) Test_hooks() {

response := hooksPost(s, req)
recorder := httptest.NewRecorder()
err := response.Render(recorder)
err := response.Render(recorder, req)
require.NoError(t.T(), err)

var resp api.Response
Expand Down
2 changes: 1 addition & 1 deletion internal/rest/resources/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func shutdownPost(state state.State, r *http.Request) response.Response {

// Run shutdown sequence synchronously.
exit, stopErr := intState.Stop()
err := response.SmartError(stopErr).Render(w)
err := response.SmartError(stopErr).Render(w, r)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions internal/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func handleAPIRequest(action rest.EndpointAction, state state.State, w http.Resp
}

if resp != nil {
err := resp.Render(w)
err := resp.Render(w, r)
if err != nil {
return response.InternalError(err)
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func HandleEndpoint(state state.State, mux *mux.Router, version string, e rest.E

intState, err := internalState.ToInternal(state)
if err != nil {
err := response.BadRequest(err).Render(w)
err := response.BadRequest(err).Render(w, r)
if err != nil {
logger.Error("Failed to write HTTP response", logger.Ctx{"url": r.URL, "err": err})
}
Expand All @@ -204,7 +204,7 @@ func HandleEndpoint(state state.State, mux *mux.Router, version string, e rest.E

// Return Unavailable Error (503) if daemon is shutting down, except for endpoints with AllowedDuringShutdown.
if intState.Context.Err() == context.Canceled && !e.AllowedDuringShutdown {
err := response.Unavailable(fmt.Errorf("Daemon is shutting down")).Render(w)
err := response.Unavailable(fmt.Errorf("Daemon is shutting down")).Render(w, r)
if err != nil {
logger.Error("Failed to write HTTP response", logger.Ctx{"url": r.URL, "err": err})
}
Expand All @@ -215,7 +215,7 @@ func HandleEndpoint(state state.State, mux *mux.Router, version string, e rest.E
if !e.AllowedBeforeInit {
err := state.Database().IsOpen(r.Context())
if err != nil {
err := response.SmartError(err).Render(w)
err := response.SmartError(err).Render(w, r)
if err != nil {
logger.Error("Failed to write HTTP response", logger.Ctx{"url": r.URL, "err": err})
}
Expand Down Expand Up @@ -259,9 +259,9 @@ func HandleEndpoint(state state.State, mux *mux.Router, version string, e rest.E
// we can ensure that the connection wasn't yet hijacked and the actual error
// can be safely returned to the caller.
if e.Path != "database" || (e.Path == "database" && resp != response.EmptySyncResponse) {
err := resp.Render(w)
err := resp.Render(w, r)
if err != nil {
err := response.InternalError(err).Render(w)
err := response.InternalError(err).Render(w, r)
if err != nil {
logger.Error("Failed writing error for HTTP response", logger.Ctx{"url": url, "error": err})
}
Expand Down

0 comments on commit fddf663

Please sign in to comment.