| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package test
- import (
- "errors"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository"
- "gorm.io/gorm"
- )
- // ServiceAccountRepository implements repository.ServiceAccountRepository
- type ServiceAccountRepository struct {
- canQuery bool
- serviceAccountCandidates []*models.ServiceAccountCandidate
- serviceAccounts []*models.ServiceAccount
- clusters []*models.Cluster
- }
- // NewServiceAccountRepository will return errors if canQuery is false
- func NewServiceAccountRepository(canQuery bool) repository.ServiceAccountRepository {
- return &ServiceAccountRepository{
- canQuery,
- []*models.ServiceAccountCandidate{},
- []*models.ServiceAccount{},
- []*models.Cluster{},
- }
- }
- // CreateServiceAccountCandidate creates a new service account candidate
- func (repo *ServiceAccountRepository) CreateServiceAccountCandidate(
- saCandidate *models.ServiceAccountCandidate,
- ) (*models.ServiceAccountCandidate, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot write database")
- }
- repo.serviceAccountCandidates = append(repo.serviceAccountCandidates, saCandidate)
- saCandidate.ID = uint(len(repo.serviceAccountCandidates))
- return saCandidate, nil
- }
- // ReadServiceAccountCandidate finds a service account candidate by id
- func (repo *ServiceAccountRepository) ReadServiceAccountCandidate(
- id uint,
- ) (*models.ServiceAccountCandidate, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot read from database")
- }
- if int(id-1) >= len(repo.serviceAccountCandidates) || repo.serviceAccountCandidates[id-1] == nil {
- return nil, gorm.ErrRecordNotFound
- }
- index := int(id - 1)
- return repo.serviceAccountCandidates[index], nil
- }
- // ListServiceAccountCandidatesByProjectID finds all service account candidates
- // for a given project id
- func (repo *ServiceAccountRepository) ListServiceAccountCandidatesByProjectID(
- projectID uint,
- ) ([]*models.ServiceAccountCandidate, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot read from database")
- }
- res := make([]*models.ServiceAccountCandidate, 0)
- for _, saCandidate := range repo.serviceAccountCandidates {
- if saCandidate.ProjectID == projectID {
- res = append(res, saCandidate)
- }
- }
- return res, nil
- }
- // CreateServiceAccount creates a new servicea account
- func (repo *ServiceAccountRepository) CreateServiceAccount(
- sa *models.ServiceAccount,
- ) (*models.ServiceAccount, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot write database")
- }
- if sa == nil {
- return nil, nil
- }
- repo.serviceAccounts = append(repo.serviceAccounts, sa)
- sa.ID = uint(len(repo.serviceAccounts))
- for i, cluster := range sa.Clusters {
- (&cluster).ServiceAccountID = sa.ID
- clusterP, _ := repo.createCluster(&cluster)
- sa.Clusters[i] = *clusterP
- }
- return sa, nil
- }
- // ReadServiceAccount finds a service account by id
- func (repo *ServiceAccountRepository) ReadServiceAccount(
- id uint,
- ) (*models.ServiceAccount, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot read from database")
- }
- if int(id-1) >= len(repo.serviceAccounts) || repo.serviceAccounts[id-1] == nil {
- return nil, gorm.ErrRecordNotFound
- }
- index := int(id - 1)
- return repo.serviceAccounts[index], nil
- }
- // ListServiceAccountsByProjectID finds all service accounts
- // for a given project id
- func (repo *ServiceAccountRepository) ListServiceAccountsByProjectID(
- projectID uint,
- ) ([]*models.ServiceAccount, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot read from database")
- }
- res := make([]*models.ServiceAccount, 0)
- for _, sa := range repo.serviceAccounts {
- if sa.ProjectID == projectID {
- res = append(res, sa)
- }
- }
- return res, nil
- }
- func (repo *ServiceAccountRepository) createCluster(
- cluster *models.Cluster,
- ) (*models.Cluster, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot write database")
- }
- if cluster == nil {
- return nil, nil
- }
- repo.clusters = append(repo.clusters, cluster)
- cluster.ID = uint(len(repo.clusters))
- return cluster, nil
- }
- // UpdateServiceAccountTokenCache updates the token cache for a service account
- func (repo *ServiceAccountRepository) UpdateServiceAccountTokenCache(
- tokenCache *models.TokenCache,
- ) (*models.ServiceAccount, error) {
- if !repo.canQuery {
- return nil, errors.New("Cannot write database")
- }
- index := int(tokenCache.ServiceAccountID - 1)
- repo.serviceAccounts[index].TokenCache.Token = tokenCache.Token
- repo.serviceAccounts[index].TokenCache.Expiry = tokenCache.Expiry
- return repo.serviceAccounts[index], nil
- }
|