-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathClusterAccountsService.go
More file actions
160 lines (145 loc) · 5.14 KB
/
ClusterAccountsService.go
File metadata and controls
160 lines (145 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cluster
import (
"encoding/json"
"github.com/devtron-labs/devtron/internal/sql/repository/cluster"
"go.uber.org/zap"
"time"
)
type ClusterAccountsBean struct {
Id int `json:"id"`
Account string `json:"account"`
Config json.RawMessage `json:"config"`
//Environment string `json:"environment, omitempty"`
//ClusterName string `json:"cluster_name, omitempty"`
//Namespace string `json:"namespace, omitempty"`
Default bool `json:"default, omitempty"`
ClusterId int `json:"cluster_id"`
}
type ClusterAccountsService interface {
Save(account *ClusterAccountsBean, userId int32) error
FindOne(clusterName string) (*ClusterAccountsBean, error)
FindOneByEnvironment(environment string) (*ClusterAccountsBean, error)
Update(account *ClusterAccountsBean, userId int32) error
FindById(id int) (*ClusterAccountsBean, error)
FindAll() ([]ClusterAccountsBean, error)
}
type ClusterAccountsServiceImpl struct {
clusterAccountsRepository cluster.ClusterAccountsRepository
environmentClusterMappingsRepository cluster.EnvironmentRepository
clusterService ClusterService
logger *zap.SugaredLogger
}
func NewClusterAccountsServiceImpl(repository cluster.ClusterAccountsRepository,
environmentClusterMappingsRepository cluster.EnvironmentRepository,
clusterService ClusterService, logger *zap.SugaredLogger) *ClusterAccountsServiceImpl {
return &ClusterAccountsServiceImpl{
clusterAccountsRepository: repository,
logger: logger,
environmentClusterMappingsRepository: environmentClusterMappingsRepository,
clusterService: clusterService,
}
}
func (impl ClusterAccountsServiceImpl) Save(request *ClusterAccountsBean, userId int32) error {
cls, err := impl.clusterService.FindById(request.Id)
if err != nil {
impl.logger.Errorw("error in getting cluster by name", "err", err)
}
var model = cluster.ClusterAccounts{
Account: request.Account,
Config: string(request.Config),
ClusterId: cls.Id,
Active: true,
Default: request.Default,
}
model.CreatedBy = userId
model.UpdatedBy = userId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
err = impl.clusterAccountsRepository.Save(&model)
return err
}
func (impl ClusterAccountsServiceImpl) FindOne(clusterName string) (*ClusterAccountsBean, error) {
account, err := impl.clusterAccountsRepository.FindOne(clusterName)
if err != nil {
impl.logger.Errorw("error in getting cluster account", "err", err)
return nil, err
}
bean := &ClusterAccountsBean{
Account: account.Account,
Config: []byte(account.Config),
Default: account.Default,
}
return bean, nil
}
func (impl ClusterAccountsServiceImpl) FindOneByEnvironment(environment string) (*ClusterAccountsBean, error) {
account, err := impl.clusterAccountsRepository.FindOneByEnvironment(environment)
if err != nil {
impl.logger.Errorw("error in getting cluster account", "err", err)
return nil, err
}
bean := &ClusterAccountsBean{
Account: account.Account,
Config: []byte(account.Config),
Default: account.Default,
}
return bean, nil
}
func (impl ClusterAccountsServiceImpl) Update(request *ClusterAccountsBean, userId int32) error {
model, err := impl.clusterAccountsRepository.FindById(request.Id)
if err != nil {
impl.logger.Errorw("error in getting cluster by name", "err", err)
}
model.Account = request.Account
model.Config = string(request.Config)
model.Active = true
model.Default = request.Default
model.UpdatedBy = userId
model.UpdatedOn = time.Now()
err = impl.clusterAccountsRepository.Update(model)
return err
}
func (impl ClusterAccountsServiceImpl) FindById(id int) (*ClusterAccountsBean, error) {
account, err := impl.clusterAccountsRepository.FindById(id)
if err != nil {
impl.logger.Errorw("error in getting cluster account", "err", err)
return nil, err
}
bean := &ClusterAccountsBean{
Account: account.Account,
Config: []byte(account.Config),
Default: account.Default,
}
return bean, nil
}
func (impl ClusterAccountsServiceImpl) FindAll() ([]ClusterAccountsBean, error) {
accounts, err := impl.clusterAccountsRepository.FindAll()
if err != nil {
impl.logger.Errorw("error in getting cluster account", "err", err)
return nil, err
}
var clusterAccountBeans []ClusterAccountsBean
for _, account := range accounts {
clusterAccountBeans = append(clusterAccountBeans, ClusterAccountsBean{
Account: account.Account,
Config: []byte(account.Config),
Default: account.Default,
})
}
return clusterAccountBeans, nil
}