Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/replicatedhq/embedded-cluster/api/controllers/auth"
Expand Down Expand Up @@ -238,6 +239,16 @@ func (a *API) RegisterRoutes(router *mux.Router) {
consoleRouter.HandleFunc("/available-network-interfaces", a.getListAvailableNetworkInterfaces).Methods("GET")
}

func (a *API) bindJSON(w http.ResponseWriter, r *http.Request, v any) error {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
a.logError(r, err, fmt.Sprintf("failed to decode %s %s request", strings.ToLower(r.Method), r.URL.Path))
a.jsonError(w, r, types.NewBadRequestError(err))
return err
}

return nil
}

func (a *API) json(w http.ResponseWriter, r *http.Request, code int, payload any) {
response, err := json.Marshal(payload)
if err != nil {
Expand All @@ -248,7 +259,7 @@ func (a *API) json(w http.ResponseWriter, r *http.Request, code int, payload any

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
_, _ = w.Write(response)
}

func (a *API) jsonError(w http.ResponseWriter, r *http.Request, err error) {
Expand All @@ -266,7 +277,7 @@ func (a *API) jsonError(w http.ResponseWriter, r *http.Request, err error) {

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(apiErr.StatusCode)
w.Write(response)
_, _ = w.Write(response)
}

func (a *API) logError(r *http.Request, err error, args ...any) {
Expand Down
6 changes: 1 addition & 5 deletions api/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"encoding/json"
"errors"
"net/http"
"strings"
Expand Down Expand Up @@ -53,10 +52,7 @@ func (a *API) authMiddleware(next http.Handler) http.Handler {
// @Router /auth/login [post]
func (a *API) postAuthLogin(w http.ResponseWriter, r *http.Request) {
var request types.AuthRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
a.logError(r, err, "failed to decode auth request")
a.jsonError(w, r, types.NewBadRequestError(err))
if err := a.bindJSON(w, r, &request); err != nil {
return
}

Expand Down
6 changes: 5 additions & 1 deletion api/controllers/install/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Controller interface {
GetInstallationConfig(ctx context.Context) (*types.InstallationConfig, error)
ConfigureInstallation(ctx context.Context, config *types.InstallationConfig) error
GetInstallationStatus(ctx context.Context) (*types.Status, error)
RunHostPreflights(ctx context.Context) error
RunHostPreflights(ctx context.Context, opts RunHostPreflightsOptions) error
GetHostPreflightStatus(ctx context.Context) (*types.Status, error)
GetHostPreflightOutput(ctx context.Context) (*types.HostPreflightsOutput, error)
GetHostPreflightTitles(ctx context.Context) ([]string, error)
Expand All @@ -31,6 +31,10 @@ type Controller interface {
GetStatus(ctx context.Context) (*types.Status, error)
}

type RunHostPreflightsOptions struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be better to move this to a types package

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should discuss the types package. Maybe break it up. I had the understanding that it was for api types (eg request response), not controller and manager options.

https://github.com/replicatedhq/embedded-cluster/pull/2300/files#diff-f62a0fad1ad21e2ffa9cf4de2cab02d8a2dccd2557065c737942d54e9a1ed736R3

IsUI bool
}

var _ Controller = (*InstallController)(nil)

type InstallController struct {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/install/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestRunHostPreflights(t *testing.T) {
)
require.NoError(t, err)

err = controller.RunHostPreflights(t.Context())
err = controller.RunHostPreflights(t.Context(), RunHostPreflightsOptions{})

if tt.expectedErr {
assert.Error(t, err)
Expand Down
3 changes: 2 additions & 1 deletion api/controllers/install/hostpreflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
)

func (c *InstallController) RunHostPreflights(ctx context.Context) error {
func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostPreflightsOptions) error {
// Get current installation config and add it to options
config, err := c.installationManager.GetConfig()
if err != nil {
Expand All @@ -28,6 +28,7 @@ func (c *InstallController) RunHostPreflights(ctx context.Context) error {
HostPreflightSpec: c.releaseData.HostPreflights,
EmbeddedClusterConfig: c.releaseData.EmbeddedClusterConfig,
IsAirgap: c.airgapBundle != "",
IsUI: opts.IsUI,
})
if err != nil {
return fmt.Errorf("failed to prepare host preflights: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading