list.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package datastore
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/api-contracts/generated/go/porter/v1/porterv1connect"
  9. "github.com/porter-dev/porter/api/server/authz"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/handlers/cloud_provider"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "github.com/porter-dev/porter/api/types"
  16. "github.com/porter-dev/porter/internal/models"
  17. "github.com/porter-dev/porter/internal/repository"
  18. "github.com/porter-dev/porter/internal/telemetry"
  19. )
  20. // ListDatastoresRequest is a struct that represents the various filter options used for
  21. // retrieving the datastores
  22. type ListDatastoresRequest struct {
  23. // Name is the name of the datastore to filter by
  24. Name string `schema:"name"`
  25. // Type is the type of the datastore to filter by
  26. Type string `schema:"type"`
  27. // IncludeEnvGroup controls whether to include the datastore env group or not
  28. IncludeEnvGroup bool `schema:"include_env_group"`
  29. // IncludeMetadata controls whether to include datastore metadata or not
  30. IncludeMetadata bool `schema:"include_metadata"`
  31. }
  32. // ListDatastoresResponse describes the list datastores response body
  33. type ListDatastoresResponse struct {
  34. // Datastores is a list of datastore entries for the http response
  35. Datastores []Datastore `json:"datastores"`
  36. }
  37. // Datastore describes an outbound datastores response entry
  38. type Datastore struct {
  39. // Name is the name of the datastore
  40. Name string `json:"name"`
  41. // Type is the type of the datastore
  42. Type string `json:"type"`
  43. // Engine is the engine of the datastore
  44. Engine string `json:"engine,omitempty"`
  45. // Env is the env group for the datastore
  46. Env *porterv1.EnvGroup `json:"env,omitempty"`
  47. // Metadata is a list of metadata objects for the datastore
  48. Metadata []*porterv1.DatastoreMetadata `json:"metadata,omitempty"`
  49. // Status is the status of the datastore
  50. Status string `json:"status"`
  51. // CreatedAtUTC is the time the datastore was created in UTC
  52. CreatedAtUTC time.Time `json:"created_at"`
  53. }
  54. // ListDatastoresHandler is a struct for listing all datastores for a given project
  55. type ListDatastoresHandler struct {
  56. handlers.PorterHandlerReadWriter
  57. authz.KubernetesAgentGetter
  58. }
  59. // NewListDatastoresHandler constructs a datastore ListDatastoresHandler
  60. func NewListDatastoresHandler(
  61. config *config.Config,
  62. decoderValidator shared.RequestDecoderValidator,
  63. writer shared.ResultWriter,
  64. ) *ListDatastoresHandler {
  65. return &ListDatastoresHandler{
  66. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  67. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  68. }
  69. }
  70. // ServeHTTP returns a list of datastores associated with the specified project
  71. func (h *ListDatastoresHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  72. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-datastores")
  73. defer span.End()
  74. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  75. resp := ListDatastoresResponse{}
  76. datastoreList := []Datastore{}
  77. datastores, err := h.Repo().Datastore().ListByProjectID(ctx, project.ID)
  78. if err != nil {
  79. err := telemetry.Error(ctx, span, err, "error getting datastores")
  80. h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  81. return
  82. }
  83. for _, datastore := range datastores {
  84. datastoreList = append(datastoreList, Datastore{
  85. Name: datastore.Name,
  86. Type: datastore.Type,
  87. Engine: datastore.Engine,
  88. CreatedAtUTC: datastore.CreatedAt,
  89. Status: string(datastore.Status),
  90. })
  91. }
  92. resp.Datastores = datastoreList
  93. h.WriteResult(w, r, resp)
  94. }
  95. // DatastoresInput is the input to the Datastores function
  96. type DatastoresInput struct {
  97. ProjectID uint
  98. CloudProvider cloud_provider.CloudProvider
  99. Name string
  100. Type porterv1.EnumDatastore
  101. IncludeEnvGroup bool
  102. IncludeMetadata bool
  103. CCPClient porterv1connect.ClusterControlPlaneServiceClient
  104. DatastoreRepository repository.DatastoreRepository
  105. }
  106. // Datastores returns a list of datastores associated with the specified project/cloud-provider
  107. func Datastores(ctx context.Context, inp DatastoresInput) ([]Datastore, error) {
  108. ctx, span := telemetry.NewSpan(ctx, "datastores-for-cloud-provider")
  109. defer span.End()
  110. telemetry.WithAttributes(span,
  111. telemetry.AttributeKV{Key: "datastore-name", Value: inp.Name},
  112. telemetry.AttributeKV{Key: "datastore-type", Value: int(inp.Type)},
  113. telemetry.AttributeKV{Key: "include-env-group", Value: inp.IncludeEnvGroup},
  114. telemetry.AttributeKV{Key: "include-metadata", Value: inp.IncludeMetadata},
  115. telemetry.AttributeKV{Key: "cloud-provider-type", Value: int(inp.CloudProvider.Type)},
  116. telemetry.AttributeKV{Key: "cloud-provider-id", Value: inp.CloudProvider.AccountID},
  117. telemetry.AttributeKV{Key: "project-id", Value: inp.ProjectID},
  118. )
  119. datastores := []Datastore{}
  120. if inp.ProjectID == 0 {
  121. return datastores, telemetry.Error(ctx, span, nil, "project id must be specified")
  122. }
  123. if inp.CloudProvider.Type == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_UNSPECIFIED {
  124. return datastores, telemetry.Error(ctx, span, nil, "cloud provider type must be specified")
  125. }
  126. if inp.CloudProvider.AccountID == "" {
  127. return datastores, telemetry.Error(ctx, span, nil, "cloud provider account id must be specified")
  128. }
  129. message := porterv1.ListDatastoresRequest{
  130. ProjectId: int64(inp.ProjectID),
  131. CloudProvider: inp.CloudProvider.Type,
  132. CloudProviderAccountId: inp.CloudProvider.AccountID,
  133. Name: inp.Name,
  134. IncludeEnvGroup: inp.IncludeEnvGroup,
  135. IncludeMetadata: inp.IncludeMetadata,
  136. }
  137. if inp.Type != porterv1.EnumDatastore_ENUM_DATASTORE_UNSPECIFIED {
  138. message.Type = &inp.Type
  139. }
  140. req := connect.NewRequest(&message)
  141. resp, ccpErr := inp.CCPClient.ListDatastores(ctx, req)
  142. if ccpErr != nil {
  143. return datastores, telemetry.Error(ctx, span, ccpErr, "error listing datastores from ccp")
  144. }
  145. if resp.Msg == nil {
  146. return datastores, telemetry.Error(ctx, span, nil, "missing response message from ccp")
  147. }
  148. for _, datastore := range resp.Msg.Datastores {
  149. encodedDatastore := Datastore{
  150. Name: datastore.Name,
  151. Metadata: datastore.Metadata,
  152. Env: datastore.Env,
  153. }
  154. datastoreRecord, err := inp.DatastoreRepository.GetByProjectIDAndName(ctx, inp.ProjectID, datastore.Name)
  155. if err != nil {
  156. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "err-datastore-name", Value: datastore.Name})
  157. return datastores, telemetry.Error(ctx, span, err, "datastore record not found")
  158. }
  159. encodedDatastore.CreatedAtUTC = datastoreRecord.CreatedAt
  160. encodedDatastore.Type = datastoreRecord.Type
  161. encodedDatastore.Engine = datastoreRecord.Engine
  162. encodedDatastore.Status = string(datastoreRecord.Status)
  163. datastores = append(datastores, encodedDatastore)
  164. }
  165. return datastores, nil
  166. }