Skip to content

Commit 2d689fa

Browse files
Merge pull request #5875 from cloudflare/deprecate-snippets
Add deprecation notice and clear errors for non-functional `snippets`
2 parents 52519cc + b242926 commit 2d689fa

File tree

6 files changed

+12
-150
lines changed

6 files changed

+12
-150
lines changed

internal/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ func (p *CloudflareProvider) DataSources(ctx context.Context) []func() datasourc
879879
snippet.NewSnippetDataSource,
880880
snippet.NewSnippetsDataSource,
881881
snippet_rules.NewSnippetRulesListDataSource,
882+
snippets.NewSnippetsDataSource, // deprecated.
883+
snippets.NewSnippetsListDataSource, // deprecated.
882884
calls_sfu_app.NewCallsSFUAppDataSource,
883885
calls_sfu_app.NewCallsSFUAppsDataSource,
884886
calls_turn_app.NewCallsTURNAppDataSource,

internal/services/snippet/list_data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewSnippetsDataSource() datasource.DataSource {
2424
}
2525

2626
func (d *SnippetsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
27-
resp.TypeName = req.ProviderTypeName + "_snippets"
27+
resp.TypeName = req.ProviderTypeName + "_snippet_list"
2828
}
2929

3030
func (d *SnippetsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {

internal/services/snippets/data_source_schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var _ datasource.DataSourceWithConfigValidators = (*SnippetsDataSource)(nil)
1414

1515
func DataSourceSchema(ctx context.Context) schema.Schema {
1616
return schema.Schema{
17+
DeprecationMessage: "The `snippets` data source has been deprecated. Use `snippet` instead.",
1718
Attributes: map[string]schema.Attribute{
1819
"snippet_name": schema.StringAttribute{
1920
Description: "The identifying name of the snippet.",

internal/services/snippets/list_data_source_schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var _ datasource.DataSourceWithConfigValidators = (*SnippetsListDataSource)(nil)
1717

1818
func ListDataSourceSchema(ctx context.Context) schema.Schema {
1919
return schema.Schema{
20+
DeprecationMessage: "The `snippets_list` data source has been deprecated. Use `snippet_list` instead.",
2021
Attributes: map[string]schema.Attribute{
2122
"zone_id": schema.StringAttribute{
2223
Description: "The unique ID of the zone.",

internal/services/snippets/resource.go

Lines changed: 6 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ package snippets
55
import (
66
"context"
77
"fmt"
8-
"io"
9-
"net/http"
108

119
"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"
1610
"github.com/hashicorp/terraform-plugin-framework/resource"
1711
)
1812

@@ -52,159 +46,22 @@ func (r *SnippetsResource) Configure(ctx context.Context, req resource.Configure
5246
r.client = client
5347
}
5448

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'")
5950

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())
9453
}
9554

9655
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())
14357
}
14458

14559
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())
18361
}
18462

18563
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())
20865
}
20966

21067
func (r *SnippetsResource) ModifyPlan(_ context.Context, _ resource.ModifyPlanRequest, _ *resource.ModifyPlanResponse) {

internal/services/snippets/schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var _ resource.ResourceWithConfigValidators = (*SnippetsResource)(nil)
1717

1818
func ResourceSchema(ctx context.Context) schema.Schema {
1919
return schema.Schema{
20+
DeprecationMessage: "The `snippets` resource has been deprecated. Use `snippet` instead.",
2021
Attributes: map[string]schema.Attribute{
2122
"snippet_name": schema.StringAttribute{
2223
Description: "The identifying name of the snippet.",

0 commit comments

Comments
 (0)