cluster.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. ints "github.com/porter-dev/porter/internal/models/integrations"
  8. )
  9. // ClusterRepository implements repository.ClusterRepository
  10. type ClusterRepository struct {
  11. canQuery bool
  12. clusterCandidates []*models.ClusterCandidate
  13. clusters []*models.Cluster
  14. }
  15. // NewClusterRepository will return errors if canQuery is false
  16. func NewClusterRepository(canQuery bool) repository.ClusterRepository {
  17. return &ClusterRepository{
  18. canQuery,
  19. []*models.ClusterCandidate{},
  20. []*models.Cluster{},
  21. }
  22. }
  23. // CreateClusterCandidate creates a new cluster candidate
  24. func (repo *ClusterRepository) CreateClusterCandidate(
  25. cc *models.ClusterCandidate,
  26. ) (*models.ClusterCandidate, error) {
  27. if !repo.canQuery {
  28. return nil, errors.New("Cannot write database")
  29. }
  30. repo.clusterCandidates = append(repo.clusterCandidates, cc)
  31. cc.ID = uint(len(repo.clusterCandidates))
  32. return cc, nil
  33. }
  34. // ReadClusterCandidate finds a service account candidate by id
  35. func (repo *ClusterRepository) ReadClusterCandidate(id uint) (*models.ClusterCandidate, error) {
  36. if !repo.canQuery {
  37. return nil, errors.New("Cannot read from database")
  38. }
  39. if int(id-1) >= len(repo.clusterCandidates) || repo.clusterCandidates[id-1] == nil {
  40. return nil, gorm.ErrRecordNotFound
  41. }
  42. index := int(id - 1)
  43. return repo.clusterCandidates[index], nil
  44. }
  45. // ListClusterCandidatesByProjectID finds all service account candidates
  46. // for a given project id
  47. func (repo *ClusterRepository) ListClusterCandidatesByProjectID(
  48. projectID uint,
  49. ) ([]*models.ClusterCandidate, error) {
  50. if !repo.canQuery {
  51. return nil, errors.New("Cannot read from database")
  52. }
  53. res := make([]*models.ClusterCandidate, 0)
  54. for _, saCandidate := range repo.clusterCandidates {
  55. if saCandidate.ProjectID == projectID {
  56. res = append(res, saCandidate)
  57. }
  58. }
  59. return res, nil
  60. }
  61. // UpdateClusterCandidateCreatedClusterID updates the CreatedClusterID for
  62. // a candidate, after the candidate has been resolved.
  63. func (repo *ClusterRepository) UpdateClusterCandidateCreatedClusterID(
  64. id uint,
  65. createdClusterID uint,
  66. ) (*models.ClusterCandidate, error) {
  67. if !repo.canQuery {
  68. return nil, errors.New("Cannot write database")
  69. }
  70. index := int(id - 1)
  71. repo.clusterCandidates[index].CreatedClusterID = createdClusterID
  72. return repo.clusterCandidates[index], nil
  73. }
  74. // CreateCluster creates a new servicea account
  75. func (repo *ClusterRepository) CreateCluster(
  76. cluster *models.Cluster,
  77. ) (*models.Cluster, error) {
  78. if !repo.canQuery {
  79. return nil, errors.New("Cannot write database")
  80. }
  81. if cluster == nil {
  82. return nil, nil
  83. }
  84. repo.clusters = append(repo.clusters, cluster)
  85. cluster.ID = uint(len(repo.clusters))
  86. return cluster, nil
  87. }
  88. // ReadCluster finds a service account by id
  89. func (repo *ClusterRepository) ReadCluster(
  90. id uint,
  91. ) (*models.Cluster, error) {
  92. if !repo.canQuery {
  93. return nil, errors.New("Cannot read from database")
  94. }
  95. if int(id-1) >= len(repo.clusters) || repo.clusters[id-1] == nil {
  96. return nil, gorm.ErrRecordNotFound
  97. }
  98. index := int(id - 1)
  99. return repo.clusters[index], nil
  100. }
  101. // ListClustersByProjectID finds all service accounts
  102. // for a given project id
  103. func (repo *ClusterRepository) ListClustersByProjectID(
  104. projectID uint,
  105. ) ([]*models.Cluster, error) {
  106. if !repo.canQuery {
  107. return nil, errors.New("Cannot read from database")
  108. }
  109. res := make([]*models.Cluster, 0)
  110. for _, cluster := range repo.clusters {
  111. if cluster != nil && cluster.ProjectID == projectID {
  112. res = append(res, cluster)
  113. }
  114. }
  115. return res, nil
  116. }
  117. // UpdateCluster modifies an existing Cluster in the database
  118. func (repo *ClusterRepository) UpdateCluster(
  119. cluster *models.Cluster,
  120. ) (*models.Cluster, error) {
  121. if !repo.canQuery {
  122. return nil, errors.New("Cannot write database")
  123. }
  124. if int(cluster.ID-1) >= len(repo.clusters) || repo.clusters[cluster.ID-1] == nil {
  125. return nil, gorm.ErrRecordNotFound
  126. }
  127. index := int(cluster.ID - 1)
  128. repo.clusters[index] = cluster
  129. return cluster, nil
  130. }
  131. // UpdateClusterTokenCache updates the token cache for a cluster
  132. func (repo *ClusterRepository) UpdateClusterTokenCache(
  133. tokenCache *ints.ClusterTokenCache,
  134. ) (*models.Cluster, error) {
  135. if !repo.canQuery {
  136. return nil, errors.New("Cannot write database")
  137. }
  138. index := int(tokenCache.ClusterID - 1)
  139. repo.clusters[index].TokenCache.Token = tokenCache.Token
  140. repo.clusters[index].TokenCache.Expiry = tokenCache.Expiry
  141. return repo.clusters[index], nil
  142. }
  143. // DeleteCluster removes a cluster from the array by setting it to nil
  144. func (repo *ClusterRepository) DeleteCluster(
  145. cluster *models.Cluster,
  146. ) error {
  147. if !repo.canQuery {
  148. return errors.New("Cannot write database")
  149. }
  150. if int(cluster.ID-1) >= len(repo.clusters) || repo.clusters[cluster.ID-1] == nil {
  151. return gorm.ErrRecordNotFound
  152. }
  153. index := int(cluster.ID - 1)
  154. repo.clusters[index] = nil
  155. return nil
  156. }