/
DockerRegistryConfig.go
229 lines (211 loc) · 8.47 KB
/
DockerRegistryConfig.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* 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 pipeline
import (
"fmt"
"github.com/devtron-labs/devtron/internal/constants"
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
"go.uber.org/zap"
"time"
)
type DockerRegistryConfig interface {
Create(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error)
ListAllActive() ([]DockerArtifactStoreBean, error)
FetchAllDockerAccounts() ([]DockerArtifactStoreBean, error)
FetchOneDockerAccount(storeId string) (*DockerArtifactStoreBean, error)
Update(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error)
Delete(storeId string) (string, error)
}
type DockerArtifactStoreBean struct {
Id string `json:"id,omitempty" validate:"required"`
PluginId string `json:"pluginId,omitempty" validate:"required"`
RegistryURL string `json:"registryUrl,omitempty"`
RegistryType repository.RegistryType `json:"registryType,omitempty" validate:"required"`
AWSAccessKeyId string `json:"awsAccessKeyId,omitempty"`
AWSSecretAccessKey string `json:"awsSecretAccessKey,omitempty"`
AWSRegion string `json:"awsRegion,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
IsDefault bool `json:"isDefault"`
Active bool `json:"active"`
User int32 `json:"-"`
}
type DockerRegistryConfigImpl struct {
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository
logger *zap.SugaredLogger
}
func NewDockerRegistryConfigImpl(dockerArtifactStoreRepository repository.DockerArtifactStoreRepository, logger *zap.SugaredLogger) *DockerRegistryConfigImpl {
return &DockerRegistryConfigImpl{
dockerArtifactStoreRepository: dockerArtifactStoreRepository,
logger: logger,
}
}
func (impl DockerRegistryConfigImpl) Create(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry create request", "request", bean)
store := &repository.DockerArtifactStore{
Id: bean.Id,
PluginId: bean.PluginId,
RegistryURL: bean.RegistryURL,
RegistryType: bean.RegistryType,
AWSAccessKeyId: bean.AWSAccessKeyId,
AWSSecretAccessKey: bean.AWSSecretAccessKey,
AWSRegion: bean.AWSRegion,
Username: bean.Username,
Password: bean.Password,
IsDefault: bean.IsDefault,
Active: true,
AuditLog: models.AuditLog{CreatedBy: bean.User, CreatedOn: time.Now(), UpdatedOn: time.Now(), UpdatedBy: bean.User},
}
err := impl.dockerArtifactStoreRepository.Save(store)
if err != nil {
impl.logger.Errorw("error in saving registry config", "config", store, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: "docker registry failed to create in db",
UserMessage: fmt.Sprintf("requested by %d", bean.User),
}
return nil, err
}
impl.logger.Infow("created repository ", "repository", store)
bean.Id = store.Id
return bean, nil
}
//list all active artifact store
func (impl DockerRegistryConfigImpl) ListAllActive() ([]DockerArtifactStoreBean, error) {
impl.logger.Debug("list docker repo request")
stores, err := impl.dockerArtifactStoreRepository.FindAllActiveForAutocomplete()
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
var storeBeans []DockerArtifactStoreBean
for _, store := range stores {
storeBean := DockerArtifactStoreBean{
Id: store.Id,
RegistryURL: store.RegistryURL,
IsDefault: store.IsDefault,
}
storeBeans = append(storeBeans, storeBean)
}
return storeBeans, err
}
/**
this method used for getting all the docker account details
*/
func (impl DockerRegistryConfigImpl) FetchAllDockerAccounts() ([]DockerArtifactStoreBean, error) {
impl.logger.Debug("list docker repo request")
stores, err := impl.dockerArtifactStoreRepository.FindAll()
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
var storeBeans []DockerArtifactStoreBean
for _, store := range stores {
storeBean := DockerArtifactStoreBean{
Id: store.Id,
PluginId: store.PluginId,
RegistryURL: store.RegistryURL,
RegistryType: store.RegistryType,
AWSAccessKeyId: store.AWSAccessKeyId,
AWSSecretAccessKey: store.AWSSecretAccessKey,
AWSRegion: store.AWSRegion,
Username: store.Username,
Password: store.Password,
IsDefault: store.IsDefault,
Active: store.Active,
}
storeBeans = append(storeBeans, storeBean)
}
return storeBeans, err
}
/**
this method used for getting all the docker account details
*/
func (impl DockerRegistryConfigImpl) FetchOneDockerAccount(storeId string) (*DockerArtifactStoreBean, error) {
impl.logger.Debug("fetch docker account by id from db")
store, err := impl.dockerArtifactStoreRepository.FindOne(storeId)
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
storeBean := &DockerArtifactStoreBean{
Id: store.Id,
PluginId: store.PluginId,
RegistryURL: store.RegistryURL,
RegistryType: store.RegistryType,
AWSAccessKeyId: store.AWSAccessKeyId,
AWSSecretAccessKey: store.AWSSecretAccessKey,
AWSRegion: store.AWSRegion,
Username: store.Username,
Password: store.Password,
IsDefault: store.IsDefault,
Active: store.Active,
}
return storeBean, err
}
func (impl DockerRegistryConfigImpl) Update(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry update request", "request", bean)
existingStore, err0 := impl.dockerArtifactStoreRepository.FindOne(bean.Id)
if err0 != nil {
impl.logger.Errorw("no matching entry found of update ..", "err", err0)
return nil, err0
}
store := &repository.DockerArtifactStore{
Id: bean.Id,
PluginId: existingStore.PluginId,
RegistryURL: bean.RegistryURL,
RegistryType: bean.RegistryType,
AWSAccessKeyId: bean.AWSAccessKeyId,
AWSSecretAccessKey: bean.AWSSecretAccessKey,
AWSRegion: bean.AWSRegion,
Username: bean.Username,
Password: bean.Password,
IsDefault: bean.IsDefault,
Active: true, // later it will change
AuditLog: models.AuditLog{CreatedBy: existingStore.CreatedBy, CreatedOn: existingStore.CreatedOn, UpdatedOn: time.Now(), UpdatedBy: bean.User},
}
err := impl.dockerArtifactStoreRepository.Update(store)
if err != nil {
impl.logger.Errorw("error in updating registry config in db", "config", store, "err", err)
err = &util.ApiError{
Code: constants.DockerRegUpdateFailedInDb,
InternalMessage: "docker registry failed to update in db",
UserMessage: "docker registry failed to update in db",
}
return nil, err
}
impl.logger.Infow("updated repository ", "repository", store)
bean.Id = store.Id
return bean, nil
}
func (impl DockerRegistryConfigImpl) Delete(storeId string) (string, error) {
impl.logger.Debugw("docker registry update request", "request", storeId)
err := impl.dockerArtifactStoreRepository.Delete(storeId)
if err != nil {
impl.logger.Errorw("error in delete registry in db", "storeId", storeId, "err", err)
err = &util.ApiError{
Code: constants.DockerRegDeleteFailedInDb,
InternalMessage: "docker registry failed to delete in db",
UserMessage: err.Error(),
}
return "", err
}
impl.logger.Infow("delete docker registry ", "storeId", storeId)
return storeId, nil
}