2
0

cluster.go 5.2 KB

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