diff --git a/kong/resource_kong_consumer.go b/kong/resource_kong_consumer.go index f0f5b63..2193a63 100644 --- a/kong/resource_kong_consumer.go +++ b/kong/resource_kong_consumer.go @@ -43,8 +43,8 @@ func resourceKongConsumer() *schema.Resource { func resourceKongConsumerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { consumerRequest := &kong.Consumer{ - Username: kong.String(d.Get("username").(string)), - CustomID: kong.String(d.Get("custom_id").(string)), + Username: NilString(d.Get("username").(string)), + CustomID: NilString(d.Get("custom_id").(string)), Tags: readStringArrayPtrFromResource(d, "tags"), } diff --git a/kong/resource_kong_consumer_test.go b/kong/resource_kong_consumer_test.go index 2168f84..a5e1320 100644 --- a/kong/resource_kong_consumer_test.go +++ b/kong/resource_kong_consumer_test.go @@ -41,6 +41,26 @@ func TestAccKongConsumer(t *testing.T) { }) } +func TestAccKongConsumerNilIDs(t *testing.T) { + + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckKongConsumerDestroy, + Steps: []resource.TestStep{ + { + Config: testCreateConsumerConfigNoCustomID, + Check: resource.ComposeTestCheckFunc( + testAccCheckKongConsumerExists("kong_consumer.consumer"), + resource.TestCheckResourceAttr("kong_consumer.consumer", "username", "User3"), + resource.TestCheckResourceAttr("kong_consumer.consumer", "custom_id", ""), + resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.#", "1"), + resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.0", "c"), + ), + }, + }, + }) +} + func TestAccKongConsumerImport(t *testing.T) { resource.Test(t, resource.TestCase{ @@ -126,3 +146,9 @@ resource "kong_consumer" "consumer" { tags = ["a"] } ` +const testCreateConsumerConfigNoCustomID = ` +resource "kong_consumer" "consumer" { + username = "User3" + tags = ["c"] +} +` diff --git a/kong/utils.go b/kong/utils.go index c9c24ca..f524adc 100644 --- a/kong/utils.go +++ b/kong/utils.go @@ -154,3 +154,13 @@ func IDToString(v *string) string { } return *v } + +// NilString converts a string to a string pointer, +// or if empty returns nil. +func NilString(str string) *string { + if str == "" { + return nil + } else { + return kong.String(str) + } +}