This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_cloudflare_logpush_job.go
255 lines (209 loc) · 8.24 KB
/
resource_cloudflare_logpush_job.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
package provider
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudflareLogpushJob() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflareLogpushJobSchema(),
CreateContext: resourceCloudflareLogpushJobCreate,
ReadContext: resourceCloudflareLogpushJobRead,
UpdateContext: resourceCloudflareLogpushJobUpdate,
DeleteContext: resourceCloudflareLogpushJobDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflareLogpushJobImport,
},
Description: `
Provides a resource which manages Cloudflare Logpush jobs. For Logpush jobs pushing to Amazon S3, Google Cloud Storage,
Microsoft Azure or Sumo Logic, this resource cannot be automatically created. In order to have this automated, you must
have:
- ` + "`cloudflare_logpush_ownership_challenge`" + `: Configured to generate the challenge
to confirm ownership of the destination.
- Either manual inspection or another Terraform Provider to get the contents of
the ` + "`ownership_challenge_filename`" + ` value from the` + "`cloudflare_logpush_ownership_challenge`" + ` resource.
- ` + "`cloudflare_logpush_job`" + `: Create and manage the Logpush Job itself.`,
}
}
func getJobFromResource(d *schema.ResourceData) (cloudflare.LogpushJob, *AccessIdentifier, error) {
id := 0
identifier, err := initIdentifier(d)
if err != nil {
return cloudflare.LogpushJob{}, identifier, err
}
if d.Id() != "" {
var err error
if id, err = strconv.Atoi(d.Id()); err != nil {
return cloudflare.LogpushJob{}, identifier, fmt.Errorf("could not extract Logpush job from resource - invalid identifier (%s): %w", d.Id(), err)
}
}
destConf := d.Get("destination_conf").(string)
ownershipChallenge := d.Get("ownership_challenge").(string)
var re = regexp.MustCompile(`^((datadog|splunk|https|r2)://|s3://.+endpoint=)`)
if ownershipChallenge == "" && !re.MatchString(destConf) {
return cloudflare.LogpushJob{}, identifier, fmt.Errorf("ownership_challenge must be set for the provided destination_conf")
}
job := cloudflare.LogpushJob{
ID: id,
Enabled: d.Get("enabled").(bool),
Name: d.Get("name").(string),
Dataset: d.Get("dataset").(string),
LogpullOptions: d.Get("logpull_options").(string),
DestinationConf: destConf,
OwnershipChallenge: ownershipChallenge,
Frequency: d.Get("frequency").(string),
}
filter := d.Get("filter")
if filter != nil {
var jobFilter cloudflare.LogpushJobFilters
if err := json.Unmarshal([]byte(filter.(string)), &jobFilter); err != nil {
return cloudflare.LogpushJob{}, identifier, err
}
err := jobFilter.Where.Validate()
if err != nil {
return job, identifier, err
}
job.Filter = &jobFilter
}
return job, identifier, nil
}
func resourceCloudflareLogpushJobRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
jobID, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("could not extract Logpush job from resource - invalid identifier (%s): %w", d.Id(), err))
}
var job cloudflare.LogpushJob
identifier, err := initIdentifier(d)
if err != nil {
return diag.FromErr(err)
}
if identifier.Type == AccountType {
job, err = client.GetAccountLogpushJob(ctx, identifier.Value, jobID)
} else {
job, err = client.GetZoneLogpushJob(ctx, identifier.Value, jobID)
}
if err != nil {
if strings.Contains(err.Error(), "404") {
tflog.Info(ctx, fmt.Sprintf("Could not find LogpushJob for %s with id: %q", identifier, jobID))
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("error reading logpush job %q for %s: %w", jobID, identifier, err))
}
if job.ID == 0 {
d.SetId("")
return nil
}
var filter string
if job.Filter != nil {
b, err := json.Marshal(job.Filter)
if err != nil {
return diag.FromErr(err)
}
filter = string(b)
}
d.Set("name", job.Name)
d.Set("enabled", job.Enabled)
d.Set("logpull_options", job.LogpullOptions)
d.Set("destination_conf", job.DestinationConf)
d.Set("ownership_challenge", d.Get("ownership_challenge"))
d.Set("frequency", job.Frequency)
d.Set("filter", filter)
return nil
}
func resourceCloudflareLogpushJobCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
job, identifier, err := getJobFromResource(d)
if err != nil {
return diag.FromErr(fmt.Errorf("error parsing logpush job from resource: %w", err))
}
tflog.Debug(ctx, fmt.Sprintf("Creating Cloudflare Logpush job for %s from struct: %+v", identifier, job))
var j *cloudflare.LogpushJob
if identifier.Type == AccountType {
j, err = client.CreateAccountLogpushJob(ctx, identifier.Value, job)
} else {
j, err = client.CreateZoneLogpushJob(ctx, identifier.Value, job)
}
if err != nil {
return diag.FromErr(fmt.Errorf("error creating logpush job for %s: %w", identifier, err))
}
if j.ID == 0 {
return diag.FromErr(fmt.Errorf("failed to find ID in Create response; resource was empty"))
}
d.SetId(strconv.Itoa(j.ID))
tflog.Info(ctx, fmt.Sprintf("Created Cloudflare Logpush Job for %s: %s", identifier, d.Id()))
return resourceCloudflareLogpushJobRead(ctx, d, meta)
}
func resourceCloudflareLogpushJobUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
job, identifier, err := getJobFromResource(d)
if err != nil {
return diag.FromErr(fmt.Errorf("error parsing logpush job from resource: %w", err))
}
tflog.Info(ctx, fmt.Sprintf("Updating Cloudflare Logpush job for %s from struct: %+v", identifier, job))
if identifier.Type == AccountType {
err = client.UpdateAccountLogpushJob(ctx, identifier.Value, job.ID, job)
} else {
err = client.UpdateZoneLogpushJob(ctx, identifier.Value, job.ID, job)
}
if err != nil {
return diag.FromErr(fmt.Errorf("error updating logpush job id %q for %s: %w", job.ID, identifier, err))
}
return resourceCloudflareLogpushJobRead(ctx, d, meta)
}
func resourceCloudflareLogpushJobDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
job, identifier, err := getJobFromResource(d)
if err != nil {
return diag.FromErr(fmt.Errorf("error parsing logpush job from resource: %w", err))
}
tflog.Debug(ctx, fmt.Sprintf("Deleting Cloudflare Logpush job for %s with id: %+v", identifier, job.ID))
if identifier.Type == AccountType {
err = client.DeleteAccountLogpushJob(ctx, identifier.Value, job.ID)
} else {
err = client.DeleteZoneLogpushJob(ctx, identifier.Value, job.ID)
}
if err != nil {
if strings.Contains(err.Error(), "job not found") {
tflog.Info(ctx, fmt.Sprintf("Could not find logpush job for %s with id: %q", identifier, job.ID))
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("error deleting logpush job id %v for %s: %w", job.ID, identifier, err))
}
d.SetId("")
return nil
}
func resourceCloudflareLogpushJobImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// split the id so we can lookup
idAttr := strings.Split(d.Id(), "/")
if len(idAttr) != 3 || (AccessIdentifierType(idAttr[0]) != AccountType && AccessIdentifierType(idAttr[0]) != ZoneType) || idAttr[1] == "" || idAttr[2] == "" {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"account/accountID/jobID\" or \"zone/zoneID/jobID\"", d.Id())
}
identifier := AccessIdentifier{
Type: AccessIdentifierType(idAttr[0]),
Value: idAttr[1],
}
logpushJobID := idAttr[2]
tflog.Debug(ctx, fmt.Sprintf("Importing Cloudflare Logpush Job for %s with id %s", identifier, logpushJobID))
if identifier.Type == AccountType {
if err := d.Set("account_id", identifier.Value); err != nil {
return nil, fmt.Errorf("failed to set account_id: %w", err)
}
} else {
if err := d.Set("zone_id", identifier.Value); err != nil {
return nil, fmt.Errorf("failed to set zone_id: %w", err)
}
}
d.SetId(logpushJobID)
resourceCloudflareLogpushJobRead(ctx, d, meta)
return []*schema.ResourceData{d}, nil
}