Skip to content

Commit 6bb1dfa

Browse files
authored
Data source for sysdig secure rule file system (#345)
* Data source for sysdig secure rule file system * Data source for sysdig secure rule file system * Data source for sysdig secure rule file system * Data source for sysdig secure rule file system * Fix test * Fix test * Fix test * Fix test * Docs * PR Comments
1 parent 56a3d29 commit 6bb1dfa

File tree

6 files changed

+200
-2
lines changed

6 files changed

+200
-2
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigSecureRuleFileSystem() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigRuleFileSystemRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createRuleDataSourceSchema(map[string]*schema.Schema{
23+
"read_only": {
24+
Type: schema.TypeList,
25+
Computed: true,
26+
Elem: &schema.Resource{
27+
Schema: map[string]*schema.Schema{
28+
"matching": {
29+
Type: schema.TypeBool,
30+
Computed: true,
31+
},
32+
"paths": {
33+
Type: schema.TypeList,
34+
Computed: true,
35+
Elem: &schema.Schema{
36+
Type: schema.TypeString,
37+
},
38+
},
39+
},
40+
},
41+
},
42+
"read_write": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"matching": {
48+
Type: schema.TypeBool,
49+
Computed: true,
50+
},
51+
"paths": {
52+
Type: schema.TypeList,
53+
Computed: true,
54+
Elem: &schema.Schema{
55+
Type: schema.TypeString,
56+
},
57+
},
58+
},
59+
},
60+
},
61+
}),
62+
}
63+
}
64+
65+
func dataSourceSysdigRuleFileSystemRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
66+
client, err := getSecureRuleClient(meta.(SysdigClients))
67+
if err != nil {
68+
return diag.FromErr(err)
69+
}
70+
71+
ruleName := d.Get("name").(string)
72+
ruleType := v2.RuleTypeFileSystem
73+
74+
rules, err := client.GetRuleGroup(ctx, ruleName, ruleType)
75+
if err != nil {
76+
return diag.FromErr(err)
77+
}
78+
79+
if len(rules) == 0 {
80+
return diag.Errorf("unable to find rule")
81+
}
82+
83+
if len(rules) > 1 {
84+
return diag.Errorf("more than one rule with that name was found")
85+
}
86+
87+
rule := rules[0]
88+
89+
ruleDataSourceToResourceData(rule, d)
90+
if len(rule.Details.ReadPaths.Items) > 0 {
91+
_ = d.Set("read_only", []map[string]interface{}{{
92+
"matching": rule.Details.ReadPaths.MatchItems,
93+
"paths": rule.Details.ReadPaths.Items,
94+
}})
95+
96+
}
97+
if len(rule.Details.ReadWritePaths.Items) > 0 {
98+
_ = d.Set("read_write", []map[string]interface{}{{
99+
"matching": rule.Details.ReadWritePaths.MatchItems,
100+
"paths": rule.Details.ReadWritePaths.Items,
101+
}})
102+
103+
}
104+
105+
return nil
106+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
14+
"github.com/draios/terraform-provider-sysdig/sysdig"
15+
)
16+
17+
func TestAccRuleFileSystemDataSource(t *testing.T) {
18+
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }
19+
20+
resource.ParallelTest(t, resource.TestCase{
21+
PreCheck: func() {
22+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
23+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
24+
}
25+
},
26+
ProviderFactories: map[string]func() (*schema.Provider, error){
27+
"sysdig": func() (*schema.Provider, error) {
28+
return sysdig.Provider(), nil
29+
},
30+
},
31+
Steps: []resource.TestStep{
32+
{
33+
Config: ruleFileSystemDataSource(rText()),
34+
},
35+
},
36+
})
37+
}
38+
39+
func ruleFileSystemDataSource(name string) string {
40+
return fmt.Sprintf(`
41+
%s
42+
43+
data "sysdig_secure_rule_file_system" "data_sample" {
44+
name = "TERRAFORM TEST %s"
45+
depends_on = [ sysdig_secure_rule_filesystem.foo ]
46+
}
47+
`, ruleFilesystemWithName(name), name)
48+
}

sysdig/internal/client/v2/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ type Rule struct {
259259
}
260260

261261
const (
262-
RuleTypeContainer = "CONTAINER"
262+
RuleTypeContainer = "CONTAINER"
263+
RuleTypeFileSystem = "FILESYSTEM"
263264
)
264265

265266
type Details struct {

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func Provider() *schema.Provider {
131131
"sysdig_secure_managed_policy": dataSourceSysdigSecureManagedPolicy(),
132132
"sysdig_secure_managed_ruleset": dataSourceSysdigSecureManagedRuleset(),
133133
"sysdig_secure_rule_container": dataSourceSysdigSecureRuleContainer(),
134+
"sysdig_secure_rule_file_system": dataSourceSysdigSecureRuleFileSystem(),
134135

135136
"sysdig_current_user": dataSourceSysdigCurrentUser(),
136137
"sysdig_user": dataSourceSysdigUser(),

sysdig/resource_sysdig_secure_rule_filesystem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func resourceSysdigRuleFilesystemDelete(ctx context.Context, d *schema.ResourceD
185185

186186
func resourceSysdigRuleFilesystemFromResourceData(d *schema.ResourceData) (rule v2.Rule, err error) {
187187
rule = ruleFromResourceData(d)
188-
rule.Details.RuleType = "FILESYSTEM"
188+
rule.Details.RuleType = v2.RuleTypeFileSystem
189189

190190
rule.Details.ReadPaths = &v2.ReadPaths{
191191
MatchItems: true,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
subcategory: "Sysdig Secure"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_rule_file_system"
5+
description: |-
6+
Retrieves a Sysdig Secure File System Rule.
7+
---
8+
9+
# Data Source: sysdig_secure_rule_file_system
10+
11+
Retrieves the information of an existing Sysdig Secure File System Rule.
12+
13+
-> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "sysdig_secure_rule_file_system" "example" {
19+
name = "Write below etc"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `name` - (Required) The name of the Secure rule to retrieve.
26+
27+
## Attributes Reference
28+
29+
In addition to the argument above, the following attributes are exported:
30+
31+
* `description` - The description of Secure rule.
32+
* `tags` - A list of tags for this rule.
33+
* `read_only` - Block that defines read only paths to match or not match.
34+
* `read_write` - Block that defines read and write paths to match or not match.
35+
* `version` - Current version of the resource in Sysdig Secure.
36+
37+
## read_write and read_only blocks
38+
39+
Description of the attributes within the read_only and read_write blocks.
40+
41+
* `matching` - Boolean value that defines if the path matches or not with the provided list.
42+
* `paths` - List of paths to match.

0 commit comments

Comments
 (0)