@@ -23,7 +23,6 @@ import (
23
23
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
24
24
"github.com/hashicorp/terraform-plugin-framework/types"
25
25
"github.com/hashicorp/terraform-plugin-log/tflog"
26
-
27
26
"github.com/labd/terraform-provider-contentful/internal/custommodifier"
28
27
"github.com/labd/terraform-provider-contentful/internal/customvalidator"
29
28
"github.com/labd/terraform-provider-contentful/internal/sdk"
@@ -285,15 +284,23 @@ func (e *contentTypeResource) Create(ctx context.Context, request resource.Creat
285
284
var contentType * sdk.ContentType
286
285
287
286
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
+
288
298
draft , err := plan .Update ()
289
299
if err != nil {
290
300
response .Diagnostics .AddError ("Error creating contenttype" , err .Error ())
291
301
return
292
302
}
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 )
297
304
if err := utils .CheckClientResponse (resp , err , http .StatusCreated ); err != nil {
298
305
response .Diagnostics .AddError ("Error creating contenttype" , "Could not create contenttype with id " + plan .ID .ValueString ()+ ", unexpected error: " + err .Error ())
299
306
return
@@ -305,10 +312,7 @@ func (e *contentTypeResource) Create(ctx context.Context, request resource.Creat
305
312
response .Diagnostics .AddError ("Error creating contenttype" , err .Error ())
306
313
return
307
314
}
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 )
312
316
if err := utils .CheckClientResponse (resp , err , http .StatusCreated ); err != nil {
313
317
response .Diagnostics .AddError ("Error creating contenttype" , "Could not create contenttype with name, unexpected error: " + err .Error ())
314
318
return
@@ -562,7 +566,14 @@ func (e *contentTypeResource) ImportState(ctx context.Context, request resource.
562
566
}
563
567
564
568
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
+ }
566
577
state .SpaceId = types .StringValue (spaceId )
567
578
state .Environment = types .StringValue (environment )
568
579
0 commit comments