-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_fastly_service_dynamic_snippet_content_v1.go
144 lines (113 loc) · 4.01 KB
/
resource_fastly_service_dynamic_snippet_content_v1.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
package fastly
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strings"
gofastly "github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceServiceDynamicSnippetContentV1() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServiceDynamicSnippetV1Create,
ReadContext: resourceServiceDynamicSnippetV1Read,
UpdateContext: resourceServiceDynamicSnippetV1Update,
DeleteContext: resourceServiceDynamicSnippetV1Delete,
Importer: &schema.ResourceImporter{
StateContext: resourceServiceDynamicSnippetContentV1Import,
},
Schema: map[string]*schema.Schema{
"service_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the service that the dynamic snippet belongs to",
},
"snippet_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the dynamic snippet that the content belong to",
},
"content": {
Type: schema.TypeString,
Required: true,
Description: "The VCL code that specifies exactly what the snippet does",
},
},
}
}
func resourceServiceDynamicSnippetV1Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
snippetID := d.Get("snippet_id").(string)
content := d.Get("content").(string)
_, err := conn.UpdateDynamicSnippet(&gofastly.UpdateDynamicSnippetInput{
ServiceID: serviceID,
ID: snippetID,
Content: content,
})
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 409 {
return diag.FromErr(err)
}
} else if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%s/%s", serviceID, snippetID))
return resourceServiceDynamicSnippetV1Read(ctx, d, meta)
}
func resourceServiceDynamicSnippetV1Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
snippetID := d.Get("snippet_id").(string)
if d.HasChange("content") {
content := d.Get("content").(string)
_, err := conn.UpdateDynamicSnippet(&gofastly.UpdateDynamicSnippetInput{
ServiceID: serviceID,
ID: snippetID,
Content: content,
})
if err != nil {
return diag.Errorf("Error updating dynamic snippet: service %s, snippet %s, %#v", serviceID, snippetID, err)
}
}
return resourceServiceDynamicSnippetV1Read(ctx, d, meta)
}
func resourceServiceDynamicSnippetV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
serviceID := d.Get("service_id").(string)
snippetID := d.Get("snippet_id").(string)
dynamicSnippet, err := conn.GetDynamicSnippet(&gofastly.GetDynamicSnippetInput{
ServiceID: serviceID,
ID: snippetID,
})
if err != nil {
return diag.FromErr(err)
}
err = d.Set("content", dynamicSnippet.Content)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceServiceDynamicSnippetV1Delete(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
// Dynamic snippet content cannot be deleted. Removing from state only
d.SetId("")
return nil
}
func resourceServiceDynamicSnippetContentV1Import(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
split := strings.Split(d.Id(), "/")
if len(split) != 2 {
return nil, fmt.Errorf("Invalid id: %s. The ID should be in the format [service_id]/[snippet_id]", d.Id())
}
serviceID := split[0]
snippetID := split[1]
err := d.Set("service_id", serviceID)
if err != nil {
return nil, fmt.Errorf("Error importing dynamic snippet content: service %s, dynamic snippet %s, %s", serviceID, snippetID, err)
}
err = d.Set("snippet_id", snippetID)
if err != nil {
return nil, fmt.Errorf("Error importing dynamic snippet content: service %s, dynamic snippet %s, %s", serviceID, snippetID, err)
}
return []*schema.ResourceData{d}, nil
}