-
Notifications
You must be signed in to change notification settings - Fork 194
feat(aad): support aad_ip_ddos_statistics
and aad_flow_block
data sources
#7623
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
jinyangyang222
wants to merge
2
commits into
huaweicloud:master
Choose a base branch
from
jinyangyang222:feat-aad/support-ip_ddos_statistics-dataSource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+445
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
subcategory: "Advanced Anti-DDoS" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_aad_flow_block" | ||
description: |- | ||
Use this data source to get the list of Advanced Anti-DDos flow block information within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_aad_flow_block | ||
|
||
Use this data source to get the list of Advanced Anti-DDos flow block information within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_aad_flow_block" "test" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `instance_id` - (Required, String) Specifies the instance ID. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID in UUID format. | ||
|
||
* `ips` - The ips list. | ||
The [ips](#ips_struct) structure is documented below. | ||
|
||
<a name="ips_struct"></a> | ||
The `ips` block supports: | ||
|
||
* `ip_id` - The IP ID. | ||
|
||
* `ip` - The IP. | ||
|
||
* `isp` - The isp. | ||
|
||
* `data_center` - The data center. | ||
|
||
* `foreign_switch_status` - The overseas region ban status. `0` represents closed, `1` represents open. | ||
|
||
* `udp_switch_status` - The UDP protocol disabled. `0` represents closed, `1` represents open. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
subcategory: "Advanced Anti-DDoS" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_aad_ip_ddos_statistics" | ||
description: |- | ||
Use this data source to get the Advanced Anti-DDos IP ddos statistics within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_aad_ip_ddos_statistics | ||
|
||
Use this data source to get the Advanced Anti-DDos IP ddos statistics within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_aad_ip_ddos_statistics" "test" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `instance_id` - (Required, String) Specifies the instance ID. | ||
|
||
* `ip` - (Required, String) Specifies the IP address. | ||
|
||
* `start_time` - (Required, String) Specifies the start time, millisecond timestamp. | ||
|
||
* `end_time` - (Required, String) Specifies the end time, millisecond timestamp. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID in UUID format. | ||
|
||
* `attack_kbps_peak` - The attack peak. | ||
|
||
* `in_kbps_peak` - The traffic peak. | ||
|
||
* `ddos_count` - The number of attacks. | ||
|
||
* `timestamp` - The attack peak timestamp. | ||
|
||
* `vip` - The Anti-DDoS IP. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
huaweicloud/services/aad/data_source_huaweicloud_aad_flow_block.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package aad | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// Due to limited testing conditions, this data source cannot be tested and the API was not successfully called. | ||
|
||
// @API AAD GET /v2/aad/policies/ddos/flow-block | ||
func DataSourceFlowBlock() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceFlowBlockRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"ips": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"ip_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ip": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"isp": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"data_center": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"foreign_switch_status": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"udp_switch_status": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func buildFlowBlockQueryParams(d *schema.ResourceData) string { | ||
return fmt.Sprintf("?instance_id=%v", d.Get("instance_id")) | ||
} | ||
|
||
func dataSourceFlowBlockRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
cfg = meta.(*config.Config) | ||
region = cfg.GetRegion(d) | ||
product = "aad" | ||
httpUrl = "v2/aad/policies/ddos/flow-block" | ||
) | ||
|
||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return diag.Errorf("error creating AAD client: %s", err) | ||
} | ||
|
||
requestPath := client.Endpoint + httpUrl | ||
requestPath += buildFlowBlockQueryParams(d) | ||
requestOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
requestResp, err := client.Request("GET", requestPath, &requestOpt) | ||
if err != nil { | ||
return diag.Errorf("error retrieving AAD flow block information: %s", err) | ||
} | ||
|
||
respBody, err := utils.FlattenResponse(requestResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
generateUUID, err := uuid.GenerateUUID() | ||
jinyangyang222 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
|
||
d.SetId(generateUUID) | ||
|
||
mErr := multierror.Append(nil, | ||
d.Set("ips", flattenFlowBlockIps(utils.PathSearch("ips", respBody, make([]interface{}, 0)).([]interface{}))), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func flattenFlowBlockIps(resp []interface{}) []interface{} { | ||
if len(resp) == 0 { | ||
return nil | ||
} | ||
|
||
rst := make([]interface{}, 0, len(resp)) | ||
for _, v := range resp { | ||
rst = append(rst, map[string]interface{}{ | ||
"ip_id": utils.PathSearch("ip_id", v, nil), | ||
"ip": utils.PathSearch("ip", v, nil), | ||
"isp": utils.PathSearch("isp", v, nil), | ||
"data_center": utils.PathSearch("data_center", v, nil), | ||
"foreign_switch_status": utils.PathSearch("foreign_switch_status", v, nil), | ||
"udp_switch_status": utils.PathSearch("udp_switch_status", v, nil), | ||
}) | ||
} | ||
|
||
return rst | ||
} |
123 changes: 123 additions & 0 deletions
123
huaweicloud/services/aad/data_source_huaweicloud_aad_ip_ddos_statistics.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package aad | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// Due to limited testing conditions, this data source cannot be tested and the API was not successfully called. | ||
|
||
// @API AAD GET /v1/aad/instances/{instance_id}/{ip}/ddos-statistics | ||
func DataSourceIpDdosStatistics() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceIpDdosStatisticsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance_id": { | ||
jinyangyang222 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"ip": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"start_time": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"end_time": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"attack_kbps_peak": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"in_kbps_peak": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"ddos_count": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"timestamp": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"vip": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func buildIpDdosStatisticsQueryParams(d *schema.ResourceData) string { | ||
queryParams := fmt.Sprintf("?start_time=%v", d.Get("start_time")) | ||
queryParams += fmt.Sprintf("&end_time=%v", d.Get("end_time")) | ||
|
||
return queryParams | ||
} | ||
|
||
func dataSourceIpDdosStatisticsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
cfg = meta.(*config.Config) | ||
region = cfg.GetRegion(d) | ||
product = "aad" | ||
httpUrl = "v1/aad/instances/{instance_id}/{ip}/ddos-statistics" | ||
) | ||
|
||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return diag.Errorf("error creating AAD client: %s", err) | ||
} | ||
|
||
requestPath := client.Endpoint + httpUrl | ||
requestPath = strings.ReplaceAll(requestPath, "{instance_id}", d.Get("instance_id").(string)) | ||
requestPath = strings.ReplaceAll(requestPath, "{ip}", d.Get("ip").(string)) | ||
requestPath += buildIpDdosStatisticsQueryParams(d) | ||
requestOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
requestResp, err := client.Request("GET", requestPath, &requestOpt) | ||
if err != nil { | ||
return diag.Errorf("error retrieving AAD IP ddos statistics: %s", err) | ||
} | ||
|
||
respBody, err := utils.FlattenResponse(requestResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
generateUUID, err := uuid.GenerateUUID() | ||
jinyangyang222 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
|
||
d.SetId(generateUUID) | ||
|
||
mErr := multierror.Append(nil, | ||
d.Set("attack_kbps_peak", utils.PathSearch("attack_kbps_peak", respBody, nil)), | ||
d.Set("in_kbps_peak", utils.PathSearch("in_kbps_peak", respBody, nil)), | ||
d.Set("ddos_count", utils.PathSearch("ddos_count", respBody, nil)), | ||
d.Set("timestamp", utils.PathSearch("timestamp", respBody, nil)), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.