Skip to content

Commit

Permalink
remove any app lookups by project id and name (#4281)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianedwards authored Feb 15, 2024
1 parent 1289a87 commit c906139
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 78 deletions.
27 changes: 0 additions & 27 deletions api/server/handlers/porter_app/app_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,8 @@ func (c *AppNotificationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})

porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
if err != nil {
err := telemetry.Error(ctx, span, err, "error getting porter apps")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) == 0 {
err := telemetry.Error(ctx, span, err, "no porter apps returned")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) > 1 {
err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}

appId := porterApps[0].ID
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: appId})

if appId == 0 {
err := telemetry.Error(ctx, span, err, "porter app id is missing")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

listAppRevisionsReq := connect.NewRequest(&porterv1.ListAppRevisionsRequest{
ProjectId: int64(project.ID),
AppId: int64(appId),
DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{Id: request.DeploymentTargetID},
AppName: appName,
})
Expand Down
27 changes: 0 additions & 27 deletions api/server/handlers/porter_app/current_app_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,8 @@ func (c *LatestAppRevisionHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
)

porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
if err != nil {
err := telemetry.Error(ctx, span, err, "error getting porter app from repo")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) == 0 {
err := telemetry.Error(ctx, span, err, "no porter apps returned")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) > 1 {
err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}

appId := porterApps[0].ID
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: appId})

if appId == 0 {
err := telemetry.Error(ctx, span, err, "porter app id is missing")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

currentAppRevisionReq := connect.NewRequest(&porterv1.CurrentAppRevisionRequest{
ProjectId: int64(project.ID),
AppId: int64(appId),
DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
Id: request.DeploymentTargetID,
Name: deploymentTargetName,
Expand Down
18 changes: 3 additions & 15 deletions api/server/handlers/porter_app/get_app_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (c *GetAppTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
defer span.End()

project, _ := ctx.Value(types.ProjectScope).(*models.Project)
cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)

if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
Expand All @@ -67,31 +68,18 @@ func (c *GetAppTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})

porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
app, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, appName)
if err != nil {
err := telemetry.Error(ctx, span, err, "error getting porter app from repo")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) == 0 {
err := telemetry.Error(ctx, span, err, "no porter apps returned")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
if len(porterApps) > 1 {
err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}

if porterApps[0].ID == 0 {
if app.ID == 0 {
err := telemetry.Error(ctx, span, err, "porter app id is missing")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

app := porterApps[0]

templateReq := connect.NewRequest(&porterv1.AppTemplateRequest{
ProjectId: int64(project.ID),
AppId: int64(app.ID),
Expand Down
16 changes: 7 additions & 9 deletions internal/porter_app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package porter_app

import (
"context"
"database/sql"
"errors"
"fmt"

Expand Down Expand Up @@ -99,18 +100,15 @@ func CreateOrGetAppRecord(ctx context.Context, input CreateOrGetAppRecordInput)
telemetry.AttributeKV{Key: "name", Value: input.Name},
)

porterAppDBEntries, err := input.PorterAppRepository.ReadPorterAppsByProjectIDAndName(input.ProjectID, input.Name)
if err != nil {
return app, telemetry.Error(ctx, span, err, "error reading porter apps by project id and name")
}
if len(porterAppDBEntries) > 1 {
return app, telemetry.Error(ctx, span, nil, "multiple apps with same name")
existingApp, err := input.PorterAppRepository.ReadPorterAppByName(input.ClusterID, input.Name)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return app, telemetry.Error(ctx, span, err, "error reading porter app by name")
}

// return existing app if one found with same name
if len(porterAppDBEntries) == 1 {
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-app-id", Value: porterAppDBEntries[0].ID})
app = porterAppDBEntries[0].ToPorterAppType()
if existingApp.ID != 0 {
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-app-id", Value: existingApp.ID})
app = existingApp.ToPorterAppType()
return app, nil
}

Expand Down

0 comments on commit c906139

Please sign in to comment.