Skip to content

Commit 339fcba

Browse files
authored
fix: compose import with default value func (#1278)
1 parent 007f4bc commit 339fcba

9 files changed

+66
-45
lines changed

tencentcloud/internal/helper/helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,12 @@ func InterfaceToMap(d map[string]interface{}, key string) (result map[string]int
164164
}
165165
return nil, false
166166
}
167+
168+
func ImportWithDefaultValue(defaultValues map[string]interface{}) schema.StateFunc {
169+
return func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
170+
for k, v := range defaultValues {
171+
_ = d.Set(k, v)
172+
}
173+
return []*schema.ResourceData{d}, nil
174+
}
175+
}

tencentcloud/resource_tc_cam_user.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ func resourceTencentCloudCamUser() *schema.Resource {
5252
Update: resourceTencentCloudCamUserUpdate,
5353
Delete: resourceTencentCloudCamUserDelete,
5454
Importer: &schema.ResourceImporter{
55-
State: schema.ImportStatePassthrough,
55+
State: helper.ImportWithDefaultValue(map[string]interface{}{
56+
"remark": "",
57+
"force_delete": false,
58+
"use_api": true,
59+
"console_login": false,
60+
"need_reset_password": true,
61+
}),
5662
},
5763

5864
Schema: map[string]*schema.Schema{

tencentcloud/resource_tc_cam_user_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ func TestAccTencentCloudCamUser_basic(t *testing.T) {
7878
resource.TestCheckResourceAttrSet("tencentcloud_cam_user.user_basic", "secret_key"),
7979
resource.TestCheckResourceAttrSet("tencentcloud_cam_user.user_basic", "secret_id"),
8080
),
81-
}, {
81+
},
82+
{
83+
ResourceName: "tencentcloud_cam_user.user_basic",
84+
ImportState: true,
85+
ImportStateVerify: true,
86+
ImportStateVerifyIgnore: []string{"secret_key", "secret_id", "password"},
87+
},
88+
{
8289
Config: testAccCamUser_update,
8390
Check: resource.ComposeTestCheckFunc(
8491
testAccCheckCamUserExists("tencentcloud_cam_user.user_basic"),
@@ -96,11 +103,6 @@ func TestAccTencentCloudCamUser_basic(t *testing.T) {
96103
resource.TestCheckResourceAttrSet("tencentcloud_cam_user.user_basic", "secret_key"),
97104
resource.TestCheckResourceAttrSet("tencentcloud_cam_user.user_basic", "secret_id"),
98105
),
99-
}, {
100-
ResourceName: "tencentcloud_cam_user.user_basic",
101-
ImportState: true,
102-
ImportStateVerify: true,
103-
ImportStateVerifyIgnore: []string{"secret_key", "secret_id", "password", "need_reset_password", "use_api", "force_delete"},
104106
},
105107
},
106108
})
@@ -214,7 +216,7 @@ resource "tencentcloud_cam_user" "user_basic" {
214216
phone_num = "12345678910"
215217
country_code = "86"
216218
217-
force_delete = true
219+
force_delete = false
218220
}
219221
`
220222

tencentcloud/resource_tc_cdn_domain.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,20 @@ func resourceTencentCloudCdnDomain() *schema.Resource {
156156
Update: resourceTencentCloudCdnDomainUpdate,
157157
Delete: resourceTencentCloudCdnDomainDelete,
158158
Importer: &schema.ResourceImporter{
159-
State: func(d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
160-
getDefaultSwitchOffMap := func() []interface{} {
161-
return []interface{}{
162-
map[string]interface{}{"switch": "off"},
163-
}
164-
}
165-
_ = d.Set("authentication", getDefaultSwitchOffMap())
166-
return []*schema.ResourceData{d}, nil
167-
},
159+
//State: func(d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
160+
// getDefaultSwitchOffMap := func() []interface{} {
161+
// return []interface{}{
162+
// map[string]interface{}{"switch": "off"},
163+
// }
164+
// }
165+
// _ = d.Set("authentication", getDefaultSwitchOffMap())
166+
// return []*schema.ResourceData{d}, nil
167+
//},
168+
State: helper.ImportWithDefaultValue(map[string]interface{}{
169+
"authentication": []interface{}{map[string]interface{}{
170+
"switch": "off",
171+
}},
172+
}),
168173
},
169174

170175
Schema: map[string]*schema.Schema{

tencentcloud/resource_tc_clb_listener.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ func resourceTencentCloudClbListener() *schema.Resource {
166166
Update: resourceTencentCloudClbListenerUpdate,
167167
Delete: resourceTencentCloudClbListenerDelete,
168168
Importer: &schema.ResourceImporter{
169-
State: schema.ImportStatePassthrough,
169+
State: helper.ImportWithDefaultValue(map[string]interface{}{
170+
"scheduler": CLB_LISTENER_SCHEDULER_WRR,
171+
}),
170172
},
171-
172173
Schema: map[string]*schema.Schema{
173174
"clb_id": {
174175
Type: schema.TypeString,

tencentcloud/resource_tc_clb_listener_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,9 @@ func TestAccTencentCloudClbListener_https(t *testing.T) {
300300
),
301301
},
302302
{
303-
ResourceName: "tencentcloud_clb_listener.listener_https",
304-
ImportState: true,
305-
ImportStateVerify: true,
306-
ImportStateVerifyIgnore: []string{"scheduler"},
303+
ResourceName: "tencentcloud_clb_listener.listener_https",
304+
ImportState: true,
305+
ImportStateVerify: true,
307306
},
308307
},
309308
})

tencentcloud/resource_tc_mysql_instance.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,18 @@ func resourceTencentCloudMysqlInstance() *schema.Resource {
329329
Delete: resourceTencentCloudMysqlInstanceDelete,
330330
Schema: specialInfo,
331331
Importer: &schema.ResourceImporter{
332-
State: func(d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
333-
_ = d.Set("charge_type", MYSQL_CHARGE_TYPE_POSTPAID)
334-
_ = d.Set("prepaid_period", 1)
335-
_ = d.Set("auto_renew_flag", 0)
336-
_ = d.Set("intranet_port", 3306)
337-
_ = d.Set("force_delete", false)
338-
_ = d.Set("internet_service", 0)
339-
_ = d.Set("engine_version", MYSQL_SUPPORTS_ENGINE[len(MYSQL_SUPPORTS_ENGINE)-2])
340-
_ = d.Set("slave_deploy_mode", 0)
341-
_ = d.Set("slave_sync_mode", 0)
342-
_ = d.Set("project_id", 0)
343-
return []*schema.ResourceData{d}, nil
344-
},
332+
State: helper.ImportWithDefaultValue(map[string]interface{}{
333+
"charge_type": MYSQL_CHARGE_TYPE_POSTPAID,
334+
"prepaid_period": 1,
335+
"auto_renew_flag": 0,
336+
"intranet_port": 3306,
337+
"force_delete": false,
338+
"internet_service": 0,
339+
"engine_version": MYSQL_SUPPORTS_ENGINE[len(MYSQL_SUPPORTS_ENGINE)-2],
340+
"slave_deploy_mode": 0,
341+
"slave_sync_mode": 0,
342+
"project_id": 0,
343+
}),
345344
},
346345
}
347346
}

tencentcloud/resource_tc_sqlserver_instance.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,9 @@ func resourceTencentCloudSqlserverInstance() *schema.Resource {
213213
Update: resourceTencentCloudSqlserverInstanceUpdate,
214214
Delete: resourceTencentCLoudSqlserverInstanceDelete,
215215
Importer: &schema.ResourceImporter{
216-
State: func(d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
217-
_ = d.Set("auto_voucher", 0)
218-
return []*schema.ResourceData{d}, nil
219-
},
216+
State: helper.ImportWithDefaultValue(map[string]interface{}{
217+
"auto_voucher": 0,
218+
}),
220219
},
221220
Schema: specialInfo,
222221
}

tencentcloud/resource_tc_tcr_vpc_attachment.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"fmt"
2727
"strings"
2828

29+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
30+
2931
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
3032
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
3133
tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924"
@@ -38,11 +40,10 @@ func resourceTencentCloudTcrVpcAttachment() *schema.Resource {
3840
Update: resourceTencentCloudTcrVpcAttachmentUpdate,
3941
Delete: resourceTencentCLoudTcrVpcAttachmentDelete,
4042
Importer: &schema.ResourceImporter{
41-
State: func(d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
42-
_ = d.Set("enable_public_domain_dns", false)
43-
_ = d.Set("enable_vpc_domain_dns", false)
44-
return []*schema.ResourceData{d}, nil
45-
},
43+
State: helper.ImportWithDefaultValue(map[string]interface{}{
44+
"enable_public_domain_dns": false,
45+
"enable_vpc_domain_dns": false,
46+
}),
4647
},
4748

4849
Schema: map[string]*schema.Schema{

0 commit comments

Comments
 (0)