Skip to content

Commit

Permalink
update linked applications when updating env group (#3779)
Browse files Browse the repository at this point in the history
Co-authored-by: David Townley <[email protected]>
  • Loading branch information
d-g-town and David Townley authored Oct 10, 2023
1 parent 57dafd2 commit 3f47bb6
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 489 deletions.
82 changes: 82 additions & 0 deletions api/server/handlers/environment_groups/update_linked_apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package environment_groups

import (
"net/http"

"connectrpc.com/connect"

porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
"github.com/porter-dev/porter/api/server/authz"
"github.com/porter-dev/porter/api/server/handlers"
"github.com/porter-dev/porter/api/server/shared"
"github.com/porter-dev/porter/api/server/shared/apierrors"
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/types"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)

// UpdateLinkedAppsHandler is the handle for the /environment-group/update-linked-apps endpoint
type UpdateLinkedAppsHandler struct {
handlers.PorterHandlerReadWriter
authz.KubernetesAgentGetter
}

// NewUpdateLinkedAppsHandler creates an instance of UpdateLinkedAppsHandler
func NewUpdateLinkedAppsHandler(
config *config.Config,
decoderValidator shared.RequestDecoderValidator,
writer shared.ResultWriter,
) *UpdateLinkedAppsHandler {
return &UpdateLinkedAppsHandler{
PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
}
}

// UpdateLinkedAppsRequest is the request object for the /environment-group/update-linked-apps endpoint
type UpdateLinkedAppsRequest struct {
Name string `json:"name"`
}

// UpdateLinkedAppsResponse is the response object for the /environment-group/update-linked-apps endpoint
type UpdateLinkedAppsResponse struct{}

// ServeHTTP updates all apps linked to an environment group
func (c *UpdateLinkedAppsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.NewSpan(r.Context(), "serve-update-apps-linked-to-env-group")
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")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusForbidden))
return
}

request := &UpdateLinkedAppsRequest{}
if ok := c.DecodeAndValidate(w, r, request); !ok {
err := telemetry.Error(ctx, span, nil, "error decoding request")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "env-group-name", Value: request.Name})

updateLinkedAppsReq := connect.NewRequest(&porterv1.UpdateAppsLinkedToEnvGroupRequest{
ProjectId: int64(project.ID),
ClusterId: int64(cluster.ID),
EnvGroupName: request.Name,
})
_, err := c.Config().ClusterControlPlaneClient.UpdateAppsLinkedToEnvGroup(ctx, updateLinkedAppsReq)
if err != nil {
err := telemetry.Error(ctx, span, err, "error calling ccp update apps linked to env group")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

res := &UpdateLinkedAppsResponse{}

c.WriteResult(w, r, res)
}
29 changes: 29 additions & 0 deletions api/server/router/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,5 +1692,34 @@ func getClusterRoutes(
Router: r,
})

// GET /api/projects/{project_id}/clusters/{cluster_id}/environment-groups/update-linked-apps
updateLinkedAppsEndpoint := factory.NewAPIEndpoint(
&types.APIRequestMetadata{
Verb: types.APIVerbUpdate,
Method: types.HTTPVerbPost,
Path: &types.Path{
Parent: basePath,
RelativePath: relPath + "/environment-groups/update-linked-apps",
},
Scopes: []types.PermissionScope{
types.UserScope,
types.ProjectScope,
types.ClusterScope,
},
},
)

updateLinkedAppsHandler := environment_groups.NewUpdateLinkedAppsHandler(
config,
factory.GetDecoderValidator(),
factory.GetResultWriter(),
)

routes = append(routes, &router.Route{
Endpoint: updateLinkedAppsEndpoint,
Handler: updateLinkedAppsHandler,
Router: r,
})

return routes, newPath
}
Loading

0 comments on commit 3f47bb6

Please sign in to comment.