update.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package api_contract
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "github.com/bufbuild/connect-go"
  7. "github.com/google/uuid"
  8. helpers "github.com/porter-dev/api-contracts/generated/go/helpers"
  9. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. type APIContractUpdateHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewAPIContractUpdateHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *APIContractUpdateHandler {
  24. return &APIContractUpdateHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. // ServeHTTP parses the Porter API contract for validity, and forwards the requests for handling on to another service
  29. // 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
  30. func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ctx := r.Context()
  32. var apiContract porterv1.Contract
  33. err := helpers.UnmarshalContractObjectFromReader(r.Body, &apiContract)
  34. if err != nil {
  35. e := fmt.Errorf("error parsing api contract: %w", err)
  36. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  37. return
  38. }
  39. if c.Config().DisableCAPIProvisioner {
  40. // return dummy data if capi provisioner disabled
  41. // TODO: remove this stub when we can spin up all services locally, easily
  42. clusterID := apiContract.Cluster.ClusterId
  43. if apiContract.Cluster.ClusterId == 0 {
  44. dbcli := models.Cluster{
  45. ProjectID: uint(apiContract.Cluster.ProjectId),
  46. Status: "UPDATING_UNAVAILABLE",
  47. ProvisionedBy: "CAPI",
  48. CloudProvider: "AWS",
  49. CloudProviderCredentialIdentifier: apiContract.Cluster.CloudProviderCredentialsId,
  50. Name: apiContract.Cluster.GetEksKind().ClusterName,
  51. VanityName: apiContract.Cluster.GetEksKind().ClusterName,
  52. }
  53. dbcl, err := c.Config().Repo.Cluster().CreateCluster(&dbcli)
  54. if err != nil {
  55. e := fmt.Errorf("error updating mock contract: %w", err)
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  57. return
  58. }
  59. clusterID = int32(dbcl.ID)
  60. }
  61. by, err := helpers.MarshalContractObject(ctx, &apiContract)
  62. if err != nil {
  63. e := fmt.Errorf("error marshalling mock api contract: %w", err)
  64. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  65. return
  66. }
  67. b64Contract := base64.StdEncoding.EncodeToString([]byte(by))
  68. revisionInput := models.APIContractRevision{
  69. ID: uuid.New(),
  70. ClusterID: int(clusterID),
  71. ProjectID: int(apiContract.Cluster.ProjectId),
  72. Base64Contract: b64Contract,
  73. }
  74. revision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, revisionInput)
  75. if err != nil {
  76. e := fmt.Errorf("error updating mock contract: %w", err)
  77. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  78. return
  79. }
  80. resp := &porterv1.ContractRevision{
  81. ClusterId: int32(clusterID),
  82. ProjectId: apiContract.Cluster.ProjectId,
  83. RevisionId: revision.ID.String(),
  84. }
  85. w.WriteHeader(http.StatusCreated)
  86. c.WriteResult(w, r, resp)
  87. return
  88. }
  89. updateRequest := connect.NewRequest(&porterv1.UpdateContractRequest{
  90. Contract: &apiContract,
  91. })
  92. revision, err := c.Config().ClusterControlPlaneClient.UpdateContract(ctx, updateRequest)
  93. if err != nil {
  94. e := fmt.Errorf("error sending contract for update: %w", err)
  95. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  96. return
  97. }
  98. w.WriteHeader(http.StatusCreated)
  99. c.WriteResult(w, r, revision.Msg)
  100. }