create.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // CreateOrGetDatastoreRecordInput is the input type for CreateOrGetDatastoreRecord
  10. type CreateOrGetDatastoreRecordInput 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. // CreateOrGetDatastoreRecord creates a datastore record if it does not exist, or returns the existing one if it does
  20. func CreateOrGetDatastoreRecord(ctx context.Context, inp CreateOrGetDatastoreRecordInput) (*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. }
  60. datastore, err = inp.DatastoreRepository.Insert(ctx, datastoreToSave)
  61. if err != nil {
  62. return datastore, telemetry.Error(ctx, span, err, "error inserting datastore")
  63. }
  64. return datastore, nil
  65. }