infra.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "gorm.io/gorm"
  7. )
  8. // AWSInfraRepository implements repository.AWSInfraRepository
  9. type AWSInfraRepository struct {
  10. canQuery bool
  11. awsInfras []*models.AWSInfra
  12. }
  13. // NewAWSInfraRepository will return errors if canQuery is false
  14. func NewAWSInfraRepository(canQuery bool) repository.AWSInfraRepository {
  15. return &AWSInfraRepository{
  16. canQuery,
  17. []*models.AWSInfra{},
  18. }
  19. }
  20. // CreateAWSInfra creates a new aws infra
  21. func (repo *AWSInfraRepository) CreateAWSInfra(
  22. infra *models.AWSInfra,
  23. ) (*models.AWSInfra, error) {
  24. if !repo.canQuery {
  25. return nil, errors.New("Cannot write database")
  26. }
  27. repo.awsInfras = append(repo.awsInfras, infra)
  28. infra.ID = uint(len(repo.awsInfras))
  29. return infra, nil
  30. }
  31. // ReadAWSInfra finds a aws infra by id
  32. func (repo *AWSInfraRepository) ReadAWSInfra(
  33. id uint,
  34. ) (*models.AWSInfra, error) {
  35. if !repo.canQuery {
  36. return nil, errors.New("Cannot read from database")
  37. }
  38. if int(id-1) >= len(repo.awsInfras) || repo.awsInfras[id-1] == nil {
  39. return nil, gorm.ErrRecordNotFound
  40. }
  41. index := int(id - 1)
  42. return repo.awsInfras[index], nil
  43. }
  44. // ListAWSInfrasByProjectID finds all aws infras
  45. // for a given project id
  46. func (repo *AWSInfraRepository) ListAWSInfrasByProjectID(
  47. projectID uint,
  48. ) ([]*models.AWSInfra, error) {
  49. if !repo.canQuery {
  50. return nil, errors.New("Cannot read from database")
  51. }
  52. res := make([]*models.AWSInfra, 0)
  53. for _, infra := range repo.awsInfras {
  54. if infra != nil && infra.ProjectID == projectID {
  55. res = append(res, infra)
  56. }
  57. }
  58. return res, nil
  59. }
  60. // UpdateAWSInfra modifies an existing AWSInfra in the database
  61. func (repo *AWSInfraRepository) UpdateAWSInfra(
  62. ai *models.AWSInfra,
  63. ) (*models.AWSInfra, error) {
  64. if !repo.canQuery {
  65. return nil, errors.New("Cannot write database")
  66. }
  67. if int(ai.ID-1) >= len(repo.awsInfras) || repo.awsInfras[ai.ID-1] == nil {
  68. return nil, gorm.ErrRecordNotFound
  69. }
  70. index := int(ai.ID - 1)
  71. repo.awsInfras[index] = ai
  72. return ai, nil
  73. }