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

Error when trying to import shipping zone rates #480

Closed fabian-kramer closed 2 months ago

fabian-kramer commented 4 months ago

Hi, I need to import the already existing shipping zone rates. Unfortunately, this error occurred when executing the import command.

commercetools_shipping_zone_rate.Stadard-Shipping-Germany-zone-rate: Importing from ID "EUR"...
╷
│ Error: Plugin did not respond
│ 
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ImportResourceState call. The plugin logs may
│ contain more details.
╵

Stack trace from the terraform-provider-commercetools_v1.14.1 plugin:

panic: runtime error: index out of range [1] with length 1

goroutine 47 [running]:
github.com/labd/terraform-provider-commercetools/commercetools.getShippingIDs({0xc00035c1e0?, 0x1?})
    github.com/labd/terraform-provider-commercetools/commercetools/resource_shipping_zone_rate.go:400 +0x89
github.com/labd/terraform-provider-commercetools/commercetools.resourceShippingZoneRateImportState({0x1717640, 0xc00045cf00}, 0xc0005c6700, {0x157a740?, 0xc0004244c8})
    github.com/labd/terraform-provider-commercetools/commercetools/resource_shipping_zone_rate.go:162 +0x139
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Provider).ImportState(0xc00007ec00, {0x1717640, 0xc00045cf00}, 0xc000493528, {0xc00035c1e0, 0x3})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.30.0/helper/schema/provider.go:394 +0x1ac
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ImportResourceState(0xc000011410, {0x1717640?, 0xc00045cde0?}, 0xc0004b4140)
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.30.0/helper/schema/grpc_provider.go:1142 +0xf4
github.com/hashicorp/terraform-plugin-mux/tf5muxserver.(*muxServer).ImportResourceState(0x1717598?, {0x1717640?, 0xc00045cab0?}, 0xc0004b4140)
    github.com/hashicorp/terraform-plugin-mux@v0.12.0/tf5muxserver/mux_server_ImportResourceState.go:36 +0x1b5
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ImportResourceState(0xc000147f40, {0x1717640?, 0xc00045c1b0?}, 0xc00062e0f0)
    github.com/hashicorp/terraform-plugin-go@v0.19.0/tfprotov5/tf5server/server.go:889 +0x1f6
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ImportResourceState_Handler({0x151d860?, 0xc000147f40}, {0x1717640, 0xc00045c1b0}, 0xc0005c6080, 0x0)
    github.com/hashicorp/terraform-plugin-go@v0.19.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:485 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001441e0, {0x1717640, 0xc00045c0f0}, {0x171c938, 0xc0003289c0}, 0xc000860480, 0xc0003244b0, 0x1f72c50, 0x0)
    google.golang.org/grpc@v1.59.0/server.go:1343 +0xe49
google.golang.org/grpc.(*Server).handleStream(0xc0001441e0, {0x171c938, 0xc0003289c0}, 0xc000860480)
    google.golang.org/grpc@v1.59.0/server.go:1737 +0xca6
google.golang.org/grpc.(*Server).serveStreams.func1.1()
    google.golang.org/grpc@v1.59.0/server.go:986 +0x8c
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/grpc@v1.59.0/server.go:997 +0x15c

Error: The terraform-provider-commercetools_v1.14.1 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

As I couldn't find any documentation or hint as to what the proper ID (if it exists) for the zone rate is, I tried it with "EUR" as the currency seems to be the unique factor. I hope you can help me here. Maybe it's not even a bug but a usage error. In that case, I'd be happy for some assistance.

demeyerthom commented 4 months ago

Hi @fabian-kramer hmm not sure what is going on here. Can you maybe share the terraform resource that is causing this, and if possible a way to reproduce the error?

fabian-kramer commented 4 months ago

Hi @demeyerthom, yes of course.

resource "commercetools_shipping_zone_rate" "Stadard-Shipping-Germany-zone-rate" {
  price {
    cent_amount = 100
    currency_code = "EUR"
  }
  shipping_method_id = commercetools_shipping_method.Stadard-Shipping-Germany.id
  shipping_zone_id = commercetools_shipping_zone.Germany.id
}

I could also find out why it was failing. After creating a new resource of that type, I could find the id structure to get the correct import command. So instead of: terraform import commercetools_shipping_zone_rate.Stadard-Shipping-Germany-zone-rate EUR I had to use this command: terraform import commercetools_shipping_zone_rate.Stadard-Shipping-Germany-zone-rate id-xyz-abc-123@id2-xyz-abc-123@EUR

I think if you create a resource like mine in Commercetools and then try to import it like I tried, then you should get the same error.

demeyerthom commented 2 months ago

Hi @fabian-kramer hmm I think you found the correct way of doing the import here. commercetools_shipping_zone_rate is not an actual resource that exists in Commercetools, but rather an entity we build in the provider to make the resource management a bit cleaner. To do this however we needed a composite ID to be able to identify which shipping zone rate we are talking about. That ID is in the format of {shipping-method-id}@{shipping-zone-id}@{currency} (which you figured out above)

This is intended behaviour (which I was also not aware of until now :P) but not very well documented. I will update the docs to reflect this.