From 5e3b15f2c63a722320234e4f67e7ab11e4714e45 Mon Sep 17 00:00:00 2001 From: gailwang Date: Fri, 9 Oct 2020 21:44:42 +0800 Subject: [PATCH 1/5] add keep_image_login with resource CVM --- CHANGELOG.md | 5 +++ tencentcloud/extension_cvm.go | 3 ++ tencentcloud/resource_tc_instance.go | 18 ++++++++++ tencentcloud/resource_tc_instance_test.go | 41 +++++++++++++++++++++++ website/docs/r/instance.html.markdown | 1 + 5 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 860bc85312..a22757831d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ ## 1.44.1 (Unreleased) + +ENHANCEMENTS: + +* Resource: `tencentcloud_instance` add new argument `keep_image_login` to support keeping image login. + ## 1.44.0 (September 25, 2020) FEATURES: diff --git a/tencentcloud/extension_cvm.go b/tencentcloud/extension_cvm.go index a94e9b015b..4763da6ecf 100644 --- a/tencentcloud/extension_cvm.go +++ b/tencentcloud/extension_cvm.go @@ -44,6 +44,9 @@ const ( CVM_SPOT_INSTANCE_TYPE_ONE_TIME = "ONE-TIME" CVM_MARKET_TYPE_SPOT = "spot" + + CVM_IMAGE_LOGIN = "TRUE" + CVM_IMAGE_LOGIN_NOT = "FALSE" ) var CVM_CHARGE_TYPE = []string{ diff --git a/tencentcloud/resource_tc_instance.go b/tencentcloud/resource_tc_instance.go index 8dd3b894e3..6e186dee52 100644 --- a/tencentcloud/resource_tc_instance.go +++ b/tencentcloud/resource_tc_instance.go @@ -338,6 +338,14 @@ func resourceTencentCloudInstance() *schema.Resource { Sensitive: true, Description: "Password to an instance. In order to take effect new password, the instance will be restarted after modifying the password.", }, + "keep_image_login": { + Type: schema.TypeBool, + Optional: true, + Default: false, + ForceNew: true, + ConflictsWith: []string{"key_name", "password"}, + Description: "Whether to keep image login or not, default is `false`. It can be set `true` with private images, shared images and imported images.", + }, "user_data": { Type: schema.TypeString, Optional: true, @@ -554,6 +562,13 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} if v, ok := d.GetOk("password"); ok { request.LoginSettings.Password = helper.String(v.(string)) } + if v, ok := d.GetOkExists("keep_image_login"); ok { + if v.(bool) { + request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN) + } else { + request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN_NOT) + } + } if v, ok := d.GetOk("user_data"); ok { request.UserData = helper.String(v.(string)) @@ -778,6 +793,9 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{}) } else { _ = d.Set("key_name", "") } + if instance.LoginSettings.KeepImageLogin != nil { + _ = d.Set("keep_image_login", *instance.LoginSettings.KeepImageLogin == CVM_IMAGE_LOGIN) + } if *instance.InstanceState == CVM_STATUS_STOPPED { _ = d.Set("running_flag", false) } else { diff --git a/tencentcloud/resource_tc_instance_test.go b/tencentcloud/resource_tc_instance_test.go index ed662beace..1989e2c9ff 100644 --- a/tencentcloud/resource_tc_instance_test.go +++ b/tencentcloud/resource_tc_instance_test.go @@ -241,6 +241,29 @@ func TestAccTencentCloudInstanceWithPassword(t *testing.T) { }) } +func TestAccTencentCloudInstanceWithImageLogin(t *testing.T) { + t.Parallel() + + id := "tencentcloud_instance.foo" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: id, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccTencentCloudInstanceWithImageLogin(), + Check: resource.ComposeTestCheckFunc( + testAccCheckTencentCloudDataSourceID(id), + testAccCheckTencentCloudInstanceExists(id), + resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"), + resource.TestCheckResourceAttr(id, "keep_image_login", "true"), + ), + }, + }, + }) +} + func TestAccTencentCloudInstanceWithName(t *testing.T) { t.Parallel() @@ -659,6 +682,24 @@ resource "tencentcloud_instance" "foo" { ) } +func testAccTencentCloudInstanceWithImageLogin() string { + return defaultInstanceVariable + ` +data "tencentcloud_images" "zoo" { + image_type = ["PRIVATE_IMAGE"] + os_name = "centos" +} +resource "tencentcloud_instance" "foo" { + instance_name = var.instance_name + availability_zone = data.tencentcloud_availability_zones.default.zones.0.name + image_id = data.tencentcloud_images.zoo.images.0.image_id + instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type + internet_max_bandwidth_out = 1 + keep_image_login = true + system_disk_type = "CLOUD_PREMIUM" +} +` +} + func testAccTencentCloudInstanceWithName(instanceName string) string { return fmt.Sprintf( defaultInstanceVariable+` diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 7dbbf77d38..9a3cc6ecd0 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -95,6 +95,7 @@ The following arguments are supported: * `instance_type` - (Optional) The type of instance to start. * `internet_charge_type` - (Optional, ForceNew) Internet charge type of the instance, Valid values are `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`. This value does not need to be set when `allocate_public_ip` is false. * `internet_max_bandwidth_out` - (Optional) Maximum outgoing bandwidth to the public network, measured in Mbps (Mega bit per second). This value does not need to be set when `allocate_public_ip` is false. +* `keep_image_login` - (Optional, ForceNew) Whether to keep image login or not, default is `false`. It can be set `true` with private images, shared images and imported images. * `key_name` - (Optional) The key pair to use for the instance, it looks like skey-16jig7tx. * `password` - (Optional) Password to an instance. In order to take effect new password, the instance will be restarted after modifying the password. * `placement_group_id` - (Optional, ForceNew) The id of a placement group. From 7cf3fd0d48749c114542162ef0e20e3d747a0759 Mon Sep 17 00:00:00 2001 From: gailwang Date: Sat, 10 Oct 2020 16:23:50 +0800 Subject: [PATCH 2/5] fix bugs --- tencentcloud/resource_tc_instance.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tencentcloud/resource_tc_instance.go b/tencentcloud/resource_tc_instance.go index 6e186dee52..407d287367 100644 --- a/tencentcloud/resource_tc_instance.go +++ b/tencentcloud/resource_tc_instance.go @@ -562,12 +562,11 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} if v, ok := d.GetOk("password"); ok { request.LoginSettings.Password = helper.String(v.(string)) } - if v, ok := d.GetOkExists("keep_image_login"); ok { - if v.(bool) { - request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN) - } else { - request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN_NOT) - } + v := d.Get("keep_image_login").(bool) + if v { + request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN) + } else { + request.LoginSettings.KeepImageLogin = helper.String(CVM_IMAGE_LOGIN_NOT) } if v, ok := d.GetOk("user_data"); ok { From 3dca01ef667eeb4d2c8cf2e58b70d2d3536c7a10 Mon Sep 17 00:00:00 2001 From: gailwang Date: Wed, 14 Oct 2020 12:21:03 +0800 Subject: [PATCH 3/5] make doc again --- website/docs/r/instance.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 6cd7f8ea40..06a58504e1 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -95,6 +95,7 @@ The following arguments are supported: * `instance_type` - (Optional) The type of the instance. * `internet_charge_type` - (Optional, ForceNew) Internet charge type of the instance, Valid values are `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`. This value does not need to be set when `allocate_public_ip` is false. * `internet_max_bandwidth_out` - (Optional) Maximum outgoing bandwidth to the public network, measured in Mbps (Mega bits per second). This value does not need to be set when `allocate_public_ip` is false. +* `keep_image_login` - (Optional, ForceNew) Whether to keep image login or not, default is `false`. It can be set `true` with private images, shared images and imported images. * `key_name` - (Optional) The key pair to use for the instance, it looks like `skey-16jig7tx`. * `password` - (Optional) Password for the instance. In order for the new password to take effect, the instance will be restarted after the password change. * `placement_group_id` - (Optional, ForceNew) The ID of a placement group. From 03e9b7497d52f45f8d56701454c342a43e48e17e Mon Sep 17 00:00:00 2001 From: gailwang Date: Wed, 14 Oct 2020 16:20:21 +0800 Subject: [PATCH 4/5] beautify code --- tencentcloud/resource_tc_instance_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tencentcloud/resource_tc_instance_test.go b/tencentcloud/resource_tc_instance_test.go index 1989e2c9ff..30cb9df1a7 100644 --- a/tencentcloud/resource_tc_instance_test.go +++ b/tencentcloud/resource_tc_instance_test.go @@ -252,7 +252,7 @@ func TestAccTencentCloudInstanceWithImageLogin(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccTencentCloudInstanceWithImageLogin(), + Config: testAccTencentCloudInstanceWithImageLogin, Check: resource.ComposeTestCheckFunc( testAccCheckTencentCloudDataSourceID(id), testAccCheckTencentCloudInstanceExists(id), @@ -682,8 +682,7 @@ resource "tencentcloud_instance" "foo" { ) } -func testAccTencentCloudInstanceWithImageLogin() string { - return defaultInstanceVariable + ` +const testAccTencentCloudInstanceWithImageLogin = defaultInstanceVariable + ` data "tencentcloud_images" "zoo" { image_type = ["PRIVATE_IMAGE"] os_name = "centos" @@ -698,7 +697,6 @@ resource "tencentcloud_instance" "foo" { system_disk_type = "CLOUD_PREMIUM" } ` -} func testAccTencentCloudInstanceWithName(instanceName string) string { return fmt.Sprintf( From cf2a63867c756f18c904eb2b00f9c46cde0b9bab Mon Sep 17 00:00:00 2001 From: gailwang Date: Wed, 14 Oct 2020 16:54:46 +0800 Subject: [PATCH 5/5] fix description --- tencentcloud/resource_tc_instance.go | 2 +- website/docs/r/instance.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tencentcloud/resource_tc_instance.go b/tencentcloud/resource_tc_instance.go index 27dc97c98c..953614752c 100644 --- a/tencentcloud/resource_tc_instance.go +++ b/tencentcloud/resource_tc_instance.go @@ -344,7 +344,7 @@ func resourceTencentCloudInstance() *schema.Resource { Default: false, ForceNew: true, ConflictsWith: []string{"key_name", "password"}, - Description: "Whether to keep image login or not, default is `false`. It can be set `true` with private images, shared images and imported images.", + Description: "Whether to keep image login or not, default is `false`. When the image type is private or shared or imported, this parameter can be set `true`.", }, "user_data": { Type: schema.TypeString, diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 06a58504e1..e2ab0cce44 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -95,7 +95,7 @@ The following arguments are supported: * `instance_type` - (Optional) The type of the instance. * `internet_charge_type` - (Optional, ForceNew) Internet charge type of the instance, Valid values are `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`. This value does not need to be set when `allocate_public_ip` is false. * `internet_max_bandwidth_out` - (Optional) Maximum outgoing bandwidth to the public network, measured in Mbps (Mega bits per second). This value does not need to be set when `allocate_public_ip` is false. -* `keep_image_login` - (Optional, ForceNew) Whether to keep image login or not, default is `false`. It can be set `true` with private images, shared images and imported images. +* `keep_image_login` - (Optional, ForceNew) Whether to keep image login or not, default is `false`. When the image type is private or shared or imported, this parameter can be set `true`. * `key_name` - (Optional) The key pair to use for the instance, it looks like `skey-16jig7tx`. * `password` - (Optional) Password for the instance. In order for the new password to take effect, the instance will be restarted after the password change. * `placement_group_id` - (Optional, ForceNew) The ID of a placement group.