@@ -5,14 +5,8 @@ package snippets
5
5
import (
6
6
"context"
7
7
"fmt"
8
- "io"
9
- "net/http"
10
8
11
9
"github.com/cloudflare/cloudflare-go/v5"
12
- "github.com/cloudflare/cloudflare-go/v5/option"
13
- "github.com/cloudflare/cloudflare-go/v5/snippets"
14
- "github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
15
- "github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
16
10
"github.com/hashicorp/terraform-plugin-framework/resource"
17
11
)
18
12
@@ -52,159 +46,22 @@ func (r *SnippetsResource) Configure(ctx context.Context, req resource.Configure
52
46
r .client = client
53
47
}
54
48
55
- func (r * SnippetsResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
56
- var data * SnippetsModel
57
-
58
- resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
49
+ var ErrNonfunctionalResource = fmt .Errorf ("use 'cloudflare_snippet' instead of 'cloudflare_snippets'" )
59
50
60
- if resp .Diagnostics .HasError () {
61
- return
62
- }
63
-
64
- dataBytes , contentType , err := data .MarshalMultipart ()
65
- if err != nil {
66
- resp .Diagnostics .AddError ("failed to serialize multipart http request" , err .Error ())
67
- return
68
- }
69
- res := new (http.Response )
70
- env := SnippetsResultEnvelope {* data }
71
- _ , err = r .client .Snippets .Update (
72
- ctx ,
73
- data .SnippetName .ValueString (),
74
- snippets.SnippetUpdateParams {
75
- ZoneID : cloudflare .F (data .ZoneID .ValueString ()),
76
- },
77
- option .WithRequestBody (contentType , dataBytes ),
78
- option .WithResponseBodyInto (& res ),
79
- option .WithMiddleware (logging .Middleware (ctx )),
80
- )
81
- if err != nil {
82
- resp .Diagnostics .AddError ("failed to make http request" , err .Error ())
83
- return
84
- }
85
- bytes , _ := io .ReadAll (res .Body )
86
- err = apijson .UnmarshalComputed (bytes , & env )
87
- if err != nil {
88
- resp .Diagnostics .AddError ("failed to deserialize http request" , err .Error ())
89
- return
90
- }
91
- data = & env .Result
92
-
93
- resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
51
+ func (r * SnippetsResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
52
+ resp .Diagnostics .AddError ("resource is non-functional" , ErrNonfunctionalResource .Error ())
94
53
}
95
54
96
55
func (r * SnippetsResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
97
- var data * SnippetsModel
98
-
99
- resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
100
-
101
- if resp .Diagnostics .HasError () {
102
- return
103
- }
104
-
105
- var state * SnippetsModel
106
-
107
- resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
108
-
109
- if resp .Diagnostics .HasError () {
110
- return
111
- }
112
-
113
- dataBytes , contentType , err := data .MarshalMultipart ()
114
- if err != nil {
115
- resp .Diagnostics .AddError ("failed to serialize multipart http request" , err .Error ())
116
- return
117
- }
118
- res := new (http.Response )
119
- env := SnippetsResultEnvelope {* data }
120
- _ , err = r .client .Snippets .Update (
121
- ctx ,
122
- data .SnippetName .ValueString (),
123
- snippets.SnippetUpdateParams {
124
- ZoneID : cloudflare .F (data .ZoneID .ValueString ()),
125
- },
126
- option .WithRequestBody (contentType , dataBytes ),
127
- option .WithResponseBodyInto (& res ),
128
- option .WithMiddleware (logging .Middleware (ctx )),
129
- )
130
- if err != nil {
131
- resp .Diagnostics .AddError ("failed to make http request" , err .Error ())
132
- return
133
- }
134
- bytes , _ := io .ReadAll (res .Body )
135
- err = apijson .UnmarshalComputed (bytes , & env )
136
- if err != nil {
137
- resp .Diagnostics .AddError ("failed to deserialize http request" , err .Error ())
138
- return
139
- }
140
- data = & env .Result
141
-
142
- resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
56
+ resp .Diagnostics .AddError ("resource is non-functional" , ErrNonfunctionalResource .Error ())
143
57
}
144
58
145
59
func (r * SnippetsResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
146
- var data * SnippetsModel
147
-
148
- resp .Diagnostics .Append (req .State .Get (ctx , & data )... )
149
-
150
- if resp .Diagnostics .HasError () {
151
- return
152
- }
153
-
154
- res := new (http.Response )
155
- env := SnippetsResultEnvelope {* data }
156
- _ , err := r .client .Snippets .Get (
157
- ctx ,
158
- data .SnippetName .ValueString (),
159
- snippets.SnippetGetParams {
160
- ZoneID : cloudflare .F (data .ZoneID .ValueString ()),
161
- },
162
- option .WithResponseBodyInto (& res ),
163
- option .WithMiddleware (logging .Middleware (ctx )),
164
- )
165
- if res != nil && res .StatusCode == 404 {
166
- resp .Diagnostics .AddWarning ("Resource not found" , "The resource was not found on the server and will be removed from state." )
167
- resp .State .RemoveResource (ctx )
168
- return
169
- }
170
- if err != nil {
171
- resp .Diagnostics .AddError ("failed to make http request" , err .Error ())
172
- return
173
- }
174
- bytes , _ := io .ReadAll (res .Body )
175
- err = apijson .Unmarshal (bytes , & env )
176
- if err != nil {
177
- resp .Diagnostics .AddError ("failed to deserialize http request" , err .Error ())
178
- return
179
- }
180
- data = & env .Result
181
-
182
- resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
60
+ resp .Diagnostics .AddError ("resource is non-functional" , ErrNonfunctionalResource .Error ())
183
61
}
184
62
185
63
func (r * SnippetsResource ) Delete (ctx context.Context , req resource.DeleteRequest , resp * resource.DeleteResponse ) {
186
- var data * SnippetsModel
187
-
188
- resp .Diagnostics .Append (req .State .Get (ctx , & data )... )
189
-
190
- if resp .Diagnostics .HasError () {
191
- return
192
- }
193
-
194
- _ , err := r .client .Snippets .Delete (
195
- ctx ,
196
- data .SnippetName .ValueString (),
197
- snippets.SnippetDeleteParams {
198
- ZoneID : cloudflare .F (data .ZoneID .ValueString ()),
199
- },
200
- option .WithMiddleware (logging .Middleware (ctx )),
201
- )
202
- if err != nil {
203
- resp .Diagnostics .AddError ("failed to make http request" , err .Error ())
204
- return
205
- }
206
-
207
- resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
64
+ resp .Diagnostics .AddError ("resource is non-functional" , ErrNonfunctionalResource .Error ())
208
65
}
209
66
210
67
func (r * SnippetsResource ) ModifyPlan (_ context.Context , _ resource.ModifyPlanRequest , _ * resource.ModifyPlanResponse ) {
0 commit comments