|
| 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 | +} |
0 commit comments