Skip to content

Commit 1c2f257

Browse files
authored
fix(cvm): [126528357] tencentcloud_instance optmize code logic for modify hostname (#3495)
* add * add
1 parent 38e62d2 commit 1c2f257

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

.changelog/3495.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_instance: optmize code logic for modify `hostname`
3+
```

tencentcloud/services/cvm/resource_tc_instance.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func ResourceTencentCloudInstance() *schema.Resource {
8181
Type: schema.TypeString,
8282
Optional: true,
8383
Computed: true,
84-
Description: "The hostname of the instance. Windows instance: The name should be a combination of 2 to 15 characters comprised of letters (case insensitive), numbers, and hyphens (-). Period (.) is not supported, and the name cannot be a string of pure numbers. Other types (such as Linux) of instances: The name should be a combination of 2 to 60 characters, supporting multiple periods (.). The piece between two periods is composed of letters (case insensitive), numbers, and hyphens (-). Modifications may lead to the reinstallation of the instance's operating system.",
84+
Description: "The hostname of the instance. Windows instance: The name should be a combination of 2 to 15 characters comprised of letters (case insensitive), numbers, and hyphens (-). Period (.) is not supported, and the name cannot be a string of pure numbers. Other types (such as Linux) of instances: The name should be a combination of 2 to 60 characters, supporting multiple periods (.). The piece between two periods is composed of letters (case insensitive), numbers, and hyphens (-). Changing the `hostname` will cause the instance system to restart.",
8585
},
8686
"project_id": {
8787
Type: schema.TypeInt,
@@ -1825,6 +1825,13 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
18251825
}
18261826
}
18271827

1828+
if d.HasChange("hostname") {
1829+
err := cvmService.ModifyHostName(ctx, instanceId, d.Get("hostname").(string))
1830+
if err != nil {
1831+
return err
1832+
}
1833+
}
1834+
18281835
if d.HasChange("disable_api_termination") {
18291836
err := cvmService.ModifyDisableApiTermination(ctx, instanceId, d.Get("disable_api_termination").(bool))
18301837
if err != nil {
@@ -1876,7 +1883,6 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
18761883
// Reset Instance
18771884
// Keep Login Info
18781885
if d.HasChange("image_id") ||
1879-
d.HasChange("hostname") ||
18801886
d.HasChange("disable_security_service") ||
18811887
d.HasChange("disable_monitor_service") ||
18821888
d.HasChange("disable_automation_service") ||
@@ -1889,10 +1895,6 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
18891895
request.ImageId = helper.String(v.(string))
18901896
}
18911897

1892-
if v, ok := d.GetOk("hostname"); ok {
1893-
request.HostName = helper.String(v.(string))
1894-
}
1895-
18961898
// enhanced service
18971899
var (
18981900
enhancedService cvm.EnhancedService

tencentcloud/services/cvm/service_tencentcloud_cvm.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ func (me *CvmService) ModifyInstanceName(ctx context.Context, instanceId, instan
237237
return nil
238238
}
239239

240+
func (me *CvmService) ModifyHostName(ctx context.Context, instanceId, hostname string) error {
241+
logId := tccommon.GetLogId(ctx)
242+
request := cvm.NewModifyInstancesAttributeRequest()
243+
request.InstanceIds = []*string{&instanceId}
244+
request.HostName = &hostname
245+
246+
ratelimit.Check(request.GetAction())
247+
response, err := me.client.UseCvmClient().ModifyInstancesAttribute(request)
248+
if err != nil {
249+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
250+
logId, request.GetAction(), request.ToJsonString(), err.Error())
251+
return err
252+
}
253+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
254+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
255+
256+
return nil
257+
}
258+
240259
func (me *CvmService) ModifyUserData(ctx context.Context, instanceId, userData string) error {
241260
logId := tccommon.GetLogId(ctx)
242261
request := cvm.NewModifyInstancesAttributeRequest()

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ The following arguments are supported:
309309
* `disable_security_service` - (Optional, Bool) Disable enhance service for security, it is enabled by default. When this options is set, security agent won't be installed. Modifications may lead to the reinstallation of the instance's operating system.
310310
* `force_delete` - (Optional, Bool) Indicate whether to force delete the instance. Default is `false`. If set true, the instance will be permanently deleted instead of being moved into the recycle bin. Note: only works for `PREPAID` instance.
311311
* `force_replace_placement_group_id` - (Optional, Bool) Whether to force the instance host to be replaced. Value range: true: Allows the instance to change the host and restart the instance. Local disk machines do not support specifying this parameter; false: Does not allow the instance to change the host and only join the placement group on the current host. This may cause the placement group to fail to change. Only useful for change `placement_group_id`, Default is false.
312-
* `hostname` - (Optional, String) The hostname of the instance. Windows instance: The name should be a combination of 2 to 15 characters comprised of letters (case insensitive), numbers, and hyphens (-). Period (.) is not supported, and the name cannot be a string of pure numbers. Other types (such as Linux) of instances: The name should be a combination of 2 to 60 characters, supporting multiple periods (.). The piece between two periods is composed of letters (case insensitive), numbers, and hyphens (-). Modifications may lead to the reinstallation of the instance's operating system.
312+
* `hostname` - (Optional, String) The hostname of the instance. Windows instance: The name should be a combination of 2 to 15 characters comprised of letters (case insensitive), numbers, and hyphens (-). Period (.) is not supported, and the name cannot be a string of pure numbers. Other types (such as Linux) of instances: The name should be a combination of 2 to 60 characters, supporting multiple periods (.). The piece between two periods is composed of letters (case insensitive), numbers, and hyphens (-). Changing the `hostname` will cause the instance system to restart.
313313
* `hpc_cluster_id` - (Optional, String, ForceNew) High-performance computing cluster ID. If the instance created is a high-performance computing instance, you need to specify the cluster in which the instance is placed, otherwise it cannot be specified.
314314
* `image_id` - (Optional, String) The image to use for the instance. Modifications may lead to the reinstallation of the instance's operating system.
315315
* `instance_charge_type_prepaid_period` - (Optional, Int) The tenancy (time unit is month) of the prepaid instance, NOTE: it only works when instance_charge_type is set to `PREPAID`. Valid values are `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`, `48`, `60`.

0 commit comments

Comments
 (0)