create_azure.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package project_integration
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/internal/telemetry"
  5. "github.com/bufbuild/connect-go"
  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. )
  15. type CreateAzureHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewCreateAzureHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *CreateAzureHandler {
  23. return &CreateAzureHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (p *CreateAzureHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. ctx, span := telemetry.NewSpan(r.Context(), "serve-create-azure-connection")
  29. defer span.End()
  30. user, _ := ctx.Value(types.UserScope).(*models.User)
  31. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  32. request := &types.CreateAzureRequest{}
  33. if ok := p.DecodeAndValidate(w, r, request); !ok {
  34. err := telemetry.Error(ctx, span, nil, "error decoding and validating request")
  35. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  36. return
  37. }
  38. az := CreateAzureIntegration(request, project.ID, user.ID)
  39. az, err := p.Repo().AzureIntegration().CreateAzureIntegration(az)
  40. if err != nil {
  41. err := telemetry.Error(ctx, span, err, "error creating azure integration")
  42. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  43. return
  44. }
  45. res := types.CreateAzureResponse{
  46. AzureIntegration: az.ToAzureIntegrationType(),
  47. }
  48. req := connect.NewRequest(&porterv1.SaveAzureCredentialsRequest{
  49. ProjectId: int64(project.ID),
  50. ClientId: request.AzureClientID,
  51. SubscriptionId: request.AzureSubscriptionID,
  52. TenantId: request.AzureTenantID,
  53. ServicePrincipalSecret: []byte(request.ServicePrincipalKey),
  54. })
  55. resp, err := p.Config().ClusterControlPlaneClient.SaveAzureCredentials(ctx, req)
  56. if err != nil {
  57. err := telemetry.Error(ctx, span, err, "error saving azure credentials")
  58. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  59. return
  60. }
  61. if resp.Msg == nil || resp.Msg.CredentialsIdentifier == "" {
  62. err := telemetry.Error(ctx, span, nil, "no cloud credential identifier returned")
  63. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  64. return
  65. }
  66. res.CloudProviderCredentialIdentifier = resp.Msg.CredentialsIdentifier
  67. p.WriteResult(w, r, res)
  68. }
  69. func CreateAzureIntegration(request *types.CreateAzureRequest, projectID, userID uint) *ints.AzureIntegration {
  70. resp := &ints.AzureIntegration{
  71. UserID: userID,
  72. ProjectID: projectID,
  73. AzureClientID: request.AzureClientID,
  74. AzureSubscriptionID: request.AzureSubscriptionID,
  75. AzureTenantID: request.AzureTenantID,
  76. ServicePrincipalSecret: []byte(request.ServicePrincipalKey),
  77. }
  78. return resp
  79. }