From db4b7184e7be4be1e81cd34a7e11468ea9fc1362 Mon Sep 17 00:00:00 2001 From: Kors van Loon Date: Fri, 14 Oct 2022 11:12:54 +0200 Subject: [PATCH] improve content-type create resilience When a content-type already exists in Amplience, but terraform tries to create it (because the resource id might have changed), this change can now deal with that by unarchiving if necessary or ignoring the create error. --- amplience/resource_content_type.go | 22 ++++++++- amplience/resource_content_type_schema.go | 58 ++++++----------------- go.mod | 5 +- go.sum | 4 +- 4 files changed, 41 insertions(+), 48 deletions(-) diff --git a/amplience/resource_content_type.go b/amplience/resource_content_type.go index 557defc..3714f6f 100644 --- a/amplience/resource_content_type.go +++ b/amplience/resource_content_type.go @@ -2,6 +2,7 @@ package amplience import ( "context" + "log" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/labd/amplience-go-sdk/content" @@ -85,13 +86,30 @@ func resourceContentTypeCreate(ctx context.Context, data *schema.ResourceData, m ci := getClient(meta) input := resourceContentTypeCreateInput(data) - content_type, err := ci.client.ContentTypeCreate(ci.hubID, input) + instance, err := ci.client.ContentTypeCreate(ci.hubID, input) + + if errResp, ok := err.(*content.ErrorResponse); ok { + if errResp.StatusCode >= 400 { + + log.Println("Received 400 conflict response; schema already exists") + + instance, err = ci.client.ContentTypeFindByUri(input.ContentTypeURI, ci.hubID) + + if err != nil { + return diag.FromErr(err) + } + + if instance.Status == string(content.StatusArchived) { + instance, err = ci.client.ContentTypeUnarchive(instance.ID) + } + } + } if err != nil { return diag.FromErr(err) } - resourceContentTypeSaveState(data, content_type) + resourceContentTypeSaveState(data, instance) return diags } diff --git a/amplience/resource_content_type_schema.go b/amplience/resource_content_type_schema.go index 961a309..5a72249 100644 --- a/amplience/resource_content_type_schema.go +++ b/amplience/resource_content_type_schema.go @@ -2,7 +2,6 @@ package amplience import ( "context" - "fmt" "log" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -59,12 +58,22 @@ func resourceContentTypeSchemaCreate(ctx context.Context, data *schema.ResourceD ValidationLevel: data.Get("validation_level").(string), } - schema, err := ci.client.ContentTypeSchemaCreate(ci.hubID, input) + instance, err := ci.client.ContentTypeSchemaCreate(ci.hubID, input) if errResp, ok := err.(*content.ErrorResponse); ok { - if errResp.StatusCode == 409 { - log.Println("Received 409 conflict response; schema must be unarchived") - schema, err = _unarchiveSchema(schemaId, ci) + if errResp.StatusCode >= 400 { + + log.Println("Received 400 conflict response; schema already exists") + + instance, err = ci.client.ContentTypeSchemaFindBySchemaId(input.SchemaID, ci.hubID) + + if err != nil { + return diag.FromErr(err) + } + + if instance.Status == string(content.StatusArchived) { + instance, err = ci.client.ContentTypeSchemaUnarchive(instance.ID, instance.Version) + } } } @@ -72,7 +81,7 @@ func resourceContentTypeSchemaCreate(ctx context.Context, data *schema.ResourceD return diag.FromErr(err) } - resourceContentTypeSchemaSaveState(data, schema) + resourceContentTypeSchemaSaveState(data, instance) return diags } @@ -142,40 +151,3 @@ func resourceContentTypeSchemaSaveState(data *schema.ResourceData, resource cont data.Set("validation_level", resource.ValidationLevel) data.Set("version", resource.Version) } - -func _unarchiveSchema(schemaId string, ci *ClientInfo) (content.ContentTypeSchema, error) { - result := content.ContentTypeSchema{} - - log.Printf("Get info for content type schema %s", schemaId) - schema, getErr := _schemaBySchemaId(schemaId, ci, content.StatusAny) - - if getErr != nil { - return result, getErr - } - - log.Printf("Received content type schema version %v", schema.Version) - schema, err := ci.client.ContentTypeSchemaUnarchive(schema.ID, schema.Version) - - if err != nil { - return schema, err - } - - return schema, err -} - -func _schemaBySchemaId(schemaId string, ci *ClientInfo, status content.ContentStatus) (content.ContentTypeSchema, error) { - dummy := content.ContentTypeSchema{} - schemaList, getErr := ci.client.ContentTypeSchemaGetAll(ci.hubID, status) - - if getErr != nil { - return dummy, getErr - } - - for _, schema := range schemaList { - if schema.SchemaID == schemaId { - return schema, nil - } - } - - return dummy, fmt.Errorf(fmt.Sprintf("Could not find schema %s", schemaId)) -} diff --git a/go.mod b/go.mod index 286c679..fb7962c 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,13 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0 - github.com/labd/amplience-go-sdk v0.0.0-20221013134454-62b5e840c6b9 + github.com/labd/amplience-go-sdk v0.0.0-20221014090308-3130001fcb1b github.com/stretchr/testify v1.7.0 ) +// Uncomment this line for local development with amplience-go-sdk +// replace github.com/labd/amplience-go-sdk => ../amplience-go-sdk + require ( cloud.google.com/go v0.65.0 // indirect cloud.google.com/go/storage v1.10.0 // indirect diff --git a/go.sum b/go.sum index a369d1e..7cc4fa8 100644 --- a/go.sum +++ b/go.sum @@ -239,8 +239,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/labd/amplience-go-sdk v0.0.0-20221013134454-62b5e840c6b9 h1:VLTSdw6JPBpfz2x7VgpsEaeOYi5zHQoOGjEywLEGjkU= -github.com/labd/amplience-go-sdk v0.0.0-20221013134454-62b5e840c6b9/go.mod h1:A6Hv5OeNhf3rYMiBq4DcVRqndRsDWtOpTucYhq0MDZA= +github.com/labd/amplience-go-sdk v0.0.0-20221014090308-3130001fcb1b h1:9jRj44wsp+I/NK0UztoBTTY8bdEuTkGERKCyL6Lb/oA= +github.com/labd/amplience-go-sdk v0.0.0-20221014090308-3130001fcb1b/go.mod h1:A6Hv5OeNhf3rYMiBq4DcVRqndRsDWtOpTucYhq0MDZA= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=