attach_env_group.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package porter_app
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. // AttachEnvGroupHandler is the handler for the /apps/attach-env-group endpoint
  16. type AttachEnvGroupHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. // NewAttachEnvGroupHandler handles POST requests to the endpoint /apps/attach-env-group
  21. func NewAttachEnvGroupHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *AttachEnvGroupHandler {
  26. return &AttachEnvGroupHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  29. }
  30. }
  31. // AttachEnvGroupRequest is the request object for the /apps/attach-env-group endpoint
  32. type AttachEnvGroupRequest struct {
  33. EnvGroupName string `json:"env_group_name"`
  34. AppInstanceIDs []string `json:"app_instance_ids"`
  35. }
  36. // ServeHTTP translates the request into a AttachEnvGroup request, then calls update on the app with the env group
  37. // The latest version of the env group will be attached (ccp makes sure of that)
  38. func (c *AttachEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  39. ctx, span := telemetry.NewSpan(r.Context(), "serve-attach-env-group")
  40. defer span.End()
  41. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  42. request := &AttachEnvGroupRequest{}
  43. if ok := c.DecodeAndValidate(w, r, request); !ok {
  44. err := telemetry.Error(ctx, span, nil, "error decoding request")
  45. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  46. return
  47. }
  48. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "env-group-name", Value: request.EnvGroupName})
  49. usingUpdateLogic := project.GetFeatureFlag(models.BetaFeaturesEnabled, c.Config().LaunchDarklyClient)
  50. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "using-update-logic", Value: usingUpdateLogic})
  51. if request.EnvGroupName == "" {
  52. err := telemetry.Error(ctx, span, nil, "env group name cannot be empty")
  53. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  54. return
  55. }
  56. for _, appInstanceId := range request.AppInstanceIDs {
  57. appInstance, err := c.Repo().AppInstance().Get(ctx, appInstanceId)
  58. if err != nil {
  59. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  60. err := telemetry.Error(ctx, span, err, "error getting app instance")
  61. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  62. return
  63. }
  64. // TODO: delete second branch once all projects are on update flow
  65. if usingUpdateLogic {
  66. updateReq := connect.NewRequest(&porterv1.UpdateAppRequest{
  67. ProjectId: int64(project.ID),
  68. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  69. Id: appInstance.DeploymentTargetID.String(),
  70. },
  71. App: &porterv1.PorterApp{
  72. Name: appInstance.Name,
  73. EnvGroups: []*porterv1.EnvGroup{
  74. {
  75. Name: request.EnvGroupName,
  76. },
  77. },
  78. },
  79. })
  80. _, err = c.Config().ClusterControlPlaneClient.UpdateApp(ctx, updateReq)
  81. if err != nil {
  82. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  83. err := telemetry.Error(ctx, span, err, "error calling ccp update app")
  84. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. return
  86. }
  87. } else {
  88. validateReq := connect.NewRequest(&porterv1.ValidatePorterAppRequest{
  89. ProjectId: int64(project.ID),
  90. DeploymentTargetId: appInstance.DeploymentTargetID.String(),
  91. App: &porterv1.PorterApp{
  92. Name: appInstance.Name,
  93. EnvGroups: []*porterv1.EnvGroup{
  94. {
  95. Name: request.EnvGroupName,
  96. },
  97. },
  98. },
  99. })
  100. ccpResp, err := c.Config().ClusterControlPlaneClient.ValidatePorterApp(ctx, validateReq)
  101. if err != nil {
  102. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  103. err := telemetry.Error(ctx, span, err, "error calling ccp validate porter app")
  104. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  105. return
  106. }
  107. if ccpResp == nil {
  108. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  109. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  111. return
  112. }
  113. if ccpResp.Msg == nil {
  114. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  115. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  116. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  117. return
  118. }
  119. if ccpResp.Msg.App == nil {
  120. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  121. err := telemetry.Error(ctx, span, err, "ccp resp app is nil")
  122. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  123. return
  124. }
  125. applyReq := connect.NewRequest(&porterv1.ApplyPorterAppRequest{
  126. ProjectId: int64(project.ID),
  127. DeploymentTargetId: appInstance.DeploymentTargetID.String(),
  128. App: ccpResp.Msg.App,
  129. })
  130. _, err = c.Config().ClusterControlPlaneClient.ApplyPorterApp(ctx, applyReq)
  131. if err != nil {
  132. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId})
  133. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  134. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  135. return
  136. }
  137. }
  138. }
  139. c.WriteResult(w, r, nil)
  140. }