infra.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // InfraRepository implements repository.InfraRepository
  9. type InfraRepository struct {
  10. canQuery bool
  11. infras []*models.Infra
  12. }
  13. // NewInfraRepository will return errors if canQuery is false
  14. func NewInfraRepository(canQuery bool) repository.InfraRepository {
  15. return &InfraRepository{
  16. canQuery,
  17. []*models.Infra{},
  18. }
  19. }
  20. // CreateInfra creates a new aws infra
  21. func (repo *InfraRepository) CreateInfra(
  22. infra *models.Infra,
  23. ) (*models.Infra, error) {
  24. if !repo.canQuery {
  25. return nil, errors.New("Cannot write database")
  26. }
  27. repo.infras = append(repo.infras, infra)
  28. infra.ID = uint(len(repo.infras))
  29. return infra, nil
  30. }
  31. // ReadInfra finds a aws infra by id
  32. func (repo *InfraRepository) ReadInfra(
  33. projectID,
  34. id uint,
  35. ) (*models.Infra, error) {
  36. if !repo.canQuery {
  37. return nil, errors.New("Cannot read from database")
  38. }
  39. if int(id-1) >= len(repo.infras) || repo.infras[id-1] == nil {
  40. return nil, gorm.ErrRecordNotFound
  41. }
  42. index := int(id - 1)
  43. return repo.infras[index], nil
  44. }
  45. // ListInfrasByProjectID finds all aws infras
  46. // for a given project id
  47. func (repo *InfraRepository) ListInfrasByProjectID(
  48. projectID uint,
  49. apiVersion string,
  50. ) ([]*models.Infra, error) {
  51. if !repo.canQuery {
  52. return nil, errors.New("Cannot read from database")
  53. }
  54. res := make([]*models.Infra, 0)
  55. for _, infra := range repo.infras {
  56. if infra != nil && infra.ProjectID == projectID {
  57. res = append(res, infra)
  58. }
  59. }
  60. return res, nil
  61. }
  62. // UpdateInfra modifies an existing Infra in the database
  63. func (repo *InfraRepository) UpdateInfra(
  64. ai *models.Infra,
  65. ) (*models.Infra, error) {
  66. if !repo.canQuery {
  67. return nil, errors.New("Cannot write database")
  68. }
  69. if int(ai.ID-1) >= len(repo.infras) || repo.infras[ai.ID-1] == nil {
  70. return nil, gorm.ErrRecordNotFound
  71. }
  72. index := int(ai.ID - 1)
  73. repo.infras[index] = ai
  74. return ai, nil
  75. }
  76. func (repo *InfraRepository) AddOperation(infra *models.Infra, operation *models.Operation) (*models.Operation, error) {
  77. panic("unimplemented")
  78. }
  79. func (repo *InfraRepository) GetLatestOperation(infra *models.Infra) (*models.Operation, error) {
  80. panic("unimplemented")
  81. }
  82. func (repo *InfraRepository) ListOperations(infraID uint) ([]*models.Operation, error) {
  83. panic("unimplemented")
  84. }
  85. func (repo *InfraRepository) ReadOperation(infraID uint, operationUID string) (*models.Operation, error) {
  86. panic("unimplemented")
  87. }
  88. func (repo *InfraRepository) UpdateOperation(
  89. operation *models.Operation,
  90. ) (*models.Operation, error) {
  91. panic("unimplemented")
  92. }