|
| 1 | +package waf |
| 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 | +// @API WAF GET /v1/{project_id}/waf/overviews/bandwidth/timeline |
| 20 | +func DataSourceWafOverviewsBandwidthTimeline() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + ReadContext: dataSourceWafOverviewsBandwidthTimelineRead, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "region": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "from": { |
| 31 | + Type: schema.TypeInt, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "to": { |
| 35 | + Type: schema.TypeInt, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + "group_by": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "display_option": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + }, |
| 46 | + "hosts": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + "instances": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + }, |
| 54 | + "enterprise_project_id": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + }, |
| 58 | + "bandwidths": { |
| 59 | + Type: schema.TypeList, |
| 60 | + Computed: true, |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "key": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "timeline": { |
| 68 | + Type: schema.TypeList, |
| 69 | + Computed: true, |
| 70 | + Elem: &schema.Resource{ |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "time": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "num": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func buildOverviewsBandwidthQueryParams(cfg *config.Config, d *schema.ResourceData) string { |
| 91 | + epsId := cfg.GetEnterpriseProjectID(d) |
| 92 | + rst := fmt.Sprintf("?from=%v&to=%v", d.Get("from"), d.Get("to")) |
| 93 | + |
| 94 | + if groupBy, ok := d.GetOk("group_by"); ok { |
| 95 | + rst += fmt.Sprintf("&group_by=%v", groupBy) |
| 96 | + } |
| 97 | + |
| 98 | + if displayOption, ok := d.GetOk("display_option"); ok { |
| 99 | + rst += fmt.Sprintf("&display_option=%v", displayOption) |
| 100 | + } |
| 101 | + |
| 102 | + if hostId, ok := d.GetOk("hosts"); ok { |
| 103 | + rst += fmt.Sprintf("&hosts=%v", hostId) |
| 104 | + } |
| 105 | + |
| 106 | + if instanceId, ok := d.GetOk("instances"); ok { |
| 107 | + rst += fmt.Sprintf("&instances=%v", instanceId) |
| 108 | + } |
| 109 | + |
| 110 | + if epsId != "" { |
| 111 | + rst += fmt.Sprintf("&enterprise_project_id=%v", epsId) |
| 112 | + } |
| 113 | + |
| 114 | + return rst |
| 115 | +} |
| 116 | + |
| 117 | +func dataSourceWafOverviewsBandwidthTimelineRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 118 | + var ( |
| 119 | + cfg = meta.(*config.Config) |
| 120 | + region = cfg.GetRegion(d) |
| 121 | + httpUrl = "v1/{project_id}/waf/overviews/bandwidth/timeline" |
| 122 | + ) |
| 123 | + |
| 124 | + client, err := cfg.NewServiceClient("waf", region) |
| 125 | + if err != nil { |
| 126 | + return diag.Errorf("error creating WAF client: %s", err) |
| 127 | + } |
| 128 | + |
| 129 | + listPath := client.Endpoint + httpUrl |
| 130 | + listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID) |
| 131 | + listPath += buildOverviewsBandwidthQueryParams(cfg, d) |
| 132 | + |
| 133 | + listOpt := golangsdk.RequestOpts{ |
| 134 | + KeepResponseBody: true, |
| 135 | + MoreHeaders: map[string]string{ |
| 136 | + "Content-Type": "application/json;charset=utf8", |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + listResp, err := client.Request("GET", listPath, &listOpt) |
| 141 | + if err != nil { |
| 142 | + return diag.Errorf("error retrieving bandwidth usage statistics: %s", err) |
| 143 | + } |
| 144 | + |
| 145 | + listRespBody, err := utils.FlattenResponse(listResp) |
| 146 | + if err != nil { |
| 147 | + return diag.FromErr(err) |
| 148 | + } |
| 149 | + |
| 150 | + listArray, ok := listRespBody.([]interface{}) |
| 151 | + if !ok { |
| 152 | + return diag.Errorf("convert inteface array failed") |
| 153 | + } |
| 154 | + |
| 155 | + dataSourceId, err := uuid.GenerateUUID() |
| 156 | + if err != nil { |
| 157 | + return diag.Errorf("unable to generate ID: %s", err) |
| 158 | + } |
| 159 | + |
| 160 | + d.SetId(dataSourceId) |
| 161 | + |
| 162 | + mErr := multierror.Append(nil, |
| 163 | + d.Set("region", region), |
| 164 | + d.Set("bandwidths", flattenOverviewsBandwidths(listArray)), |
| 165 | + ) |
| 166 | + |
| 167 | + return diag.FromErr(mErr.ErrorOrNil()) |
| 168 | +} |
| 169 | + |
| 170 | +func flattenOverviewsBandwidths(rawBandwidths []interface{}) []interface{} { |
| 171 | + if len(rawBandwidths) == 0 { |
| 172 | + return nil |
| 173 | + } |
| 174 | + |
| 175 | + rst := make([]interface{}, 0, len(rawBandwidths)) |
| 176 | + for _, v := range rawBandwidths { |
| 177 | + rst = append(rst, map[string]interface{}{ |
| 178 | + "key": utils.PathSearch("key", v, nil), |
| 179 | + "timeline": flattenOverviewsBandwidthsTimeline(utils.PathSearch("timeline", v, make([]interface{}, 0)).([]interface{})), |
| 180 | + }) |
| 181 | + } |
| 182 | + |
| 183 | + return rst |
| 184 | +} |
| 185 | + |
| 186 | +func flattenOverviewsBandwidthsTimeline(rawTimeline []interface{}) []interface{} { |
| 187 | + if len(rawTimeline) == 0 { |
| 188 | + return nil |
| 189 | + } |
| 190 | + |
| 191 | + rst := make([]interface{}, 0, len(rawTimeline)) |
| 192 | + for _, v := range rawTimeline { |
| 193 | + rst = append(rst, map[string]interface{}{ |
| 194 | + "time": utils.PathSearch("time", v, nil), |
| 195 | + "num": utils.PathSearch("num", v, nil), |
| 196 | + }) |
| 197 | + } |
| 198 | + |
| 199 | + return rst |
| 200 | +} |
0 commit comments