-
Notifications
You must be signed in to change notification settings - Fork 100
/
resource_artifactory_pull_replication.go
132 lines (109 loc) · 4.35 KB
/
resource_artifactory_pull_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
package artifactory
import (
"context"
"encoding/json"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceArtifactoryPullReplication() *schema.Resource {
return &schema.Resource{
CreateContext: resourcePullReplicationCreate,
ReadContext: resourcePullReplicationRead,
UpdateContext: resourcePullReplicationUpdate,
DeleteContext: resourceReplicationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: mergeSchema(replicationSchemaCommon, replicationSchema),
Description: "Used for configuring pull replication on remote repos.",
}
}
func unpackPullReplication(s *schema.ResourceData) *ReplicationBody {
d := &ResourceData{s}
replicationConfig := new(ReplicationBody)
replicationConfig.RepoKey = d.getString("repo_key", false)
replicationConfig.CronExp = d.getString("cron_exp", false)
replicationConfig.EnableEventReplication = d.getBool("enable_event_replication", false)
replicationConfig.URL = d.getString("url", false)
replicationConfig.Username = d.getString("username", false)
replicationConfig.Enabled = d.getBool("enabled", false)
replicationConfig.SyncDeletes = d.getBool("sync_deletes", false)
replicationConfig.SyncProperties = d.getBool("sync_properties", false)
replicationConfig.SyncStatistics = d.getBool("sync_statistics", false)
replicationConfig.PathPrefix = d.getString("path_prefix", false)
return replicationConfig
}
func packPullReplicationBody(config PullReplication, d *schema.ResourceData) diag.Diagnostics {
setValue := mkLens(d)
setValue("repo_key", config.RepoKey)
setValue("cron_exp", config.CronExp)
setValue("enable_event_replication", config.EnableEventReplication)
setValue("enabled", config.Enabled)
setValue("sync_deletes", config.SyncDeletes)
setValue("sync_properties", config.SyncProperties)
errors := setValue("path_prefix", config.PathPrefix)
if errors != nil && len(errors) > 0 {
return diag.Errorf("failed to pack replication config %q", errors)
}
return nil
}
func resourcePullReplicationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
replicationConfig := unpackPullReplication(d)
// The password is sent clear
_, err := m.(*resty.Client).R().SetBody(replicationConfig).Put(replicationEndpoint + replicationConfig.RepoKey)
if err != nil {
return diag.FromErr(err)
}
d.SetId(replicationConfig.RepoKey)
return resourcePullReplicationRead(ctx, d, m)
}
// PullReplication this is the structure for a PULL replication on a remote repo
type PullReplication struct {
Enabled bool `json:"enabled"`
CronExp string `json:"cronExp"`
SyncDeletes bool `json:"syncDeletes"`
SyncProperties bool `json:"syncProperties"`
PathPrefix string `json:"pathPrefix"`
RepoKey string `json:"repoKey"`
ReplicationKey string `json:"replicationKey"`
EnableEventReplication bool `json:"enableEventReplication"`
Username string `json:"username"`
URL string `json:"url"`
}
func resourcePullReplicationRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var result interface{}
resp, err := m.(*resty.Client).R().SetResult(&result).Get(replicationEndpoint + d.Id())
// password comes back scrambled
if err != nil {
return diag.FromErr(err)
}
switch result.(type) {
case []interface{}:
if len(result.([]interface{})) > 1 {
return diag.Errorf("received more than one replication payload. expect only one in array")
}
var final []PullReplication
err = json.Unmarshal(resp.Body(), &final)
if err != nil {
return diag.FromErr(err)
}
return packPullReplicationBody(final[0], d)
default:
final := PullReplication{}
err = json.Unmarshal(resp.Body(), &final)
if err != nil {
return diag.FromErr(err)
}
return packPullReplicationBody(final, d)
}
}
func resourcePullReplicationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
replicationConfig := unpackPullReplication(d)
_, err := m.(*resty.Client).R().SetBody(replicationConfig).Post(replicationEndpoint + replicationConfig.RepoKey)
if err != nil {
return diag.FromErr(err)
}
d.SetId(replicationConfig.RepoKey)
return resourcePullReplicationRead(ctx, d, m)
}