-
Notifications
You must be signed in to change notification settings - Fork 102
/
repository.go
393 lines (341 loc) · 13.3 KB
/
repository.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package repository
import (
"context"
"fmt"
"net/http"
"regexp"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
utilsdk "github.com/jfrog/terraform-provider-shared/util/sdk"
"golang.org/x/exp/slices"
"github.com/jfrog/terraform-provider-shared/client"
"github.com/jfrog/terraform-provider-shared/packer"
"github.com/jfrog/terraform-provider-shared/testutil"
"github.com/jfrog/terraform-provider-shared/unpacker"
"github.com/jfrog/terraform-provider-shared/validator"
)
var BaseRepoSchema = map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: RepoKeyValidator,
Description: "A mandatory identifier for the repository that must be unique. Must be 3 - 10 lowercase alphanumeric and hyphen characters. It cannot begin with a number or contain spaces or special characters.",
},
"project_key": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validator.ProjectKey,
Description: "Project key for assigning this repository to. Must be 2 - 20 lowercase alphanumeric and hyphen characters. When assigning repository to a project, repository key must be prefixed with project key, separated by a dash.",
},
"project_environments": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 0,
MaxItems: 2,
Set: schema.HashString,
Optional: true,
Computed: true,
Description: "Project environment for assigning this repository to. Allow values: \"DEV\", \"PROD\", or one of custom environment. " +
"Before Artifactory 7.53.1, up to 2 values (\"DEV\" and \"PROD\") are allowed. From 7.53.1 onward, only one value is allowed. " +
"The attribute should only be used if the repository is already assigned to the existing project. If not, " +
"the attribute will be ignored by Artifactory, but will remain in the Terraform state, which will create " +
"state drift during the update.",
},
"package_type": {
Type: schema.TypeString,
Required: false,
Computed: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Public description.",
},
"notes": {
Type: schema.TypeString,
Optional: true,
Description: "Internal description.",
},
"includes_pattern": {
Type: schema.TypeString,
Optional: true,
Default: "**/*",
Description: "List of comma-separated artifact patterns to include when evaluating artifact requests in the form of x/y/**/z/*. " +
"When used, only artifacts matching one of the include patterns are served. By default, all artifacts are included (**/*).",
},
"excludes_pattern": {
Type: schema.TypeString,
Optional: true,
Description: "List of artifact patterns to exclude when evaluating artifact requests, in the form of x/y/**/z/*." +
"By default no artifacts are excluded.",
},
"repo_layout_ref": {
Type: schema.TypeString,
Optional: true,
// The default value in the UI is simple-default, in API maven-2-default. Provider will always override it ro math the UI.
ValidateDiagFunc: ValidateRepoLayoutRefSchemaOverride,
Description: "Sets the layout that the repository should use for storing and identifying modules. A recommended layout that corresponds to the package type defined is suggested, and index packages uploaded and calculate metadata accordingly.",
},
}
var CompressionFormats = map[string]*schema.Schema{
"index_compression_formats": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
Optional: true,
},
}
type ContentSynchronisation struct {
Enabled bool `json:"enabled"`
Statistics ContentSynchronisationStatistics `json:"statistics"`
Properties ContentSynchronisationProperties `json:"properties"`
Source ContentSynchronisationSource `json:"source"`
}
type ContentSynchronisationStatistics struct {
Enabled bool `hcl:"statistics_enabled" json:"enabled"`
}
type ContentSynchronisationProperties struct {
Enabled bool `hcl:"properties_enabled" json:"enabled"`
}
type ContentSynchronisationSource struct {
OriginAbsenceDetection bool `hcl:"source_origin_absence_detection" json:"originAbsenceDetection"`
}
type ReadFunc func(d *schema.ResourceData, m interface{}) error
// Constructor Must return a pointer to a struct. When just returning a struct, resty gets confused and thinks it's a map
type Constructor func() (interface{}, error)
func MkRepoCreate(unpack unpacker.UnpackFunc, read schema.ReadContextFunc) schema.CreateContextFunc {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
repo, key, err := unpack(d)
if err != nil {
return diag.FromErr(err)
}
// repo must be a pointer
_, err = m.(utilsdk.ProvderMetadata).Client.R().
AddRetryCondition(client.RetryOnMergeError).
SetBody(repo).
SetPathParam("key", key).
Put(RepositoriesEndpoint)
if err != nil {
return diag.FromErr(err)
}
d.SetId(key)
return read(ctx, d, m)
}
}
func MkRepoRead(pack packer.PackFunc, construct Constructor) schema.ReadContextFunc {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
repo, err := construct()
if err != nil {
return diag.FromErr(err)
}
// repo must be a pointer
resp, err := m.(utilsdk.ProvderMetadata).Client.R().
SetResult(repo).
SetPathParam("key", d.Id()).
Get(RepositoriesEndpoint)
if err != nil {
if resp != nil && (resp.StatusCode() == http.StatusBadRequest || resp.StatusCode() == http.StatusNotFound) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
return diag.FromErr(pack(repo, d))
}
}
func MkRepoUpdate(unpack unpacker.UnpackFunc, read schema.ReadContextFunc) schema.UpdateContextFunc {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
repo, key, err := unpack(d)
if err != nil {
return diag.FromErr(err)
}
_, err = m.(utilsdk.ProvderMetadata).Client.R().
AddRetryCondition(client.RetryOnMergeError).
SetBody(repo).
SetPathParam("key", d.Id()).
Post(RepositoriesEndpoint)
if err != nil {
return diag.FromErr(err)
}
d.SetId(key)
projectKeyChanged := d.HasChange("project_key")
tflog.Debug(ctx, fmt.Sprintf("projectKeyChanged: %v", projectKeyChanged))
if projectKeyChanged {
old, newProject := d.GetChange("project_key")
oldProjectKey := old.(string)
newProjectKey := newProject.(string)
tflog.Debug(ctx, fmt.Sprintf("oldProjectKey: %v, newProjectKey: %v", oldProjectKey, newProjectKey))
assignToProject := oldProjectKey == "" && len(newProjectKey) > 0
unassignFromProject := len(oldProjectKey) > 0 && newProjectKey == ""
tflog.Debug(ctx, fmt.Sprintf("assignToProject: %v, unassignFromProject: %v", assignToProject, unassignFromProject))
var err error
if assignToProject {
err = assignRepoToProject(key, newProjectKey, m.(utilsdk.ProvderMetadata).Client)
} else if unassignFromProject {
err = unassignRepoFromProject(key, m.(utilsdk.ProvderMetadata).Client)
}
if err != nil {
return diag.FromErr(err)
}
}
return read(ctx, d, m)
}
}
func assignRepoToProject(repoKey string, projectKey string, client *resty.Client) error {
_, err := client.R().
SetPathParams(map[string]string{
"repoKey": repoKey,
"projectKey": projectKey,
}).
Put("access/api/v1/projects/_/attach/repositories/{repoKey}/{projectKey}")
return err
}
func unassignRepoFromProject(repoKey string, client *resty.Client) error {
_, err := client.R().
SetPathParam("repoKey", repoKey).
Delete("access/api/v1/projects/_/attach/repositories/{repoKey}")
return err
}
func DeleteRepo(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
resp, err := m.(utilsdk.ProvderMetadata).Client.R().
AddRetryCondition(client.RetryOnMergeError).
SetPathParam("key", d.Id()).
Delete(RepositoriesEndpoint)
if err != nil && (resp != nil && (resp.StatusCode() == http.StatusBadRequest || resp.StatusCode() == http.StatusNotFound)) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
func Retry400(response *resty.Response, _ error) bool {
return response.StatusCode() == http.StatusBadRequest
}
var RepoKeyValidator = validation.All(
validation.StringDoesNotMatch(regexp.MustCompile("^[0-9].*"), "repo key cannot start with a number"),
validation.StringDoesNotContainAny(" !@#$%^&*()+={}[]:;<>,/?~`|\\"),
)
var GradleLikePackageTypes = []string{
"gradle",
"sbt",
"ivy",
}
var ProjectEnvironmentsSupported = []string{"DEV", "PROD"}
func RepoLayoutRefSchema(repositoryType string, packageType string) map[string]*schema.Schema {
return map[string]*schema.Schema{
"repo_layout_ref": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: GetDefaultRepoLayoutRef(repositoryType, packageType),
Description: fmt.Sprintf("Repository layout key for the %s repository", repositoryType),
},
}
}
// HandleResetWithNonExistentValue Special handling for field that requires non-existant value for RT
//
// Artifactory REST API will not accept empty string or null to reset value to not set
// Instead, using a non-existant value works as a workaround
// To ensure we don't accidentally set the value to a valid value, we use a UUID v4 string
func HandleResetWithNonExistentValue(d *utilsdk.ResourceData, key string) string {
value := d.GetString(key, false)
// When value has changed and is empty string, then it has been removed from
// the Terraform configuration.
if value == "" && d.HasChange(key) {
return fmt.Sprintf("non-existant-value-%d", testutil.RandomInt())
}
return value
}
const CustomProjectEnvironmentSupportedVersion = "7.53.1"
func ProjectEnvironmentsDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if data, ok := diff.GetOk("project_environments"); ok {
projectEnvironments := data.(*schema.Set).List()
providerMetadata := meta.(utilsdk.ProvderMetadata)
isSupported, err := utilsdk.CheckVersion(providerMetadata.ArtifactoryVersion, CustomProjectEnvironmentSupportedVersion)
if err != nil {
return fmt.Errorf("failed to check version %s", err)
}
if isSupported {
if len(projectEnvironments) == 2 {
return fmt.Errorf("for Artifactory %s or later, only one environment can be assigned to a repository", CustomProjectEnvironmentSupportedVersion)
}
} else { // Before 7.53.1
projectEnvironments := data.(*schema.Set).List()
for _, projectEnvironment := range projectEnvironments {
if !slices.Contains(ProjectEnvironmentsSupported, projectEnvironment.(string)) {
return fmt.Errorf("project_environment %s not allowed", projectEnvironment)
}
}
}
}
return nil
}
func MkResourceSchema(skeema map[string]*schema.Schema, packer packer.PackFunc, unpack unpacker.UnpackFunc, constructor Constructor) *schema.Resource {
var reader = MkRepoRead(packer, constructor)
return &schema.Resource{
CreateContext: MkRepoCreate(unpack, reader),
ReadContext: reader,
UpdateContext: MkRepoUpdate(unpack, reader),
DeleteContext: DeleteRepo,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: skeema,
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
// this only works because the schema hasn't changed, except the removal of default value
// from `project_key` attribute. Future common schema changes that involve attributes should
// figure out a way to create a previous and new version.
Type: resourceV0(skeema).CoreConfigSchema().ImpliedType(),
Upgrade: ResourceUpgradeProjectKey,
Version: 0,
},
},
CustomizeDiff: ProjectEnvironmentsDiff,
}
}
func resourceV0(skeema map[string]*schema.Schema) *schema.Resource {
return &schema.Resource{
Schema: skeema,
}
}
func ResourceUpgradeProjectKey(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
if rawState["project_key"] == "default" {
rawState["project_key"] = ""
}
return rawState, nil
}
const RepositoriesEndpoint = "artifactory/api/repositories/{key}"
func CheckRepo(id string, request *resty.Request) (*resty.Response, error) {
// artifactory returns 400 instead of 404. but regardless, it's an error
return request.SetPathParam("key", id).Head(RepositoriesEndpoint)
}
func ValidateRepoLayoutRefSchemaOverride(_ interface{}, _ cty.Path) diag.Diagnostics {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Always override repo_layout_ref attribute in the schema",
Detail: "Always override repo_layout_ref attribute in the schema on top of base schema",
},
}
}
type SupportedRepoClasses struct {
RepoLayoutRef string
SupportedRepoTypes map[string]bool
}
// GetDefaultRepoLayoutRef return the default repo layout by Repository Type & Package Type
func GetDefaultRepoLayoutRef(repositoryType, packageType string) func() (interface{}, error) {
return func() (interface{}, error) {
if v, ok := defaultRepoLayoutMap[packageType].SupportedRepoTypes[repositoryType]; ok && v {
return defaultRepoLayoutMap[packageType].RepoLayoutRef, nil
}
return nil, fmt.Errorf("default repo layout not found for repository type %s & package type %s", repositoryType, packageType)
}
}