update.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package api_contract
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "connectrpc.com/connect"
  6. "github.com/google/uuid"
  7. helpers "github.com/porter-dev/api-contracts/generated/go/helpers"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/porter-dev/porter/internal/telemetry"
  16. )
  17. type APIContractUpdateHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewAPIContractUpdateHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *APIContractUpdateHandler {
  25. return &APIContractUpdateHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // ServeHTTP parses the Porter API contract for validity, and forwards the requests for handling on to another service
  30. // For now, this handling cluster creation only, by inserting a row into the cluster table in order to create an ID for this cluster, as well as stores the raw request JSON for updating later
  31. func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. ctx, span := telemetry.NewSpan(r.Context(), "serve-update-api-contract")
  33. defer span.End()
  34. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  35. user, _ := ctx.Value(types.UserScope).(*models.User)
  36. var apiContract porterv1.Contract
  37. err := helpers.UnmarshalContractObjectFromReader(r.Body, &apiContract)
  38. if err != nil {
  39. e := telemetry.Error(ctx, span, err, "error parsing api contract")
  40. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  41. return
  42. }
  43. if !project.CapiProvisionerEnabled && !c.Config().EnableCAPIProvisioner {
  44. // return dummy data if capi provisioner disabled in project settings, and as env var
  45. // TODO: remove this stub when we can spin up all services locally, easily
  46. clusterID := apiContract.Cluster.ClusterId
  47. if apiContract.Cluster.ClusterId == 0 {
  48. dbcli := models.Cluster{
  49. ProjectID: uint(apiContract.Cluster.ProjectId),
  50. Status: "UPDATING_UNAVAILABLE",
  51. ProvisionedBy: "CAPI",
  52. CloudProvider: "AWS",
  53. CloudProviderCredentialIdentifier: apiContract.Cluster.CloudProviderCredentialsId,
  54. Name: apiContract.Cluster.GetEksKind().ClusterName,
  55. VanityName: apiContract.Cluster.GetEksKind().ClusterName,
  56. }
  57. dbcl, err := c.Config().Repo.Cluster().CreateCluster(&dbcli)
  58. if err != nil {
  59. e := telemetry.Error(ctx, span, err, "error updating mocking contract")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  61. return
  62. }
  63. clusterID = int32(dbcl.ID)
  64. }
  65. by, err := helpers.MarshalContractObject(ctx, &apiContract)
  66. if err != nil {
  67. e := telemetry.Error(ctx, span, err, "error marshalling mock api contract")
  68. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  69. return
  70. }
  71. b64Contract := base64.StdEncoding.EncodeToString([]byte(by))
  72. revisionInput := models.APIContractRevision{
  73. ID: uuid.New(),
  74. ClusterID: int(clusterID),
  75. ProjectID: int(apiContract.Cluster.ProjectId),
  76. Base64Contract: b64Contract,
  77. }
  78. revision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, revisionInput)
  79. if err != nil {
  80. e := telemetry.Error(ctx, span, err, "error updating mock api contract")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  82. return
  83. }
  84. resp := &porterv1.ContractRevision{
  85. ClusterId: int32(clusterID),
  86. ProjectId: apiContract.Cluster.ProjectId,
  87. RevisionId: revision.ID.String(),
  88. }
  89. w.WriteHeader(http.StatusCreated)
  90. c.WriteResult(w, r, resp)
  91. return
  92. }
  93. apiContract.User = &porterv1.User{
  94. Id: int32(user.ID),
  95. }
  96. updateRequest := connect.NewRequest(&porterv1.UpdateContractRequest{
  97. Contract: &apiContract,
  98. })
  99. revision, err := c.Config().ClusterControlPlaneClient.UpdateContract(ctx, updateRequest)
  100. if err != nil {
  101. e := telemetry.Error(ctx, span, err, "error sending contract for update")
  102. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  103. return
  104. }
  105. w.WriteHeader(http.StatusCreated)
  106. c.WriteResult(w, r, revision.Msg)
  107. }