This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_cloudflare_logpush_job.go
149 lines (116 loc) · 3.71 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
package cloudflare
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCloudflareLogpushJob() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareLogpushJobCreate,
Read: resourceCloudflareLogpushJobRead,
Update: resourceCloudflareLogpushJobUpdate,
Delete: resourceCloudflareLogpushJobDelete,
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"logpull_options": {
Type: schema.TypeString,
Optional: true,
},
"destination_conf": {
Type: schema.TypeString,
Required: true,
},
"ownership_challenge": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func getJobFromResource(d *schema.ResourceData) cloudflare.LogpushJob {
id, err := strconv.Atoi(d.Id())
if err != nil {
fmt.Errorf("Could not extract Logpush job from resource - invalid identifier: %+v", id)
}
job := cloudflare.LogpushJob{
ID: id,
Enabled: d.Get("enabled").(bool),
Name: d.Get("name").(string),
LogpullOptions: d.Get("logpull_options").(string),
DestinationConf: d.Get("destination_conf").(string),
OwnershipChallenge: d.Get("ownership_challenge").(string),
}
return job
}
func resourceCloudflareLogpushJobRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
jobId, err := strconv.Atoi(d.Id())
job, err := client.LogpushJob(d.Get("zone_id").(string), jobId)
if err != nil {
if strings.Contains(err.Error(), "404") {
log.Printf("[INFO] Could not find LogpushJob with id: %q", jobId)
return nil
}
return fmt.Errorf("error finding logpush job %q: %s", jobId, err)
}
if job.ID == 0 {
d.SetId("")
return nil
}
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"))
return nil
}
func resourceCloudflareLogpushJobCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
job := getJobFromResource(d)
log.Printf("[DEBUG] Creating Cloudflare Logpush Job from struct: %+v", job)
j, err := client.CreateLogpushJob(d.Get("zone_id").(string), job)
if err != nil {
return fmt.Errorf("error creating logpush job")
}
if j.ID == 0 {
return fmt.Errorf("failed to find ID in Create response; resource was empty")
}
d.SetId(strconv.Itoa(j.ID))
log.Printf("[INFO] Created Cloudflare Logpush Job ID: %s", d.Id())
return resourceCloudflareLogpushJobRead(d, meta)
}
func resourceCloudflareLogpushJobUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
job := getJobFromResource(d)
log.Printf("[INFO] Updating Cloudflare Logpush Job from struct: %+v", job)
updateErr := client.UpdateLogpushJob(d.Get("zone_id").(string), job.ID, job)
if updateErr != nil {
return fmt.Errorf("error updating logpush job: %+v", job.ID)
}
return resourceCloudflareLogpushJobRead(d, meta)
}
func resourceCloudflareLogpushJobDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
job := getJobFromResource(d)
log.Printf("[DEBUG] Deleting Cloudflare Logpush job from zone :%+v with id: %+v", d.Get("zone_id"), job.ID)
deleteErr := client.DeleteLogpushJob(d.Get("zone_id").(string), job.ID)
if deleteErr != nil {
return fmt.Errorf("error deleting logpush job: %+v", job.ID)
}
return resourceCloudflareLogpushJobRead(d, meta)
}