From 6e5782455b19219bfda8cd875060e9e984ac9261 Mon Sep 17 00:00:00 2001 From: Domrev Igor Date: Sun, 28 Mar 2021 15:01:14 +0300 Subject: [PATCH] adding support for parent category --- commercetools/resource_category.go | 47 ++++++++++--- commercetools/resource_category_test.go | 91 +++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 11 deletions(-) diff --git a/commercetools/resource_category.go b/commercetools/resource_category.go index dd995c7b..6b36a033 100644 --- a/commercetools/resource_category.go +++ b/commercetools/resource_category.go @@ -3,6 +3,7 @@ package commercetools import ( "context" "log" + "net/url" "time" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -38,8 +39,10 @@ func resourceCategory() *schema.Resource { Type: schema.TypeMap, Required: true, }, - //parent - //order_hint + "parent_key": { + Type: schema.TypeString, + Optional: true, + }, "order_hint": { Type: schema.TypeString, Optional: true, @@ -81,15 +84,16 @@ func resourceCategoryCreate(d *schema.ResourceData, m interface{}) error { 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), - ExternalID: d.Get("external_id").(string), - MetaTitle: &metaTitle, + Key: d.Get("key").(string), + Name: &name, + Description: &desc, + Slug: &slug, + OrderHint: d.Get("order_hint").(string), + ExternalID: d.Get("external_id").(string), + MetaTitle: &metaTitle, MetaDescription: &metaDescription, - MetaKeywords: &metaKeywords, + MetaKeywords: &metaKeywords, + Parent: getParentRef(d), } err := resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -116,11 +120,21 @@ func resourceCategoryCreate(d *schema.ResourceData, m interface{}) error { return resourceCategoryRead(d, m) } +func getParentRef(d *schema.ResourceData) *commercetools.CategoryResourceIdentifier { + parentKey := d.Get("parent_key").(string) + if parentKey != "" { + return &commercetools.CategoryResourceIdentifier{Key: parentKey} + } + return nil +} + func resourceCategoryRead(d *schema.ResourceData, m interface{}) error { log.Printf("[DEBUG] Reading category from commercetools, with category id: %s", d.Id()) client := getClient(m) - category, err := client.CategoryGetWithID(context.Background(), d.Id()) + category, err := client.CategoryGetWithID(context.Background(), d.Id(), func(v *url.Values) { + v.Add("expand","parent") + }) if err != nil { if ctErr, ok := err.(commercetools.ErrorResponse); ok { @@ -144,6 +158,9 @@ func resourceCategoryRead(d *schema.ResourceData, m interface{}) error { d.Set("name", *category.Name) d.Set("description", *category.Description) d.Set("order_hint", category.OrderHint) + if category.Parent != nil { + d.Set("parent_key", category.Parent.Obj.Key) + } d.Set("external_id", category.ExternalID) if category.MetaTitle != nil { d.Set("meta_title", *category.MetaTitle) @@ -238,6 +255,14 @@ func resourceCategoryUpdate(d *schema.ResourceData, m interface{}) error { &commercetools.CategorySetMetaKeywordsAction{MetaKeywords: &newMetaKeywords}) } + if d.HasChange("parent_key") { + newVal := d.Get("parent_key").(string) + input.Actions = append( + input.Actions, + &commercetools.CategoryChangeParentAction{Parent: &commercetools.CategoryResourceIdentifier{ + Key: newVal }}) + } + log.Printf( "[DEBUG] Will perform update operation with the following actions:\n%s", stringFormatActions(input.Actions)) diff --git a/commercetools/resource_category_test.go b/commercetools/resource_category_test.go index 0d03d1d0..01ddf2e1 100644 --- a/commercetools/resource_category_test.go +++ b/commercetools/resource_category_test.go @@ -1,6 +1,7 @@ package commercetools import ( + "fmt" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "testing" ) @@ -45,6 +46,26 @@ func testCategoryUpdate() string { }` } +func testCreateChildCategory() string { + return fmt.Sprintf(`%s + +resource "commercetools_category" "bracelets" { + name = { + en = "bracelets" + } + key = "bracelets123" + description = { + en = "nice bracelets" + } + slug = { + en = "foo_bracelets" + } + order_hint = "0.008" + parent_key = "bananas123" + }`,testCategoryUpdate()) +} + + func TestCategoryCreate_basic(t *testing.T) { @@ -80,7 +101,77 @@ func TestCategoryCreate_basic(t *testing.T) { resource.TestCheckResourceAttr("commercetools_category.accessoriesz", "meta_description.en", "foo"), resource.TestCheckResourceAttr("commercetools_category.accessoriesz", "meta_keywords.en", "bar"), ), + },{ + Config: testCreateChildCategory(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("commercetools_category.bracelets", "name.en", "bracelets"), + resource.TestCheckResourceAttr("commercetools_category.bracelets", "key", "bracelets123"), + resource.TestCheckResourceAttr("commercetools_category.bracelets", "description.en", "nice bracelets"), + resource.TestCheckResourceAttr("commercetools_category.bracelets", "slug.en", "foo_bracelets"), + resource.TestCheckResourceAttr("commercetools_category.bracelets", "parent_key", "bananas123"), + + ), + }, + { + Config: testChangeParents(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("commercetools_category.bracelets", "parent_key", "new_parent"), + ), }, }, }) } + + +func testChangeParents() string { + return ` + +resource "commercetools_category" "accessoriesz" { + name = { + en = "accessories" + } + key = "bananas123" + description = { + en = "vi very viniversum vivus vicy" + } + slug = { + en = "bananas_accessories" + } + order_hint = "0.002" + external_id = "idclip" + meta_title = { en = "baz" } + meta_description = { en = "foo" } + meta_keywords = { en = "bar" } +} + + +resource "commercetools_category" "accessoriesxx" { + name = { + en = "accessoriesxx" + } + key = "new_parent" + description = { + en = "fooo" + } + slug = { + en = "bar_accessories" + } + order_hint = "0.002" +} + +resource "commercetools_category" "bracelets" { + name = { + en = "bracelets" + } + key = "bracelets123" + description = { + en = "nice bracelets" + } + slug = { + en = "foo_bracelets" + } + order_hint = "0.008" + parent_key = "new_parent" +} +` +}