Skip to content

Commit aeca6a0

Browse files
committed
fix: add validation to prevent creation of existing content types
1 parent 52292cf commit aeca6a0

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Added contenttype check for existing resources when creating
3+
time: 2025-06-13T10:31:25.216356419+02:00

internal/resources/contenttype/resource.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
2424
"github.com/hashicorp/terraform-plugin-framework/types"
2525
"github.com/hashicorp/terraform-plugin-log/tflog"
26-
2726
"github.com/labd/terraform-provider-contentful/internal/custommodifier"
2827
"github.com/labd/terraform-provider-contentful/internal/customvalidator"
2928
"github.com/labd/terraform-provider-contentful/internal/sdk"
@@ -285,15 +284,23 @@ func (e *contentTypeResource) Create(ctx context.Context, request resource.Creat
285284
var contentType *sdk.ContentType
286285

287286
if !plan.ID.IsUnknown() && !plan.ID.IsNull() {
287+
existingContentType, err := e.client.GetContentTypeWithResponse(ctx, spaceId, environment, plan.ID.ValueString())
288+
if err != nil {
289+
response.Diagnostics.AddError("Error creating contenttype", "Could not retrieve contenttype with id "+plan.ID.ValueString()+", unexpected error: "+err.Error())
290+
return
291+
}
292+
293+
if existingContentType.StatusCode() == http.StatusOK {
294+
response.Diagnostics.AddError("Error creating contenttype", "Content type with id "+plan.ID.ValueString()+" already exists. Please import it and use the update resource to modify it, or remove before retrying.")
295+
return
296+
}
297+
288298
draft, err := plan.Update()
289299
if err != nil {
290300
response.Diagnostics.AddError("Error creating contenttype", err.Error())
291301
return
292302
}
293-
params := &sdk.UpdateContentTypeParams{
294-
// XContentfulVersion: 1,
295-
}
296-
resp, err := e.client.UpdateContentTypeWithResponse(ctx, spaceId, environment, plan.ID.ValueString(), params, *draft)
303+
resp, err := e.client.UpdateContentTypeWithResponse(ctx, spaceId, environment, plan.ID.ValueString(), nil, *draft)
297304
if err := utils.CheckClientResponse(resp, err, http.StatusCreated); err != nil {
298305
response.Diagnostics.AddError("Error creating contenttype", "Could not create contenttype with id "+plan.ID.ValueString()+", unexpected error: "+err.Error())
299306
return
@@ -305,10 +312,7 @@ func (e *contentTypeResource) Create(ctx context.Context, request resource.Creat
305312
response.Diagnostics.AddError("Error creating contenttype", err.Error())
306313
return
307314
}
308-
params := &sdk.UpdateContentTypeParams{
309-
// XContentfulVersion: 1,
310-
}
311-
resp, err := e.client.UpdateContentTypeWithResponse(ctx, spaceId, environment, plan.Name.ValueString(), params, *draft)
315+
resp, err := e.client.UpdateContentTypeWithResponse(ctx, spaceId, environment, plan.Name.ValueString(), nil, *draft)
312316
if err := utils.CheckClientResponse(resp, err, http.StatusCreated); err != nil {
313317
response.Diagnostics.AddError("Error creating contenttype", "Could not create contenttype with name, unexpected error: "+err.Error())
314318
return
@@ -562,7 +566,14 @@ func (e *contentTypeResource) ImportState(ctx context.Context, request resource.
562566
}
563567

564568
state := &ContentType{}
565-
state.Import(resp.JSON200)
569+
err = state.Import(resp.JSON200)
570+
if err != nil {
571+
response.Diagnostics.AddError(
572+
"Error importing contenttype to state",
573+
"Could not import contenttype to state, unexpected error: "+err.Error(),
574+
)
575+
return
576+
}
566577
state.SpaceId = types.StringValue(spaceId)
567578
state.Environment = types.StringValue(environment)
568579

0 commit comments

Comments
 (0)