Skip to content

Commit

Permalink
integrate list env group api (#4221)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-town authored Feb 1, 2024
1 parent 2859b0a commit fb1408a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
102 changes: 59 additions & 43 deletions api/server/handlers/environment_groups/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"connectrpc.com/connect"

porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"

"github.com/porter-dev/porter/api/server/authz"
Expand Down Expand Up @@ -71,6 +72,43 @@ func (c *ListEnvironmentGroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "env-group-type", Value: request.Type})

if project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
listEnvGroupsReq := connect.NewRequest(&porterv1.ListEnvGroupsRequest{
ProjectId: int64(project.ID),
ClusterId: int64(cluster.ID),
IncludeSecrets: false,
})

listEnvGroupResp, err := c.Config().ClusterControlPlaneClient.ListEnvGroups(ctx, listEnvGroupsReq)
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to get linked applications")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}
if listEnvGroupResp == nil || listEnvGroupResp.Msg == nil {
err = telemetry.Error(ctx, span, err, "ccp resp is nil")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

var envGroups []EnvironmentGroupListItem
for _, envGroup := range listEnvGroupResp.Msg.EnvGroups {
envGroups = append(envGroups, EnvironmentGroupListItem{
Name: envGroup.Name,
Type: translateProtoTypeToEnvGroupType[envGroup.Type],
LatestVersion: int(envGroup.Version),
Variables: envGroup.Variables,
SecretVariables: envGroup.SecretVariables,
CreatedAtUTC: envGroup.CreatedAt.AsTime(),
LinkedApplications: envGroup.LinkedApplications,
})
}

// return early for cleaner change
c.WriteResult(w, r, ListEnvironmentGroupsResponse{EnvironmentGroups: envGroups})
return
}

agent, err := c.GetAgent(r, cluster, "")
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to connect to cluster")
Expand Down Expand Up @@ -115,53 +153,26 @@ func (c *ListEnvironmentGroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.
}

var linkedApplications []string
if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
applications, err := environmentgroups.LinkedApplications(ctx, agent, latestVersion.Name, true)
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to get linked applications")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

applicationSetForEnvGroup := make(map[string]struct{})
for _, app := range applications {
if app.Namespace == "" {
continue
}
if _, ok := applicationSetForEnvGroup[app.Namespace]; !ok {
applicationSetForEnvGroup[app.Namespace] = struct{}{}
}
}
for appNamespace := range applicationSetForEnvGroup {
porterAppName := strings.TrimPrefix(appNamespace, "porter-stack-")
linkedApplications = append(linkedApplications, porterAppName)
}
} else {
appsLinkedToEnvGroupReq := connect.NewRequest(&porterv1.AppsLinkedToEnvGroupRequest{
ProjectId: int64(project.ID),
ClusterId: int64(cluster.ID),
EnvGroupName: envGroupName,
IgnorePreview: true,
})
applications, err := environmentgroups.LinkedApplications(ctx, agent, latestVersion.Name, true)
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to get linked applications")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

appsLinkedToEnvGroupResp, err := c.Config().ClusterControlPlaneClient.AppsLinkedToEnvGroup(ctx, appsLinkedToEnvGroupReq)
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to get linked applications")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
applicationSetForEnvGroup := make(map[string]struct{})
for _, app := range applications {
if app.Namespace == "" {
continue
}
if appsLinkedToEnvGroupResp == nil || appsLinkedToEnvGroupResp.Msg == nil {
err = telemetry.Error(ctx, span, err, "ccp resp is nil")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

for _, app := range appsLinkedToEnvGroupResp.Msg.LinkedApps {
if app != nil {
linkedApplications = append(linkedApplications, app.Name)
}
if _, ok := applicationSetForEnvGroup[app.Namespace]; !ok {
applicationSetForEnvGroup[app.Namespace] = struct{}{}
}
}
for appNamespace := range applicationSetForEnvGroup {
porterAppName := strings.TrimPrefix(appNamespace, "porter-stack-")
linkedApplications = append(linkedApplications, porterAppName)
}

secrets := make(map[string]string)
for k, v := range latestVersion.SecretVariables {
Expand All @@ -180,3 +191,8 @@ func (c *ListEnvironmentGroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.

c.WriteResult(w, r, ListEnvironmentGroupsResponse{EnvironmentGroups: envGroups})
}

var translateProtoTypeToEnvGroupType = map[porterv1.EnumEnvGroupProviderType]string{
porterv1.EnumEnvGroupProviderType_ENUM_ENV_GROUP_PROVIDER_TYPE_DOPPLER: "doppler",
porterv1.EnumEnvGroupProviderType_ENUM_ENV_GROUP_PROVIDER_TYPE_PORTER: "porter",
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ export const ExpandedEnvGroupFC = ({
}, [currentEnvGroup]);
const populateEnvGroup = async () => {

if (currentProject?.simplified_view_enabled) {
// apply v2 already supplies the full env group
if (currentProject?.validate_apply_v2) {
updateEnvGroup(envGroup);
} else if (!currentProject?.simplified_view_enabled) {
try {
const populatedEnvGroup = await api
.getAllEnvGroups(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
github.com/matryer/is v1.4.0
github.com/nats-io/nats.go v1.24.0
github.com/open-policy-agent/opa v0.44.0
github.com/porter-dev/api-contracts v0.2.91
github.com/porter-dev/api-contracts v0.2.96
github.com/riandyrn/otelchi v0.5.1
github.com/santhosh-tekuri/jsonschema/v5 v5.0.1
github.com/stefanmcshane/helm v0.0.0-20221213002717-88a4a2c6e77d
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,10 @@ github.com/porter-dev/api-contracts v0.2.90 h1:0ceIXz0xWNQpqVqhUMt3/RDeEawccfXx3
github.com/porter-dev/api-contracts v0.2.90/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
github.com/porter-dev/api-contracts v0.2.91 h1:jiFWQ+WISAtfjXalOmWJdSr1ZOm7/ov+3ozrCeYA9Ws=
github.com/porter-dev/api-contracts v0.2.91/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
github.com/porter-dev/api-contracts v0.2.95 h1:QsTwEpdLm+yx3PWJ5FERpB5rWwV7mvEp2CpV4QAC5Xo=
github.com/porter-dev/api-contracts v0.2.95/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
github.com/porter-dev/api-contracts v0.2.96 h1:r380Tn23daY+fH7REDZf+HlH6PcG0cihrZxMibmATT4=
github.com/porter-dev/api-contracts v0.2.96/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
github.com/porter-dev/switchboard v0.0.3 h1:dBuYkiVLa5Ce7059d6qTe9a1C2XEORFEanhbtV92R+M=
github.com/porter-dev/switchboard v0.0.3/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down

0 comments on commit fb1408a

Please sign in to comment.