Skip to content

Commit 5af17fe

Browse files
authored
add eip resource (#1780)
* add eip resource * add changelog
1 parent 00c615a commit 5af17fe

22 files changed

+1188
-0
lines changed

.changelog/1780.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```release-note:new-resource
2+
tencentcloud_eip_public_address_adjust
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_eip_normal_address_return
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_eip_address_quota
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_eip_network_account_type
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_vpc_bandwidth_package_quota
19+
```
20+
21+
```release-note:new-data-source
22+
tencentcloud_vpc_bandwidth_package_bill_usage
23+
```
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Use this data source to query detailed information of vpc address_quota
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_eip_address_quota" "address_quota" {}
8+
```
9+
*/
10+
package tencentcloud
11+
12+
import (
13+
"context"
14+
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
18+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
19+
)
20+
21+
func dataSourceTencentCloudEipAddressQuota() *schema.Resource {
22+
return &schema.Resource{
23+
Read: dataSourceTencentCloudEipAddressQuotaRead,
24+
Schema: map[string]*schema.Schema{
25+
"quota_set": {
26+
Computed: true,
27+
Type: schema.TypeList,
28+
Description: "The specified account EIP quota information.",
29+
Elem: &schema.Resource{
30+
Schema: map[string]*schema.Schema{
31+
"quota_id": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "Quota name: TOTAL_EIP_QUOTA,DAILY_EIP_APPLY,DAILY_PUBLIC_IP_ASSIGN.",
35+
},
36+
"quota_current": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
Description: "Current count.",
40+
},
41+
"quota_limit": {
42+
Type: schema.TypeInt,
43+
Computed: true,
44+
Description: "quota count.",
45+
},
46+
},
47+
},
48+
},
49+
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used to save results.",
54+
},
55+
},
56+
}
57+
}
58+
59+
func dataSourceTencentCloudEipAddressQuotaRead(d *schema.ResourceData, meta interface{}) error {
60+
defer logElapsed("data_source.tencentcloud_vpc_address_quota.read")()
61+
defer inconsistentCheck(d, meta)()
62+
63+
logId := getLogId(contextNil)
64+
65+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
66+
67+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
68+
69+
var quotaSet []*vpc.Quota
70+
71+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
72+
result, e := service.DescribeEipAddressQuota(ctx)
73+
if e != nil {
74+
return retryError(e)
75+
}
76+
quotaSet = result
77+
return nil
78+
})
79+
if err != nil {
80+
return err
81+
}
82+
83+
ids := make([]string, 0, len(quotaSet))
84+
tmpList := make([]map[string]interface{}, 0, len(quotaSet))
85+
86+
if quotaSet != nil {
87+
for _, quota := range quotaSet {
88+
quotaMap := map[string]interface{}{}
89+
90+
if quota.QuotaId != nil {
91+
quotaMap["quota_id"] = quota.QuotaId
92+
}
93+
94+
if quota.QuotaCurrent != nil {
95+
quotaMap["quota_current"] = quota.QuotaCurrent
96+
}
97+
98+
if quota.QuotaLimit != nil {
99+
quotaMap["quota_limit"] = quota.QuotaLimit
100+
}
101+
102+
ids = append(ids, *quota.QuotaId)
103+
tmpList = append(tmpList, quotaMap)
104+
}
105+
106+
_ = d.Set("quota_set", tmpList)
107+
}
108+
109+
d.SetId(helper.DataResourceIdsHash(ids))
110+
output, ok := d.GetOk("result_output_file")
111+
if ok && output.(string) != "" {
112+
if e := writeToFile(output.(string), tmpList); e != nil {
113+
return e
114+
}
115+
}
116+
return nil
117+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudEipAddressQuotaDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccEipAddressQuotaDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_eip_address_quota.address_quota")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccEipAddressQuotaDataSource = `
26+
27+
data "tencentcloud_eip_address_quota" "address_quota" {}
28+
29+
`
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Use this data source to query detailed information of eip network_account_type
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_eip_network_account_type" "network_account_type" {}
8+
```
9+
*/
10+
package tencentcloud
11+
12+
import (
13+
"context"
14+
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
)
18+
19+
func dataSourceTencentCloudEipNetworkAccountType() *schema.Resource {
20+
return &schema.Resource{
21+
Read: dataSourceTencentCloudEipNetworkAccountTypeRead,
22+
Schema: map[string]*schema.Schema{
23+
"network_account_type": {
24+
Computed: true,
25+
Type: schema.TypeString,
26+
Description: "The network type of the user account, STANDARD is a standard user, LEGACY is a traditional user.",
27+
},
28+
29+
"result_output_file": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Description: "Used to save results.",
33+
},
34+
},
35+
}
36+
}
37+
38+
func dataSourceTencentCloudEipNetworkAccountTypeRead(d *schema.ResourceData, meta interface{}) error {
39+
defer logElapsed("data_source.tencentcloud_eip_network_account_type.read")()
40+
defer inconsistentCheck(d, meta)()
41+
42+
var networkAccountType *string
43+
logId := getLogId(contextNil)
44+
45+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
46+
47+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
48+
49+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
50+
result, e := service.DescribeEipNetworkAccountType(ctx)
51+
if e != nil {
52+
return retryError(e)
53+
}
54+
networkAccountType = result
55+
return nil
56+
})
57+
if err != nil {
58+
return err
59+
}
60+
61+
if networkAccountType != nil {
62+
_ = d.Set("network_account_type", networkAccountType)
63+
}
64+
65+
d.SetId(*networkAccountType)
66+
output, ok := d.GetOk("result_output_file")
67+
if ok && output.(string) != "" {
68+
if e := writeToFile(output.(string), *networkAccountType); e != nil {
69+
return e
70+
}
71+
}
72+
return nil
73+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudEipNetworkAccountTypeDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccEipNetworkAccountTypeDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_eip_network_account_type.network_account_type")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccEipNetworkAccountTypeDataSource = `
26+
27+
data "tencentcloud_eip_network_account_type" "network_account_type" {}
28+
29+
`
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Use this data source to query detailed information of vpc bandwidth_package_bill_usage
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_vpc_bandwidth_package_bill_usage" "bandwidth_package_bill_usage" {
8+
bandwidth_package_id = "bwp-234rfgt5"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
"fmt"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudVpcBandwidthPackageBillUsage() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudVpcBandwidthPackageBillUsageRead,
27+
Schema: map[string]*schema.Schema{
28+
"bandwidth_package_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "The unique ID of the postpaid bandwidth package.",
32+
},
33+
34+
"bandwidth_package_bill_bandwidth_set": {
35+
Computed: true,
36+
Type: schema.TypeList,
37+
Description: "current billing amount.",
38+
Elem: &schema.Resource{
39+
Schema: map[string]*schema.Schema{
40+
"bandwidth_usage": {
41+
Type: schema.TypeFloat,
42+
Computed: true,
43+
Description: "Current billing amount in Mbps.",
44+
},
45+
},
46+
},
47+
},
48+
49+
"result_output_file": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
Description: "Used to save results.",
53+
},
54+
},
55+
}
56+
}
57+
58+
func dataSourceTencentCloudVpcBandwidthPackageBillUsageRead(d *schema.ResourceData, meta interface{}) error {
59+
defer logElapsed("data_source.tencentcloud_vpc_bandwidth_package_bill_usage.read")()
60+
defer inconsistentCheck(d, meta)()
61+
62+
logId := getLogId(contextNil)
63+
64+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
65+
66+
paramMap := make(map[string]interface{})
67+
if v, ok := d.GetOk("bandwidth_package_id"); ok {
68+
paramMap["BandwidthPackageId"] = helper.String(v.(string))
69+
}
70+
71+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
72+
73+
var bandwidthPackageBillBandwidthSet []*vpc.BandwidthPackageBillBandwidth
74+
75+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
76+
result, e := service.DescribeVpcBandwidthPackageBillUsageByFilter(ctx, paramMap)
77+
if e != nil {
78+
return retryError(e)
79+
}
80+
bandwidthPackageBillBandwidthSet = result
81+
return nil
82+
})
83+
if err != nil {
84+
return err
85+
}
86+
87+
ids := make([]string, 0, len(bandwidthPackageBillBandwidthSet))
88+
tmpList := make([]map[string]interface{}, 0, len(bandwidthPackageBillBandwidthSet))
89+
90+
if bandwidthPackageBillBandwidthSet != nil {
91+
for _, bandwidthPackageBillBandwidth := range bandwidthPackageBillBandwidthSet {
92+
bandwidthPackageBillBandwidthMap := map[string]interface{}{}
93+
94+
if bandwidthPackageBillBandwidth.BandwidthUsage != nil {
95+
bandwidthPackageBillBandwidthMap["bandwidth_usage"] = bandwidthPackageBillBandwidth.BandwidthUsage
96+
}
97+
98+
ids = append(ids, fmt.Sprintf("%f", *bandwidthPackageBillBandwidth.BandwidthUsage))
99+
tmpList = append(tmpList, bandwidthPackageBillBandwidthMap)
100+
}
101+
102+
_ = d.Set("bandwidth_package_bill_bandwidth_set", tmpList)
103+
}
104+
105+
d.SetId(helper.DataResourceIdsHash(ids))
106+
output, ok := d.GetOk("result_output_file")
107+
if ok && output.(string) != "" {
108+
if e := writeToFile(output.(string), tmpList); e != nil {
109+
return e
110+
}
111+
}
112+
return nil
113+
}

0 commit comments

Comments
 (0)