|
| 1 | +package hss |
| 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 HSS GET /v5/{project_id}/asset/process/statistics |
| 20 | +func DataSourceAssetProcessStatistics() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + ReadContext: dataSourceAssetProcessStatisticsRead, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "region": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Computed: true, |
| 28 | + Description: "Specifies the region in which to query the resource.", |
| 29 | + }, |
| 30 | + "path": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "Specifies the process path.", |
| 34 | + }, |
| 35 | + "category": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Description: "Specifies the type. The default value is host.", |
| 39 | + }, |
| 40 | + "enterprise_project_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "Specifies the ID of the enterprise project to which the resource belongs.", |
| 44 | + }, |
| 45 | + "data_list": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Elem: assetProcessStatisticsSchema(), |
| 49 | + Description: "The process statistics list.", |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func assetProcessStatisticsSchema() *schema.Resource { |
| 56 | + sc := schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "path": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "The process path.", |
| 62 | + }, |
| 63 | + "num": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "The number of processes.", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + return &sc |
| 71 | +} |
| 72 | + |
| 73 | +func buildAssetProcessStatisticsQueryParams(d *schema.ResourceData, cfg *config.Config) string { |
| 74 | + epsId := cfg.GetEnterpriseProjectID(d) |
| 75 | + queryParams := "?limit=100" |
| 76 | + if epsId != "" { |
| 77 | + queryParams = fmt.Sprintf("%s&enterprise_project_id=%v", queryParams, epsId) |
| 78 | + } |
| 79 | + if v, ok := d.GetOk("path"); ok { |
| 80 | + queryParams = fmt.Sprintf("%s&path=%v", queryParams, v) |
| 81 | + } |
| 82 | + if v, ok := d.GetOk("category"); ok { |
| 83 | + queryParams = fmt.Sprintf("%s&category=%v", queryParams, v) |
| 84 | + } |
| 85 | + return queryParams |
| 86 | +} |
| 87 | + |
| 88 | +func flattenAssetProcessStatistics(resp []interface{}) []interface{} { |
| 89 | + if len(resp) == 0 { |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + rst := make([]interface{}, 0, len(resp)) |
| 94 | + for _, v := range resp { |
| 95 | + rst = append(rst, map[string]interface{}{ |
| 96 | + "path": utils.PathSearch("path", v, nil), |
| 97 | + "num": utils.PathSearch("num", v, nil), |
| 98 | + }) |
| 99 | + } |
| 100 | + return rst |
| 101 | +} |
| 102 | + |
| 103 | +func dataSourceAssetProcessStatisticsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 104 | + var ( |
| 105 | + cfg = meta.(*config.Config) |
| 106 | + region = cfg.GetRegion(d) |
| 107 | + product = "hss" |
| 108 | + mErr *multierror.Error |
| 109 | + ) |
| 110 | + |
| 111 | + client, err := cfg.NewServiceClient(product, region) |
| 112 | + if err != nil { |
| 113 | + return diag.Errorf("error creating HSS client: %s", err) |
| 114 | + } |
| 115 | + |
| 116 | + requestPath := client.Endpoint + "v5/{project_id}/asset/process/statistics" |
| 117 | + requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID) |
| 118 | + requestPath += buildAssetProcessStatisticsQueryParams(d, cfg) |
| 119 | + allProcessStatistics := make([]interface{}, 0) |
| 120 | + offset := 0 |
| 121 | + |
| 122 | + listOpt := golangsdk.RequestOpts{ |
| 123 | + KeepResponseBody: true, |
| 124 | + } |
| 125 | + |
| 126 | + for { |
| 127 | + currentPath := fmt.Sprintf("%s&offset=%v", requestPath, offset) |
| 128 | + resp, err := client.Request("GET", currentPath, &listOpt) |
| 129 | + if err != nil { |
| 130 | + return diag.Errorf("error retrieving HSS process statistics: %s", err) |
| 131 | + } |
| 132 | + |
| 133 | + respBody, err := utils.FlattenResponse(resp) |
| 134 | + if err != nil { |
| 135 | + return diag.FromErr(err) |
| 136 | + } |
| 137 | + |
| 138 | + processStatisticsResp := utils.PathSearch("data_list", respBody, make([]interface{}, 0)).([]interface{}) |
| 139 | + if len(processStatisticsResp) == 0 { |
| 140 | + break |
| 141 | + } |
| 142 | + allProcessStatistics = append(allProcessStatistics, processStatisticsResp...) |
| 143 | + offset += len(processStatisticsResp) |
| 144 | + } |
| 145 | + |
| 146 | + id, err := uuid.GenerateUUID() |
| 147 | + if err != nil { |
| 148 | + return diag.Errorf("unable to generate ID: %s", err) |
| 149 | + } |
| 150 | + d.SetId(id) |
| 151 | + |
| 152 | + mErr = multierror.Append( |
| 153 | + mErr, |
| 154 | + d.Set("region", region), |
| 155 | + d.Set("data_list", flattenAssetProcessStatistics(allProcessStatistics)), |
| 156 | + ) |
| 157 | + |
| 158 | + return diag.FromErr(mErr.ErrorOrNil()) |
| 159 | +} |
0 commit comments