helm_repo.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. ints "github.com/porter-dev/porter/internal/models/integrations"
  6. "github.com/porter-dev/porter/internal/repository"
  7. "gorm.io/gorm"
  8. )
  9. // HelmRepoRepository implements repository.HelmRepoRepository
  10. type HelmRepoRepository struct {
  11. canQuery bool
  12. helmRepos []*models.HelmRepo
  13. }
  14. // NewHelmRepoRepository will return errors if canQuery is false
  15. func NewHelmRepoRepository(canQuery bool) repository.HelmRepoRepository {
  16. return &HelmRepoRepository{
  17. canQuery,
  18. []*models.HelmRepo{},
  19. }
  20. }
  21. // CreateHelmRepo creates a new repoistry
  22. func (repo *HelmRepoRepository) CreateHelmRepo(
  23. hr *models.HelmRepo,
  24. ) (*models.HelmRepo, error) {
  25. if !repo.canQuery {
  26. return nil, errors.New("Cannot write database")
  27. }
  28. repo.helmRepos = append(repo.helmRepos, hr)
  29. hr.ID = uint(len(repo.helmRepos))
  30. return hr, nil
  31. }
  32. // ReadHelmRepo finds a repoistry by id
  33. func (repo *HelmRepoRepository) ReadHelmRepo(
  34. projectID uint,
  35. id uint,
  36. ) (*models.HelmRepo, error) {
  37. if !repo.canQuery {
  38. return nil, errors.New("Cannot read from database")
  39. }
  40. if int(id-1) >= len(repo.helmRepos) || repo.helmRepos[id-1] == nil {
  41. return nil, gorm.ErrRecordNotFound
  42. }
  43. index := int(id - 1)
  44. return repo.helmRepos[index], nil
  45. }
  46. // ListHelmReposByProjectID finds all repoistries
  47. // for a given project id
  48. func (repo *HelmRepoRepository) ListHelmReposByProjectID(
  49. projectID uint,
  50. ) ([]*models.HelmRepo, error) {
  51. if !repo.canQuery {
  52. return nil, errors.New("Cannot read from database")
  53. }
  54. res := make([]*models.HelmRepo, 0)
  55. for _, hr := range repo.helmRepos {
  56. if hr != nil && hr.ProjectID == projectID {
  57. res = append(res, hr)
  58. }
  59. }
  60. return res, nil
  61. }
  62. // UpdateHelmRepo modifies an existing HelmRepo in the database
  63. func (repo *HelmRepoRepository) UpdateHelmRepo(
  64. hr *models.HelmRepo,
  65. ) (*models.HelmRepo, error) {
  66. if !repo.canQuery {
  67. return nil, errors.New("Cannot write database")
  68. }
  69. if int(hr.ID-1) >= len(repo.helmRepos) || repo.helmRepos[hr.ID-1] == nil {
  70. return nil, gorm.ErrRecordNotFound
  71. }
  72. index := int(hr.ID - 1)
  73. repo.helmRepos[index] = hr
  74. return hr, nil
  75. }
  76. // UpdateHelmRepoTokenCache updates the token cache for a repoistry
  77. func (repo *HelmRepoRepository) UpdateHelmRepoTokenCache(
  78. tokenCache *ints.HelmRepoTokenCache,
  79. ) (*models.HelmRepo, error) {
  80. if !repo.canQuery {
  81. return nil, errors.New("Cannot write database")
  82. }
  83. index := int(tokenCache.HelmRepoID - 1)
  84. repo.helmRepos[index].TokenCache.Token = tokenCache.Token
  85. repo.helmRepos[index].TokenCache.Expiry = tokenCache.Expiry
  86. return repo.helmRepos[index], nil
  87. }
  88. // DeleteHelmRepo removes a repoistry from the array by setting it to nil
  89. func (repo *HelmRepoRepository) DeleteHelmRepo(
  90. hr *models.HelmRepo,
  91. ) error {
  92. if !repo.canQuery {
  93. return errors.New("Cannot write database")
  94. }
  95. if int(hr.ID-1) >= len(repo.helmRepos) || repo.helmRepos[hr.ID-1] == nil {
  96. return gorm.ErrRecordNotFound
  97. }
  98. index := int(hr.ID - 1)
  99. repo.helmRepos[index] = nil
  100. return nil
  101. }