aliyun / terraform-provider-alicloud

Terraform AliCloud provider
https://www.terraform.io/docs/providers/alicloud/
Mozilla Public License 2.0
582 stars 548 forks source link

how to import alicloud_security_group_rule? #4888

Open stanxing opened 2 years ago

stanxing commented 2 years ago

Terraform Version

Terraform v1.1.7 on darwin_arm64

Affected Resource(s)

Please list the resources as a list, for example:

Terraform Command

terraform import 'alicloud_security_group_rule.allow_icmp_ingress' sg-bp11r3e0h88zlzt7n9y7_ingress_icmp_1_65535_0.0.0.0/0

Panic Logs

Stack trace from the terraform-provider-alicloud_v1.162.0 plugin:

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

goroutine 98 [running]:
github.com/aliyun/terraform-provider-alicloud/alicloud.resourceAliyunSecurityGroupRuleRead(0x14001b12850, 0x10b1c1220, 0x14001cc0000, 0x14001b12850, 0x0)
        github.com/aliyun/terraform-provider-alicloud/alicloud/resource_alicloud_security_group_rule.go:187 +0xb94
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0x14000d6fd00, 0x140008fe730, 0x10b1c1220, 0x14001cc0000, 0x14002036800, 0x0, 0x0)
        github.com/hashicorp/terraform-plugin-sdk@v1.4.0/helper/schema/resource.go:455 +0xf4
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0x140005e63e8, 0x10b341df0, 0x14001b24660, 0x140008fe500, 0x140005e63e8, 0x105452220, 0x10a908460)
        github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/helper/plugin/grpc_provider.go:525 +0x328
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler(0x10aac1440, 0x140005e63e8, 0x10b341df0, 0x14001b24660, 0x14001b061e0, 0x0, 0x10b341df0, 0x14001b24660, 0x14000080120, 0x120)
        github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/tfplugin5/tfplugin5.pb.go:3153 +0x1c8
google.golang.org/grpc.(*Server).processUnaryRPC(0x140005c2a80, 0x10b3b6ed8, 0x14000602600, 0x1400002cd00, 0x1400140c8a0, 0x110b020b0, 0x0, 0x0, 0x0)
        google.golang.org/grpc@v1.27.1/server.go:1024 +0x3e8
google.golang.org/grpc.(*Server).handleStream(0x140005c2a80, 0x10b3b6ed8, 0x14000602600, 0x1400002cd00, 0x0)
        google.golang.org/grpc@v1.27.1/server.go:1313 +0xab0
google.golang.org/grpc.(*Server).serveStreams.func1.1(0x140004381d0, 0x140005c2a80, 0x10b3b6ed8, 0x14000602600, 0x1400002cd00)
        google.golang.org/grpc@v1.27.1/server.go:722 +0x94
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.27.1/server.go:720 +0x8c

I can not find import docs, so I referenced the docs of aws. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule#import

zhongyuanzyh commented 3 months ago

still cannot import sgr provider: required_providers { alicloud = { source = "aliyun/alicloud" version = "~> 1.129" } }

command: terraform import alicloud_security_group_rule.rule1 sgr-wz94ntdd0kr6ns3m6hgy

error stack: │ Error: Plugin did not respond │ │ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more details. ╵

Stack trace from the terraform-provider-alicloud_v1.224.0 plugin:

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

goroutine 88 [running]: github.com/aliyun/terraform-provider-alicloud/alicloud.resourceAliyunSecurityGroupRuleRead(0x14003156770, {0x10b8dcda0?, 0x140006cc000?}) github.com/aliyun/terraform-provider-alicloud/alicloud/resource_alicloud_security_group_rule.go:300 +0x878 github.com/hashicorp/terraform-plugin-sdk/helper/schema.(Resource).RefreshWithoutUpgrade(0x1400110dc20, 0x14003140aa0, {0x10b8dcda0, 0x140006cc000}) github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/resource.go:470 +0x1a8 github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(GRPCProviderServer).ReadResource(0x14000144e40, {0x140014dc5a0?, 0x14003152680?}, 0x140014dc5a0) github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/helper/plugin/grpc_provider.go:535 +0x338 github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0x10b0de3c0?, 0x14000144e40}, {0x10b953910, 0x1400316b350}, 0x14003152680, 0x0) github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/tfplugin5/tfplugin5.pb.go:3269 +0x170 google.golang.org/grpc.(Server).processUnaryRPC(0x140006f6800, {0x10b953910, 0x1400316b2c0}, {0x10b9a9340, 0x14000d1a680}, 0x14002cff9e0, 0x14000e78c90, 0x11276d630, 0x0) google.golang.org/grpc@v1.62.1/server.go:1386 +0xc84 google.golang.org/grpc.(Server).handleStream(0x140006f6800, {0x10b9a9340, 0x14000d1a680}, 0x14002cff9e0) google.golang.org/grpc@v1.62.1/server.go:1797 +0xcc0 google.golang.org/grpc.(Server).serveStreams.func2.1() google.golang.org/grpc@v1.62.1/server.go:1027 +0x8c created by google.golang.org/grpc.(Server).serveStreams.func2 google.golang.org/grpc@v1.62.1/server.go:1038 +0x160

Error: The terraform-provider-alicloud_v1.224.0 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.

svg1007 commented 1 month ago

I had the same problem but then found out that a security group rule ID has the format <security_group_id>:<type>:<ip_protocol>:<port_range>:<nic_type>:<cidr_ip>:<policy>:<priority> according to this https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule#attributes-reference

So to import a security group rule into a terraform state the import command would be like the following one terraform import 'alicloud_security_group_rule.this["10.0.0.0/8"]' 'sg-1whsjcurngkeirir2qwe:ingress:all:-1/-1:internet:10.0.0.0/8:accept:1'