我正在使用 Go 构建一个 Terraform 插件/提供程序。我的架构(链接)中有以下内容:
Importer: &schema.ResourceImporter{
State: resourceKubernetesClusterNodePoolImport,
}
这是功能(链接):
func resourceKubernetesClusterNodePoolImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
apiClient := m.(*civogo.Client)
clusterID, nodePoolID, err := utils.ResourceCommonParseID(d.Id())
if err != nil {
return nil, err
}
log.Printf("[INFO] retriving the node pool %s", nodePoolID)
resp, err := apiClient.GetKubernetesCluster(clusterID)
if err != nil {
if resp != nil {
return nil, err
}
}
d.Set("cluster_id", resp.ID)
for _, v := range resp.Pools {
if v.ID == nodePoolID {
d.Set("num_target_nodes", v.Count)
d.Set("target_nodes_size", v.Size)
}
}
return []*schema.ResourceData{d}, nil
}
当我在上面的函数顶部添加这些调试行时,结果 (reg
和ok
) 是空的并且false
:
reg, ok := d.GetOk("region")
log.Printf("resourceKubernetesClusterNodePoolImport reg %+v\n", reg)
log.Println("resourceKubernetesClusterNodePoolImport ok", ok)
log.Println("resourceKubernetesClusterNodePoolImport d.Get()", d.Get("region"))
if !ok {
return nil, fmt.Errorf("[ERR] Please provide a region in your import configuration")
}
痕迹:
$ echo $TF_LOG
INFO
$ tf import civo_kubernetes_node_pool.my_pool 1b272dc1-836c-4ea7-8f20-4569f20ddbdd:d0c22622-54b3-41c2-9eae-24591737d5ad
2021-08-12T11:22:30.552+0800 [INFO] Terraform version: 1.0.3
2021-08-12T11:22:30.552+0800 [INFO] Go runtime version: go1.16.4
2021-08-12T11:22:30.552+0800 [INFO] CLI args: []string{"/usr/local/bin/terraform", "import", "civo_kubernetes_node_pool.my_pool", "1b272dc1-836c-4ea7-8f20-4569f20ddbdd:d0c22622-54b3-41c2-9eae-24591737d5ad"}
2021-08-12T11:22:30.552+0800 [INFO] Loading CLI configuration from /Users/zulh/.terraformrc
2021-08-12T11:22:30.553+0800 [INFO] CLI command args: []string{"import", "civo_kubernetes_node_pool.my_pool", "1b272dc1-836c-4ea7-8f20-4569f20ddbdd:d0c22622-54b3-41c2-9eae-24591737d5ad"}
2021-08-12T11:22:30.629+0800 [INFO] Failed to read plugin lock file .terraform/plugins/darwin_amd64/lock.json: open .terraform/plugins/darwin_amd64/lock.json: no such file or directory
2021-08-12T11:22:30.631+0800 [INFO] provider: configuring client automatic mTLS
2021-08-12T11:22:31.284+0800 [INFO] provider.terraform-provider-civo_v99.0.0: configuring server automatic mTLS: timestamp=2021-08-12T11:22:31.283+0800
2021-08-12T11:22:31.358+0800 [INFO] provider: configuring client automatic mTLS
2021-08-12T11:22:31.404+0800 [INFO] provider.terraform-provider-civo_v99.0.0: configuring server automatic mTLS: timestamp=2021-08-12T11:22:31.404+0800
2021-08-12T11:22:31.478+0800 [WARN] ValidateProviderConfig from "provider[\"registry.terraform.io/civo/civo\"]" changed the config value, but that value is unused
civo_kubernetes_node_pool.my_pool: Importing from ID "1b272dc1-836c-4ea7-8f20-4569f20ddbdd:d0c22622-54b3-41c2-9eae-24591737d5ad"...
2021-08-12T11:22:31.479+0800 [INFO] provider.terraform-provider-civo_v99.0.0: 2021/08/12 11:22:31 resourceKubernetesClusterNodePoolImport reg: timestamp=2021-08-12T11:22:31.479+0800 <-- this line
2021-08-12T11:22:31.479+0800 [INFO] provider.terraform-provider-civo_v99.0.0: 2021/08/12 11:22:31 resourceKubernetesClusterNodePoolImport ok false: timestamp=2021-08-12T11:22:31.479+0800 <-- this line
2021-08-12T11:22:31.479+0800 [INFO] provider.terraform-provider-civo_v99.0.0: 2021/08/12 11:22:31 resourceKubernetesClusterNodePoolImport d.Get(): timestamp=2021-08-12T11:22:31.479+0800 <-- this line
╷
│ Error: [ERR] Please provide a region in your import configuration
│
│
╵
问题是,在我运行上面的命令之前,我已经region
在我的 Terraform 配置main.tf文件中添加了(并保存了它) 。tf import
这是我的 main.tf 文件:
resource "civo_kubernetes_cluster" "my_cluster" {
name = "my-cluster"
}
resource "civo_kubernetes_node_pool" "my_pool" {
region = "LON1" <-- this line
}
为什么我的 Terraform 插件/提供程序将该区域视为空的?
据我了解,Terraform 在运行导入逻辑之前会先读取配置文件。我错了吗?我在这里错过了什么吗?
为了完整起见,我使用的是 Terraform v1.0.3,这是我的provider.tf文件...
terraform {
required_providers {
civo = {
source = "civo/civo"
version = "99.0.0"
}
}
}
provider "civo" {
token = “my-api-key-here”
}
...99.0.0
我仅为测试而构建的版本在哪里。当我发布此问题时,此提供程序的最新/产品版本是0.10.9 。