| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package datastore
- import (
- "context"
- "github.com/google/uuid"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository"
- "github.com/porter-dev/porter/internal/telemetry"
- )
- // CreateOrGetDatastoreRecordInput is the input type for CreateOrGetDatastoreRecord
- type CreateOrGetDatastoreRecordInput struct {
- ProjectID uint
- ClusterID uint
- Name string
- Type string
- Engine string
- DatastoreRepository repository.DatastoreRepository
- ClusterRepository repository.ClusterRepository
- }
- // CreateOrGetDatastoreRecord creates a datastore record if it does not exist, or returns the existing one if it does
- func CreateOrGetDatastoreRecord(ctx context.Context, inp CreateOrGetDatastoreRecordInput) (*models.Datastore, error) {
- ctx, span := telemetry.NewSpan(ctx, "create-or-get-datastore-record")
- defer span.End()
- telemetry.WithAttributes(span,
- telemetry.AttributeKV{Key: "project-id", Value: inp.ProjectID},
- telemetry.AttributeKV{Key: "name", Value: inp.Name},
- telemetry.AttributeKV{Key: "cluster-id", Value: inp.ClusterID},
- telemetry.AttributeKV{Key: "type", Value: inp.Type},
- telemetry.AttributeKV{Key: "engine", Value: inp.Engine},
- )
- var datastore *models.Datastore
- if inp.ProjectID == 0 {
- return datastore, telemetry.Error(ctx, span, nil, "project id is 0")
- }
- if inp.Name == "" {
- return datastore, telemetry.Error(ctx, span, nil, "name is empty")
- }
- datastore, err := inp.DatastoreRepository.GetByProjectIDAndName(ctx, inp.ProjectID, inp.Name)
- if err != nil {
- return datastore, telemetry.Error(ctx, span, err, "error reading datastore by project id and name")
- }
- if datastore != nil && datastore.ID != uuid.Nil {
- telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-datastore-id", Value: datastore.ID})
- return datastore, nil
- }
- if inp.ClusterID == 0 {
- return datastore, telemetry.Error(ctx, span, nil, "cluster id is 0")
- }
- cluster, err := inp.ClusterRepository.ReadCluster(inp.ProjectID, inp.ClusterID)
- if err != nil {
- return datastore, telemetry.Error(ctx, span, err, "error reading cluster")
- }
- datastoreToSave := &models.Datastore{
- ProjectID: inp.ProjectID,
- Name: inp.Name,
- Type: inp.Type,
- Engine: inp.Engine,
- CloudProvider: cluster.CloudProvider,
- CloudProviderCredentialIdentifier: cluster.CloudProviderCredentialIdentifier,
- }
- datastore, err = inp.DatastoreRepository.Insert(ctx, datastoreToSave)
- if err != nil {
- return datastore, telemetry.Error(ctx, span, err, "error inserting datastore")
- }
- return datastore, nil
- }
|