Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "Provider produced invalid object" when import existing commercetools_project_settings into terraform state #538

Open
stanislavhordiyenko opened this issue Nov 15, 2024 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@stanislavhordiyenko
Copy link
Contributor

Version information

  • terraform: 1.9.2
  • terraform provider: 1.17.0

Describe the bug

I would like to create commercetools_project_settings resource and map it (with import { }) to already created project in Commercetools. When I run plan, I get the error below:

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Provider produced invalid object
│ 
│ Provider "registry.terraform.io/labd/commercetools" planned an invalid value for commercetools_project_settings.default during refresh: The returned
│ state contains unknown values.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵

Unfortunately, no information is provided when I ran plan command with debug option, no enough information what went wrong.

2024-11-15T11:35:52.238+1100 [INFO]  Terraform version: 1.9.2
2024-11-15T11:35:52.238+1100 [DEBUG] using github.com/hashicorp/go-tfe v1.51.0
2024-11-15T11:35:52.238+1100 [DEBUG] using github.com/hashicorp/hcl/v2 v2.20.0
2024-11-15T11:35:52.238+1100 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2024-11-15T11:35:52.238+1100 [DEBUG] using github.com/zclconf/go-cty v1.14.4
2024-11-15T11:35:52.238+1100 [INFO]  Go runtime version: go1.22.4
2024-11-15T11:35:52.238+1100 [INFO]  CLI args: []string{"terraform", "plan"}
2024-11-15T11:35:52.238+1100 [DEBUG] Attempting to open CLI config file: /Users/work/.terraformrc
2024-11-15T11:35:52.238+1100 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2024-11-15T11:35:52.239+1100 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2024-11-15T11:35:52.239+1100 [DEBUG] ignoring non-existing provider search directory /Users/work/.terraform.d/plugins
2024-11-15T11:35:52.239+1100 [DEBUG] ignoring non-existing provider search directory /Users/work/Library/Application Support/io.terraform/plugins
2024-11-15T11:35:52.239+1100 [DEBUG] ignoring non-existing provider search directory /Library/Application Support/io.terraform/plugins
2024-11-15T11:35:52.239+1100 [INFO]  CLI command args: []string{"plan"}
2024-11-15T11:35:52.258+1100 [DEBUG] checking for provisioner in "."
2024-11-15T11:35:52.260+1100 [DEBUG] checking for provisioner in "/opt/homebrew/bin"
2024-11-15T11:35:52.260+1100 [INFO]  backend/local: starting Plan operation
2024-11-15T11:35:52.264+1100 [DEBUG] created provider logger: level=debug
2024-11-15T11:35:52.264+1100 [INFO]  provider: configuring client automatic mTLS
2024-11-15T11:35:52.270+1100 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 args=[".terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0"]
2024-11-15T11:35:52.272+1100 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 pid=70221
2024-11-15T11:35:52.272+1100 [DEBUG] provider: waiting for RPC address: plugin=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0
2024-11-15T11:35:52.278+1100 [INFO]  provider.terraform-provider-commercetools_v1.17.0: configuring server automatic mTLS: timestamp="2024-11-15T11:35:52.278+1100"
2024-11-15T11:35:52.283+1100 [DEBUG] provider: using plugin: version=5
2024-11-15T11:35:52.283+1100 [DEBUG] provider.terraform-provider-commercetools_v1.17.0: plugin address: address=/var/folders/ws/j88jlndn1kjfvpcd0fd_6ccw0000gn/T/plugin1850456622 network=unix timestamp="2024-11-15T11:35:52.283+1100"
2024-11-15T11:35:52.291+1100 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-11-15T11:35:52.291+1100 [INFO]  provider: plugin process exited: plugin=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 id=70221
2024-11-15T11:35:52.291+1100 [DEBUG] provider: plugin exited
2024-11-15T11:35:52.291+1100 [DEBUG] Building and walking validate graph
2024-11-15T11:35:52.291+1100 [DEBUG] ProviderTransformer: "commercetools_project_settings.default" (*terraform.NodeValidatableResource) needs provider["registry.terraform.io/labd/commercetools"]
2024-11-15T11:35:52.291+1100 [DEBUG] ReferenceTransformer: "commercetools_project_settings.default" references: []
2024-11-15T11:35:52.291+1100 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/labd/commercetools\"]" references: []
2024-11-15T11:35:52.291+1100 [DEBUG] Starting graph walk: walkValidate
2024-11-15T11:35:52.292+1100 [DEBUG] created provider logger: level=debug
2024-11-15T11:35:52.292+1100 [INFO]  provider: configuring client automatic mTLS
2024-11-15T11:35:52.294+1100 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 args=[".terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0"]
2024-11-15T11:35:52.295+1100 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 pid=70222
2024-11-15T11:35:52.295+1100 [DEBUG] provider: waiting for RPC address: plugin=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0
2024-11-15T11:35:52.302+1100 [INFO]  provider.terraform-provider-commercetools_v1.17.0: configuring server automatic mTLS: timestamp="2024-11-15T11:35:52.302+1100"
2024-11-15T11:35:52.306+1100 [DEBUG] provider: using plugin: version=5
2024-11-15T11:35:52.306+1100 [DEBUG] provider.terraform-provider-commercetools_v1.17.0: plugin address: network=unix address=/var/folders/ws/j88jlndn1kjfvpcd0fd_6ccw0000gn/T/plugin1344303626 timestamp="2024-11-15T11:35:52.306+1100"
2024-11-15T11:35:52.313+1100 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-11-15T11:35:52.313+1100 [INFO]  provider: plugin process exited: plugin=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 id=70222
2024-11-15T11:35:52.313+1100 [DEBUG] provider: plugin exited
2024-11-15T11:35:52.313+1100 [INFO]  backend/local: plan calling Plan
2024-11-15T11:35:52.313+1100 [DEBUG] Building and walking plan graph for NormalMode
2024-11-15T11:35:52.313+1100 [DEBUG] ProviderTransformer: "commercetools_project_settings.default (expand)" (*terraform.nodeExpandPlannableResource) needs provider["registry.terraform.io/labd/commercetools"]
2024-11-15T11:35:52.313+1100 [DEBUG] ReferenceTransformer: "commercetools_project_settings.default (expand)" references: []
2024-11-15T11:35:52.313+1100 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/labd/commercetools\"]" references: []
2024-11-15T11:35:52.314+1100 [DEBUG] Starting graph walk: walkPlan
2024-11-15T11:35:52.314+1100 [DEBUG] created provider logger: level=debug
2024-11-15T11:35:52.314+1100 [INFO]  provider: configuring client automatic mTLS
2024-11-15T11:35:52.315+1100 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 args=[".terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0"]
2024-11-15T11:35:52.317+1100 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0 pid=70223
2024-11-15T11:35:52.317+1100 [DEBUG] provider: waiting for RPC address: plugin=.terraform/providers/registry.terraform.io/labd/commercetools/1.17.0/darwin_arm64/terraform-provider-commercetools_v1.17.0
2024-11-15T11:35:52.323+1100 [INFO]  provider.terraform-provider-commercetools_v1.17.0: configuring server automatic mTLS: timestamp="2024-11-15T11:35:52.323+1100"
2024-11-15T11:35:52.327+1100 [DEBUG] provider: using plugin: version=5
2024-11-15T11:35:52.327+1100 [DEBUG] provider.terraform-provider-commercetools_v1.17.0: plugin address: address=/var/folders/ws/j88jlndn1kjfvpcd0fd_6ccw0000gn/T/plugin3845565809 network=unix timestamp="2024-11-15T11:35:52.327+1100"
2024-11-15T11:35:52.332+1100 [DEBUG] Resource instance state not found for node "commercetools_project_settings.default", instance commercetools_project_settings.default
2024-11-15T11:35:52.332+1100 [DEBUG] ReferenceTransformer: "commercetools_project_settings.default" references: []
commercetools_project_settings.default: Preparing import... [id=xxx-prod]
commercetools_project_settings.default: Refreshing state... [id=xxx-prod]
2024-11-15T11:35:52.474+1100 [DEBUG] provider.terraform-provider-commercetools_v1.17.0: Value switched to prior value due to semantic equality logic: tf_rpc=ReadResource @module=sdk.framework tf_mux_provider="*proto5server.Server" tf_provider_addr=registry.terraform.io/labd/commercetools tf_req_id=a172a6c9-d13c-a232-e3a6-67dc81af1af3 tf_resource_type=commercetools_project_settings @caller=github.com/hashicorp/[email protected]/internal/fwschemadata/value_semantic_equality.go:91 tf_attribute_path=id timestamp="2024-11-15T11:35:52.473+1100"
2024-11-15T11:35:52.475+1100 [ERROR] vertex "commercetools_project_settings.default" error: Provider produced invalid object
2024-11-15T11:35:52.475+1100 [ERROR] vertex "commercetools_project_settings.default (expand)" error: Provider produced invalid object
2024-11-15T11:35:52.475+1100 [WARN]  Planning encountered errors, so plan is not applyable
2024-11-15T11:35:52.475+1100 [INFO]  backend/local: plan operation completed

