From 642dec8289d6bb5bc186bcb6f6e0c5f2072de789 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Mon, 2 Dec 2024 14:43:58 +0200 Subject: [PATCH] fix(cvm): Fix optional parameters never being omitted from requests Initially noticed this problem when I tried to create a launch template containing the following block: ``` internet_accessible { public_ip_assigned = true internet_max_bandwidth_out = 100 } ``` This resulted in an error during apply like this: ``` Code=InvalidParameterValue, Message=The value `` specified in the parameter `.InternetAccessible.InternetChargeType` is invalid. ``` I figured okay, I'll just add what should be the defaults and get on with it. But ultimately I got the same error from the `BandwidthPackageId` field. There is no good default value for it, so I was unable to make the launch template. Turns out that throughout the SDK, all scalar fields are always included, at least wherever `InterfacesHeadMap` is used. The value of the individual fields is never checked, so every field ends up being sent, with the default value being not the default value defined in the schema, but the default value for the data type, which is definitely wrong. Since the default value for strings is an empty string, any field inside a block that takes an ID parameter of some kind will fail with an API validation error. In this commit I've only fixed the `internet_accessible` block for launch templates, but ultimately all locations where `InterfacesHeadMap` (and possibly others, I haven't investigated that much) need to be updated. FWIW, the AWS Terraform provider uses this very same way of dealing with this issue. --- .../cvm/resource_tc_cvm_launch_template.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tencentcloud/services/cvm/resource_tc_cvm_launch_template.go b/tencentcloud/services/cvm/resource_tc_cvm_launch_template.go index e9faf3e4d4..ea48b016fe 100644 --- a/tencentcloud/services/cvm/resource_tc_cvm_launch_template.go +++ b/tencentcloud/services/cvm/resource_tc_cvm_launch_template.go @@ -738,17 +738,17 @@ func resourceTencentCloudCvmLaunchTemplateCreate(d *schema.ResourceData, meta in if dMap, ok := helper.InterfacesHeadMap(d, "internet_accessible"); ok { internetAccessible := cvm.InternetAccessible{} - if v, ok := dMap["internet_charge_type"]; ok { - internetAccessible.InternetChargeType = helper.String(v.(string)) + if v, ok := dMap["internet_charge_type"].(string); ok && v != "" { + internetAccessible.InternetChargeType = helper.String(v) } - if v, ok := dMap["internet_max_bandwidth_out"]; ok { - internetAccessible.InternetMaxBandwidthOut = helper.IntInt64(v.(int)) + if v, ok := dMap["internet_max_bandwidth_out"].(int); ok && v != 0 { + internetAccessible.InternetMaxBandwidthOut = helper.IntInt64(v) } - if v, ok := dMap["public_ip_assigned"]; ok { - internetAccessible.PublicIpAssigned = helper.Bool(v.(bool)) + if v, ok := dMap["public_ip_assigned"].(bool); ok && v { + internetAccessible.PublicIpAssigned = helper.Bool(v) } - if v, ok := dMap["bandwidth_package_id"]; ok { - internetAccessible.BandwidthPackageId = helper.String(v.(string)) + if v, ok := dMap["bandwidth_package_id"].(string); ok && v != "" { + internetAccessible.BandwidthPackageId = helper.String(v) } request.InternetAccessible = &internetAccessible }