diff --git a/commercetools/resource_category.go b/commercetools/resource_category.go index aea3708c..170d9483 100644 --- a/commercetools/resource_category.go +++ b/commercetools/resource_category.go @@ -145,21 +145,33 @@ func resourceCategoryCreate(d *schema.ResourceData, m interface{}) error { var category *commercetools.Category name := commercetools.LocalizedString(expandStringMap(d.Get("name").(map[string]interface{}))) - desc := commercetools.LocalizedString(expandStringMap(d.Get("description").(map[string]interface{}))) slug := commercetools.LocalizedString(expandStringMap(d.Get("slug").(map[string]interface{}))) - metaTitle := commercetools.LocalizedString(expandStringMap(d.Get("meta_title").(map[string]interface{}))) - metaDescription := commercetools.LocalizedString(expandStringMap(d.Get("meta_description").(map[string]interface{}))) - metaKeywords := commercetools.LocalizedString(expandStringMap(d.Get("meta_keywords").(map[string]interface{}))) draft := &commercetools.CategoryDraft{ - Key: d.Get("key").(string), - Name: &name, - Description: &desc, - Slug: &slug, - OrderHint: d.Get("order_hint").(string), - MetaTitle: &metaTitle, - MetaDescription: &metaDescription, - MetaKeywords: &metaKeywords, + Key: d.Get("key").(string), + Name: &name, + Slug: &slug, + OrderHint: d.Get("order_hint").(string), + } + + if d.Get("description") != nil { + desc := commercetools.LocalizedString(expandStringMap(d.Get("description").(map[string]interface{}))) + draft.Description = &desc + } + + if d.Get("meta_title") != nil { + metaTitle := commercetools.LocalizedString(expandStringMap(d.Get("meta_title").(map[string]interface{}))) + draft.MetaTitle = &metaTitle + } + + if d.Get("meta_description") != nil { + metaDescription := commercetools.LocalizedString(expandStringMap(d.Get("meta_description").(map[string]interface{}))) + draft.MetaDescription = &metaDescription + } + + if d.Get("meta_keywords") != nil { + metaKeywords := commercetools.LocalizedString(expandStringMap(d.Get("meta_keywords").(map[string]interface{}))) + draft.MetaKeywords = &metaKeywords } if d.Get("parent").(string) != "" { @@ -224,9 +236,11 @@ func resourceCategoryRead(d *schema.ResourceData, m interface{}) error { d.Set("version", category.Version) d.Set("key", category.Key) d.Set("name", *category.Name) - d.Set("description", *category.Description) d.Set("parent", category.Parent) d.Set("order_hint", category.OrderHint) + if category.Description != nil { + d.Set("description", *category.Description) + } if category.MetaTitle != nil { d.Set("meta_title", *category.MetaTitle) } diff --git a/commercetools/resource_category_test.go b/commercetools/resource_category_test.go index 44219d2f..851515c0 100644 --- a/commercetools/resource_category_test.go +++ b/commercetools/resource_category_test.go @@ -3,11 +3,12 @@ package commercetools import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-sdk/terraform" - "github.com/labd/commercetools-go-sdk/commercetools" "strings" "testing" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/labd/commercetools-go-sdk/commercetools" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) @@ -21,6 +22,9 @@ func TestAccCategoryCreate_basic(t *testing.T) { { Config: testAccCategoryConfig(), Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "commercetools_category.accessories_minimal", "name.en", "accessories_m", + ), resource.TestCheckResourceAttr( "commercetools_category.accessories", "name.en", "accessories", ), @@ -155,6 +159,15 @@ func testAccCategoryConfig() string { order_hint = "0.00001614336548703960465522" } + resource "commercetools_category" "accessories_minimal" { + name = { + en = "accessories_m" + } + slug = { + en = "accessories_m" + } + } + resource "commercetools_category" "accessories" { name = { en = "accessories" @@ -210,6 +223,15 @@ func testAccCategoryUpdate() string { order_hint = "0.00001614336548703960465522" } + resource "commercetools_category" "accessories_minimal" { + name = { + en = "accessories_m" + } + slug = { + en = "accessories_m" + } + } + resource "commercetools_category" "accessories" { name = { en = "accessories"