Planning failed. Terraform encountered an error while generating this plan.

I added all scopes for API key.

I usually add a bare minimum to the resource, and then add import block so that terraform can show me exactly what is missing or needs to be changed, so that I can reflect this in code.

Could you please suggest me what information to provide so that we can identify and fix the issue?

On contrary, when I run this on an empty project with no configuration, it works. Even with limited scopes for the API key.

Thank you.

@stanislavhordiyenko stanislavhordiyenko added bug Something isn't working triage Needs triage labels Nov 15, 2024
@stanislavhordiyenko
Copy link
Contributor Author

Looks like it is something to do with totalPriceDiscountDoesNotReduceExternalTax field of the Cart that was recently introduced. The resource works fine for the newly created projects, but for the old one this field is set to false by default. Resource commercetools_project_settings works perfectly fine util I asked support to sync this particular field with the other existing project where it is set to false and terraform doesn't work after this change. Same error as above.

@demeyerthom
Copy link
Member

Hi @stanislavhordiyenko Thanks for the additional info. I will take a look!

@demeyerthom demeyerthom removed the triage Needs triage label Nov 22, 2024
@demeyerthom demeyerthom self-assigned this Nov 22, 2024
@stanislavhordiyenko
Copy link
Contributor Author

@demeyerthom please let me know if you need more information on this issue. I am more than happy to provide you with more details.

