Skip to content

Commit 626861a

Browse files
authored
add dc datasource (#1823)
* add dc datasource * add dc datasource
1 parent 6fe8f45 commit 626861a

13 files changed

+770
-0
lines changed

.changelog/1823.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:new-data-source
2+
tencentcloud_dc_internet_address_quota
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_dc_internet_address_statistics
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_dc_public_direct_connect_tunnel_routes
11+
```
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Use this data source to query detailed information of dc internet_address_quota
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dc_internet_address_quota" "internet_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+
dc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410"
18+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
19+
)
20+
21+
func dataSourceTencentCloudDcInternetAddressQuota() *schema.Resource {
22+
return &schema.Resource{
23+
Read: dataSourceTencentCloudDcInternetAddressQuotaRead,
24+
Schema: map[string]*schema.Schema{
25+
"ipv6_prefix_len": {
26+
Computed: true,
27+
Type: schema.TypeInt,
28+
Description: "The minimum prefix length allowed on the IPv6 Internet public network.",
29+
},
30+
31+
"ipv4_bgp_quota": {
32+
Computed: true,
33+
Type: schema.TypeInt,
34+
Description: "BGP type IPv4 Internet address quota.",
35+
},
36+
37+
"ipv4_other_quota": {
38+
Computed: true,
39+
Type: schema.TypeInt,
40+
Description: "Non-BGP type IPv4 Internet address quota.",
41+
},
42+
43+
"ipv4_bgp_num": {
44+
Computed: true,
45+
Type: schema.TypeInt,
46+
Description: "Number of used BGP type IPv4 Internet addresses.",
47+
},
48+
49+
"ipv4_other_num": {
50+
Computed: true,
51+
Type: schema.TypeInt,
52+
Description: "The number of non-BGP Internet addresses used.",
53+
},
54+
55+
"result_output_file": {
56+
Type: schema.TypeString,
57+
Optional: true,
58+
Description: "Used to save results.",
59+
},
60+
},
61+
}
62+
}
63+
64+
func dataSourceTencentCloudDcInternetAddressQuotaRead(d *schema.ResourceData, meta interface{}) error {
65+
defer logElapsed("data_source.tencentcloud_dc_internet_address_quota.read")()
66+
defer inconsistentCheck(d, meta)()
67+
68+
logId := getLogId(contextNil)
69+
70+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
71+
72+
service := DcService{client: meta.(*TencentCloudClient).apiV3Conn}
73+
74+
var quota *dc.DescribeInternetAddressQuotaResponse
75+
76+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
77+
result, e := service.DescribeDcInternetAddressQuota(ctx)
78+
if e != nil {
79+
return retryError(e)
80+
}
81+
quota = result
82+
return nil
83+
})
84+
if err != nil {
85+
return err
86+
}
87+
88+
if quota.Response.Ipv6PrefixLen != nil {
89+
_ = d.Set("ipv6_prefix_len", quota.Response.Ipv6PrefixLen)
90+
}
91+
92+
if quota.Response.Ipv4BgpQuota != nil {
93+
_ = d.Set("ipv4_bgp_quota", quota.Response.Ipv4BgpQuota)
94+
}
95+
96+
if quota.Response.Ipv4OtherQuota != nil {
97+
_ = d.Set("ipv4_other_quota", quota.Response.Ipv4OtherQuota)
98+
}
99+
100+
if quota.Response.Ipv4BgpNum != nil {
101+
_ = d.Set("ipv4_bgp_num", quota.Response.Ipv4BgpNum)
102+
}
103+
104+
if quota.Response.Ipv4OtherNum != nil {
105+
_ = d.Set("ipv4_other_num", quota.Response.Ipv4OtherNum)
106+
}
107+
108+
tmpList := []map[string]interface{}{
109+
{
110+
"ipv6_prefix_len": quota.Response.Ipv6PrefixLen,
111+
"ipv4_bgp_quota": quota.Response.Ipv4BgpQuota,
112+
"ipv4_other_quota": quota.Response.Ipv4OtherQuota,
113+
"ipv4_bgp_num": quota.Response.Ipv4BgpNum,
114+
"ipv4_other_num": quota.Response.Ipv4OtherNum,
115+
},
116+
}
117+
118+
d.SetId(helper.Int64ToStr(*quota.Response.Ipv4BgpQuota))
119+
output, ok := d.GetOk("result_output_file")
120+
if ok && output.(string) != "" {
121+
if e := writeToFile(output.(string), tmpList); e != nil {
122+
return e
123+
}
124+
}
125+
return nil
126+
}
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 TestAccTencentCloudDcInternetAddressQuotaDataSource_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: testAccDcInternetAddressQuotaDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_dc_internet_address_quota.internet_address_quota")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccDcInternetAddressQuotaDataSource = `
26+
27+
data "tencentcloud_dc_internet_address_quota" "internet_address_quota" {}
28+
29+
`
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Use this data source to query detailed information of dc internet_address_statistics
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dc_internet_address_statistics" "internet_address_statistics" {}
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+
dc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410"
18+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
19+
)
20+
21+
func dataSourceTencentCloudDcInternetAddressStatistics() *schema.Resource {
22+
return &schema.Resource{
23+
Read: dataSourceTencentCloudDcInternetAddressStatisticsRead,
24+
Schema: map[string]*schema.Schema{
25+
"internet_address_statistics": {
26+
Computed: true,
27+
Type: schema.TypeList,
28+
Description: "Statistical Information List of Internet Public Network Addresses.",
29+
Elem: &schema.Resource{
30+
Schema: map[string]*schema.Schema{
31+
"region": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "region.",
35+
},
36+
"subnet_num": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
Description: "Number of Internet public network addresses.",
40+
},
41+
},
42+
},
43+
},
44+
45+
"result_output_file": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Used to save results.",
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceTencentCloudDcInternetAddressStatisticsRead(d *schema.ResourceData, meta interface{}) error {
55+
defer logElapsed("data_source.tencentcloud_dc_internet_address_statistics.read")()
56+
defer inconsistentCheck(d, meta)()
57+
58+
logId := getLogId(contextNil)
59+
60+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
61+
62+
service := DcService{client: meta.(*TencentCloudClient).apiV3Conn}
63+
64+
var internetAddressStatistics []*dc.InternetAddressStatistics
65+
66+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
67+
result, e := service.DescribeDcInternetAddressStatistics(ctx)
68+
if e != nil {
69+
return retryError(e)
70+
}
71+
internetAddressStatistics = result
72+
return nil
73+
})
74+
if err != nil {
75+
return err
76+
}
77+
78+
ids := make([]string, 0, len(internetAddressStatistics))
79+
tmpList := make([]map[string]interface{}, 0, len(internetAddressStatistics))
80+
81+
if internetAddressStatistics != nil {
82+
for _, internetAddressStatistics := range internetAddressStatistics {
83+
internetAddressStatisticsMap := map[string]interface{}{}
84+
85+
if internetAddressStatistics.Region != nil {
86+
internetAddressStatisticsMap["region"] = internetAddressStatistics.Region
87+
}
88+
89+
if internetAddressStatistics.SubnetNum != nil {
90+
internetAddressStatisticsMap["subnet_num"] = internetAddressStatistics.SubnetNum
91+
}
92+
93+
ids = append(ids, *internetAddressStatistics.Region)
94+
tmpList = append(tmpList, internetAddressStatisticsMap)
95+
}
96+
97+
_ = d.Set("internet_address_statistics", tmpList)
98+
}
99+
100+
d.SetId(helper.DataResourceIdsHash(ids))
101+
output, ok := d.GetOk("result_output_file")
102+
if ok && output.(string) != "" {
103+
if e := writeToFile(output.(string), tmpList); e != nil {
104+
return e
105+
}
106+
}
107+
return nil
108+
}
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 TestAccTencentCloudDcInternetAddressStatisticsDataSource_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: testAccDcInternetAddressStatisticsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_dc_internet_address_statistics.internet_address_statistics")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccDcInternetAddressStatisticsDataSource = `
26+
27+
data "tencentcloud_dc_internet_address_statistics" "internet_address_statistics" {}
28+
29+
`

0 commit comments

Comments
 (0)