2
0

create_gcp.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package project_integration
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  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. ints "github.com/porter-dev/porter/internal/models/integrations"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. )
  16. type CreateGCPHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewCreateGCPHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *CreateGCPHandler {
  24. return &CreateGCPHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (p *CreateGCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. ctx, span := telemetry.NewSpan(r.Context(), "serve-create-gcp-credentials")
  30. defer span.End()
  31. user, _ := ctx.Value(types.UserScope).(*models.User)
  32. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  33. request := &types.CreateGCPRequest{}
  34. if ok := p.DecodeAndValidate(w, r, request); !ok {
  35. return
  36. }
  37. if project.GetFeatureFlag(models.CapiProvisionerEnabled, p.Config().LaunchDarklyClient) {
  38. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "capi-provisioner-enabled", Value: true})
  39. b64Key := base64.StdEncoding.EncodeToString([]byte(request.GCPKeyData))
  40. ccpCredentialsInput := &connect.Request[porterv1.UpdateCloudProviderCredentialsRequest]{
  41. Msg: &porterv1.UpdateCloudProviderCredentialsRequest{
  42. ProjectId: int64(project.ID),
  43. CloudProvider: porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_GCP,
  44. CloudProviderCredentials: &porterv1.UpdateCloudProviderCredentialsRequest_GcpCredentials{
  45. GcpCredentials: &porterv1.GCPCredentials{
  46. ServiceAccountJsonBase64: b64Key,
  47. },
  48. },
  49. },
  50. }
  51. ccpCredentialsResponse, err := p.Config().ClusterControlPlaneClient.UpdateCloudProviderCredentials(ctx, ccpCredentialsInput)
  52. if err != nil {
  53. e := telemetry.Error(ctx, span, err, "failed to update cloud provider credentials")
  54. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  55. return
  56. }
  57. if ccpCredentialsResponse.Msg == nil {
  58. e := telemetry.Error(ctx, span, nil, "nil response when updating provider credentials")
  59. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  60. return
  61. }
  62. res := types.CreateGCPResponse{
  63. IsCCPCluster: true,
  64. CloudProviderCredentialIdentifier: ccpCredentialsResponse.Msg.CredentialsIdentifier,
  65. }
  66. p.WriteResult(w, r, res)
  67. return
  68. }
  69. gcp := CreateGCPIntegration(request, project.ID, user.ID)
  70. gcp, err := p.Repo().GCPIntegration().CreateGCPIntegration(gcp)
  71. if err != nil {
  72. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  73. return
  74. }
  75. res := types.CreateGCPResponse{
  76. GCPIntegration: gcp.ToGCPIntegrationType(),
  77. }
  78. p.WriteResult(w, r, res)
  79. }
  80. func CreateGCPIntegration(request *types.CreateGCPRequest, projectID, userID uint) *ints.GCPIntegration {
  81. resp := &ints.GCPIntegration{
  82. UserID: userID,
  83. ProjectID: projectID,
  84. GCPKeyData: []byte(request.GCPKeyData),
  85. GCPProjectID: request.GCPProjectID,
  86. GCPRegion: request.GCPRegion,
  87. }
  88. resp.PopulateGCPMetadata()
  89. return resp
  90. }