app_instance.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package gorm
  2. import (
  3. "context"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "github.com/porter-dev/porter/internal/telemetry"
  7. "gorm.io/gorm"
  8. )
  9. // AppInstanceRepository uses gorm.DB for querying the database
  10. type AppInstanceRepository struct {
  11. db *gorm.DB
  12. }
  13. // NewAppInstanceRepository returns a AppInstanceRepository which uses
  14. // gorm.DB for querying the database
  15. func NewAppInstanceRepository(db *gorm.DB) repository.AppInstanceRepository {
  16. return &AppInstanceRepository{db}
  17. }
  18. // Get returns an app instance by its id
  19. func (repo *AppInstanceRepository) Get(ctx context.Context, id string) (*models.AppInstance, error) {
  20. ctx, span := telemetry.NewSpan(ctx, "gorm-get-app-instance")
  21. defer span.End()
  22. appInstance := &models.AppInstance{}
  23. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-instance-id", Value: id})
  24. if id == "" {
  25. return nil, telemetry.Error(ctx, span, nil, "id is empty")
  26. }
  27. if err := repo.db.Where("id = ?", id).First(&appInstance).Error; err != nil {
  28. return nil, telemetry.Error(ctx, span, err, "error getting app instance")
  29. }
  30. return appInstance, nil
  31. }