Skip to content

Commit 543ac38

Browse files
feat(secmaster): add datasource to get list of playbook review result (#7103)
1 parent d61df5a commit 543ac38

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
subcategory: "SecMaster"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_secmaster_playbook_approvals"
5+
description: |-
6+
Use this data source to get the list of SecMaster playbook review results.
7+
---
8+
9+
# huaweicloud_secmaster_playbook_approvals
10+
11+
Use this data source to get the list of SecMaster playbook review results.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "workspace_id" {}
17+
variable "resource_id" {}
18+
variable "approve_type" {}
19+
20+
data "huaweicloud_secmaster_playbook_approvals" "test" {
21+
workspace_id = var.workspace_id
22+
resource_id = var.resource_id
23+
approve_type = var.approve_type
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
The following arguments are supported:
30+
31+
* `region` - (Optional, String) Specifies the region in which to query the resource.
32+
If omitted, the provider-level region will be used.
33+
34+
* `workspace_id` - (Required, String) Specifies the workspace ID.
35+
36+
* `resource_id` - (Required, String) Specifies the resource ID.
37+
38+
* `approve_type` - (Required, String) Specifies the review type.
39+
The valid values are as follows:
40+
+ **PLAYBOOK**: Indicates playbook.
41+
+ **AOP_WORKFLOW**: Indicates workflow.
42+
43+
## Attribute Reference
44+
45+
In addition to all arguments above, the following attributes are exported:
46+
47+
* `id` - The data source ID.
48+
49+
* `data` - The list of playbook review result.
50+
51+
The [data](#data_struct) structure is documented below.
52+
53+
<a name="data_struct"></a>
54+
The `data` block supports:
55+
56+
* `id` - The approval ID.
57+
58+
* `result` - The review result.
59+
The valid values are as follows:
60+
+ **PASS**: Indicates review pass.
61+
+ **UN_PASS**: Indicates review not pass.
62+
63+
* `content` - The review content.
64+
65+
* `type` - The resource type.
66+
67+
* `resource_id` - The resource ID.
68+
69+
* `user_id` - The reviewer ID.
70+
71+
* `create_time` - The creation time of the playbook review.
72+
73+
* `update_time` - The update time of the playbook review.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ func Provider() *schema.Provider {
13411341
"huaweicloud_secmaster_playbook_statistics": secmaster.DataSourceSecmasterPlaybookStatistics(),
13421342
"huaweicloud_secmaster_playbook_audit_logs": secmaster.DataSourceSecmasterPlaybookAuditLogs(),
13431343
"huaweicloud_secmaster_playbook_monitors": secmaster.DataSourceSecmasterPlaybookMonitors(),
1344+
"huaweicloud_secmaster_playbook_approvals": secmaster.DataSourcePlaybookApprovals(),
13441345
"huaweicloud_secmaster_alert_rule_metrics": secmaster.DataSourceSecmasterAlertRuleMetrics(),
13451346

13461347
// Querying by Ver.2 APIs

huaweicloud/services/acceptance/acceptance.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ var (
422422
// The flag of whether to create a SecMaster workspace
423423
HW_SECMASTER_WORKSPACE = os.Getenv("HW_SECMASTER_WORKSPACE")
424424

425+
// The SecMaster playbook version ID
426+
HW_SECMASTER_VERSION_ID = os.Getenv("HW_SECMASTER_VERSION_ID")
427+
425428
HW_MODELARTS_HAS_SUBSCRIBE_MODEL = os.Getenv("HW_MODELARTS_HAS_SUBSCRIBE_MODEL")
426429
HW_MODELARTS_USER_LOGIN_PASSWORD = os.Getenv("HW_MODELARTS_USER_LOGIN_PASSWORD")
427430
HW_MODELARTS_DEVSERVER_FLAVOR = os.Getenv("HW_MODELARTS_DEVSERVER_FLAVOR")
@@ -2238,6 +2241,13 @@ func TestAccPreCheckSecMaster(t *testing.T) {
22382241
}
22392242
}
22402243

2244+
// lintignore:AT003
2245+
func TestAccPreCheckSecMasterVersionId(t *testing.T) {
2246+
if HW_SECMASTER_VERSION_ID == "" {
2247+
t.Skip("HW_SECMASTER_VERSION_ID must be set for SecMaster acceptance tests")
2248+
}
2249+
}
2250+
22412251
// lintignore:AT003
22422252
func TestAccPreCheckCcePartitionAz(t *testing.T) {
22432253
if HW_CCE_PARTITION_AZ == "" || HW_CCE_PARTITION_GROUP == "" {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package secmaster
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourcePlaybookApprovals_basic(t *testing.T) {
13+
var (
14+
dataSource = "data.huaweicloud_secmaster_playbook_approvals.test"
15+
dc = acceptance.InitDataSourceCheck(dataSource)
16+
)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acceptance.TestAccPreCheck(t)
21+
acceptance.TestAccPreCheckSecMasterWorkspaceID(t)
22+
acceptance.TestAccPreCheckSecMasterVersionId(t)
23+
},
24+
ProviderFactories: acceptance.TestAccProviderFactories,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testDataSourcePlaybookApprovals_basic(),
28+
Check: resource.ComposeTestCheckFunc(
29+
dc.CheckResourceExists(),
30+
resource.TestCheckResourceAttrSet(dataSource, "data.#"),
31+
resource.TestCheckResourceAttrSet(dataSource, "data.0.id"),
32+
resource.TestCheckResourceAttrSet(dataSource, "data.0.result"),
33+
resource.TestCheckResourceAttrSet(dataSource, "data.0.content"),
34+
resource.TestCheckResourceAttrSet(dataSource, "data.0.type"),
35+
resource.TestCheckResourceAttrSet(dataSource, "data.0.resource_id"),
36+
resource.TestCheckResourceAttrSet(dataSource, "data.0.user_id"),
37+
resource.TestCheckResourceAttrSet(dataSource, "data.0.create_time"),
38+
resource.TestCheckResourceAttrSet(dataSource, "data.0.update_time"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
func testDataSourcePlaybookApprovals_basic() string {
46+
return fmt.Sprintf(`
47+
data "huaweicloud_secmaster_playbook_approvals" "test" {
48+
workspace_id = "%[1]s"
49+
resource_id = "%[2]s"
50+
approve_type = "PLAYBOOK"
51+
}
52+
`, acceptance.HW_SECMASTER_WORKSPACE_ID, acceptance.HW_SECMASTER_VERSION_ID)
53+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package secmaster
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 Secmaster GET /v1/{project_id}/workspaces/{workspace_id}/soc/playbooks/versions/approval
20+
func DataSourcePlaybookApprovals() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourcePlaybookApprovalsRead,
23+
24+
Schema: map[string]*schema.Schema{
25+
"region": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
},
30+
"workspace_id": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
},
34+
"resource_id": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
"approve_type": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
},
42+
"data": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"id": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"result": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"content": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
59+
"type": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"resource_id": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"user_id": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
},
71+
"create_time": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
75+
"update_time": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
},
79+
},
80+
},
81+
},
82+
},
83+
}
84+
}
85+
86+
func dataSourcePlaybookApprovalsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
87+
var (
88+
cfg = meta.(*config.Config)
89+
region = cfg.GetRegion(d)
90+
httpUrl = "v1/{project_id}/workspaces/{workspace_id}/soc/playbooks/versions/approval"
91+
)
92+
93+
client, err := cfg.NewServiceClient("secmaster", region)
94+
if err != nil {
95+
return diag.Errorf("error creating SecMaster client: %s", err)
96+
}
97+
98+
listPath := client.Endpoint + httpUrl
99+
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID)
100+
listPath = strings.ReplaceAll(listPath, "{workspace_id}", d.Get("workspace_id").(string))
101+
listPath = fmt.Sprintf("%s?resource_id=%s&approve_type=%s", listPath, d.Get("resource_id").(string), d.Get("approve_type").(string))
102+
103+
listOpt := golangsdk.RequestOpts{
104+
KeepResponseBody: true,
105+
MoreHeaders: map[string]string{
106+
"Content-Type": "application/json",
107+
},
108+
}
109+
110+
listResp, err := client.Request("GET", listPath, &listOpt)
111+
if err != nil {
112+
return diag.Errorf("error retrieving playbook version approval: %s", err)
113+
}
114+
115+
listRespBody, err := utils.FlattenResponse(listResp)
116+
if err != nil {
117+
return diag.FromErr(err)
118+
}
119+
120+
dataList := utils.PathSearch("data", listRespBody, make([]interface{}, 0)).([]interface{})
121+
122+
dataSourceId, err := uuid.GenerateUUID()
123+
if err != nil {
124+
return diag.Errorf("unable to generate ID: %s", err)
125+
}
126+
127+
d.SetId(dataSourceId)
128+
129+
mErr := multierror.Append(nil,
130+
d.Set("region", region),
131+
d.Set("data", flattenPlaybookApprovals(dataList)),
132+
)
133+
134+
return diag.FromErr(mErr.ErrorOrNil())
135+
}
136+
137+
func flattenPlaybookApprovals(playbookApprovals []interface{}) []interface{} {
138+
if len(playbookApprovals) == 0 {
139+
return nil
140+
}
141+
142+
rst := make([]interface{}, 0, len(playbookApprovals))
143+
for _, v := range playbookApprovals {
144+
rst = append(rst, map[string]interface{}{
145+
"id": utils.PathSearch("id", v, nil),
146+
"result": utils.PathSearch("result", v, nil),
147+
"content": utils.PathSearch("content", v, nil),
148+
"type": utils.PathSearch("type", v, nil),
149+
"resource_id": utils.PathSearch("resource_id", v, nil),
150+
"user_id": utils.PathSearch("user_id", v, nil),
151+
"create_time": utils.PathSearch("create_time", v, nil),
152+
"update_time": utils.PathSearch("update_time", v, nil),
153+
})
154+
}
155+
156+
return rst
157+
}

0 commit comments

Comments
 (0)