create.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package datastore
  2. import (
  3. "context"
  4. "github.com/google/uuid"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/internal/repository"
  7. "github.com/porter-dev/porter/internal/telemetry"
  8. )
  9. // CreateOrGetRecordInput is the input type for CreateOrGetDatastoreRecord
  10. type CreateOrGetRecordInput struct {
  11. ProjectID uint
  12. ClusterID uint
  13. Name string
  14. Type string
  15. Engine string
  16. DatastoreRepository repository.DatastoreRepository
  17. ClusterRepository repository.ClusterRepository
  18. }
  19. // CreateOrGetRecord creates a datastore record if it does not exist, or returns the existing one if it does
  20. func CreateOrGetRecord(ctx context.Context, inp CreateOrGetRecordInput) (*models.Datastore, error) {
  21. ctx, span := telemetry.NewSpan(ctx, "create-or-get-datastore-record")
  22. defer span.End()
  23. telemetry.WithAttributes(span,
  24. telemetry.AttributeKV{Key: "project-id", Value: inp.ProjectID},
  25. telemetry.AttributeKV{Key: "name", Value: inp.Name},
  26. telemetry.AttributeKV{Key: "cluster-id", Value: inp.ClusterID},
  27. telemetry.AttributeKV{Key: "type", Value: inp.Type},
  28. telemetry.AttributeKV{Key: "engine", Value: inp.Engine},
  29. )
  30. var datastore *models.Datastore
  31. if inp.ProjectID == 0 {
  32. return datastore, telemetry.Error(ctx, span, nil, "project id is 0")
  33. }
  34. if inp.Name == "" {
  35. return datastore, telemetry.Error(ctx, span, nil, "name is empty")
  36. }
  37. datastore, err := inp.DatastoreRepository.GetByProjectIDAndName(ctx, inp.ProjectID, inp.Name)
  38. if err != nil {
  39. return datastore, telemetry.Error(ctx, span, err, "error reading datastore by project id and name")
  40. }
  41. if datastore != nil && datastore.ID != uuid.Nil {
  42. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-datastore-id", Value: datastore.ID})
  43. return datastore, nil
  44. }
  45. if inp.ClusterID == 0 {
  46. return datastore, telemetry.Error(ctx, span, nil, "cluster id is 0")
  47. }
  48. cluster, err := inp.ClusterRepository.ReadCluster(inp.ProjectID, inp.ClusterID)
  49. if err != nil {
  50. return datastore, telemetry.Error(ctx, span, err, "error reading cluster")
  51. }
  52. datastoreToSave := &models.Datastore{
  53. ProjectID: inp.ProjectID,
  54. Name: inp.Name,
  55. Type: inp.Type,
  56. Engine: inp.Engine,
  57. CloudProvider: cluster.CloudProvider,
  58. CloudProviderCredentialIdentifier: cluster.CloudProviderCredentialIdentifier,
  59. Status: models.DatastoreStatus_Creating,
  60. }
  61. datastore, err = inp.DatastoreRepository.Insert(ctx, datastoreToSave)
  62. if err != nil {
  63. return datastore, telemetry.Error(ctx, span, err, "error inserting datastore")
  64. }
  65. return datastore, nil
  66. }