@demeyerthom
Copy link
Member

Hi @stanislavhordiyenko I am having some issue reproducing the settings. Could you maybe provide me with the bit of terraform you are trying to apply, as well as the json of the project before you run the terraform plan/apply? I think this is related to unexpected values coming back from the API.

Also, I am not 100% certain what configuration is totalPriceDiscountDoesNotReduceExternalTax. I don't think this is part of the terraform provider currently, and I cannot find it in the docs either :)

@stanislavhordiyenko
Copy link
Contributor Author

@demeyerthom I will create a separate CT project this week, reproduce a bug there, and then will share tf with you as well as the access to the project (I will need an email to invite).

@stanislavhordiyenko
Copy link
Contributor Author

@demeyerthom I was able to reproduce the issue in a blank CT project. Could you please let me know how can I share access to the project with you as I need to add your email. Alternatively, I can share my tf file with API key with you so that you can run it locally. Please, suggest a secure channel for communication.

Thank you.

@stanislavhordiyenko
Copy link
Contributor Author

Terraform file:

terraform {
  required_providers {
    commercetools = {
      source  = "labd/commercetools"
      version = "1.17.0"
    }
  }
}

provider "commercetools" {
  client_id     = "VQ..._O"
  client_secret = "s6I...vzn"
  project_key   = "hordiyenko-terraform"
  scopes        = "manage_project:hordiyenko-terraform"
  api_url       = "https://api.australia-southeast1.gcp.commercetools.com"
  token_url     = "https://auth.australia-southeast1.gcp.commercetools.com"
}

