Skip to content

fix(cvm): [] Optimize code logic #2621

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .changelog/2621.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data_source/tencentcloud_eips: Optimize code logic.
```
72 changes: 41 additions & 31 deletions tencentcloud/services/cvm/data_source_tc_eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
func DataSourceTencentCloudEips() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudEipsRead,

Schema: map[string]*schema.Schema{
"eip_id": {
Type: schema.TypeString,
Expand All @@ -40,12 +39,6 @@ func DataSourceTencentCloudEips() *schema.Resource {
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,
Expand Down Expand Up @@ -100,91 +93,108 @@ func DataSourceTencentCloudEips() *schema.Resource {
},
},
},
"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)

client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
vpcService := svcvpc.NewVpcService(client)
tagService := svctag.NewTagService(client)
region := client.Region
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
client = meta.(tccommon.ProviderMeta).GetAPIV3Conn()
vpcService = svcvpc.NewVpcService(client)
tagService = svctag.NewTagService(client)
region = client.Region
eips []*vpc.Address
errRet error
)

filter := make(map[string][]string)
if v, ok := d.GetOk("eip_id"); ok {
filter["address-id"] = []string{v.(string)}
}

if v, ok := d.GetOk("eip_name"); ok {
filter["address-name"] = []string{v.(string)}
}

if v, ok := d.GetOk("public_ip"); ok {
filter["public-ip"] = []string{v.(string)}
}

tags := helper.GetTags(d, "tags")

var eips []*vpc.Address
var errRet error
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
eips, errRet = vpcService.DescribeEipByFilter(ctx, filter)
if errRet != nil {
return tccommon.RetryError(errRet, tccommon.InternalError)
}

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
}

tagsMatch := true

for k, v := range tags {
if respTags[k] != v {
continue EIP_LOOP
tagsMatch = false
break
}
}

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,
}
if tagsMatch {
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)
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())
return err
}

d.SetId(helper.DataResourceIdsHash(ids))

output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err := tccommon.WriteToFile(output.(string), eipList); err != nil {
return err
}
}

return nil
}
27 changes: 25 additions & 2 deletions tencentcloud/services/cvm/data_source_tc_eips.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@ Use this data source to query eip instances.

Example Usage

Query all eips
```hcl
data "tencentcloud_eips" "foo" {
data "tencentcloud_eips" "example" {}
```

Filter eips by `eip_id`

```hcl
data "tencentcloud_eips" "example" {
eip_id = "eip-ry9h95hg"
}
```
```

Filter eips by `eip_name`

```hcl
data "tencentcloud_eips" "example" {
eip_name = "tf_example"
}
```

Filter eips by `public_ip`

```hcl
data "tencentcloud_eips" "example" {
public_ip = "1.123.31.21"
}
```
26 changes: 25 additions & 1 deletion website/docs/d/eips.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,36 @@ Use this data source to query eip instances.

## Example Usage

### Query all eips

```hcl
data "tencentcloud_eips" "foo" {
data "tencentcloud_eips" "example" {}
```



```hcl
data "tencentcloud_eips" "example" {
eip_id = "eip-ry9h95hg"
}
```



```hcl
data "tencentcloud_eips" "example" {
eip_name = "tf_example"
}
```



```hcl
data "tencentcloud_eips" "example" {
public_ip = "1.123.31.21"
}
```

## Argument Reference

The following arguments are supported:
Expand Down
Loading