-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_fastly_service_dynamic_snippet_content.go
153 lines (122 loc) · 4.41 KB
/
resource_fastly_service_dynamic_snippet_content.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
package fastly
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
gofastly "github.com/fastly/go-fastly/v6/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceServiceDynamicSnippetContent() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServiceDynamicSnippetCreate,
ReadContext: resourceServiceDynamicSnippetRead,
UpdateContext: resourceServiceDynamicSnippetUpdate,
DeleteContext: resourceServiceDynamicSnippetDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceServiceDynamicSnippetContentImport,
},
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",
},
"manage_snippets": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: "Whether to reapply changes if the state of the snippets drifts, i.e. if snippets are managed externally",
},
"content": {
Type: schema.TypeString,
Required: true,
Description: "The VCL code that specifies exactly what the snippet does",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.HasChange("snippet_id") == false && d.Get("manage_snippets") == false
},
},
},
}
}
func resourceServiceDynamicSnippetCreate(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: gofastly.String(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 resourceServiceDynamicSnippetRead(ctx, d, meta)
}
func resourceServiceDynamicSnippetUpdate(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: gofastly.String(content),
})
if err != nil {
return diag.Errorf("Error updating dynamic snippet: service %s, snippet %s, %#v", serviceID, snippetID, err)
}
}
return resourceServiceDynamicSnippetRead(ctx, d, meta)
}
func resourceServiceDynamicSnippetRead(_ 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 resourceServiceDynamicSnippetDelete(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
// Dynamic snippet content cannot be deleted. Removing from state only
d.SetId("")
return nil
}
func resourceServiceDynamicSnippetContentImport(_ 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
}