Skip to content

Commit 5fb57ff

Browse files
committed
feat(hss): add new data source to query hss process statistics
1 parent 059fd22 commit 5fb57ff

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
subcategory: "Host Security Service (HSS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_hss_asset_process_statistics"
5+
description: |-
6+
Use this data source to get the list of HSS process statistics within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_hss_asset_process_statistics
10+
11+
Use this data source to get the list of HSS process statistics within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_hss_asset_process_statistics" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource.
24+
If omitted, the provider-level region will be used.
25+
26+
* `path` - (Optional, String) Specifies the process path.
27+
28+
* `category` - (Optional, String) Specifies the type. The default value is **host**.
29+
The valid values are as follows:
30+
+ **host**: Host.
31+
+ **container**: Container.
32+
33+
* `enterprise_project_id` - (Optional, String) Specifies the ID of the enterprise project to which the resource belongs.
34+
This parameter is valid only when the enterprise project function is enabled.
35+
The value **all_granted_eps** indicates all enterprise projects.
36+
If omitted, the default enterprise project will be used.
37+
38+
## Attribute Reference
39+
40+
In addition to all arguments above, the following attributes are exported:
41+
42+
* `id` - The data source ID.
43+
44+
* `data_list` - The process statistics list.
45+
The [data_list](#process_statistics_structure) structure is documented below.
46+
47+
<a name="process_statistics_structure"></a>
48+
The `data_list` block supports:
49+
50+
* `path` - The process path.
51+
52+
* `num` - The number of processes.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ func Provider() *schema.Provider {
10311031
"huaweicloud_hss_auto_launch_statistics": hss.DataSourceAutoLaunchStatistics(),
10321032
"huaweicloud_hss_event_unblock_ips": hss.DataSourceEventUnblockIps(),
10331033
"huaweicloud_hss_asset_apps": hss.DataSourceAssetApps(),
1034+
"huaweicloud_hss_asset_process_statistics": hss.DataSourceAssetProcessStatistics(),
10341035

10351036
"huaweicloud_identity_permissions": iam.DataSourceIdentityPermissions(),
10361037
"huaweicloud_identity_role": iam.DataSourceIdentityRole(),
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package hss
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataSourceAssetProcessStatistics_basic(t *testing.T) {
12+
var (
13+
dataSource = "data.huaweicloud_hss_asset_process_statistics.test"
14+
dc = acceptance.InitDataSourceCheck(dataSource)
15+
)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
acceptance.TestAccPreCheck(t)
20+
// This test case requires setting a host ID with host protection enabled.
21+
acceptance.TestAccPreCheckHSSHostProtectionHostId(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testDataSourceAssetProcessStatistics_basic,
27+
Check: resource.ComposeTestCheckFunc(
28+
dc.CheckResourceExists(),
29+
resource.TestCheckResourceAttrSet(dataSource, "data_list.#"),
30+
resource.TestCheckResourceAttrSet(dataSource, "data_list.0.path"),
31+
resource.TestCheckResourceAttrSet(dataSource, "data_list.0.num"),
32+
33+
resource.TestCheckOutput("is_path_filter_useful", "true"),
34+
resource.TestCheckOutput("is_category_filter_useful", "true"),
35+
resource.TestCheckOutput("not_found_validation_pass", "true"),
36+
),
37+
},
38+
},
39+
})
40+
}
41+
42+
const testDataSourceAssetProcessStatistics_basic string = `
43+
data "huaweicloud_hss_asset_process_statistics" "test" {}
44+
45+
# Filter using path.
46+
locals {
47+
path = data.huaweicloud_hss_asset_process_statistics.test.data_list[0].path
48+
}
49+
50+
data "huaweicloud_hss_asset_process_statistics" "path_filter" {
51+
path = local.path
52+
}
53+
54+
output "is_path_filter_useful" {
55+
value = length(data.huaweicloud_hss_asset_process_statistics.path_filter.data_list) > 0 && alltrue(
56+
[for v in data.huaweicloud_hss_asset_process_statistics.path_filter.data_list[*].path : v == local.path]
57+
)
58+
}
59+
60+
# Filter using category.
61+
locals {
62+
category = "host"
63+
}
64+
65+
data "huaweicloud_hss_asset_process_statistics" "category_filter" {
66+
category = local.category
67+
}
68+
69+
output "is_category_filter_useful" {
70+
value = length(data.huaweicloud_hss_asset_process_statistics.category_filter.data_list) > 0 && alltrue(
71+
[for v in data.huaweicloud_hss_asset_process_statistics.category_filter.data_list[*].path : v != ""]
72+
)
73+
}
74+
75+
# Filter using non existent path.
76+
data "huaweicloud_hss_asset_process_statistics" "not_found" {
77+
path = "resource_not_found"
78+
}
79+
80+
output "not_found_validation_pass" {
81+
value = length(data.huaweicloud_hss_asset_process_statistics.not_found.data_list) == 0
82+
}
83+
`
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)