import {
  id = "hordiyenko-terraform"
  to = commercetools_project_settings.default
}

resource "commercetools_project_settings" "default" {
  name       = "hordiyenko-terraform"
  countries  = ["AU", "NZ"]
  currencies = ["AUD", "NZD"]
  languages  = ["en-AU", "en-NZ"]

  messages {
    enabled                    = true
    delete_days_after_creation = 15
  }

  enable_search_index_products       = true
  enable_search_index_product_search = true
  enable_search_index_orders         = true
  enable_search_index_customers      = true

  carts {
    delete_days_after_last_modification = 30
    country_tax_rate_fallback_enabled   = false
  }
}

Project JSON before apply:

{
  "key": "hordiyenko-terraform",
  "name": "hordiyenko-terraform",
  "countries": [
    "AU",
    "NZ"
  ],
  "currencies": [
    "AUD",
    "NZD"
  ],
  "languages": [
    "en-AU",
    "en-NZ"
  ],
  "createdAt": "2024-12-01T11:18:19.842Z",
  "createdBy": {
    "isPlatformClient": true,
    "user": {
      "typeId": "user",
      "id": "3d43dc68-2390-41e9-bfdc-976d32fd9ccc"
    }
  },
  "lastModifiedAt": "2024-12-01T11:24:32.026Z",
  "lastModifiedBy": {
    "isPlatformClient": true
  },
  "trialUntil": "2025-01",
  "messages": {
    "enabled": false,
    "deleteDaysAfterCreation": 15
  },
  "carts": {
    "deleteDaysAfterLastModification": 90,
    "allowAddingUnpublishedProducts": false,
    "countryTaxRateFallbackEnabled": false,
    "cartDiscountCache": {
      "enabled": false
    },
    "totalPriceDiscountDoesNotReduceExternalTax": true,
    "loadParsedDiscountsEnabled": false
  },
  "shoppingLists": {
    "deleteDaysAfterLastModification": 360
  },
  "version": 10,
  "searchIndexing": {
    "products": {
      "status": "Indexing",
      "lastModifiedAt": "2024-12-01T11:24:32.021Z",
      "lastModifiedBy": {
        "isPlatformClient": true
      }
    },
    "productsSearch": {
      "status": "Activated",
      "lastModifiedAt": "2024-12-01T11:24:32.022Z",
      "lastModifiedBy": {
        "isPlatformClient": true
      }
    },
    "orders": {
      "status": "Activated",
      "lastModifiedAt": "2024-12-01T11:24:32.018Z",
      "lastModifiedBy": {
        "isPlatformClient": true
      }
    },
    "customers": {
      "status": "Activated",
      "lastModifiedAt": "2024-12-01T11:24:32.023Z",
      "lastModifiedBy": {
        "isPlatformClient": true
      }
    }
  }
}

@demeyerthom as I said earlier, I am more than happy to provide you access to the project as well as the keys so that you can reproduce the issue on your local machine. This is a test project and no confidential data can be found there.

Thank you.

@demeyerthom
Copy link
Member

Hi @stanislavhordiyenko Gave it another go with your terraform and a clean trial environment. I noticed that your json has some country codes that are not default, as well as the indexing for products etc enabled. Did this happen manually before doing the apply?

I made the same changes by hand to bring the state in line, but the terraform import still succeeds.

Some of the projects fields are eventually consistent (like the search statusses) which can cause errors when terraform receives a different status from what it expected.

You can add me to the project if you want, I can take a look there if I can reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants