Skip to content

Commit ba9fadd

Browse files
feat(aad): support aad_ip_ddos_statistics data source
1 parent 92a3fe8 commit ba9fadd

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "Advanced Anti-DDoS"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_aad_ip_ddos_statistics"
5+
description: |-
6+
Use this data source to get the Advanced Anti-DDos IP ddos statistics within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_aad_ip_ddos_statistics
10+
11+
Use this data source to get the Advanced Anti-DDos IP ddos statistics within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_aad_ip_ddos_statistics" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `instance_id` - (Required, String) Specifies the instance ID.
24+
25+
* `ip` - (Required, String) Specifies the IP address.
26+
27+
* `start_time` - (Required, String) Specifies the start time, millisecond timestamp.
28+
29+
* `end_time` - (Required, String) Specifies the end time, millisecond timestamp.
30+
31+
## Attribute Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
* `id` - The data source ID in UUID format.
36+
37+
* `attack_kbps_peak` - The attack peak.
38+
39+
* `in_kbps_peak` - The traffic peak.
40+
41+
* `ddos_count` - The number of attacks.
42+
43+
* `timestamp` - The attack peak timestamp.
44+
45+
* `vip` - The Anti-DDoS IP.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ func Provider() *schema.Provider {
476476
"huaweicloud_aad_bandwidth_curve": aad.DataSourceBandwidthCurve(),
477477
"huaweicloud_aad_custom_rules": aad.DataSourceCustomRules(),
478478
"huaweicloud_aad_frequency_control_rules": aad.DataSourceFrequencyControlRules(),
479+
"huaweicloud_aad_ip_ddos_statistics": aad.DataSourceIpDdosStatistics(),
479480

480481
"huaweicloud_antiddos_config_ranges": antiddos.DataSourceConfigRanges(),
481482
"huaweicloud_antiddos_weekly_protection_statistics": antiddos.DataSourceWeeklyProtectionStatistics(),
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package aad
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/go-uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/chnsz/golangsdk"
14+
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
// Due to limited testing conditions, this data source cannot be tested and the API was not successfully called.
20+
21+
// @API AAD GET /v1/aad/instances/{instance_id}/{ip}/ddos-statistics
22+
func DataSourceIpDdosStatistics() *schema.Resource {
23+
return &schema.Resource{
24+
ReadContext: dataSourceIpDdosStatisticsRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"instance_id": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
"ip": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
},
35+
"start_time": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
},
39+
"end_time": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
},
43+
"attack_kbps_peak": {
44+
Type: schema.TypeInt,
45+
Computed: true,
46+
},
47+
"in_kbps_peak": {
48+
Type: schema.TypeInt,
49+
Computed: true,
50+
},
51+
"ddos_count": {
52+
Type: schema.TypeInt,
53+
Computed: true,
54+
},
55+
"timestamp": {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
},
59+
"vip": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
},
64+
}
65+
}
66+
67+
func buildIpDdosStatisticsQueryParams(d *schema.ResourceData) string {
68+
queryParams := fmt.Sprintf("?start_time=%v", d.Get("start_time"))
69+
queryParams += fmt.Sprintf("&end_time=%v", d.Get("end_time"))
70+
71+
return queryParams
72+
}
73+
74+
func dataSourceIpDdosStatisticsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
75+
var (
76+
cfg = meta.(*config.Config)
77+
region = cfg.GetRegion(d)
78+
product = "aad"
79+
httpUrl = "v1/aad/instances/{instance_id}/{ip}/ddos-statistics"
80+
)
81+
82+
client, err := cfg.NewServiceClient(product, region)
83+
if err != nil {
84+
return diag.Errorf("error creating AAD client: %s", err)
85+
}
86+
87+
requestPath := client.Endpoint + httpUrl
88+
requestPath = strings.ReplaceAll(requestPath, "{instance_id}", d.Get("instance_id").(string))
89+
requestPath = strings.ReplaceAll(requestPath, "{ip}", d.Get("ip").(string))
90+
requestPath += buildIpDdosStatisticsQueryParams(d)
91+
requestOpt := golangsdk.RequestOpts{
92+
KeepResponseBody: true,
93+
MoreHeaders: map[string]string{
94+
"Content-Type": "application/json",
95+
},
96+
}
97+
98+
requestResp, err := client.Request("GET", requestPath, &requestOpt)
99+
if err != nil {
100+
return diag.Errorf("error retrieving AAD IP ddos statistics: %s", err)
101+
}
102+
103+
respBody, err := utils.FlattenResponse(requestResp)
104+
if err != nil {
105+
return diag.FromErr(err)
106+
}
107+
108+
generateUUID, err := uuid.GenerateUUID()
109+
if err != nil {
110+
return diag.Errorf("unable to generate ID: %s", err)
111+
}
112+
113+
d.SetId(generateUUID)
114+
115+
mErr := multierror.Append(nil,
116+
d.Set("attack_kbps_peak", utils.PathSearch("attack_kbps_peak", respBody, nil)),
117+
d.Set("in_kbps_peak", utils.PathSearch("in_kbps_peak", respBody, nil)),
118+
d.Set("ddos_count", utils.PathSearch("ddos_count", respBody, nil)),
119+
d.Set("timestamp", utils.PathSearch("timestamp", respBody, nil)),
120+
)
121+
122+
return diag.FromErr(mErr.ErrorOrNil())
123+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package antiddos
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
// Note: Due to limited test conditions, this test case cannot be executed successfully.
13+
func TestAccIpDdosStatisticsDataSource_basic(t *testing.T) {
14+
var (
15+
dataSourceName = "data.huaweicloud_aad_ip_ddos_statistics.test"
16+
dc = acceptance.InitDataSourceCheck(dataSourceName)
17+
)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acceptance.TestAccPreCheck(t)
22+
acceptance.TestAccPreCheckAadInstanceID(t)
23+
},
24+
ProviderFactories: acceptance.TestAccProviderFactories,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testIpDdosStatistics_basic(),
28+
Check: resource.ComposeTestCheckFunc(
29+
dc.CheckResourceExists(),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "attack_kbps_peak"),
31+
resource.TestCheckResourceAttrSet(dataSourceName, "in_kbps_peak"),
32+
resource.TestCheckResourceAttrSet(dataSourceName, "ddos_count"),
33+
resource.TestCheckResourceAttrSet(dataSourceName, "timestamp"),
34+
resource.TestCheckResourceAttrSet(dataSourceName, "vip"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
// Parameter `ip` are mock data.
42+
func testIpDdosStatistics_basic() string {
43+
return fmt.Sprintf(`
44+
data "huaweicloud_aad_ip_ddos_statistics" "test" {
45+
instance_id = "%[1]s"
46+
ip = "12.1.2.117"
47+
start_time = "1755734400"
48+
end_time = "1755820800"
49+
}
50+
`, acceptance.HW_AAD_INSTANCE_ID)
51+
}

0 commit comments

Comments
 (0)