From 6abf49b263bc27abbcd57a19ced0f54599723d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Wed, 6 Nov 2024 12:48:43 +0100 Subject: [PATCH 1/4] Revert "Revert "internal/daemon: Update Render and pass request"" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 7c0e93968d44a0f05faaa0ad637588140c3b3f56. Signed-off-by: Julian Pelizäus --- internal/daemon/daemon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 11bdf3b2..05ee1be7 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -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}) } @@ -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}) } From f2a93c2a0d3498e90c831cf6db6d9a29302b460e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Wed, 6 Nov 2024 12:49:00 +0100 Subject: [PATCH 2/4] Revert "Revert "internal/rest/resources: Update Render and pass request"" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 715d05b068f8281dd7d9a8e9e8060f63ae2e2287. Signed-off-by: Julian Pelizäus --- internal/rest/resources/cluster.go | 6 +++--- internal/rest/resources/hooks_test.go | 8 ++++++-- internal/rest/resources/shutdown.go | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/rest/resources/cluster.go b/internal/rest/resources/cluster.go index 264bdd70..f1a62d7b 100644 --- a/internal/rest/resources/cluster.go +++ b/internal/rest/resources/cluster.go @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/rest/resources/hooks_test.go b/internal/rest/resources/hooks_test.go index ccc88de7..e84d9ba6 100644 --- a/internal/rest/resources/hooks_test.go +++ b/internal/rest/resources/hooks_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/http/httptest" + "net/url" "strings" "testing" @@ -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) @@ -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 diff --git a/internal/rest/resources/shutdown.go b/internal/rest/resources/shutdown.go index bc3beb84..c33e8e78 100644 --- a/internal/rest/resources/shutdown.go +++ b/internal/rest/resources/shutdown.go @@ -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 } From a8057a71b02583808d0543438e6f40da0f939f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Wed, 6 Nov 2024 12:49:09 +0100 Subject: [PATCH 3/4] Revert "Revert "internal/rest: Update Render and pass request"" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3673c40e169fe3734693e42a57b8e68107307b7b. Signed-off-by: Julian Pelizäus --- internal/rest/rest.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/rest/rest.go b/internal/rest/rest.go index 2991b3ae..e1399fbf 100644 --- a/internal/rest/rest.go +++ b/internal/rest/rest.go @@ -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) } @@ -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}) } @@ -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}) } @@ -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}) } @@ -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}) } From 66a77d827769a203b94e4ac3bbee70129b6013a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Wed, 6 Nov 2024 13:35:54 +0100 Subject: [PATCH 4/4] go: Update LXD dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julian Pelizäus --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f4d4238d..bfc78876 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f2d2b768..1fdef4ac 100644 --- a/go.sum +++ b/go.sum @@ -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=