list.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // CloudProvider is the cloud provider associated with the datastore
  54. CloudProvider string `json:"cloud_provider"`
  55. // CloudProviderCredentialIdentifier is the cloud provider credential identifier associated with the datastore
  56. CloudProviderCredentialIdentifier string `json:"cloud_provider_credential_identifier"`
  57. }
  58. // ListDatastoresHandler is a struct for listing all datastores for a given project
  59. type ListDatastoresHandler struct {
  60. handlers.PorterHandlerReadWriter
  61. authz.KubernetesAgentGetter
  62. }
  63. // NewListDatastoresHandler constructs a datastore ListDatastoresHandler
  64. func NewListDatastoresHandler(
  65. config *config.Config,
  66. decoderValidator shared.RequestDecoderValidator,
  67. writer shared.ResultWriter,
  68. ) *ListDatastoresHandler {
  69. return &ListDatastoresHandler{
  70. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  71. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  72. }
  73. }
  74. // ServeHTTP returns a list of datastores associated with the specified project
  75. func (h *ListDatastoresHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  76. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-datastores")
  77. defer span.End()
  78. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  79. resp := ListDatastoresResponse{}
  80. datastoreList := []Datastore{}
  81. datastores, err := h.Repo().Datastore().ListByProjectID(ctx, project.ID)
  82. if err != nil {
  83. err := telemetry.Error(ctx, span, err, "error getting datastores")
  84. h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. return
  86. }
  87. for _, datastore := range datastores {
  88. datastoreList = append(datastoreList, Datastore{
  89. Name: datastore.Name,
  90. Type: datastore.Type,
  91. Engine: datastore.Engine,
  92. CreatedAtUTC: datastore.CreatedAt,
  93. Status: string(datastore.Status),
  94. })
  95. }
  96. resp.Datastores = datastoreList
  97. h.WriteResult(w, r, resp)
  98. }
  99. // DatastoresInput is the input to the Datastores function
  100. type DatastoresInput struct {
  101. ProjectID uint
  102. CloudProvider cloud_provider.CloudProvider
  103. Name string
  104. Type porterv1.EnumDatastore
  105. IncludeEnvGroup bool
  106. IncludeMetadata bool
  107. CCPClient porterv1connect.ClusterControlPlaneServiceClient
  108. DatastoreRepository repository.DatastoreRepository
  109. }
  110. // Datastores returns a list of datastores associated with the specified project/cloud-provider
  111. func Datastores(ctx context.Context, inp DatastoresInput) ([]Datastore, error) {
  112. ctx, span := telemetry.NewSpan(ctx, "datastores-for-cloud-provider")
  113. defer span.End()
  114. telemetry.WithAttributes(span,
  115. telemetry.AttributeKV{Key: "datastore-name", Value: inp.Name},
  116. telemetry.AttributeKV{Key: "datastore-type", Value: int(inp.Type)},
  117. telemetry.AttributeKV{Key: "include-env-group", Value: inp.IncludeEnvGroup},
  118. telemetry.AttributeKV{Key: "include-metadata", Value: inp.IncludeMetadata},
  119. telemetry.AttributeKV{Key: "cloud-provider-type", Value: int(inp.CloudProvider.Type)},
  120. telemetry.AttributeKV{Key: "cloud-provider-id", Value: inp.CloudProvider.AccountID},
  121. telemetry.AttributeKV{Key: "project-id", Value: inp.ProjectID},
  122. )
  123. datastores := []Datastore{}
  124. if inp.ProjectID == 0 {
  125. return datastores, telemetry.Error(ctx, span, nil, "project id must be specified")
  126. }
  127. if inp.CloudProvider.Type == porterv1.EnumCloudProvider_ENUM_CLOUD_PROVIDER_UNSPECIFIED {
  128. return datastores, telemetry.Error(ctx, span, nil, "cloud provider type must be specified")
  129. }
  130. if inp.CloudProvider.AccountID == "" {
  131. return datastores, telemetry.Error(ctx, span, nil, "cloud provider account id must be specified")
  132. }
  133. message := porterv1.ListDatastoresRequest{
  134. ProjectId: int64(inp.ProjectID),
  135. CloudProvider: inp.CloudProvider.Type,
  136. CloudProviderAccountId: inp.CloudProvider.AccountID,
  137. Name: inp.Name,
  138. IncludeEnvGroup: inp.IncludeEnvGroup,
  139. IncludeMetadata: inp.IncludeMetadata,
  140. }
  141. if inp.Type != porterv1.EnumDatastore_ENUM_DATASTORE_UNSPECIFIED {
  142. message.Type = &inp.Type
  143. }
  144. req := connect.NewRequest(&message)
  145. resp, ccpErr := inp.CCPClient.ListDatastores(ctx, req)
  146. if ccpErr != nil {
  147. return datastores, telemetry.Error(ctx, span, ccpErr, "error listing datastores from ccp")
  148. }
  149. if resp.Msg == nil {
  150. return datastores, telemetry.Error(ctx, span, nil, "missing response message from ccp")
  151. }
  152. for _, datastore := range resp.Msg.Datastores {
  153. datastoreRecord, err := inp.DatastoreRepository.GetByProjectIDAndName(ctx, inp.ProjectID, datastore.Name)
  154. if err != nil {
  155. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "err-datastore-name", Value: datastore.Name})
  156. return datastores, telemetry.Error(ctx, span, err, "datastore record not found")
  157. }
  158. datastores = append(datastores, Datastore{
  159. Name: datastore.Name,
  160. Type: datastoreRecord.Type,
  161. Engine: datastoreRecord.Engine,
  162. CreatedAtUTC: datastoreRecord.CreatedAt,
  163. Status: string(datastoreRecord.Status),
  164. Metadata: datastore.Metadata,
  165. Env: datastore.Env,
  166. CloudProvider: datastoreRecord.CloudProvider,
  167. CloudProviderCredentialIdentifier: datastoreRecord.CloudProviderCredentialIdentifier,
  168. })
  169. }
  170. return datastores, nil
  171. }