create_gcp.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package project_integration
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. ints "github.com/porter-dev/porter/internal/models/integrations"
  11. )
  12. type CreateGCPHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. func NewCreateGCPHandler(
  16. config *config.Config,
  17. decoderValidator shared.RequestDecoderValidator,
  18. writer shared.ResultWriter,
  19. ) *CreateGCPHandler {
  20. return &CreateGCPHandler{
  21. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  22. }
  23. }
  24. func (p *CreateGCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. user, _ := r.Context().Value(types.UserScope).(*models.User)
  26. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  27. request := &types.CreateGCPRequest{}
  28. if ok := p.DecodeAndValidate(w, r, request); !ok {
  29. return
  30. }
  31. gcp := CreateGCPIntegration(request, project.ID, user.ID)
  32. gcp, err := p.Repo().GCPIntegration().CreateGCPIntegration(gcp)
  33. if err != nil {
  34. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. res := types.CreateGCPResponse{
  38. GCPIntegration: gcp.ToGCPIntegrationType(),
  39. }
  40. p.WriteResult(w, r, res)
  41. }
  42. func CreateGCPIntegration(request *types.CreateGCPRequest, projectID, userID uint) *ints.GCPIntegration {
  43. resp := &ints.GCPIntegration{
  44. UserID: userID,
  45. ProjectID: projectID,
  46. GCPKeyData: []byte(request.GCPKeyData),
  47. GCPProjectID: request.GCPProjectID,
  48. GCPRegion: request.GCPRegion,
  49. }
  50. resp.PopulateGCPMetadata()
  51. return resp
  52. }