Skip to content

Commit

Permalink
let the porter app creation api call fail if build and image are miss…
Browse files Browse the repository at this point in the history
…ing, not cli (#3615)

Co-authored-by: David Townley <[email protected]>
  • Loading branch information
d-g-town and David Townley authored Sep 20, 2023
1 parent 6fca050 commit 8b662a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
3 changes: 0 additions & 3 deletions api/client/porter_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,6 @@ func (c *Client) CreatePorterAppDBEntry(
Tag: inp.ImageTag,
}
}
if sourceType == "" {
return fmt.Errorf("cannot determine source type")
}

req := &porter_app.CreateAppRequest{
Name: inp.AppName,
Expand Down
18 changes: 11 additions & 7 deletions api/server/handlers/porter_app/create_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package porter_app

import (
"context"
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -31,6 +32,9 @@ func NewCreateAppHandler(
}
}

// ErrMissingSourceType is returned when the source type is not specified
var ErrMissingSourceType = errors.New("missing source type")

// SourceType is a string type specifying the source type of an app. This is specified in the incoming request
type SourceType string

Expand Down Expand Up @@ -117,13 +121,6 @@ func (c *CreateAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: request.Name})

if request.SourceType == "" {
err := telemetry.Error(ctx, span, nil, "source type is required")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: request.SourceType})

porterAppDBEntries, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, request.Name)
if err != nil {
err := telemetry.Error(ctx, span, nil, "error reading porter apps by project id and name")
Expand All @@ -142,6 +139,13 @@ func (c *CreateAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if request.SourceType == "" {
err := telemetry.Error(ctx, span, ErrMissingSourceType, "source type is required")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "source-type", Value: request.SourceType})

var porterApp *types.PorterApp
switch request.SourceType {
case SourceType_Github:
Expand Down
21 changes: 12 additions & 9 deletions cli/cmd/v2/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,24 @@ func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, por
}
b64AppProto = parseResp.B64AppProto

// we only need to create the app if a porter yaml is provided (otherwise it must already exist)
createPorterAppDBEntryInp, err := createPorterAppDbEntryInputFromProtoAndEnv(parseResp.B64AppProto)
// override app name if provided
appName, err = appNameFromB64AppProto(parseResp.B64AppProto)
if err != nil {
return fmt.Errorf("error creating porter app db entry input from proto: %w", err)
return fmt.Errorf("error getting app name from b64 app proto: %w", err)
}

err = client.CreatePorterAppDBEntry(ctx, cliConf.Project, cliConf.Cluster, createPorterAppDBEntryInp)
// we only need to create the app if a porter yaml is provided (otherwise it must already exist)
createPorterAppDBEntryInp, err := createPorterAppDbEntryInputFromProtoAndEnv(parseResp.B64AppProto)
if err != nil {
return fmt.Errorf("error creating porter app db entry: %w", err)
return fmt.Errorf("unable to form porter app creation input from yaml: %w", err)
}

// override app name if provided
appName, err = appNameFromB64AppProto(parseResp.B64AppProto)
err = client.CreatePorterAppDBEntry(ctx, cliConf.Project, cliConf.Cluster, createPorterAppDBEntryInp)
if err != nil {
return fmt.Errorf("error getting app name from b64 app proto: %w", err)
if err.Error() == porter_app.ErrMissingSourceType.Error() {
return fmt.Errorf("cannot find existing Porter app with name %s and no build or image settings were specified in porter.yaml", appName)
}
return fmt.Errorf("unable to create porter app from yaml: %w", err)
}

envGroupResp, err := client.CreateOrUpdateAppEnvironment(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID, parseResp.EnvVariables, parseResp.EnvSecrets, parseResp.B64AppProto)
Expand Down Expand Up @@ -309,7 +312,7 @@ func createPorterAppDbEntryInputFromProtoAndEnv(base64AppProto string) (api.Crea
return input, nil
}

return input, fmt.Errorf("app does not contain build or image settings")
return input, nil
}

func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
Expand Down

0 comments on commit 8b662a3

Please sign in to comment.