-
Notifications
You must be signed in to change notification settings - Fork 101
/
resource_artifactory_push_replication.go
311 lines (258 loc) · 8.57 KB
/
resource_artifactory_push_replication.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
package artifactory
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
type ReplicationBody struct {
Username string `json:"username"`
Password string `json:"password"`
URL string `json:"url"`
CronExp string `json:"cronExp"`
RepoKey string `json:"repoKey"`
EnableEventReplication bool `json:"enableEventReplication"`
SocketTimeoutMillis int `json:"socketTimeoutMillis"`
Enabled bool `json:"enabled"`
SyncDeletes bool `json:"syncDeletes"`
SyncProperties bool `json:"syncProperties"`
SyncStatistics bool `json:"syncStatistics"`
PathPrefix string `json:"pathPrefix"`
}
type getReplicationBody struct {
ReplicationBody
ProxyRef string `json:"proxyRef"`
}
type updateReplicationBody struct {
ReplicationBody
Proxy string `json:"proxy"`
}
type GetPushReplication struct {
RepoKey string `json:"-"`
CronExp string `json:"cronExp,omitempty"`
EnableEventReplication bool `json:"enableEventReplication,omitempty"`
Replications []getReplicationBody `json:"replications,omitempty"`
}
type UpdatePushReplication struct {
RepoKey string `json:"-"`
CronExp string `json:"cronExp,omitempty"`
EnableEventReplication bool `json:"enableEventReplication,omitempty"`
Replications []updateReplicationBody `json:"replications,omitempty"`
}
var pushReplicationSchemaCommon = map[string]*schema.Schema{
"repo_key": {
Type: schema.TypeString,
Required: true,
},
"cron_exp": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateCron,
},
"enable_event_replication": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
}
var pushRepMultipleSchema = map[string]*schema.Schema{
"replications": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: pushReplicationSchema,
},
},
}
var pushReplicationSchema = map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},
"socket_timeout_millis": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(0),
},
"username": {
Type: schema.TypeString,
Optional: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "If a password is used to create the resource, it will be returned as encrypted and this will become the new state." +
"Practically speaking, what this means is that, the password can only be set, not gotten. ",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"sync_deletes": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"sync_properties": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"sync_statistics": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"path_prefix": {
Type: schema.TypeString,
Optional: true,
},
"proxy": {
Type: schema.TypeString,
Optional: true,
Description: "Proxy key from Artifactory Proxies setting",
},
}
func resourceArtifactoryPushReplication() *schema.Resource {
return &schema.Resource{
CreateContext: resourcePushReplicationCreate,
ReadContext: resourcePushReplicationRead,
UpdateContext: resourcePushReplicationUpdate,
DeleteContext: resourceReplicationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: mergeSchema(pushReplicationSchemaCommon, pushRepMultipleSchema),
}
}
func unpackPushReplication(s *schema.ResourceData) UpdatePushReplication {
d := &ResourceData{s}
pushReplication := new(UpdatePushReplication)
repo := d.getString("repo_key", false)
if v, ok := d.GetOk("replications"); ok {
arr := v.([]interface{})
tmp := make([]updateReplicationBody, 0, len(arr))
pushReplication.Replications = tmp
for i, o := range arr {
if i == 0 {
pushReplication.RepoKey = repo
pushReplication.CronExp = d.getString("cron_exp", false)
pushReplication.EnableEventReplication = d.getBool("enable_event_replication", false)
}
m := o.(map[string]interface{})
var replication updateReplicationBody
replication.RepoKey = repo
if v, ok = m["url"]; ok {
replication.URL = v.(string)
}
if v, ok = m["socket_timeout_millis"]; ok {
replication.SocketTimeoutMillis = v.(int)
}
if v, ok = m["username"]; ok {
replication.Username = v.(string)
}
if v, ok = m["enabled"]; ok {
replication.Enabled = v.(bool)
}
if v, ok = m["sync_deletes"]; ok {
replication.SyncDeletes = v.(bool)
}
if v, ok = m["sync_properties"]; ok {
replication.SyncProperties = v.(bool)
}
if v, ok = m["sync_statistics"]; ok {
replication.SyncStatistics = v.(bool)
}
if prefix, ok := m["path_prefix"]; ok {
replication.PathPrefix = prefix.(string)
}
if _, ok := m["proxy"]; ok {
replication.Proxy = handleResetWithNonExistantValue(d, fmt.Sprintf("replications.%d.proxy", i))
}
if pass, ok := m["password"]; ok {
replication.Password = pass.(string)
}
pushReplication.Replications = append(pushReplication.Replications, replication)
}
}
return *pushReplication
}
func packPushReplication(pushReplication *GetPushReplication, d *schema.ResourceData) diag.Diagnostics {
var errors []error
setValue := mkLens(d)
setValue("repo_key", pushReplication.RepoKey)
setValue("cron_exp", pushReplication.CronExp)
errors = setValue("enable_event_replication", pushReplication.EnableEventReplication)
if pushReplication.Replications != nil {
var replications []map[string]interface{}
for _, repo := range pushReplication.Replications {
replication := make(map[string]interface{})
replication["url"] = repo.URL
replication["socket_timeout_millis"] = repo.SocketTimeoutMillis
replication["username"] = repo.Username
replication["password"] = repo.Password
replication["enabled"] = repo.Enabled
replication["sync_deletes"] = repo.SyncDeletes
replication["sync_properties"] = repo.SyncProperties
replication["sync_statistics"] = repo.SyncStatistics
replication["path_prefix"] = repo.PathPrefix
replication["proxy"] = repo.ProxyRef
replications = append(replications, replication)
}
errors = setValue("replications", replications)
}
if errors != nil && len(errors) > 0 {
return diag.Errorf("failed to pack replication config %q", errors)
}
return nil
}
func resourcePushReplicationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pushReplication := unpackPushReplication(d)
_, err := m.(*resty.Client).R().SetBody(pushReplication).Put("artifactory/api/replications/multiple/" + pushReplication.RepoKey)
if err != nil {
return diag.FromErr(err)
}
d.SetId(pushReplication.RepoKey)
return resourcePushReplicationRead(ctx, d, m)
}
func resourcePushReplicationRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*resty.Client)
var replications []getReplicationBody
_, err := c.R().SetResult(&replications).Get("artifactory/api/replications/" + d.Id())
if err != nil {
return diag.FromErr(err)
}
repConfig := GetPushReplication{
RepoKey: d.Id(),
Replications: replications,
}
if len(replications) > 0 {
repConfig.EnableEventReplication = replications[0].EnableEventReplication
repConfig.CronExp = replications[0].CronExp
}
return packPushReplication(&repConfig, d)
}
func resourcePushReplicationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pushReplication := unpackPushReplication(d)
_, err := m.(*resty.Client).R().SetBody(pushReplication).Post("/api/replications/" + d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId(pushReplication.RepoKey)
return resourcePushReplicationRead(ctx, d, m)
}
func resourceReplicationDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
_, err := m.(*resty.Client).R().Delete("artifactory/api/replications/" + d.Id())
return diag.FromErr(err)
}
func repConfigExists(id string, m interface{}) (bool, error) {
_, err := m.(*resty.Client).R().Head("artifactory/api/replications/" + id)
return err == nil, err
}