Skip to content

Commit

Permalink
adding support for parent category
Browse files Browse the repository at this point in the history
  • Loading branch information
viggin543 committed Mar 28, 2021
1 parent e359861 commit 6e57824
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 11 deletions.
47 changes: 36 additions & 11 deletions commercetools/resource_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commercetools
import (
"context"
"log"
"net/url"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
91 changes: 91 additions & 0 deletions commercetools/resource_category_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commercetools

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"testing"
)
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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"
}
`
}

0 comments on commit 6e57824

Please sign in to comment.