Skip to content

Commit 3d327a3

Browse files
emosbaughajp-io
andauthored
feat(preflights): different messaging for ui and cli installs (#2300)
* feat(preflights): different messaging for ui and cli installs * f * Update host-preflight.yaml * Update host-preflight.yaml * f --------- Co-authored-by: Alex Parker <7272359+ajp-io@users.noreply.github.com>
1 parent 663cc10 commit 3d327a3

File tree

17 files changed

+174
-76
lines changed

17 files changed

+174
-76
lines changed

api/api.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"strings"
89

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

242+
func (a *API) bindJSON(w http.ResponseWriter, r *http.Request, v any) error {
243+
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
244+
a.logError(r, err, fmt.Sprintf("failed to decode %s %s request", strings.ToLower(r.Method), r.URL.Path))
245+
a.jsonError(w, r, types.NewBadRequestError(err))
246+
return err
247+
}
248+
249+
return nil
250+
}
251+
241252
func (a *API) json(w http.ResponseWriter, r *http.Request, code int, payload any) {
242253
response, err := json.Marshal(payload)
243254
if err != nil {
@@ -248,7 +259,7 @@ func (a *API) json(w http.ResponseWriter, r *http.Request, code int, payload any
248259

249260
w.Header().Set("Content-Type", "application/json")
250261
w.WriteHeader(code)
251-
w.Write(response)
262+
_, _ = w.Write(response)
252263
}
253264

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

267278
w.Header().Set("Content-Type", "application/json")
268279
w.WriteHeader(apiErr.StatusCode)
269-
w.Write(response)
280+
_, _ = w.Write(response)
270281
}
271282

272283
func (a *API) logError(r *http.Request, err error, args ...any) {

api/auth.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"encoding/json"
54
"errors"
65
"net/http"
76
"strings"
@@ -53,10 +52,7 @@ func (a *API) authMiddleware(next http.Handler) http.Handler {
5352
// @Router /auth/login [post]
5453
func (a *API) postAuthLogin(w http.ResponseWriter, r *http.Request) {
5554
var request types.AuthRequest
56-
err := json.NewDecoder(r.Body).Decode(&request)
57-
if err != nil {
58-
a.logError(r, err, "failed to decode auth request")
59-
a.jsonError(w, r, types.NewBadRequestError(err))
55+
if err := a.bindJSON(w, r, &request); err != nil {
6056
return
6157
}
6258

api/controllers/install/controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Controller interface {
2121
GetInstallationConfig(ctx context.Context) (*types.InstallationConfig, error)
2222
ConfigureInstallation(ctx context.Context, config *types.InstallationConfig) error
2323
GetInstallationStatus(ctx context.Context) (*types.Status, error)
24-
RunHostPreflights(ctx context.Context) error
24+
RunHostPreflights(ctx context.Context, opts RunHostPreflightsOptions) error
2525
GetHostPreflightStatus(ctx context.Context) (*types.Status, error)
2626
GetHostPreflightOutput(ctx context.Context) (*types.HostPreflightsOutput, error)
2727
GetHostPreflightTitles(ctx context.Context) ([]string, error)
@@ -31,6 +31,10 @@ type Controller interface {
3131
GetStatus(ctx context.Context) (*types.Status, error)
3232
}
3333

34+
type RunHostPreflightsOptions struct {
35+
IsUI bool
36+
}
37+
3438
var _ Controller = (*InstallController)(nil)
3539

3640
type InstallController struct {

api/controllers/install/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func TestRunHostPreflights(t *testing.T) {
321321
)
322322
require.NoError(t, err)
323323

324-
err = controller.RunHostPreflights(t.Context())
324+
err = controller.RunHostPreflights(t.Context(), RunHostPreflightsOptions{})
325325

326326
if tt.expectedErr {
327327
assert.Error(t, err)

api/controllers/install/hostpreflight.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
1111
)
1212

13-
func (c *InstallController) RunHostPreflights(ctx context.Context) error {
13+
func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostPreflightsOptions) error {
1414
// Get current installation config and add it to options
1515
config, err := c.installationManager.GetConfig()
1616
if err != nil {
@@ -28,6 +28,7 @@ func (c *InstallController) RunHostPreflights(ctx context.Context) error {
2828
HostPreflightSpec: c.releaseData.HostPreflights,
2929
EmbeddedClusterConfig: c.releaseData.EmbeddedClusterConfig,
3030
IsAirgap: c.airgapBundle != "",
31+
IsUI: opts.IsUI,
3132
})
3233
if err != nil {
3334
return fmt.Errorf("failed to prepare host preflights: %w", err)

api/docs/docs.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)