Skip to content

Commit

Permalink
improve content-type create resilience
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
korsvanloon committed Oct 14, 2022
1 parent 3aa04d2 commit db4b718
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
22 changes: 20 additions & 2 deletions amplience/resource_content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
58 changes: 15 additions & 43 deletions amplience/resource_content_type_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package amplience

import (
"context"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -59,20 +58,30 @@ 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)
}
}
}

if err != nil {
return diag.FromErr(err)
}

resourceContentTypeSchemaSaveState(data, schema)
resourceContentTypeSchemaSaveState(data, instance)
return diags
}

Expand Down Expand Up @@ -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))
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit db4b718

Please sign in to comment.