cluster.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(projectID, 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. projectID, 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. func (repo *ClusterRepository) ReadClusterByInfraID(
  102. projectID, infraID uint,
  103. ) (*models.Cluster, error) {
  104. panic("unimplemented")
  105. }
  106. // ListClustersByProjectID finds all service accounts
  107. // for a given project id
  108. func (repo *ClusterRepository) ListClustersByProjectID(
  109. projectID uint,
  110. ) ([]*models.Cluster, error) {
  111. if !repo.canQuery {
  112. return nil, errors.New("Cannot read from database")
  113. }
  114. res := make([]*models.Cluster, 0)
  115. for _, cluster := range repo.clusters {
  116. if cluster != nil && cluster.ProjectID == projectID {
  117. res = append(res, cluster)
  118. }
  119. }
  120. return res, nil
  121. }
  122. // UpdateCluster modifies an existing Cluster in the database
  123. func (repo *ClusterRepository) UpdateCluster(
  124. cluster *models.Cluster,
  125. ) (*models.Cluster, error) {
  126. if !repo.canQuery {
  127. return nil, errors.New("Cannot write database")
  128. }
  129. if int(cluster.ID-1) >= len(repo.clusters) || repo.clusters[cluster.ID-1] == nil {
  130. return nil, gorm.ErrRecordNotFound
  131. }
  132. index := int(cluster.ID - 1)
  133. repo.clusters[index] = cluster
  134. return cluster, nil
  135. }
  136. // UpdateClusterTokenCache updates the token cache for a cluster
  137. func (repo *ClusterRepository) UpdateClusterTokenCache(
  138. tokenCache *ints.ClusterTokenCache,
  139. ) (*models.Cluster, error) {
  140. if !repo.canQuery {
  141. return nil, errors.New("Cannot write database")
  142. }
  143. index := int(tokenCache.ClusterID - 1)
  144. repo.clusters[index].TokenCache.Token = tokenCache.Token
  145. repo.clusters[index].TokenCache.Expiry = tokenCache.Expiry
  146. return repo.clusters[index], nil
  147. }
  148. // DeleteCluster removes a cluster from the array by setting it to nil
  149. func (repo *ClusterRepository) DeleteCluster(
  150. cluster *models.Cluster,
  151. ) error {
  152. if !repo.canQuery {
  153. return errors.New("Cannot write database")
  154. }
  155. if int(cluster.ID-1) >= len(repo.clusters) || repo.clusters[cluster.ID-1] == nil {
  156. return gorm.ErrRecordNotFound
  157. }
  158. index := int(cluster.ID - 1)
  159. repo.clusters[index] = nil
  160. return nil
  161. }