serviceaccount.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // ServiceAccountRepository implements repository.ServiceAccountRepository
  9. type ServiceAccountRepository struct {
  10. canQuery bool
  11. serviceAccountCandidates []*models.ServiceAccountCandidate
  12. serviceAccounts []*models.ServiceAccount
  13. clusters []*models.Cluster
  14. }
  15. // NewServiceAccountRepository will return errors if canQuery is false
  16. func NewServiceAccountRepository(canQuery bool) repository.ServiceAccountRepository {
  17. return &ServiceAccountRepository{
  18. canQuery,
  19. []*models.ServiceAccountCandidate{},
  20. []*models.ServiceAccount{},
  21. []*models.Cluster{},
  22. }
  23. }
  24. // CreateServiceAccountCandidate creates a new service account candidate
  25. func (repo *ServiceAccountRepository) CreateServiceAccountCandidate(
  26. saCandidate *models.ServiceAccountCandidate,
  27. ) (*models.ServiceAccountCandidate, error) {
  28. if !repo.canQuery {
  29. return nil, errors.New("Cannot write database")
  30. }
  31. repo.serviceAccountCandidates = append(repo.serviceAccountCandidates, saCandidate)
  32. saCandidate.ID = uint(len(repo.serviceAccountCandidates))
  33. return saCandidate, nil
  34. }
  35. // ReadServiceAccountCandidate finds a service account candidate by id
  36. func (repo *ServiceAccountRepository) ReadServiceAccountCandidate(
  37. id uint,
  38. ) (*models.ServiceAccountCandidate, error) {
  39. if !repo.canQuery {
  40. return nil, errors.New("Cannot read from database")
  41. }
  42. if int(id-1) >= len(repo.serviceAccountCandidates) || repo.serviceAccountCandidates[id-1] == nil {
  43. return nil, gorm.ErrRecordNotFound
  44. }
  45. index := int(id - 1)
  46. return repo.serviceAccountCandidates[index], nil
  47. }
  48. // ListServiceAccountCandidatesByProjectID finds all service account candidates
  49. // for a given project id
  50. func (repo *ServiceAccountRepository) ListServiceAccountCandidatesByProjectID(
  51. projectID uint,
  52. ) ([]*models.ServiceAccountCandidate, error) {
  53. if !repo.canQuery {
  54. return nil, errors.New("Cannot read from database")
  55. }
  56. res := make([]*models.ServiceAccountCandidate, 0)
  57. for _, saCandidate := range repo.serviceAccountCandidates {
  58. if saCandidate.ProjectID == projectID {
  59. res = append(res, saCandidate)
  60. }
  61. }
  62. return res, nil
  63. }
  64. // CreateServiceAccount creates a new servicea account
  65. func (repo *ServiceAccountRepository) CreateServiceAccount(
  66. sa *models.ServiceAccount,
  67. ) (*models.ServiceAccount, error) {
  68. if !repo.canQuery {
  69. return nil, errors.New("Cannot write database")
  70. }
  71. if sa == nil {
  72. return nil, nil
  73. }
  74. repo.serviceAccounts = append(repo.serviceAccounts, sa)
  75. sa.ID = uint(len(repo.serviceAccounts))
  76. for i, cluster := range sa.Clusters {
  77. (&cluster).ServiceAccountID = sa.ID
  78. clusterP, _ := repo.createCluster(&cluster)
  79. sa.Clusters[i] = *clusterP
  80. }
  81. return sa, nil
  82. }
  83. // ReadServiceAccount finds a service account by id
  84. func (repo *ServiceAccountRepository) ReadServiceAccount(
  85. id uint,
  86. ) (*models.ServiceAccount, error) {
  87. if !repo.canQuery {
  88. return nil, errors.New("Cannot read from database")
  89. }
  90. if int(id-1) >= len(repo.serviceAccounts) || repo.serviceAccounts[id-1] == nil {
  91. return nil, gorm.ErrRecordNotFound
  92. }
  93. index := int(id - 1)
  94. return repo.serviceAccounts[index], nil
  95. }
  96. // ListServiceAccountsByProjectID finds all service accounts
  97. // for a given project id
  98. func (repo *ServiceAccountRepository) ListServiceAccountsByProjectID(
  99. projectID uint,
  100. ) ([]*models.ServiceAccount, error) {
  101. if !repo.canQuery {
  102. return nil, errors.New("Cannot read from database")
  103. }
  104. res := make([]*models.ServiceAccount, 0)
  105. for _, sa := range repo.serviceAccounts {
  106. if sa.ProjectID == projectID {
  107. res = append(res, sa)
  108. }
  109. }
  110. return res, nil
  111. }
  112. func (repo *ServiceAccountRepository) createCluster(
  113. cluster *models.Cluster,
  114. ) (*models.Cluster, error) {
  115. if !repo.canQuery {
  116. return nil, errors.New("Cannot write database")
  117. }
  118. if cluster == nil {
  119. return nil, nil
  120. }
  121. repo.clusters = append(repo.clusters, cluster)
  122. cluster.ID = uint(len(repo.clusters))
  123. return cluster, nil
  124. }
  125. // UpdateServiceAccountTokenCache updates the token cache for a service account
  126. func (repo *ServiceAccountRepository) UpdateServiceAccountTokenCache(
  127. tokenCache *models.TokenCache,
  128. ) (*models.ServiceAccount, error) {
  129. if !repo.canQuery {
  130. return nil, errors.New("Cannot write database")
  131. }
  132. index := int(tokenCache.ServiceAccountID - 1)
  133. repo.serviceAccounts[index].TokenCache.Token = tokenCache.Token
  134. repo.serviceAccounts[index].TokenCache.Expiry = tokenCache.Expiry
  135. return repo.serviceAccounts[index], nil
  136. }