-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrepository_configuration.go
256 lines (226 loc) · 7.66 KB
/
repository_configuration.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package models
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/utils"
"github.com/labstack/gommon/random"
"github.com/lib/pq"
"gorm.io/gorm"
)
type RepositoryConfiguration struct {
Base
Name string `json:"name" gorm:"default:null"`
Versions pq.StringArray `json:"version" gorm:"type:text[],default:null"`
Arch string `json:"arch" gorm:"default:''"`
GpgKey string `json:"gpg_key" gorm:"default:''"`
Label string `json:"label" gorm:"default:''"`
MetadataVerification bool `json:"metadata_verification" gorm:"default:false"`
ModuleHotfixes bool `json:"module_hotfixes" gorm:"default:false"`
AccountID string `json:"account_id" gorm:"default:null"`
OrgID string `json:"org_id" gorm:"default:null"`
RepositoryUUID string `json:"repository_uuid" gorm:"not null"`
Repository Repository `json:"repository,omitempty"`
Snapshot bool `json:"snapshot"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
LastSnapshotUUID string `json:"last_snapshot_uuid" gorm:"default:null"`
LastSnapshot *Snapshot `json:"last_snapshot,omitempty" gorm:"foreignKey:last_snapshot_uuid"`
LastSnapshotTaskUUID string `json:"last_snapshot_task_uuid" gorm:"default:null"`
LastSnapshotTask *TaskInfo `json:"last_snapshot_task" gorm:"foreignKey:last_snapshot_task_uuid"`
FeatureName string `json:"feature_name" gorm:"default:null"`
}
// When updating a model with gorm, we want to explicitly update any field that is set to
// empty string. We always fetch the object and then update it before saving
// so every update is the full model of user changeable fields.
// So OrgId and account Id are excluded
func (rc *RepositoryConfiguration) MapForUpdate() map[string]interface{} {
forUpdate := make(map[string]interface{})
forUpdate["Name"] = rc.Name
forUpdate["Arch"] = rc.Arch
forUpdate["Versions"] = rc.Versions
forUpdate["GpgKey"] = rc.GpgKey
forUpdate["MetadataVerification"] = rc.MetadataVerification
forUpdate["AccountID"] = rc.AccountID
forUpdate["OrgID"] = rc.OrgID
forUpdate["RepositoryUUID"] = rc.RepositoryUUID
forUpdate["snapshot"] = rc.Snapshot
forUpdate["module_hotfixes"] = rc.ModuleHotfixes
return forUpdate
}
// BeforeCreate perform validations and sets UUID of Repository Configurations
func (rc *RepositoryConfiguration) BeforeCreate(tx *gorm.DB) error {
if err := rc.Base.BeforeCreate(tx); err != nil {
return err
}
if err := rc.SetLabel(tx); err != nil {
return err
}
if err := rc.DedupeVersions(tx); err != nil {
return err
}
if err := rc.ReplaceEmptyValues(tx); err != nil {
return err
}
if err := rc.validate(); err != nil {
return err
}
return nil
}
// BeforeUpdate perform validations of Repository Configurations
func (rc *RepositoryConfiguration) BeforeUpdate(tx *gorm.DB) error {
if err := rc.DedupeVersions(tx); err != nil {
return err
}
if err := rc.ReplaceEmptyValues(tx); err != nil {
return err
}
if err := rc.validate(); err != nil {
return err
}
return nil
}
func (rc *RepositoryConfiguration) AfterFind(tx *gorm.DB) error {
if err := rc.Base.AfterFind(tx); err != nil {
return err
}
rc.DeletedAt = gorm.DeletedAt{
Time: rc.DeletedAt.Time.UTC(),
Valid: rc.DeletedAt.Valid,
}
return nil
}
func (rc *RepositoryConfiguration) AfterSave(tx *gorm.DB) error {
return rc.AfterFind(tx)
}
func (rc *RepositoryConfiguration) DedupeVersions(tx *gorm.DB) error {
var versionMap = make(map[string]bool)
var unique = make(pq.StringArray, 0)
for i := 0; i < len(rc.Versions); i++ {
if _, found := versionMap[rc.Versions[i]]; !found {
versionMap[rc.Versions[i]] = true
unique = append(unique, rc.Versions[i])
}
}
sort.Strings(unique)
tx.Statement.SetColumn("Versions", unique)
return nil
}
func (rc *RepositoryConfiguration) ReplaceEmptyValues(tx *gorm.DB) error {
if rc.Versions != nil && len(rc.Versions) == 0 {
tx.Statement.SetColumn("Versions", fmt.Sprintf("{%s}", config.ANY_VERSION))
}
if rc.Arch == "" {
tx.Statement.SetColumn("Arch", config.ANY_ARCH)
}
return nil
}
func (rc *RepositoryConfiguration) IsRedHat() bool {
return rc.OrgID == config.RedHatOrg
}
func (rc *RepositoryConfiguration) validate() error {
var err error
if rc.Name == "" {
err = Error{Message: "Name cannot be blank.", Validation: true}
return err
}
if rc.OrgID == "" {
err = Error{Message: "Org ID cannot be blank.", Validation: true}
return err
}
if rc.RepositoryUUID == "" {
err = Error{Message: "Repository UUID foreign key cannot be blank.", Validation: true}
return err
}
if rc.Arch != "" && !config.ValidArchLabel(rc.Arch) {
return Error{Message: fmt.Sprintf("Specified distribution architecture %s is invalid.", rc.Arch),
Validation: true}
}
valid, invalidVer := config.ValidDistributionVersionLabels(rc.Versions)
if len(rc.Versions) > 0 && !valid {
return Error{Message: fmt.Sprintf("Specified distribution version %s is invalid.", invalidVer),
Validation: true}
}
if versionContainsAnyAndOthers(rc.Versions) {
AnyOrErrMsg := fmt.Sprintf("Specified a distribution version of '%s' along with other version types, this is invalid.", config.ANY_VERSION)
return Error{Message: AnyOrErrMsg, Validation: true}
}
if rc.Repository.Origin == config.OriginUpload && !rc.Snapshot {
return Error{Message: "Snapshot must be true for upload repositories", Validation: true}
}
return nil
}
func versionContainsAnyAndOthers(arr []string) bool {
if len(arr) <= 1 {
return false
}
for _, a := range arr {
if a == config.ANY_VERSION {
return true
}
}
return false
}
func (rc *RepositoryConfiguration) DeepCopyInto(out *RepositoryConfiguration) {
if rc == nil || out == nil || rc == out {
return
}
rc.Base.DeepCopyInto(&out.Base)
out.Name = rc.Name
out.Versions = rc.Versions
out.Arch = rc.Arch
out.GpgKey = rc.GpgKey
out.MetadataVerification = rc.MetadataVerification
out.AccountID = rc.AccountID
out.OrgID = rc.OrgID
out.RepositoryUUID = rc.RepositoryUUID
}
func (rc *RepositoryConfiguration) DeepCopy() *RepositoryConfiguration {
var out = &RepositoryConfiguration{}
rc.DeepCopyInto(out)
return out
}
func (rc *RepositoryConfiguration) SetLabel(tx *gorm.DB) error {
var label string
var err error
if rc.IsRedHat() {
return nil
}
// Replace any nonalphanumeric characters with an underscore
// e.g: "!!my repo?test15()" => "__my_repo_test15__"
re, err := regexp.Compile(`[^a-zA-Z0-9:space]`)
if err != nil {
return err
}
label = re.ReplaceAllString(rc.Name, "_")
var found int64
res := tx.Model(&RepositoryConfiguration{}).Where("org_id = ? and label = ?", rc.OrgID, label).Count(&found)
if res.Error != nil {
return err
}
if found > 0 {
label = label + "_" + random.String(10, random.Alphabetic)
}
rc.Label = label
return nil
}
func CandlepinContentGpgKeyUrl(orgID string, repoConfigUUID string) *string {
if orgID == config.RedHatOrg {
return nil // We should never be setting it for red hat content
} else {
// Add trailing slash if not present
host := config.Get().Options.ExternalURL
host = strings.TrimSuffix(host, "/")
url := fmt.Sprintf("%v%v/repository_gpg_key/%v", host, api.FullRootPath(), repoConfigUUID)
return utils.Ptr(url)
}
}
func RepoConfigGpgKeyURL(orgID string, repoConfigUUID string) *string {
if orgID == config.RedHatOrg {
return utils.Ptr(config.RedHatGpgKeyPath)
} else {
return CandlepinContentGpgKeyUrl(orgID, repoConfigUUID)
}
}