update.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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)
  100. // if apiContract.Cluster == nil {
  101. // e := errors.New("missing cluster object")
  102. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  103. // return
  104. // }
  105. // cl := apiContract.Cluster
  106. // if cl.CloudProviderCredentialsId == "" {
  107. // e := errors.New("missing cloud_provider_credential_identifier")
  108. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  109. // return
  110. // }
  111. // if cl.GetEksKind() == nil {
  112. // e := errors.New("missing eks_kind_values")
  113. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  114. // return
  115. // }
  116. // if cl.ClusterId == 0 {
  117. // dbClusterInput := models.Cluster{
  118. // ProjectID: uint(cl.ProjectId),
  119. // Status: types.UpdatingUnavailable,
  120. // ProvisionedBy: "CAPI",
  121. // CloudProvider: "AWS",
  122. // CloudProviderCredentialIdentifier: cl.CloudProviderCredentialsId,
  123. // Name: cl.GetEksKind().ClusterName,
  124. // VanityName: cl.GetEksKind().ClusterName,
  125. // }
  126. // dbCluster, err := c.Config().Repo.Cluster().CreateCluster(&dbClusterInput)
  127. // if err != nil {
  128. // e := fmt.Errorf("error creating new cluster: %w", err)
  129. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  130. // return
  131. // }
  132. // apiContract.Cluster.ClusterId = int32(dbCluster.ID)
  133. // }
  134. // by, err := helpers.MarshalContractObject(ctx, &apiContract)
  135. // if err != nil {
  136. // e := fmt.Errorf("error marshalling api contract: %w", err)
  137. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  138. // return
  139. // }
  140. // b64 := base64.StdEncoding.EncodeToString([]byte(by))
  141. // apiContractRevision := models.APIContractRevision{
  142. // ClusterID: int(cl.ClusterId),
  143. // ProjectID: int(cl.ProjectId),
  144. // Base64Contract: string(b64),
  145. // }
  146. // contractRevision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, apiContractRevision)
  147. // if err != nil {
  148. // e := fmt.Errorf("error creating new capi config: %w", err)
  149. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  150. // return
  151. // }
  152. // // This gates the cluster actually being provisioned by CAPI
  153. // // This can be removed whenever we are able to run NATS and CCP locally, easier
  154. // if !c.Config().DisableCAPIProvisioner {
  155. // resp := porterv1.ContractRevision{
  156. // ProjectId: cl.ProjectId,
  157. // ClusterId: cl.ClusterId,
  158. // RevisionId: contractRevision.ID.String(),
  159. // }
  160. // kubeBy, err := helpers.MarshalContractObject(ctx, &resp)
  161. // if err != nil {
  162. // e := fmt.Errorf("error marshalling api contract: %w", err)
  163. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  164. // return
  165. // }
  166. // subject := "porter.system.infrastructure.update"
  167. // _, err = c.Config().NATS.JetStream.Publish(subject, kubeBy, nats.Context(ctx))
  168. // if err != nil {
  169. // e := fmt.Errorf("error publishing cluster for creation: %w", err)
  170. // c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  171. // return
  172. // }
  173. // }
  174. // w.WriteHeader(http.StatusCreated)
  175. // c.WriteResult(w, r, contractRevision)
  176. }