labd / terraform-provider-commercetools

Terraform provider for commercetools
https://registry.terraform.io/providers/labd/commercetools/latest/docs
Mozilla Public License 2.0
64 stars 68 forks source link

Terraform apply is failing with duplicate key issue with commercetools_product_type resource. #461

Closed vndrzrfshKxR2 closed 5 months ago

vndrzrfshKxR2 commented 7 months ago

We have manually setup commercetools dev environment using terraform from local machine for commercetools_product_type resource and store_specific_data. When we are deploying the same infra using terraform with TFC , apply is failing with duplicate keys issue.

Solution tried:

  1. import existing infra from commercetools dev using terraform --> import successful.
  2. rerun terraform apply with complete infra in automated terraform script with TFC using Github actions (includes new changes + existing infra (returned from import command)) --> failed with duplicate keys

Resource:

resource "commercetools_product_type" "this" { key = product name = product }

error: Error: A duplicate value '"product"' exists for field 'key'. 752753│ with module.product_type.commercetools_product_type.this, 754│ on modules/product_type/main.tf line 9, in resource "commercetools_product_type" "this": 755│ 9: resource "commercetools_product_type" "this" {

Suggest if anything is missed or is there other way to handle the same in commercetools TF.

demeyerthom commented 7 months ago

@vndrzrfshKxR2 thanks for reaching out!

Quick question: with what id did you do the import? It should be the ID you use to fetch a product through the following call: https://docs.commercetools.com/api/projects/products#get-product-by-id. This is the same as what you can see as the last element in your browser when in the product view in the merchant center.

If you used the key or any other value it will have imported that as ID instead, and it will break when trying to apply because it is trying to create 2 products with the same key