diff --git a/api/server/handlers/porter_app/app_notifications.go b/api/server/handlers/porter_app/app_notifications.go index b7e3c8c2a7..09ce23cec4 100644 --- a/api/server/handlers/porter_app/app_notifications.go +++ b/api/server/handlers/porter_app/app_notifications.go @@ -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, }) diff --git a/api/server/handlers/porter_app/current_app_revision.go b/api/server/handlers/porter_app/current_app_revision.go index ffca7ddac6..db78741a29 100644 --- a/api/server/handlers/porter_app/current_app_revision.go +++ b/api/server/handlers/porter_app/current_app_revision.go @@ -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, diff --git a/api/server/handlers/porter_app/get_app_template.go b/api/server/handlers/porter_app/get_app_template.go index 4f1a7dbca6..55c65ed7fa 100644 --- a/api/server/handlers/porter_app/get_app_template.go +++ b/api/server/handlers/porter_app/get_app_template.go @@ -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") @@ -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), diff --git a/internal/porter_app/create.go b/internal/porter_app/create.go index 8c235e0af6..2b226bdd09 100644 --- a/internal/porter_app/create.go +++ b/internal/porter_app/create.go @@ -2,6 +2,7 @@ package porter_app import ( "context" + "database/sql" "errors" "fmt" @@ -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 }