diff --git a/tencentcloud/services/cvm/data_source_tc_eips.go b/tencentcloud/services/cvm/data_source_tc_eips.go
index 58057f6c3d..5879d45ed7 100644
--- a/tencentcloud/services/cvm/data_source_tc_eips.go
+++ b/tencentcloud/services/cvm/data_source_tc_eips.go
@@ -2,49 +2,24 @@ package cvm
import (
"context"
- "log"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
- svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
- svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudEips() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudEipsRead,
-
Schema: map[string]*schema.Schema{
"eip_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the EIP to be queried.",
},
- "eip_name": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Name of the EIP to be queried.",
- },
- "public_ip": {
- Type: schema.TypeString,
- Optional: true,
- Description: "The elastic ip address.",
- },
- "tags": {
- Type: schema.TypeMap,
- Optional: true,
- Description: "The tags of EIP.",
- },
- "result_output_file": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Used to save results.",
- },
"eip_list": {
Type: schema.TypeList,
@@ -52,6 +27,11 @@ func DataSourceTencentCloudEips() *schema.Resource {
Description: "An information list of EIP. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
+ "create_time": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Creation time of the EIP.",
+ },
"eip_id": {
Type: schema.TypeString,
Computed: true,
@@ -67,30 +47,25 @@ func DataSourceTencentCloudEips() *schema.Resource {
Computed: true,
Description: "Type of the EIP.",
},
- "status": {
- Type: schema.TypeString,
- Computed: true,
- Description: "The EIP current status.",
- },
- "public_ip": {
+ "eni_id": {
Type: schema.TypeString,
Computed: true,
- Description: "The elastic ip address.",
+ Description: "The eni id to bind with the EIP.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "The instance id to bind with the EIP.",
},
- "eni_id": {
+ "public_ip": {
Type: schema.TypeString,
Computed: true,
- Description: "The eni id to bind with the EIP.",
+ Description: "The elastic ip address.",
},
- "create_time": {
+ "status": {
Type: schema.TypeString,
Computed: true,
- Description: "Creation time of the EIP.",
+ Description: "The EIP current status.",
},
"tags": {
Type: schema.TypeMap,
@@ -100,91 +75,101 @@ func DataSourceTencentCloudEips() *schema.Resource {
},
},
},
+
+ "eip_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Name of the EIP to be queried.",
+ },
+
+ "public_ip": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "The elastic ip address.",
+ },
+
+ "tags": {
+ Type: schema.TypeMap,
+ Optional: true,
+ Description: "The tags of EIP.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudEipsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_eips.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
+ defer tccommon.InconsistentCheck(d, meta)()
+
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
- vpcService := svcvpc.NewVpcService(client)
- tagService := svctag.NewTagService(client)
- region := client.Region
+ service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
- filter := make(map[string][]string)
+ paramMap := make(map[string]interface{})
+ var filtersList []*vpc.Filter
+ filtersMap := map[string]*vpc.Filter{}
+ filter := vpc.Filter{}
+ name := "address-id"
+ filter.Name = &name
if v, ok := d.GetOk("eip_id"); ok {
- filter["address-id"] = []string{v.(string)}
+ filter.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp0"] = &filter
+ if v, ok := filtersMap["Temp0"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter2 := vpc.Filter{}
+ name2 := "address-name"
+ filter2.Name = &name2
if v, ok := d.GetOk("eip_name"); ok {
- filter["address-name"] = []string{v.(string)}
+ filter2.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp1"] = &filter2
+ if v, ok := filtersMap["Temp1"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ filter3 := vpc.Filter{}
+ name3 := "public-ip"
+ filter3.Name = &name3
if v, ok := d.GetOk("public_ip"); ok {
- filter["public-ip"] = []string{v.(string)}
+ filter3.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp2"] = &filter3
+ if v, ok := filtersMap["Temp2"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ paramMap["Filters"] = filtersList
- tags := helper.GetTags(d, "tags")
-
- var eips []*vpc.Address
- var errRet error
+ var respData *vpc.DescribeAddressesResponseParams
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- eips, errRet = vpcService.DescribeEipByFilter(ctx, filter)
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+ result, e := service.DescribeEipsByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- eipList := make([]map[string]interface{}, 0, len(eips))
- ids := make([]string, 0, len(eips))
-
-EIP_LOOP:
- for _, eip := range eips {
- respTags, err := tagService.DescribeResourceTags(ctx, svcvpc.VPC_SERVICE_TYPE, svcvpc.EIP_RESOURCE_TYPE, region, *eip.AddressId)
- if err != nil {
- log.Printf("[CRITAL]%s describe eip tags failed: %+v", logId, err)
- return err
- }
-
- for k, v := range tags {
- if respTags[k] != v {
- continue EIP_LOOP
- }
- }
-
- mapping := map[string]interface{}{
- "eip_id": eip.AddressId,
- "eip_name": eip.AddressName,
- "eip_type": eip.AddressType,
- "status": eip.AddressStatus,
- "public_ip": eip.AddressIp,
- "instance_id": eip.InstanceId,
- "eni_id": eip.NetworkInterfaceId,
- "create_time": eip.CreatedTime,
- "tags": respTags,
- }
-
- eipList = append(eipList, mapping)
- ids = append(ids, *eip.AddressId)
- }
-
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("eip_list", eipList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set eip list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudEipsReadPostHandleResponse0(ctx, paramMap, respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), eipList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudEipsReadOutputContent(ctx)); e != nil {
+ return e
}
}
+
return nil
}
diff --git a/tencentcloud/services/cvm/data_source_tc_eips_extension.go b/tencentcloud/services/cvm/data_source_tc_eips_extension.go
new file mode 100644
index 0000000000..522e73c259
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_eips_extension.go
@@ -0,0 +1,65 @@
+package cvm
+
+import (
+ "context"
+ "log"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+ svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
+ svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
+
+ vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
+)
+
+func dataSourceTencentCloudEipsReadOutputContent(ctx context.Context) interface{} {
+ eipList := ctx.Value("eipList")
+ return eipList
+}
+
+func dataSourceTencentCloudEipsReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *vpc.DescribeAddressesResponseParams) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ meta := tccommon.ProviderMetaFromContext(ctx)
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
+ tagService := svctag.NewTagService(client)
+ region := client.Region
+
+ tags := helper.GetTags(d, "tags")
+ eipList := make([]map[string]interface{}, 0, len(resp.AddressSet))
+ ids := make([]string, 0, len(resp.AddressSet))
+
+EIP_LOOP:
+ for _, eip := range resp.AddressSet {
+ respTags, err := tagService.DescribeResourceTags(ctx, svcvpc.VPC_SERVICE_TYPE, svcvpc.EIP_RESOURCE_TYPE, region, *eip.AddressId)
+ if err != nil {
+ log.Printf("[CRITAL]%s describe eip tags failed: %+v", logId, err)
+ return err
+ }
+
+ for k, v := range tags {
+ if respTags[k] != v {
+ continue EIP_LOOP
+ }
+ }
+
+ mapping := map[string]interface{}{
+ "eip_id": eip.AddressId,
+ "eip_name": eip.AddressName,
+ "eip_type": eip.AddressType,
+ "status": eip.AddressStatus,
+ "public_ip": eip.AddressIp,
+ "instance_id": eip.InstanceId,
+ "eni_id": eip.NetworkInterfaceId,
+ "create_time": eip.CreatedTime,
+ "tags": respTags,
+ }
+
+ eipList = append(eipList, mapping)
+ ids = append(ids, *eip.AddressId)
+ }
+
+ context.WithValue(ctx, "eipList", eipList)
+ d.SetId(helper.DataResourceIdsHash(ids))
+ return nil
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_image.go b/tencentcloud/services/cvm/data_source_tc_image.go
index 807b6ca342..00527b2729 100644
--- a/tencentcloud/services/cvm/data_source_tc_image.go
+++ b/tencentcloud/services/cvm/data_source_tc_image.go
@@ -2,24 +2,18 @@ package cvm
import (
"context"
- "errors"
- "fmt"
- "regexp"
- "strings"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudImageRead,
-
Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeSet,
@@ -35,137 +29,100 @@ func DataSourceTencentCloudImage() *schema.Resource {
"values": {
Type: schema.TypeList,
Required: true,
- Elem: &schema.Schema{Type: schema.TypeString},
Description: "Values of the filter.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
},
},
},
- "image_name_regex": {
- Type: schema.TypeString,
- Optional: true,
- ValidateFunc: tccommon.ValidateNameRegex,
- Description: "A regex string to apply to the image list returned by TencentCloud. **NOTE**: it is not wildcard, should look like `image_name_regex = \"^CentOS\\s+6\\.8\\s+64\\w*\"`.",
- },
- "os_name": {
- Type: schema.TypeString,
- Optional: true,
- ValidateFunc: tccommon.ValidateNotEmpty,
- Description: "A string to apply with fuzzy match to the os_name attribute on the image list returned by TencentCloud. **NOTE**: when os_name is provided, highest priority is applied in this field instead of `image_name_regex`.",
- },
- "result_output_file": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Used to save results.",
- },
+
"image_id": {
Type: schema.TypeString,
Computed: true,
Description: "An image id indicate the uniqueness of a certain image, which can be used for instance creation or resetting.",
},
+
"image_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of this image.",
},
+
+ "image_name_regex": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "A regex string to apply to the image list returned by TencentCloud. **NOTE**: it is not wildcard, should look like `image_name_regex = \"^CentOS\\s+6\\.8\\s+64\\w*\"`.",
+ },
+
+ "os_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "A string to apply with fuzzy match to the os_name attribute on the image list returned by TencentCloud. **NOTE**: when os_name is provided, highest priority is applied in this field instead of `image_name_regex`.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudImageRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_image.read")()
-
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
-
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
-
- filter := make(map[string][]string)
- filters, ok := d.GetOk("filter")
- if ok {
- for _, v := range filters.(*schema.Set).List() {
- vv := v.(map[string]interface{})
- name := vv["name"].(string)
- filter[name] = []string{}
- for _, vvv := range vv["values"].([]interface{}) {
- filter[name] = append(filter[name], vvv.(string))
+ defer tccommon.InconsistentCheck(d, meta)()
+
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
+
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+
+ paramMap := make(map[string]interface{})
+ if v, ok := d.GetOk("filters"); ok {
+ filtersSet := v.([]interface{})
+ tmpSet := make([]*cvm.Filter, 0, len(filtersSet))
+ for _, item := range filtersSet {
+ filtersMap := item.(map[string]interface{})
+ filter := cvm.Filter{}
+ if v, ok := filtersMap["name"]; ok {
+ filter.Name = helper.String(v.(string))
+ }
+ if v, ok := filtersMap["values"]; ok {
+ valuesSet := v.(*schema.Set).List()
+ for i := range valuesSet {
+ values := valuesSet[i].(string)
+ filter.Values = append(filter.Values, helper.String(values))
+ }
}
+ tmpSet = append(tmpSet, &filter)
}
+ paramMap["Filters"] = tmpSet
}
- var images []*cvm.Image
- var errRet error
+ var respData []*cvm.Image
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- images, errRet = cvmService.DescribeImagesByFilter(ctx, filter, "")
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+ result, e := service.DescribeImageByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- if len(images) == 0 {
- return errors.New("No image found")
- }
-
- var osName string
- if v, ok := d.GetOk("os_name"); ok {
- osName = v.(string)
- }
-
- var regImageName string
- var imageNameRegex *regexp.Regexp
- if v, ok := d.GetOk("image_name_regex"); ok {
- regImageName = v.(string)
- imageNameRegex, err = regexp.Compile(regImageName)
- if err != nil {
- return fmt.Errorf("image_name_regex format error,%s", err.Error())
- }
- }
-
- var resultImageId string
- images = sortImages(images)
- for _, image := range images {
- if osName != "" {
- if strings.Contains(strings.ToLower(*image.OsName), strings.ToLower(osName)) {
- resultImageId = *image.ImageId
- _ = d.Set("image_name", *image.ImageName)
- break
- }
- continue
- }
-
- if imageNameRegex != nil {
- if imageNameRegex.MatchString(*image.ImageName) {
- resultImageId = *image.ImageId
- _ = d.Set("image_name", *image.ImageName)
- break
- }
- continue
- }
-
- resultImageId = *image.ImageId
- _ = d.Set("image_name", *image.ImageName)
- break
- }
-
- if resultImageId == "" {
- return errors.New("No image found")
- }
-
- d.SetId(helper.DataResourceIdHash(resultImageId))
- if err := d.Set("image_id", resultImageId); err != nil {
+ if err := dataSourceTencentCloudImageReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err = tccommon.WriteToFile(output.(string), resultImageId); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudImageReadOutputContent(ctx)); e != nil {
+ return e
}
}
diff --git a/tencentcloud/services/cvm/data_source_tc_image_extension.go b/tencentcloud/services/cvm/data_source_tc_image_extension.go
new file mode 100644
index 0000000000..97eca70db8
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_image_extension.go
@@ -0,0 +1,81 @@
+package cvm
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "regexp"
+ "strings"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudImageReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.Image) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ images := *resp
+ var err error
+ if len(images) == 0 {
+ return errors.New("No image found")
+ }
+
+ var osName string
+ if v, ok := d.GetOk("os_name"); ok {
+ osName = v.(string)
+ }
+
+ var regImageName string
+ var imageNameRegex *regexp.Regexp
+ if v, ok := d.GetOk("image_name_regex"); ok {
+ regImageName = v.(string)
+ imageNameRegex, err = regexp.Compile(regImageName)
+ if err != nil {
+ return fmt.Errorf("image_name_regex format error,%s", err.Error())
+ }
+ }
+
+ var resultImageId string
+ images = sortImages(images)
+ for _, image := range images {
+ if osName != "" {
+ if strings.Contains(strings.ToLower(*image.OsName), strings.ToLower(osName)) {
+ resultImageId = *image.ImageId
+ _ = d.Set("image_name", *image.ImageName)
+ break
+ }
+ continue
+ }
+
+ if imageNameRegex != nil {
+ if imageNameRegex.MatchString(*image.ImageName) {
+ resultImageId = *image.ImageId
+ _ = d.Set("image_name", *image.ImageName)
+ break
+ }
+ continue
+ }
+
+ resultImageId = *image.ImageId
+ _ = d.Set("image_name", *image.ImageName)
+ break
+ }
+
+ if resultImageId == "" {
+ return errors.New("No image found")
+ }
+
+ d.SetId(helper.DataResourceIdHash(resultImageId))
+ if err := d.Set("image_id", resultImageId); err != nil {
+ return err
+ }
+
+ context.WithValue(ctx, "resultImageId", resultImageId)
+ return nil
+}
+
+func dataSourceTencentCloudImageReadOutputContent(ctx context.Context) interface{} {
+ resultImageId := ctx.Value("resultImageId")
+ return resultImageId
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_images.go b/tencentcloud/services/cvm/data_source_tc_images.go
index b105b0ac25..f966348370 100644
--- a/tencentcloud/services/cvm/data_source_tc_images.go
+++ b/tencentcloud/services/cvm/data_source_tc_images.go
@@ -2,137 +2,106 @@ package cvm
import (
"context"
- "fmt"
- "log"
- "regexp"
- "strings"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
-
- svccbs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cbs"
)
func DataSourceTencentCloudImages() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudImagesRead,
-
Schema: map[string]*schema.Schema{
"image_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the image to be queried.",
},
- "image_type": {
- Type: schema.TypeList,
- Optional: true,
- Elem: &schema.Schema{Type: schema.TypeString},
- Description: "A list of the image type to be queried. Valid values: 'PUBLIC_IMAGE', 'PRIVATE_IMAGE', 'SHARED_IMAGE', 'MARKET_IMAGE'.",
- },
+
"image_name_regex": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"os_name"},
- ValidateFunc: tccommon.ValidateNameRegex,
Description: "A regex string to apply to the image list returned by TencentCloud, conflict with 'os_name'. **NOTE**: it is not wildcard, should look like `image_name_regex = \"^CentOS\\s+6\\.8\\s+64\\w*\"`.",
},
- "os_name": {
- Type: schema.TypeString,
- Optional: true,
- ConflictsWith: []string{"image_name_regex"},
- ValidateFunc: tccommon.ValidateNotEmpty,
- Description: "A string to apply with fuzzy match to the os_name attribute on the image list returned by TencentCloud, conflict with 'image_name_regex'.",
- },
- "instance_type": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Instance type, such as `S1.SMALL1`.",
- },
- "result_output_file": {
- Type: schema.TypeString,
+
+ "image_type": {
+ Type: schema.TypeList,
Optional: true,
- Description: "Used to save results.",
+ Description: "A list of the image type to be queried. Valid values: 'PUBLIC_IMAGE', 'PRIVATE_IMAGE', 'SHARED_IMAGE', 'MARKET_IMAGE'.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
+
"images": {
Type: schema.TypeList,
Computed: true,
Description: "An information list of image. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "image_id": {
+ "architecture": {
Type: schema.TypeString,
Computed: true,
- Description: "ID of the image.",
+ Description: "Architecture of the image.",
},
- "os_name": {
+ "created_time": {
Type: schema.TypeString,
Computed: true,
- Description: "OS name of the image.",
+ Description: "Created time of the image.",
},
- "image_type": {
+ "image_creator": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the image.",
+ Description: "Image creator of the image.",
},
- "created_time": {
+ "image_description": {
Type: schema.TypeString,
Computed: true,
- Description: "Created time of the image.",
+ Description: "Description of the image.",
},
- "image_name": {
+ "image_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Name of the image.",
+ Description: "ID of the image.",
},
- "image_description": {
+ "image_name": {
Type: schema.TypeString,
Computed: true,
- Description: "Description of the image.",
+ Description: "Name of the image.",
},
"image_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Size of the image.",
},
- "architecture": {
+ "image_source": {
Type: schema.TypeString,
Computed: true,
- Description: "Architecture of the image.",
+ Description: "Image source of the image.",
},
"image_state": {
Type: schema.TypeString,
Computed: true,
Description: "State of the image.",
},
- "platform": {
+ "image_type": {
Type: schema.TypeString,
Computed: true,
- Description: "Platform of the image.",
+ Description: "Type of the image.",
},
- "image_creator": {
+ "os_name": {
Type: schema.TypeString,
Computed: true,
- Description: "Image creator of the image.",
+ Description: "OS name of the image.",
},
- "image_source": {
+ "platform": {
Type: schema.TypeString,
Computed: true,
- Description: "Image source of the image.",
- },
- "sync_percent": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "Sync percent of the image.",
- },
- "support_cloud_init": {
- Type: schema.TypeBool,
- Computed: true,
- Description: "Whether support cloud-init.",
+ Description: "Platform of the image.",
},
"snapshots": {
Type: schema.TypeList,
@@ -140,202 +109,116 @@ func DataSourceTencentCloudImages() *schema.Resource {
Description: "List of snapshot details.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "snapshot_id": {
- Type: schema.TypeString,
+ "disk_size": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Snapshot ID.",
+ Description: "Size of the cloud disk used to create the snapshot; unit: GB.",
},
- "snapshot_name": {
+ "disk_usage": {
Type: schema.TypeString,
Computed: true,
- Description: "Snapshot name, the user-defined snapshot alias.",
+ Description: "Type of the cloud disk used to create the snapshot.",
},
- "disk_usage": {
+ "snapshot_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the cloud disk used to create the snapshot.",
+ Description: "Snapshot ID.",
},
- "disk_size": {
- Type: schema.TypeInt,
+ "snapshot_name": {
+ Type: schema.TypeString,
Computed: true,
- Description: "Size of the cloud disk used to create the snapshot; unit: GB.",
+ Description: "Snapshot name, the user-defined snapshot alias.",
},
},
},
},
+ "support_cloud_init": {
+ Type: schema.TypeBool,
+ Computed: true,
+ Description: "Whether support cloud-init.",
+ },
+ "sync_percent": {
+ Type: schema.TypeInt,
+ Computed: true,
+ Description: "Sync percent of the image.",
+ },
},
},
},
+
+ "instance_type": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Instance type, such as `S1.SMALL1`.",
+ },
+
+ "os_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ ConflictsWith: []string{"image_name_regex"},
+ Description: "A string to apply with fuzzy match to the os_name attribute on the image list returned by TencentCloud, conflict with 'image_name_regex'.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_images.read")()
+ defer tccommon.InconsistentCheck(d, meta)()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
-
- cbsService := svccbs.NewCbsService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
-
- var (
- imageId string
- imageType []string
- imageName string
- osName string
- imageNameRegex *regexp.Regexp
- err error
- )
-
- filter := make(map[string][]string)
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+ paramMap := make(map[string]interface{})
+ var filtersList []*cvm.Filter
+ filtersMap := map[string]*cvm.Filter{}
+ filter := cvm.Filter{}
+ name := "image-id"
+ filter.Name = &name
if v, ok := d.GetOk("image_id"); ok {
- imageId = v.(string)
- if imageId != "" {
- filter["image-id"] = []string{imageId}
- }
+ filter.Values = []*string{helper.String(v.(string))}
}
-
- if v, ok := d.GetOk("image_type"); ok {
- for _, vv := range v.([]interface{}) {
- if vv.(string) != "" {
- imageType = append(imageType, vv.(string))
- }
- }
- if len(imageType) > 0 {
- filter["image-type"] = imageType
- }
- }
-
- if v, ok := d.GetOk("image_name_regex"); ok {
- imageName = v.(string)
- if imageName != "" {
- imageNameRegex, err = regexp.Compile(imageName)
- if err != nil {
- return fmt.Errorf("image_name_regex format error,%s", err.Error())
- }
- }
- }
-
- if v, ok := d.GetOk("os_name"); ok {
- osName = v.(string)
+ filtersMap["Temp0"] = &filter
+ if v, ok := filtersMap["Temp0"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ paramMap["Filters"] = filtersList
- var instanceType string
- if v, ok := d.GetOk("instance_type"); ok {
- instanceType = v.(string)
+ if err := dataSourceTencentCloudImagesReadPostFillRequest0(ctx, paramMap); err != nil {
+ return err
}
- var images []*cvm.Image
- err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- var e error
- images, e = cvmService.DescribeImagesByFilter(ctx, filter, instanceType)
+ var respData []*cvm.Image
+ err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
+ result, e := service.DescribeImagesByFilter(ctx, paramMap)
if e != nil {
- return tccommon.RetryError(e, tccommon.InternalError)
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- var results []*cvm.Image
- images = sortImages(images)
-
- if osName == "" && imageName == "" {
- results = images
- } else {
- for _, image := range images {
- if osName != "" {
- if strings.Contains(strings.ToLower(*image.OsName), strings.ToLower(osName)) {
- results = append(results, image)
- continue
- }
- }
- if imageNameRegex != nil {
- if imageNameRegex.MatchString(*image.ImageName) {
- results = append(results, image)
- continue
- }
- }
- }
- }
-
- imageList := make([]map[string]interface{}, 0, len(results))
- ids := make([]string, 0, len(results))
- for _, image := range results {
- snapshots, err := imagesReadSnapshotByIds(ctx, cbsService, image)
- if err != nil {
- return err
- }
-
- mapping := map[string]interface{}{
- "image_id": image.ImageId,
- "os_name": image.OsName,
- "image_type": image.ImageType,
- "created_time": image.CreatedTime,
- "image_name": image.ImageName,
- "image_description": image.ImageDescription,
- "image_size": image.ImageSize,
- "architecture": image.Architecture,
- "image_state": image.ImageState,
- "platform": image.Platform,
- "image_creator": image.ImageCreator,
- "image_source": image.ImageSource,
- "sync_percent": image.SyncPercent,
- "support_cloud_init": image.IsSupportCloudinit,
- "snapshots": snapshots,
- }
- imageList = append(imageList, mapping)
- ids = append(ids, *image.ImageId)
- }
-
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("images", imageList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set image list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudImagesReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), imageList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudImagesReadOutputContent(ctx)); e != nil {
+ return e
}
}
return nil
}
-
-func imagesReadSnapshotByIds(ctx context.Context, cbsService svccbs.CbsService, image *cvm.Image) (snapshotResults []map[string]interface{}, errRet error) {
- if len(image.SnapshotSet) == 0 {
- return
- }
-
- snapshotByIds := make([]*string, 0, len(image.SnapshotSet))
- for _, snapshot := range image.SnapshotSet {
- snapshotByIds = append(snapshotByIds, snapshot.SnapshotId)
- }
-
- snapshots, errRet := cbsService.DescribeSnapshotByIds(ctx, snapshotByIds)
- if errRet != nil {
- return
- }
-
- snapshotResults = make([]map[string]interface{}, 0, len(snapshots))
- for _, snapshot := range snapshots {
- snapshotMap := make(map[string]interface{}, 4)
- snapshotMap["snapshot_id"] = snapshot.SnapshotId
- snapshotMap["disk_usage"] = snapshot.DiskUsage
- snapshotMap["disk_size"] = snapshot.DiskSize
- snapshotMap["snapshot_name"] = snapshot.SnapshotName
-
- snapshotResults = append(snapshotResults, snapshotMap)
- }
-
- return
-}
diff --git a/tencentcloud/services/cvm/data_source_tc_images_extension.go b/tencentcloud/services/cvm/data_source_tc_images_extension.go
new file mode 100644
index 0000000000..34c4db07c9
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_images_extension.go
@@ -0,0 +1,178 @@
+package cvm
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "regexp"
+ "strings"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+ svccbs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cbs"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudImagesReadOutputContent(ctx context.Context) interface{} {
+ imageList := ctx.Value("imageList")
+ return imageList
+}
+
+func dataSourceTencentCloudImagesReadPreRequest0(ctx context.Context, req *cvm.DescribeImagesRequest) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ if v, ok := d.GetOk("instance_type"); ok {
+ req.InstanceType = helper.String(v.(string))
+ }
+
+ return nil
+}
+
+func dataSourceTencentCloudImagesReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.Image) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ images := *resp
+ var (
+ imageName string
+ osName string
+ imageNameRegex *regexp.Regexp
+ err error
+ )
+
+ if v, ok := d.GetOk("os_name"); ok {
+ osName = v.(string)
+ }
+
+ if v, ok := d.GetOk("image_name_regex"); ok {
+ imageName = v.(string)
+ if imageName != "" {
+ imageNameRegex, err = regexp.Compile(imageName)
+ if err != nil {
+ return fmt.Errorf("image_name_regex format error,%s", err.Error())
+ }
+ }
+ }
+
+ var results []*cvm.Image
+ images = sortImages(images)
+ if osName == "" && imageName == "" {
+ results = images
+ } else {
+ for _, image := range images {
+ if osName != "" {
+ if strings.Contains(strings.ToLower(*image.OsName), strings.ToLower(osName)) {
+ results = append(results, image)
+ continue
+ }
+ }
+ if imageNameRegex != nil {
+ if imageNameRegex.MatchString(*image.ImageName) {
+ results = append(results, image)
+ continue
+ }
+ }
+ }
+ }
+
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ meta := tccommon.ProviderMetaFromContext(ctx)
+ cbsService := svccbs.NewCbsService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
+ imageList := make([]map[string]interface{}, 0, len(results))
+ ids := make([]string, 0, len(results))
+ for _, image := range results {
+ snapshots, err := imagesReadSnapshotByIds(ctx, cbsService, image)
+ if err != nil {
+ return err
+ }
+
+ mapping := map[string]interface{}{
+ "image_id": image.ImageId,
+ "os_name": image.OsName,
+ "image_type": image.ImageType,
+ "created_time": image.CreatedTime,
+ "image_name": image.ImageName,
+ "image_description": image.ImageDescription,
+ "image_size": image.ImageSize,
+ "architecture": image.Architecture,
+ "image_state": image.ImageState,
+ "platform": image.Platform,
+ "image_creator": image.ImageCreator,
+ "image_source": image.ImageSource,
+ "sync_percent": image.SyncPercent,
+ "support_cloud_init": image.IsSupportCloudinit,
+ "snapshots": snapshots,
+ }
+ imageList = append(imageList, mapping)
+ ids = append(ids, *image.ImageId)
+ }
+
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err = d.Set("images", imageList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set image list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+
+ context.WithValue(ctx, "imageList", imageList)
+ return nil
+}
+
+func dataSourceTencentCloudImagesReadPostFillRequest0(ctx context.Context, req map[string]interface{}) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ var (
+ imageName string
+ imageType []string
+ err error
+ )
+
+ if v, ok := d.GetOk("image_name_regex"); ok {
+ imageName = v.(string)
+ if imageName != "" {
+ _, err = regexp.Compile(imageName)
+ if err != nil {
+ return fmt.Errorf("image_name_regex format error,%s", err.Error())
+ }
+ }
+ }
+
+ if v, ok := d.GetOk("image_type"); ok {
+ for _, vv := range v.([]interface{}) {
+ if vv.(string) != "" {
+ imageType = append(imageType, vv.(string))
+ }
+ }
+ if len(imageType) > 0 {
+ req["image-type"] = imageType
+ }
+ }
+
+ return nil
+}
+
+func imagesReadSnapshotByIds(ctx context.Context, cbsService svccbs.CbsService, image *cvm.Image) (snapshotResults []map[string]interface{}, errRet error) {
+ if len(image.SnapshotSet) == 0 {
+ return
+ }
+
+ snapshotByIds := make([]*string, 0, len(image.SnapshotSet))
+ for _, snapshot := range image.SnapshotSet {
+ snapshotByIds = append(snapshotByIds, snapshot.SnapshotId)
+ }
+
+ snapshots, errRet := cbsService.DescribeSnapshotByIds(ctx, snapshotByIds)
+ if errRet != nil {
+ return
+ }
+
+ snapshotResults = make([]map[string]interface{}, 0, len(snapshots))
+ for _, snapshot := range snapshots {
+ snapshotMap := make(map[string]interface{}, 4)
+ snapshotMap["snapshot_id"] = snapshot.SnapshotId
+ snapshotMap["disk_usage"] = snapshot.DiskUsage
+ snapshotMap["disk_size"] = snapshot.DiskSize
+ snapshotMap["snapshot_name"] = snapshot.SnapshotName
+
+ snapshotResults = append(snapshotResults, snapshotMap)
+ }
+
+ return
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_instance_types.go b/tencentcloud/services/cvm/data_source_tc_instance_types.go
index 5b1fe66b7e..1a9eea539f 100644
--- a/tencentcloud/services/cvm/data_source_tc_instance_types.go
+++ b/tencentcloud/services/cvm/data_source_tc_instance_types.go
@@ -2,43 +2,39 @@ package cvm
import (
"context"
- "log"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudInstanceTypes() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudInstanceTypesRead,
-
Schema: map[string]*schema.Schema{
+ "availability_zone": {
+ Type: schema.TypeString,
+ Optional: true,
+ ConflictsWith: []string{"filter"},
+ Description: "The available zone that the CVM instance locates at. This field is conflict with `filter`.",
+ },
+
"cpu_core_count": {
Type: schema.TypeInt,
Optional: true,
Description: "The number of CPU cores of the instance.",
},
- "gpu_core_count": {
- Type: schema.TypeInt,
- Optional: true,
- Description: "The number of GPU cores of the instance.",
- },
- "memory_size": {
- Type: schema.TypeInt,
+
+ "exclude_sold_out": {
+ Type: schema.TypeBool,
Optional: true,
- Description: "Instance memory capacity, unit in GB.",
- },
- "availability_zone": {
- Type: schema.TypeString,
- Optional: true,
- ConflictsWith: []string{"filter"},
- Description: "The available zone that the CVM instance locates at. This field is conflict with `filter`.",
+ Default: false,
+ Description: "Indicate to filter instances types that is sold out or not, default is false.",
},
+
"filter": {
Type: schema.TypeSet,
Optional: true,
@@ -55,25 +51,21 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
"values": {
Type: schema.TypeList,
Required: true,
- Elem: &schema.Schema{Type: schema.TypeString},
Description: "The filter values.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
},
},
},
- "exclude_sold_out": {
- Type: schema.TypeBool,
- Optional: true,
- Default: false,
- Description: "Indicate to filter instances types that is sold out or not, default is false.",
- },
- "result_output_file": {
- Type: schema.TypeString,
+
+ "gpu_core_count": {
+ Type: schema.TypeInt,
Optional: true,
- Description: "Used to save results.",
+ Description: "The number of GPU cores of the instance.",
},
- // Computed values.
"instance_types": {
Type: schema.TypeList,
Computed: true,
@@ -85,35 +77,35 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
Computed: true,
Description: "The available zone that the CVM instance locates at.",
},
- "instance_type": {
- Type: schema.TypeString,
- Computed: true,
- Description: "Type of the instance.",
- },
"cpu_core_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of CPU cores of the instance.",
},
+ "family": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Type series of the instance.",
+ },
"gpu_core_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of GPU cores of the instance.",
},
- "memory_size": {
- Type: schema.TypeInt,
+ "instance_charge_type": {
+ Type: schema.TypeString,
Computed: true,
- Description: "Instance memory capacity, unit in GB.",
+ Description: "Charge type of the instance.",
},
- "family": {
+ "instance_type": {
Type: schema.TypeString,
Computed: true,
- Description: "Type series of the instance.",
+ Description: "Type of the instance.",
},
- "instance_charge_type": {
- Type: schema.TypeString,
+ "memory_size": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Charge type of the instance.",
+ Description: "Instance memory capacity, unit in GB.",
},
"status": {
Type: schema.TypeString,
@@ -123,102 +115,80 @@ func DataSourceTencentCloudInstanceTypes() *schema.Resource {
},
},
},
+
+ "memory_size": {
+ Type: schema.TypeInt,
+ Optional: true,
+ Description: "Instance memory capacity, unit in GB.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudInstanceTypesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_instance_types.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
+ defer tccommon.InconsistentCheck(d, meta)()
- isExcludeSoldOut := d.Get("exclude_sold_out").(bool)
- cpu, cpuOk := d.GetOk("cpu_core_count")
- gpu, gpuOk := d.GetOk("gpu_core_count")
- memory, memoryOk := d.GetOk("memory_size")
- var instanceSellTypes []*cvm.InstanceTypeQuotaItem
- var errRet error
- var err error
- typeList := make([]map[string]interface{}, 0)
- ids := make([]string, 0)
-
- var zone string
- var zone_in = 0
- if v, ok := d.GetOk("availability_zone"); ok {
- zone = v.(string)
- zone_in = 1
- }
- filters := d.Get("filter").(*schema.Set).List()
- filterMap := make(map[string][]string, len(filters)+zone_in)
- for _, v := range filters {
- item := v.(map[string]interface{})
- name := item["name"].(string)
- values := item["values"].([]interface{})
- filterValues := make([]string, 0, len(values))
- for _, value := range values {
- filterValues = append(filterValues, value.(string))
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
+
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+
+ paramMap := make(map[string]interface{})
+ if v, ok := d.GetOk("filters"); ok {
+ filtersSet := v.([]interface{})
+ tmpSet := make([]*cvm.Filter, 0, len(filtersSet))
+ for _, item := range filtersSet {
+ filtersMap := item.(map[string]interface{})
+ filter := cvm.Filter{}
+ if v, ok := filtersMap["name"]; ok {
+ filter.Name = helper.String(v.(string))
+ }
+ if v, ok := filtersMap["values"]; ok {
+ valuesSet := v.(*schema.Set).List()
+ for i := range valuesSet {
+ values := valuesSet[i].(string)
+ filter.Values = append(filter.Values, helper.String(values))
+ }
+ }
+ tmpSet = append(tmpSet, &filter)
}
- filterMap[name] = filterValues
+ paramMap["Filters"] = tmpSet
}
- if zone != "" {
- filterMap["zone"] = []string{zone}
+
+ if err := dataSourceTencentCloudInstanceTypesReadPostFillRequest0(ctx, paramMap); err != nil {
+ return err
}
- err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- instanceSellTypes, errRet = cvmService.DescribeInstancesSellTypeByFilter(ctx, filterMap)
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+
+ var respData *cvm.DescribeZoneInstanceConfigInfosResponseParams
+ err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
+ result, e := service.DescribeInstanceTypesByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- for _, instanceType := range instanceSellTypes {
- flag := true
- if cpuOk && int64(cpu.(int)) != *instanceType.Cpu {
- flag = false
- }
- if gpuOk && int64(gpu.(int)) != *instanceType.Gpu {
- flag = false
- }
- if memoryOk && int64(memory.(int)) != *instanceType.Memory {
- flag = false
- }
- if isExcludeSoldOut && CVM_SOLD_OUT_STATUS == *instanceType.Status {
- flag = false
- }
-
- if flag {
- mapping := map[string]interface{}{
- "availability_zone": instanceType.Zone,
- "cpu_core_count": instanceType.Cpu,
- "gpu_core_count": instanceType.Gpu,
- "memory_size": instanceType.Memory,
- "family": instanceType.InstanceFamily,
- "instance_type": instanceType.InstanceType,
- "instance_charge_type": instanceType.InstanceChargeType,
- "status": instanceType.Status,
- }
- typeList = append(typeList, mapping)
- ids = append(ids, *instanceType.InstanceType)
- }
- }
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("instance_types", typeList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set instance type list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudInstanceTypesReadPostHandleResponse0(ctx, paramMap, respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), typeList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudInstanceTypesReadOutputContent(ctx)); e != nil {
+ return e
}
}
+
return nil
}
diff --git a/tencentcloud/services/cvm/data_source_tc_instance_types_extension.go b/tencentcloud/services/cvm/data_source_tc_instance_types_extension.go
new file mode 100644
index 0000000000..79c109f98c
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_instance_types_extension.go
@@ -0,0 +1,97 @@
+package cvm
+
+import (
+ "context"
+ "log"
+
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudInstanceTypesReadPostFillRequest0(ctx context.Context, req map[string]interface{}) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ var zone string
+ var zone_in = 0
+ if v, ok := d.GetOk("availability_zone"); ok {
+ zone = v.(string)
+ zone_in = 1
+ }
+ filters := d.Get("filter").(*schema.Set).List()
+ filterMap := make(map[string][]string, len(filters)+zone_in)
+ for _, v := range filters {
+ item := v.(map[string]interface{})
+ name := item["name"].(string)
+ values := item["values"].([]interface{})
+ filterValues := make([]string, 0, len(values))
+ for _, value := range values {
+ filterValues = append(filterValues, value.(string))
+ }
+ filterMap[name] = filterValues
+ }
+ if zone != "" {
+ filterMap["zone"] = []string{zone}
+ }
+
+ return nil
+}
+
+func dataSourceTencentCloudInstanceTypesReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *cvm.DescribeZoneInstanceConfigInfosResponseParams) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ isExcludeSoldOut := d.Get("exclude_sold_out").(bool)
+ cpu, cpuOk := d.GetOk("cpu_core_count")
+ gpu, gpuOk := d.GetOk("gpu_core_count")
+ memory, memoryOk := d.GetOk("memory_size")
+ var err error
+ typeList := make([]map[string]interface{}, 0)
+ ids := make([]string, 0)
+
+ instanceSellTypes := resp.InstanceTypeQuotaSet
+ for _, instanceType := range instanceSellTypes {
+ flag := true
+ if cpuOk && int64(cpu.(int)) != *instanceType.Cpu {
+ flag = false
+ }
+ if gpuOk && int64(gpu.(int)) != *instanceType.Gpu {
+ flag = false
+ }
+ if memoryOk && int64(memory.(int)) != *instanceType.Memory {
+ flag = false
+ }
+ if isExcludeSoldOut && CVM_SOLD_OUT_STATUS == *instanceType.Status {
+ flag = false
+ }
+
+ if flag {
+ mapping := map[string]interface{}{
+ "availability_zone": instanceType.Zone,
+ "cpu_core_count": instanceType.Cpu,
+ "gpu_core_count": instanceType.Gpu,
+ "memory_size": instanceType.Memory,
+ "family": instanceType.InstanceFamily,
+ "instance_type": instanceType.InstanceType,
+ "instance_charge_type": instanceType.InstanceChargeType,
+ "status": instanceType.Status,
+ }
+ typeList = append(typeList, mapping)
+ ids = append(ids, *instanceType.InstanceType)
+ }
+ }
+
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err = d.Set("instance_types", typeList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set instance type list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+ context.WithValue(ctx, "typeList", typeList)
+ return nil
+}
+
+func dataSourceTencentCloudInstanceTypesReadOutputContent(ctx context.Context) interface{} {
+ typeList := ctx.Value("typeList").([]interface{})
+ return typeList
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_instances.go b/tencentcloud/services/cvm/data_source_tc_instances.go
index fb0a158882..f852e76029 100644
--- a/tencentcloud/services/cvm/data_source_tc_instances.go
+++ b/tencentcloud/services/cvm/data_source_tc_instances.go
@@ -2,146 +2,66 @@ package cvm
import (
"context"
- "fmt"
- "log"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudInstancesRead,
-
Schema: map[string]*schema.Schema{
- "instance_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of the instances to be queried.",
- },
- "instance_name": {
- Type: schema.TypeString,
- Optional: true,
- ValidateFunc: tccommon.ValidateStringLengthInRange(1, 30),
- Description: "Name of the instances to be queried.",
- },
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Description: "The available zone that the CVM instance locates at.",
},
- "project_id": {
- Type: schema.TypeInt,
- Optional: true,
- Description: "The project CVM belongs to.",
- },
- "vpc_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of the vpc to be queried.",
- },
- "subnet_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of a vpc subnetwork.",
- },
- "instance_set_ids": {
- Type: schema.TypeList,
- Optional: true,
- MaxItems: 100,
- ConflictsWith: []string{"instance_id", "instance_name", "availability_zone", "project_id", "vpc_id", "subnet_id", "tags"},
- Elem: &schema.Schema{
- Type: schema.TypeString,
- },
- Description: "Instance set ids, max length is 100, conflict with other field.",
- },
- "tags": {
- Type: schema.TypeMap,
- Optional: true,
- Description: "Tags of the instance.",
- },
- "result_output_file": {
+
+ "instance_id": {
Type: schema.TypeString,
Optional: true,
- Description: "Used to save results.",
+ Description: "ID of the instances to be queried.",
},
- // computed
"instance_list": {
Type: schema.TypeList,
Computed: true,
Description: "An information list of cvm instance. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "instance_id": {
- Type: schema.TypeString,
+ "allocate_public_ip": {
+ Type: schema.TypeBool,
Computed: true,
- Description: "ID of the instances.",
+ Description: "Indicates whether public ip is assigned.",
},
- "instance_name": {
+ "availability_zone": {
Type: schema.TypeString,
Computed: true,
- Description: "Name of the instances.",
+ Description: "The available zone that the CVM instance locates at.",
},
- "instance_type": {
+ "cam_role_name": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the instance.",
+ Description: "CAM role name authorized to access.",
},
"cpu": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of CPU cores of the instance.",
},
- "memory": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "Instance memory capacity, unit in GB.",
- },
"os_name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance os name.",
},
- "availability_zone": {
- Type: schema.TypeString,
- Computed: true,
- Description: "The available zone that the CVM instance locates at.",
- },
- "project_id": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "The project CVM belongs to.",
- },
- "image_id": {
- Type: schema.TypeString,
- Computed: true,
- Description: "ID of the image.",
- },
- "instance_charge_type": {
- Type: schema.TypeString,
- Computed: true,
- Description: "The charge type of the instance.",
- },
- "system_disk_type": {
- Type: schema.TypeString,
- Computed: true,
- Description: "Type of the system disk.",
- },
- "system_disk_size": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "Size of the system disk.",
- },
- "system_disk_id": {
+ "create_time": {
Type: schema.TypeString,
Computed: true,
- Description: "Image ID of the system disk.",
+ Description: "Creation time of the instance.",
},
"data_disks": {
Type: schema.TypeList,
@@ -149,20 +69,20 @@ func DataSourceTencentCloudInstances() *schema.Resource {
Description: "An information list of data disk. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "data_disk_type": {
+ "data_disk_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the data disk.",
+ Description: "Image ID of the data disk.",
},
"data_disk_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Size of the data disk.",
},
- "data_disk_id": {
+ "data_disk_type": {
Type: schema.TypeString,
Computed: true,
- Description: "Image ID of the data disk.",
+ Description: "Type of the data disk.",
},
"delete_with_instance": {
Type: schema.TypeBool,
@@ -172,15 +92,40 @@ func DataSourceTencentCloudInstances() *schema.Resource {
},
},
},
- "vpc_id": {
+ "expired_time": {
Type: schema.TypeString,
Computed: true,
- Description: "ID of the vpc.",
+ Description: "Expired time of the instance.",
},
- "subnet_id": {
+ "image_id": {
Type: schema.TypeString,
Computed: true,
- Description: "ID of a vpc subnetwork.",
+ Description: "ID of the image.",
+ },
+ "instance_charge_type": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "The charge type of the instance.",
+ },
+ "instance_charge_type_prepaid_renew_flag": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "The way that CVM instance will be renew automatically or not when it reach the end of the prepaid tenancy.",
+ },
+ "instance_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "ID of the instances.",
+ },
+ "instance_name": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Name of the instances.",
+ },
+ "instance_type": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Type of the instance.",
},
"internet_charge_type": {
Type: schema.TypeString,
@@ -192,180 +137,224 @@ func DataSourceTencentCloudInstances() *schema.Resource {
Computed: true,
Description: "Public network maximum output bandwidth of the instance.",
},
- "allocate_public_ip": {
- Type: schema.TypeBool,
+ "memory": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Indicates whether public ip is assigned.",
+ Description: "Instance memory capacity, unit in GB.",
},
- "status": {
+ "private_ip": {
Type: schema.TypeString,
Computed: true,
- Description: "Status of the instance.",
+ Description: "Private IP of the instance.",
},
- "public_ip": {
- Type: schema.TypeString,
+ "project_id": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Public IP of the instance.",
+ Description: "The project CVM belongs to.",
},
- "private_ip": {
+ "public_ip": {
Type: schema.TypeString,
Computed: true,
- Description: "Private IP of the instance.",
+ Description: "Public IP of the instance.",
},
"security_groups": {
Type: schema.TypeList,
Computed: true,
- Elem: &schema.Schema{Type: schema.TypeString},
Description: "Security groups of the instance.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
- "tags": {
- Type: schema.TypeMap,
+ "status": {
+ Type: schema.TypeString,
Computed: true,
- Description: "Tags of the instance.",
+ Description: "Status of the instance.",
},
- "create_time": {
+ "subnet_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Creation time of the instance.",
+ Description: "ID of a vpc subnetwork.",
},
- "expired_time": {
+ "system_disk_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Expired time of the instance.",
+ Description: "Image ID of the system disk.",
},
- "instance_charge_type_prepaid_renew_flag": {
+ "system_disk_size": {
+ Type: schema.TypeInt,
+ Computed: true,
+ Description: "Size of the system disk.",
+ },
+ "system_disk_type": {
Type: schema.TypeString,
Computed: true,
- Description: "The way that CVM instance will be renew automatically or not when it reach the end of the prepaid tenancy.",
+ Description: "Type of the system disk.",
},
- "cam_role_name": {
+ "tags": {
+ Type: schema.TypeMap,
+ Computed: true,
+ Description: "Tags of the instance.",
+ },
+ "vpc_id": {
Type: schema.TypeString,
Computed: true,
- Description: "CAM role name authorized to access.",
+ Description: "ID of the vpc.",
},
},
},
},
+
+ "instance_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Name of the instances to be queried.",
+ },
+
+ "instance_set_ids": {
+ Type: schema.TypeList,
+ Optional: true,
+ MaxItems: 100,
+ ConflictsWith: []string{"instance_id", "instance_name", "availability_zone", "project_id", "vpc_id", "subnet_id", "tags"},
+ Description: "Instance set ids, max length is 100, conflict with other field.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
+ },
+
+ "project_id": {
+ Type: schema.TypeInt,
+ Optional: true,
+ Description: "The project CVM belongs to.",
+ },
+
+ "subnet_id": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "ID of a vpc subnetwork.",
+ },
+
+ "tags": {
+ Type: schema.TypeMap,
+ Optional: true,
+ Description: "Tags of the instance.",
+ },
+
+ "vpc_id": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "ID of the vpc to be queried.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudInstancesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_instances.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
+ defer tccommon.InconsistentCheck(d, meta)()
- var instanceSetIds []*string
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- filter := make(map[string]string)
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+
+ paramMap := make(map[string]interface{})
+ var filtersList []*cvm.Filter
+ filtersMap := map[string]*cvm.Filter{}
+ filter := cvm.Filter{}
+ name := "instance-id"
+ filter.Name = &name
if v, ok := d.GetOk("instance_id"); ok {
- filter["instance-id"] = v.(string)
+ filter.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp0"] = &filter
+ if v, ok := filtersMap["Temp0"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ filter2 := cvm.Filter{}
+ name2 := "instance-name"
+ filter2.Name = &name2
if v, ok := d.GetOk("instance_name"); ok {
- filter["instance-name"] = v.(string)
+ filter2.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp1"] = &filter2
+ if v, ok := filtersMap["Temp1"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter3 := cvm.Filter{}
+ name3 := "zone"
+ filter3.Name = &name3
if v, ok := d.GetOk("availability_zone"); ok {
- filter["zone"] = v.(string)
+ filter3.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp2"] = &filter3
+ if v, ok := filtersMap["Temp2"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
- if v, ok := d.GetOkExists("project_id"); ok {
- filter["project-id"] = fmt.Sprintf("%d", v.(int))
+ filter4 := cvm.Filter{}
+ name4 := "project-id"
+ filter4.Name = &name4
+ if v, ok := d.GetOk("project_id"); ok {
+ filter4.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp3"] = &filter4
+ if v, ok := filtersMap["Temp3"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter5 := cvm.Filter{}
+ name5 := "vpc-id"
+ filter5.Name = &name5
if v, ok := d.GetOk("vpc_id"); ok {
- filter["vpc-id"] = v.(string)
+ filter5.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp4"] = &filter5
+ if v, ok := filtersMap["Temp4"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ filter6 := cvm.Filter{}
+ name6 := "subnet-id"
+ filter6.Name = &name6
if v, ok := d.GetOk("subnet_id"); ok {
- filter["subnet-id"] = v.(string)
+ filter6.Values = []*string{helper.String(v.(string))}
}
- if v, ok := d.GetOk("instance_set_ids"); ok {
- instanceSetIds = helper.InterfacesStringsPoint(v.([]interface{}))
+ filtersMap["Temp5"] = &filter6
+ if v, ok := filtersMap["Temp5"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ paramMap["Filters"] = filtersList
- if v, ok := d.GetOk("tags"); ok {
- for key, value := range v.(map[string]interface{}) {
- filter["tag:"+key] = value.(string)
- }
+ if err := dataSourceTencentCloudInstancesReadPostFillRequest0(ctx, paramMap); err != nil {
+ return err
}
- var instances []*cvm.Instance
- var errRet error
+ var respData []*cvm.Instance
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- instances, errRet = cvmService.DescribeInstanceByFilter(ctx, instanceSetIds, filter)
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+ result, e := service.DescribeInstancesByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- instanceList := make([]map[string]interface{}, 0, len(instances))
- ids := make([]string, 0, len(instances))
- for _, instance := range instances {
- mapping := map[string]interface{}{
- "instance_id": instance.InstanceId,
- "instance_name": instance.InstanceName,
- "instance_type": instance.InstanceType,
- "cpu": instance.CPU,
- "memory": instance.Memory,
- "os_name": instance.OsName,
- "availability_zone": instance.Placement.Zone,
- "project_id": instance.Placement.ProjectId,
- "image_id": instance.ImageId,
- "instance_charge_type": instance.InstanceChargeType,
- "system_disk_type": instance.SystemDisk.DiskType,
- "system_disk_size": instance.SystemDisk.DiskSize,
- "system_disk_id": instance.SystemDisk.DiskId,
- "vpc_id": instance.VirtualPrivateCloud.VpcId,
- "subnet_id": instance.VirtualPrivateCloud.SubnetId,
- "internet_charge_type": instance.InternetAccessible.InternetChargeType,
- "internet_max_bandwidth_out": instance.InternetAccessible.InternetMaxBandwidthOut,
- "allocate_public_ip": instance.InternetAccessible.PublicIpAssigned,
- "status": instance.InstanceState,
- "security_groups": helper.StringsInterfaces(instance.SecurityGroupIds),
- "tags": flattenCvmTagsMapping(instance.Tags),
- "create_time": instance.CreatedTime,
- "expired_time": instance.ExpiredTime,
- "instance_charge_type_prepaid_renew_flag": instance.RenewFlag,
- "cam_role_name": instance.CamRoleName,
- }
- if len(instance.PublicIpAddresses) > 0 {
- mapping["public_ip"] = *instance.PublicIpAddresses[0]
- }
- if len(instance.PrivateIpAddresses) > 0 {
- mapping["private_ip"] = *instance.PrivateIpAddresses[0]
- }
- dataDisks := make([]map[string]interface{}, 0, len(instance.DataDisks))
- for _, v := range instance.DataDisks {
- dataDisk := map[string]interface{}{
- "data_disk_type": v.DiskType,
- "data_disk_size": v.DiskSize,
- "data_disk_id": v.DiskId,
- "delete_with_instance": v.DeleteWithInstance,
- }
- dataDisks = append(dataDisks, dataDisk)
- }
- mapping["data_disks"] = dataDisks
- instanceList = append(instanceList, mapping)
- ids = append(ids, *instance.InstanceId)
- }
-
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("instance_list", instanceList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudInstancesReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), instanceList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudInstancesReadOutputContent(ctx)); e != nil {
+ return e
}
}
- return nil
+ return nil
}
diff --git a/tencentcloud/services/cvm/data_source_tc_instances_extension.go b/tencentcloud/services/cvm/data_source_tc_instances_extension.go
new file mode 100644
index 0000000000..a2cf6be7b3
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_instances_extension.go
@@ -0,0 +1,103 @@
+package cvm
+
+import (
+ "context"
+ "log"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudInstancesReadPostFillRequest0(ctx context.Context, req map[string]interface{}) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ if v, ok := d.GetOk("tags"); ok {
+ for key, value := range v.(map[string]interface{}) {
+ req["tag:"+key] = value.(string)
+ }
+ }
+
+ return nil
+}
+
+func dataSourceTencentCloudInstancesReadPreRequest0(ctx context.Context, req *cvm.DescribeInstancesRequest) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ if v, ok := d.GetOk("instance_set_ids"); ok {
+ req.InstanceIds = helper.InterfacesStringsPoint(v.([]interface{}))
+ }
+
+ return nil
+}
+
+func dataSourceTencentCloudInstancesReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.Instance) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ var err error
+ instances := *resp
+ instanceList := make([]map[string]interface{}, 0, len(instances))
+ ids := make([]string, 0, len(instances))
+ for _, instance := range instances {
+ mapping := map[string]interface{}{
+ "instance_id": instance.InstanceId,
+ "instance_name": instance.InstanceName,
+ "instance_type": instance.InstanceType,
+ "cpu": instance.CPU,
+ "memory": instance.Memory,
+ "os_name": instance.OsName,
+ "availability_zone": instance.Placement.Zone,
+ "project_id": instance.Placement.ProjectId,
+ "image_id": instance.ImageId,
+ "instance_charge_type": instance.InstanceChargeType,
+ "system_disk_type": instance.SystemDisk.DiskType,
+ "system_disk_size": instance.SystemDisk.DiskSize,
+ "system_disk_id": instance.SystemDisk.DiskId,
+ "vpc_id": instance.VirtualPrivateCloud.VpcId,
+ "subnet_id": instance.VirtualPrivateCloud.SubnetId,
+ "internet_charge_type": instance.InternetAccessible.InternetChargeType,
+ "internet_max_bandwidth_out": instance.InternetAccessible.InternetMaxBandwidthOut,
+ "allocate_public_ip": instance.InternetAccessible.PublicIpAssigned,
+ "status": instance.InstanceState,
+ "security_groups": helper.StringsInterfaces(instance.SecurityGroupIds),
+ "tags": flattenCvmTagsMapping(instance.Tags),
+ "create_time": instance.CreatedTime,
+ "expired_time": instance.ExpiredTime,
+ "instance_charge_type_prepaid_renew_flag": instance.RenewFlag,
+ "cam_role_name": instance.CamRoleName,
+ }
+ if len(instance.PublicIpAddresses) > 0 {
+ mapping["public_ip"] = *instance.PublicIpAddresses[0]
+ }
+ if len(instance.PrivateIpAddresses) > 0 {
+ mapping["private_ip"] = *instance.PrivateIpAddresses[0]
+ }
+ dataDisks := make([]map[string]interface{}, 0, len(instance.DataDisks))
+ for _, v := range instance.DataDisks {
+ dataDisk := map[string]interface{}{
+ "data_disk_type": v.DiskType,
+ "data_disk_size": v.DiskSize,
+ "data_disk_id": v.DiskId,
+ "delete_with_instance": v.DeleteWithInstance,
+ }
+ dataDisks = append(dataDisks, dataDisk)
+ }
+ mapping["data_disks"] = dataDisks
+ instanceList = append(instanceList, mapping)
+ ids = append(ids, *instance.InstanceId)
+ }
+
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err = d.Set("instance_list", instanceList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+
+ context.WithValue(ctx, "instanceList", instanceList)
+ return nil
+}
+
+func dataSourceTencentCloudInstancesReadOutputContent(ctx context.Context) interface{} {
+ instanceList := ctx.Value("instanceList")
+ return instanceList
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_instances_set.go b/tencentcloud/services/cvm/data_source_tc_instances_set.go
index 4f964a74bf..32f4a1ad54 100644
--- a/tencentcloud/services/cvm/data_source_tc_instances_set.go
+++ b/tencentcloud/services/cvm/data_source_tc_instances_set.go
@@ -2,130 +2,61 @@ package cvm
import (
"context"
- "fmt"
- "log"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudInstancesSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudInstancesSetRead,
-
Schema: map[string]*schema.Schema{
- "instance_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of the instances to be queried.",
- },
- "instance_name": {
- Type: schema.TypeString,
- Optional: true,
- ValidateFunc: tccommon.ValidateStringLengthInRange(1, 30),
- Description: "Name of the instances to be queried.",
- },
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Description: "The available zone that the CVM instance locates at.",
},
- "project_id": {
- Type: schema.TypeInt,
- Optional: true,
- Description: "The project CVM belongs to.",
- },
- "vpc_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of the vpc to be queried.",
- },
- "subnet_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of a vpc subnetwork.",
- },
- "tags": {
- Type: schema.TypeMap,
- Optional: true,
- Description: "Tags of the instance.",
- },
- "result_output_file": {
+
+ "instance_id": {
Type: schema.TypeString,
Optional: true,
- Description: "Used to save results.",
+ Description: "ID of the instances to be queried.",
},
- // computed
"instance_list": {
Type: schema.TypeList,
Computed: true,
Description: "An information list of cvm instance. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "instance_id": {
- Type: schema.TypeString,
- Computed: true,
- Description: "ID of the instances.",
- },
- "instance_name": {
- Type: schema.TypeString,
- Computed: true,
- Description: "Name of the instances.",
- },
- "instance_type": {
- Type: schema.TypeString,
- Computed: true,
- Description: "Type of the instance.",
- },
- "cpu": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "The number of CPU cores of the instance.",
- },
- "memory": {
- Type: schema.TypeInt,
+ "allocate_public_ip": {
+ Type: schema.TypeBool,
Computed: true,
- Description: "Instance memory capacity, unit in GB.",
+ Description: "Indicates whether public ip is assigned.",
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Description: "The available zone that the CVM instance locates at.",
},
- "project_id": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "The project CVM belongs to.",
- },
- "image_id": {
- Type: schema.TypeString,
- Computed: true,
- Description: "ID of the image.",
- },
- "instance_charge_type": {
- Type: schema.TypeString,
- Computed: true,
- Description: "The charge type of the instance.",
- },
- "system_disk_type": {
+ "cam_role_name": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the system disk.",
+ Description: "CAM role name authorized to access.",
},
- "system_disk_size": {
+ "cpu": {
Type: schema.TypeInt,
Computed: true,
- Description: "Size of the system disk.",
+ Description: "The number of CPU cores of the instance.",
},
- "system_disk_id": {
+ "create_time": {
Type: schema.TypeString,
Computed: true,
- Description: "Image ID of the system disk.",
+ Description: "Creation time of the instance.",
},
"data_disks": {
Type: schema.TypeList,
@@ -133,20 +64,20 @@ func DataSourceTencentCloudInstancesSet() *schema.Resource {
Description: "An information list of data disk. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "data_disk_type": {
+ "data_disk_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Type of the data disk.",
+ Description: "Image ID of the data disk.",
},
"data_disk_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Size of the data disk.",
},
- "data_disk_id": {
+ "data_disk_type": {
Type: schema.TypeString,
Computed: true,
- Description: "Image ID of the data disk.",
+ Description: "Type of the data disk.",
},
"delete_with_instance": {
Type: schema.TypeBool,
@@ -156,15 +87,40 @@ func DataSourceTencentCloudInstancesSet() *schema.Resource {
},
},
},
- "vpc_id": {
+ "expired_time": {
Type: schema.TypeString,
Computed: true,
- Description: "ID of the vpc.",
+ Description: "Expired time of the instance.",
},
- "subnet_id": {
+ "image_id": {
Type: schema.TypeString,
Computed: true,
- Description: "ID of a vpc subnetwork.",
+ Description: "ID of the image.",
+ },
+ "instance_charge_type": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "The charge type of the instance.",
+ },
+ "instance_charge_type_prepaid_renew_flag": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "The way that CVM instance will be renew automatically or not when it reach the end of the prepaid tenancy.",
+ },
+ "instance_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "ID of the instances.",
+ },
+ "instance_name": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Name of the instances.",
+ },
+ "instance_type": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Type of the instance.",
},
"internet_charge_type": {
Type: schema.TypeString,
@@ -176,172 +132,213 @@ func DataSourceTencentCloudInstancesSet() *schema.Resource {
Computed: true,
Description: "Public network maximum output bandwidth of the instance.",
},
- "allocate_public_ip": {
- Type: schema.TypeBool,
+ "memory": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Indicates whether public ip is assigned.",
+ Description: "Instance memory capacity, unit in GB.",
},
- "status": {
+ "private_ip": {
Type: schema.TypeString,
Computed: true,
- Description: "Status of the instance.",
+ Description: "Private IP of the instance.",
},
- "public_ip": {
- Type: schema.TypeString,
+ "project_id": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Public IP of the instance.",
+ Description: "The project CVM belongs to.",
},
- "private_ip": {
+ "public_ip": {
Type: schema.TypeString,
Computed: true,
- Description: "Private IP of the instance.",
+ Description: "Public IP of the instance.",
},
"security_groups": {
Type: schema.TypeList,
Computed: true,
- Elem: &schema.Schema{Type: schema.TypeString},
Description: "Security groups of the instance.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
- "tags": {
- Type: schema.TypeMap,
+ "status": {
+ Type: schema.TypeString,
Computed: true,
- Description: "Tags of the instance.",
+ Description: "Status of the instance.",
},
- "create_time": {
+ "subnet_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Creation time of the instance.",
+ Description: "ID of a vpc subnetwork.",
},
- "expired_time": {
+ "system_disk_id": {
Type: schema.TypeString,
Computed: true,
- Description: "Expired time of the instance.",
+ Description: "Image ID of the system disk.",
},
- "instance_charge_type_prepaid_renew_flag": {
+ "system_disk_size": {
+ Type: schema.TypeInt,
+ Computed: true,
+ Description: "Size of the system disk.",
+ },
+ "system_disk_type": {
Type: schema.TypeString,
Computed: true,
- Description: "The way that CVM instance will be renew automatically or not when it reach the end of the prepaid tenancy.",
+ Description: "Type of the system disk.",
},
- "cam_role_name": {
+ "tags": {
+ Type: schema.TypeMap,
+ Computed: true,
+ Description: "Tags of the instance.",
+ },
+ "vpc_id": {
Type: schema.TypeString,
Computed: true,
- Description: "CAM role name authorized to access.",
+ Description: "ID of the vpc.",
},
},
},
},
+
+ "instance_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Name of the instances to be queried.",
+ },
+
+ "project_id": {
+ Type: schema.TypeInt,
+ Optional: true,
+ Description: "The project CVM belongs to.",
+ },
+
+ "subnet_id": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "ID of a vpc subnetwork.",
+ },
+
+ "tags": {
+ Type: schema.TypeMap,
+ Optional: true,
+ Description: "Tags of the instance.",
+ },
+
+ "vpc_id": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "ID of the vpc to be queried.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudInstancesSetRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_instances_set.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
+ defer tccommon.InconsistentCheck(d, meta)()
+
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- filter := make(map[string]string)
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+
+ paramMap := make(map[string]interface{})
+ var filtersList []*cvm.Filter
+ filtersMap := map[string]*cvm.Filter{}
+ filter := cvm.Filter{}
+ name := "instance-id"
+ filter.Name = &name
if v, ok := d.GetOk("instance_id"); ok {
- filter["instance-id"] = v.(string)
+ filter.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp0"] = &filter
+ if v, ok := filtersMap["Temp0"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ filter2 := cvm.Filter{}
+ name2 := "instance-name"
+ filter2.Name = &name2
if v, ok := d.GetOk("instance_name"); ok {
- filter["instance-name"] = v.(string)
+ filter2.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp1"] = &filter2
+ if v, ok := filtersMap["Temp1"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter3 := cvm.Filter{}
+ name3 := "zone"
+ filter3.Name = &name3
if v, ok := d.GetOk("availability_zone"); ok {
- filter["zone"] = v.(string)
+ filter3.Values = []*string{helper.String(v.(string))}
+ }
+ filtersMap["Temp2"] = &filter3
+ if v, ok := filtersMap["Temp2"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter4 := cvm.Filter{}
+ name4 := "project-id"
+ filter4.Name = &name4
+ if v, ok := d.GetOk("project_id"); ok {
+ filter4.Values = []*string{helper.String(v.(string))}
}
- if v, ok := d.GetOkExists("project_id"); ok {
- filter["project-id"] = fmt.Sprintf("%d", v.(int))
+ filtersMap["Temp3"] = &filter4
+ if v, ok := filtersMap["Temp3"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ filter5 := cvm.Filter{}
+ name5 := "vpc-id"
+ filter5.Name = &name5
if v, ok := d.GetOk("vpc_id"); ok {
- filter["vpc-id"] = v.(string)
+ filter5.Values = []*string{helper.String(v.(string))}
}
+ filtersMap["Temp4"] = &filter5
+ if v, ok := filtersMap["Temp4"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
+ }
+ filter6 := cvm.Filter{}
+ name6 := "subnet-id"
+ filter6.Name = &name6
if v, ok := d.GetOk("subnet_id"); ok {
- filter["subnet-id"] = v.(string)
+ filter6.Values = []*string{helper.String(v.(string))}
}
-
- if v, ok := d.GetOk("tags"); ok {
- for key, value := range v.(map[string]interface{}) {
- filter["tag:"+key] = value.(string)
- }
+ filtersMap["Temp5"] = &filter6
+ if v, ok := filtersMap["Temp5"]; ok && len(v.Values) > 0 {
+ filtersList = append(filtersList, v)
}
+ paramMap["Filters"] = filtersList
- var instances []*cvm.Instance
- var errRet error
-
- instances, errRet = cvmService.DescribeInstanceInParallelByFilter(ctx, filter)
-
- if errRet != nil {
- return errRet
+ if err := dataSourceTencentCloudInstancesSetReadPostFillRequest0(ctx, paramMap); err != nil {
+ return err
}
- instanceList := make([]map[string]interface{}, 0, len(instances))
- ids := make([]string, 0, len(instances))
- for _, instance := range instances {
- mapping := map[string]interface{}{
- "instance_id": instance.InstanceId,
- "instance_name": instance.InstanceName,
- "instance_type": instance.InstanceType,
- "cpu": instance.CPU,
- "memory": instance.Memory,
- "availability_zone": instance.Placement.Zone,
- "project_id": instance.Placement.ProjectId,
- "image_id": instance.ImageId,
- "instance_charge_type": instance.InstanceChargeType,
- "system_disk_type": instance.SystemDisk.DiskType,
- "system_disk_size": instance.SystemDisk.DiskSize,
- "system_disk_id": instance.SystemDisk.DiskId,
- "vpc_id": instance.VirtualPrivateCloud.VpcId,
- "subnet_id": instance.VirtualPrivateCloud.SubnetId,
- "internet_charge_type": instance.InternetAccessible.InternetChargeType,
- "internet_max_bandwidth_out": instance.InternetAccessible.InternetMaxBandwidthOut,
- "allocate_public_ip": instance.InternetAccessible.PublicIpAssigned,
- "status": instance.InstanceState,
- "security_groups": helper.StringsInterfaces(instance.SecurityGroupIds),
- "tags": flattenCvmTagsMapping(instance.Tags),
- "create_time": instance.CreatedTime,
- "expired_time": instance.ExpiredTime,
- "instance_charge_type_prepaid_renew_flag": instance.RenewFlag,
- "cam_role_name": instance.CamRoleName,
- }
- if len(instance.PublicIpAddresses) > 0 {
- mapping["public_ip"] = *instance.PublicIpAddresses[0]
+ var respData []*cvm.Instance
+ err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
+ result, e := service.DescribeInstancesSetByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
- if len(instance.PrivateIpAddresses) > 0 {
- mapping["private_ip"] = *instance.PrivateIpAddresses[0]
- }
- dataDisks := make([]map[string]interface{}, 0, len(instance.DataDisks))
- for _, v := range instance.DataDisks {
- dataDisk := map[string]interface{}{
- "data_disk_type": v.DiskType,
- "data_disk_size": v.DiskSize,
- "data_disk_id": v.DiskId,
- "delete_with_instance": v.DeleteWithInstance,
- }
- dataDisks = append(dataDisks, dataDisk)
- }
- mapping["data_disks"] = dataDisks
- instanceList = append(instanceList, mapping)
- ids = append(ids, *instance.InstanceId)
- }
- log.Printf("[DEBUG]%s set instance attribute finished", logId)
- d.SetId(helper.DataResourceIdsHash(ids))
- err := d.Set("instance_list", instanceList)
+ respData = result
+ return nil
+ })
if err != nil {
- log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+
+ if err := dataSourceTencentCloudInstancesSetReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), instanceList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudInstancesSetReadOutputContent(ctx)); e != nil {
+ return e
}
}
- log.Printf("[DEBUG]%s all operate finished", logId)
return nil
-
}
diff --git a/tencentcloud/services/cvm/data_source_tc_instances_set_extension.go b/tencentcloud/services/cvm/data_source_tc_instances_set_extension.go
new file mode 100644
index 0000000000..e99cf86092
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_instances_set_extension.go
@@ -0,0 +1,91 @@
+package cvm
+
+import (
+ "context"
+ "log"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudInstancesSetReadPostFillRequest0(ctx context.Context, req map[string]interface{}) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ if v, ok := d.GetOk("tags"); ok {
+ for key, value := range v.(map[string]interface{}) {
+ req["tag:"+key] = value.(string)
+ }
+ }
+
+ return nil
+}
+
+func dataSourceTencentCloudInstancesSetReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.Instance) error {
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ d := tccommon.ResourceDataFromContext(ctx)
+ instances := *resp
+ instanceList := make([]map[string]interface{}, 0, len(instances))
+ ids := make([]string, 0, len(instances))
+ for _, instance := range instances {
+ mapping := map[string]interface{}{
+ "instance_id": instance.InstanceId,
+ "instance_name": instance.InstanceName,
+ "instance_type": instance.InstanceType,
+ "cpu": instance.CPU,
+ "memory": instance.Memory,
+ "availability_zone": instance.Placement.Zone,
+ "project_id": instance.Placement.ProjectId,
+ "image_id": instance.ImageId,
+ "instance_charge_type": instance.InstanceChargeType,
+ "system_disk_type": instance.SystemDisk.DiskType,
+ "system_disk_size": instance.SystemDisk.DiskSize,
+ "system_disk_id": instance.SystemDisk.DiskId,
+ "vpc_id": instance.VirtualPrivateCloud.VpcId,
+ "subnet_id": instance.VirtualPrivateCloud.SubnetId,
+ "internet_charge_type": instance.InternetAccessible.InternetChargeType,
+ "internet_max_bandwidth_out": instance.InternetAccessible.InternetMaxBandwidthOut,
+ "allocate_public_ip": instance.InternetAccessible.PublicIpAssigned,
+ "status": instance.InstanceState,
+ "security_groups": helper.StringsInterfaces(instance.SecurityGroupIds),
+ "tags": flattenCvmTagsMapping(instance.Tags),
+ "create_time": instance.CreatedTime,
+ "expired_time": instance.ExpiredTime,
+ "instance_charge_type_prepaid_renew_flag": instance.RenewFlag,
+ "cam_role_name": instance.CamRoleName,
+ }
+ if len(instance.PublicIpAddresses) > 0 {
+ mapping["public_ip"] = *instance.PublicIpAddresses[0]
+ }
+ if len(instance.PrivateIpAddresses) > 0 {
+ mapping["private_ip"] = *instance.PrivateIpAddresses[0]
+ }
+ dataDisks := make([]map[string]interface{}, 0, len(instance.DataDisks))
+ for _, v := range instance.DataDisks {
+ dataDisk := map[string]interface{}{
+ "data_disk_type": v.DiskType,
+ "data_disk_size": v.DiskSize,
+ "data_disk_id": v.DiskId,
+ "delete_with_instance": v.DeleteWithInstance,
+ }
+ dataDisks = append(dataDisks, dataDisk)
+ }
+ mapping["data_disks"] = dataDisks
+ instanceList = append(instanceList, mapping)
+ ids = append(ids, *instance.InstanceId)
+ }
+ log.Printf("[DEBUG]%s set instance attribute finished", logId)
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err := d.Set("instance_list", instanceList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+ context.WithValue(ctx, "instanceList", instanceList)
+ return nil
+}
+
+func dataSourceTencentCloudInstancesSetReadOutputContent(ctx context.Context) interface{} {
+ instanceList := ctx.Value("instanceList")
+ return instanceList
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_key_pairs.go b/tencentcloud/services/cvm/data_source_tc_key_pairs.go
index 6dbbf49d5f..e541d2a6d6 100644
--- a/tencentcloud/services/cvm/data_source_tc_key_pairs.go
+++ b/tencentcloud/services/cvm/data_source_tc_key_pairs.go
@@ -2,24 +2,17 @@ package cvm
import (
"context"
- "fmt"
- "log"
- "regexp"
- "strings"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
- "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
)
func DataSourceTencentCloudKeyPairs() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudKeyPairsRead,
-
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
@@ -27,31 +20,25 @@ func DataSourceTencentCloudKeyPairs() *schema.Resource {
ConflictsWith: []string{"key_name", "project_id"},
Description: "ID of the key pair to be queried.",
},
+
"key_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"key_id"},
Description: "Name of the key pair to be queried. Support regular expression search, only `^` and `$` are supported.",
},
- "project_id": {
- Type: schema.TypeInt,
- Optional: true,
- ConflictsWith: []string{"key_id"},
- Description: "Project ID of the key pair to be queried.",
- },
- "result_output_file": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Used to save results.",
- },
- // computed
"key_pair_list": {
Type: schema.TypeList,
Computed: true,
Description: "An information list of key pair. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
+ "create_time": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Creation time of the key pair.",
+ },
"key_id": {
Type: schema.TypeString,
Computed: true,
@@ -72,98 +59,59 @@ func DataSourceTencentCloudKeyPairs() *schema.Resource {
Computed: true,
Description: "public key of the key pair.",
},
- "create_time": {
- Type: schema.TypeString,
- Computed: true,
- Description: "Creation time of the key pair.",
- },
},
},
},
+
+ "project_id": {
+ Type: schema.TypeInt,
+ Optional: true,
+ ConflictsWith: []string{"key_id"},
+ Description: "Project ID of the key pair to be queried.",
+ },
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudKeyPairsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_key_pairs.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
+ defer tccommon.InconsistentCheck(d, meta)()
- keyId := d.Get("key_id").(string)
- keyName := d.Get("key_name").(string)
- name := keyName
- if keyName != "" {
- if name[0] == '^' {
- name = name[1:]
- }
- length := len(name)
- if length > 0 && name[length-1] == '$' {
- name = name[:length-1]
- }
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- pattern := `^[a-zA-Z0-9_]+$`
- if match, _ := regexp.MatchString(pattern, name); !match {
- return fmt.Errorf("key_name only support letters, numbers, and _ : %s", keyName)
- }
- }
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
- var projectId *int
- if v, ok := d.GetOkExists("project_id"); ok {
- vv := v.(int)
- projectId = &vv
- }
-
- var keyPairs []*cvm.KeyPair
- var errRet error
+ paramMap := make(map[string]interface{})
+ var respData []*cvm.KeyPair
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- keyPairs, errRet = cvmService.DescribeKeyPairByFilter(ctx, keyId, name, projectId)
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+ result, e := service.DescribeKeyPairsByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- keyPairList := make([]map[string]interface{}, 0, len(keyPairs))
- ids := make([]string, 0, len(keyPairs))
- namePattern, _ := regexp.Compile(keyName)
- for _, keyPair := range keyPairs {
- if match := namePattern.MatchString(*keyPair.KeyName); !match {
- continue
- }
- mapping := map[string]interface{}{
- "key_id": keyPair.KeyId,
- "key_name": keyPair.KeyName,
- "project_id": keyPair.ProjectId,
- "create_time": keyPair.CreatedTime,
- }
- if keyPair.PublicKey != nil {
- publicKey := *keyPair.PublicKey
- split := strings.Split(publicKey, " ")
- publicKey = strings.Join(split[0:len(split)-1], " ")
- mapping["public_key"] = publicKey
- }
- keyPairList = append(keyPairList, mapping)
- ids = append(ids, *keyPair.KeyId)
- }
-
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("key_pair_list", keyPairList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set key pair list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudKeyPairsReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), keyPairList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudKeyPairsReadOutputContent(ctx)); e != nil {
+ return e
}
}
+
return nil
}
diff --git a/tencentcloud/services/cvm/data_source_tc_key_pairs_extension.go b/tencentcloud/services/cvm/data_source_tc_key_pairs_extension.go
new file mode 100644
index 0000000000..5c7e95ebf9
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_key_pairs_extension.go
@@ -0,0 +1,109 @@
+package cvm
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "regexp"
+ "strings"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudKeyPairsReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.KeyPair) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ var err error
+ keyPairs := *resp
+ keyPairList := make([]map[string]interface{}, 0, len(keyPairs))
+ ids := make([]string, 0, len(keyPairs))
+ keyName := d.Get("key_name").(string)
+ namePattern, _ := regexp.Compile(keyName)
+ for _, keyPair := range keyPairs {
+ if match := namePattern.MatchString(*keyPair.KeyName); !match {
+ continue
+ }
+ mapping := map[string]interface{}{
+ "key_id": keyPair.KeyId,
+ "key_name": keyPair.KeyName,
+ "project_id": keyPair.ProjectId,
+ "create_time": keyPair.CreatedTime,
+ }
+ if keyPair.PublicKey != nil {
+ publicKey := *keyPair.PublicKey
+ split := strings.Split(publicKey, " ")
+ publicKey = strings.Join(split[0:len(split)-1], " ")
+ mapping["public_key"] = publicKey
+ }
+ keyPairList = append(keyPairList, mapping)
+ ids = append(ids, *keyPair.KeyId)
+ }
+
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err = d.Set("key_pair_list", keyPairList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set key pair list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+
+ context.WithValue(ctx, "keyPairList", keyPairList)
+ return nil
+}
+
+func dataSourceTencentCloudKeyPairsReadOutputContent(ctx context.Context) interface{} {
+ eipList := ctx.Value("keyPairList")
+ return eipList
+}
+
+func dataSourceTencentCloudKeyPairsReadPreRequest0(ctx context.Context, req *cvm.DescribeKeyPairsRequest) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ keyId := d.Get("key_id").(string)
+ keyName := d.Get("key_name").(string)
+ name := keyName
+ if keyName != "" {
+ if name[0] == '^' {
+ name = name[1:]
+ }
+ length := len(name)
+ if length > 0 && name[length-1] == '$' {
+ name = name[:length-1]
+ }
+
+ pattern := `^[a-zA-Z0-9_]+$`
+ if match, _ := regexp.MatchString(pattern, name); !match {
+ return fmt.Errorf("key_name only support letters, numbers, and _ : %s", keyName)
+ }
+ }
+
+ var projectId *int
+ if v, ok := d.GetOkExists("project_id"); ok {
+ vv := v.(int)
+ projectId = &vv
+ }
+
+ if keyId != "" {
+ req.KeyIds = []*string{&keyId}
+ }
+
+ req.Filters = make([]*cvm.Filter, 0)
+ if name != "" {
+ filter := &cvm.Filter{
+ Name: helper.String("key-name"),
+ Values: []*string{&name},
+ }
+ req.Filters = append(req.Filters, filter)
+ }
+
+ if projectId != nil {
+ filter := &cvm.Filter{
+ Name: helper.String("project-id"),
+ Values: []*string{helper.String(fmt.Sprintf("%d", *projectId))},
+ }
+ req.Filters = append(req.Filters, filter)
+ }
+
+ return nil
+}
diff --git a/tencentcloud/services/cvm/data_source_tc_placement_groups.go b/tencentcloud/services/cvm/data_source_tc_placement_groups.go
index 4820d818d8..b798881eba 100644
--- a/tencentcloud/services/cvm/data_source_tc_placement_groups.go
+++ b/tencentcloud/services/cvm/data_source_tc_placement_groups.go
@@ -2,36 +2,29 @@ package cvm
import (
"context"
- "log"
-
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudPlacementGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudPlacementGroupsRead,
-
Schema: map[string]*schema.Schema{
- "placement_group_id": {
- Type: schema.TypeString,
- Optional: true,
- Description: "ID of the placement group to be queried.",
- },
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the placement group to be queried.",
},
- "result_output_file": {
+
+ "placement_group_id": {
Type: schema.TypeString,
Optional: true,
- Description: "Used to save results.",
+ Description: "ID of the placement group to be queried.",
},
"placement_group_list": {
@@ -40,107 +33,98 @@ func DataSourceTencentCloudPlacementGroups() *schema.Resource {
Description: "An information list of placement group. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
- "placement_group_id": {
- Type: schema.TypeString,
- Computed: true,
- Description: "ID of the placement group.",
- },
- "name": {
+ "create_time": {
Type: schema.TypeString,
Computed: true,
- Description: "Name of the placement group.",
+ Description: "Creation time of the placement group.",
},
- "type": {
- Type: schema.TypeString,
+ "current_num": {
+ Type: schema.TypeInt,
Computed: true,
- Description: "Type of the placement group.",
+ Description: "Number of hosts in the placement group.",
},
"cvm_quota_total": {
Type: schema.TypeInt,
Computed: true,
Description: "Maximum number of hosts in the placement group.",
},
- "current_num": {
- Type: schema.TypeInt,
- Computed: true,
- Description: "Number of hosts in the placement group.",
- },
"instance_ids": {
Type: schema.TypeList,
Computed: true,
- Elem: &schema.Schema{Type: schema.TypeString},
Description: "Host IDs in the placement group.",
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
},
- "create_time": {
+ "name": {
Type: schema.TypeString,
Computed: true,
- Description: "Creation time of the placement group.",
+ Description: "Name of the placement group.",
+ },
+ "placement_group_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "ID of the placement group.",
+ },
+ "type": {
+ Type: schema.TypeString,
+ Computed: true,
+ Description: "Type of the placement group.",
},
},
},
},
+
+ "result_output_file": {
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Used to save results.",
+ },
},
}
}
func dataSourceTencentCloudPlacementGroupsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_placement_groups.read")()
- logId := tccommon.GetLogId(tccommon.ContextNil)
- ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
- cvmService := CvmService{
- client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
- }
+ defer tccommon.InconsistentCheck(d, meta)()
+
+ logId := tccommon.GetLogId(nil)
+ ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
- var placementGroupId string
- var name string
+ service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
+
+ paramMap := make(map[string]interface{})
if v, ok := d.GetOk("placement_group_id"); ok {
- placementGroupId = v.(string)
+ paramMap["DisasterRecoverGroupIds"] = []*string{helper.String(v.(string))}
}
+
if v, ok := d.GetOk("name"); ok {
- name = v.(string)
+ paramMap["Name"] = helper.String(v.(string))
}
- var placementGroups []*cvm.DisasterRecoverGroup
- var errRet error
+ var respData []*cvm.DisasterRecoverGroup
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
- placementGroups, errRet = cvmService.DescribePlacementGroupByFilter(ctx, placementGroupId, name)
- if errRet != nil {
- return tccommon.RetryError(errRet, tccommon.InternalError)
+ result, e := service.DescribePlacementGroupsByFilter(ctx, paramMap)
+ if e != nil {
+ return tccommon.RetryError(e)
}
+ respData = result
return nil
})
if err != nil {
return err
}
- placementGroupList := make([]map[string]interface{}, 0, len(placementGroups))
- ids := make([]string, 0, len(placementGroups))
- for _, placement := range placementGroups {
- mapping := map[string]interface{}{
- "placement_group_id": placement.DisasterRecoverGroupId,
- "name": placement.Name,
- "type": placement.Type,
- "cvm_quota_total": placement.CvmQuotaTotal,
- "current_num": placement.CurrentNum,
- "instance_ids": helper.StringsInterfaces(placement.InstanceIds),
- "create_time": placement.CreateTime,
- }
- placementGroupList = append(placementGroupList, mapping)
- ids = append(ids, *placement.DisasterRecoverGroupId)
- }
-
- d.SetId(helper.DataResourceIdsHash(ids))
- err = d.Set("placement_group_list", placementGroupList)
- if err != nil {
- log.Printf("[CRITAL]%s provider set placement group list fail, reason:%s\n ", logId, err.Error())
+ if err := dataSourceTencentCloudPlacementGroupsReadPostHandleResponse0(ctx, paramMap, &respData); err != nil {
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
- if err := tccommon.WriteToFile(output.(string), placementGroupList); err != nil {
- return err
+ if e := tccommon.WriteToFile(output.(string), dataSourceTencentCloudPlacementGroupsReadOutputContent(ctx)); e != nil {
+ return e
}
}
+
return nil
}
diff --git a/tencentcloud/services/cvm/data_source_tc_placement_groups_extension.go b/tencentcloud/services/cvm/data_source_tc_placement_groups_extension.go
new file mode 100644
index 0000000000..63d2f7b420
--- /dev/null
+++ b/tencentcloud/services/cvm/data_source_tc_placement_groups_extension.go
@@ -0,0 +1,48 @@
+package cvm
+
+import (
+ "context"
+ "log"
+
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
+
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+)
+
+func dataSourceTencentCloudPlacementGroupsReadPostHandleResponse0(ctx context.Context, req map[string]interface{}, resp *[]*cvm.DisasterRecoverGroup) error {
+ d := tccommon.ResourceDataFromContext(ctx)
+ logId := tccommon.GetLogId(tccommon.ContextNil)
+ placementGroups := *resp
+ var err error
+ placementGroupList := make([]map[string]interface{}, 0, len(placementGroups))
+ ids := make([]string, 0, len(placementGroups))
+ for _, placement := range placementGroups {
+ mapping := map[string]interface{}{
+ "placement_group_id": placement.DisasterRecoverGroupId,
+ "name": placement.Name,
+ "type": placement.Type,
+ "cvm_quota_total": placement.CvmQuotaTotal,
+ "current_num": placement.CurrentNum,
+ "instance_ids": helper.StringsInterfaces(placement.InstanceIds),
+ "create_time": placement.CreateTime,
+ }
+ placementGroupList = append(placementGroupList, mapping)
+ ids = append(ids, *placement.DisasterRecoverGroupId)
+ }
+
+ d.SetId(helper.DataResourceIdsHash(ids))
+ err = d.Set("placement_group_list", placementGroupList)
+ if err != nil {
+ log.Printf("[CRITAL]%s provider set placement group list fail, reason:%s\n ", logId, err.Error())
+ return err
+ }
+
+ context.WithValue(ctx, "placementGroupList", placementGroupList)
+ return nil
+}
+
+func dataSourceTencentCloudPlacementGroupsReadOutputContent(ctx context.Context) interface{} {
+ eipList := ctx.Value("placementGroupList")
+ return eipList
+}
diff --git a/tencentcloud/services/cvm/resource_tc_cvm_renew_instance.go b/tencentcloud/services/cvm/resource_tc_cvm_renew_instance.go
index 37758c7f76..ac982d00d7 100644
--- a/tencentcloud/services/cvm/resource_tc_cvm_renew_instance.go
+++ b/tencentcloud/services/cvm/resource_tc_cvm_renew_instance.go
@@ -1,14 +1,14 @@
package cvm
import (
+ "context"
"log"
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
@@ -17,19 +17,18 @@ func ResourceTencentCloudCvmRenewInstance() *schema.Resource {
Create: resourceTencentCloudCvmRenewInstanceCreate,
Read: resourceTencentCloudCvmRenewInstanceRead,
Delete: resourceTencentCloudCvmRenewInstanceDelete,
-
Schema: map[string]*schema.Schema{
"instance_id": {
+ Type: schema.TypeString,
Required: true,
ForceNew: true,
- Type: schema.TypeString,
- Description: "Instance ID.",
+ Description: "Instance ID. To obtain the instance IDs, you can call DescribeInstances and look for InstanceId in the response.",
},
"instance_charge_prepaid": {
+ Type: schema.TypeList,
Optional: true,
ForceNew: true,
- Type: schema.TypeList,
MaxItems: 1,
Description: "Prepaid mode, that is, yearly and monthly subscription related parameter settings. Through this parameter, you can specify the renewal duration of the Subscription instance, whether to set automatic renewal, and other attributes. For yearly and monthly subscription instances, this parameter is required.",
Elem: &schema.Resource{
@@ -40,26 +39,19 @@ func ResourceTencentCloudCvmRenewInstance() *schema.Resource {
Description: "Subscription period; unit: month; valid values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 24, 36, 48, 60. Note: This field may return null, indicating that no valid value is found.",
},
"renew_flag": {
- Type: schema.TypeString,
- Optional: true,
- Description: "Auto renewal flag. Valid values:\n" +
- "- `NOTIFY_AND_AUTO_RENEW`: notify upon expiration and renew automatically;\n" +
- "- `NOTIFY_AND_MANUAL_RENEW`: notify upon expiration but do not renew automatically;\n" +
- "- `DISABLE_NOTIFY_AND_MANUAL_RENEW`: neither notify upon expiration nor renew automatically;\n" +
- "Default value: NOTIFY_AND_MANUAL_RENEW. If this parameter is specified as NOTIFY_AND_AUTO_RENEW, the instance will be automatically renewed on a monthly basis if the account balance is sufficient. Note: This field may return null, indicating that no valid value is found.",
+ Type: schema.TypeString,
+ Optional: true,
+ Description: "Auto renewal flag. Valid values: