labd/terraform-provider-commercetools

Discrepancy in different CI pipeline when it comes to adding category

zoeyzou opened this issue · 2 comments

There’s some bug in the TF registry as I noticed and hopefully can get some support from you. I created a new type def for category, it has a field that’s Set of String. The bug I encountered is, when I create a PR in our repo, GH actions keeps complaining element needs to be String, so I changed it to String, but in the master CI it then complains element needs to be Array. I have tried every possible way but maybe either I still did it wrong, or there is indeed some bug.

Type def

resource "commercetools_type" "category-exclusive-group-type" {
  key         = "category-exclusive-group-type"
  name        = { en = "Category exclusive group type" }
  description = { en = "Add exclusive group field to category" }

  resource_type_ids = ["category"]

  field {
    name = "exclusiveGroups"
    label = {
      en = "Exclusive group tags"
    }
    type {
      name = "Set"
      element_type {
        name = "String"
      }
    }
  }
}

I’ve tried the following way to define custom field when actually define category:

custom {
    type_id = "type"
    fields = {
      exclusiveGroups = ["genesis:Unity Editor"]
    }
  }

or

custom {
    type_id = commercetools_type.category-exclusive-group-type.id
    fields = {
      exclusiveGroups = ["genesis:Unity Editor"]
    }
  }

or

data "commercetools_type" "category-exclusive-group-type" {
  key = "category-exclusive-group-type"
}

custom {
    type_id = data.commercetools_type.category-exclusive-group-type.id
    fields = {
      exclusiveGroups = ["genesis:Unity Editor"]
    }
  }

None of them works. However using API or UI to create works, and the new type def is there:

{
  "name" : {
    "en" : "test"
  },
  "slug" : {
    "en" : "test"
  },
  "orderHint" : "1",
  "custom": {
      "type": {
          "typeId": "type",
          "key": "category-exclusive-group-type"
      },
      "fields": {
          "exclusiveGroups": ["genesis:Unity Editor"]
      }
  }
}

Let me know if you need more info

Screenshot 2023-11-24 at 13 54 33 Screenshot 2023-11-24 at 13 54 54

Hi @zoeyzou thanks for reaching out!

Yeah, you ran into something we need to document a bit better here; because we don't know to what to encode a custom type in the provider we only allow for a map of key values as inputs in terraform. However, the way to work around this is to provide the input value as a json string instead. So in your case that would look like this:

resource "commercetools_category" "my-category" {
 key = "my-category-key"

 name = {
   en = "My category"
 }
 description = {
   en = "Standard description"
 }
 slug = {
   en = "my_category"
 }
 meta_title = {
   en = "Meta title"
 }

 custom {
   type_id = commercetools_type.category-exclusive-group-type.id
   fields  = {
     exclusiveGroups = jsonencode(["group1", "group2"])
   }
 }
}

I will expand on the documentation to make this a bit more clear.

@demeyerthom Thanks Thomas, it works perfect now!