Skip to content

[WIP]feat(cvm): [117301871] cvm iacg v2 #2677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 78 additions & 93 deletions tencentcloud/services/cvm/data_source_tc_eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,36 @@

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,
Computed: true,
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,
Expand All @@ -67,30 +47,25 @@
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,
Expand All @@ -100,91 +75,101 @@
},
},
},

"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)

Check failure on line 110 in tencentcloud/services/cvm/data_source_tc_eips.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
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
}
65 changes: 65 additions & 0 deletions tencentcloud/services/cvm/data_source_tc_eips_extension.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 62 in tencentcloud/services/cvm/data_source_tc_eips_extension.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unusedresult: result of context.WithValue call not used (govet)
d.SetId(helper.DataResourceIdsHash(ids))
return nil
}
Loading
Loading