Skip to content

Commit c4bb7cc

Browse files
authored
Merge pull request #360 from aquasecurity/feature/monitoring-system
feat: implementation of monitoring system
2 parents 33b7c9e + 976bfaf commit c4bb7cc

File tree

12 files changed

+665
-0
lines changed

12 files changed

+665
-0
lines changed

aquasec/data_monitoring_system.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package aquasec
2+
3+
import (
4+
"context"
5+
6+
"github.com/aquasecurity/terraform-provider-aquasec/client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceMonitoringSystem() *schema.Resource {
12+
return &schema.Resource{
13+
ReadContext: dataSourceMonitoringSystemRead,
14+
Schema: map[string]*schema.Schema{
15+
"monitors": {
16+
Type: schema.TypeList,
17+
Description: "List of existing monitoring systems.",
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Description: "",
24+
Computed: true,
25+
},
26+
"token": {
27+
Type: schema.TypeString,
28+
Description: "",
29+
Computed: true,
30+
Sensitive: true,
31+
},
32+
"type": {
33+
Type: schema.TypeString,
34+
Description: "",
35+
Computed: true,
36+
},
37+
"enabled": {
38+
Type: schema.TypeBool,
39+
Description: "",
40+
Computed: true,
41+
},
42+
"interval": {
43+
Type: schema.TypeInt,
44+
Description: "",
45+
Computed: true,
46+
},
47+
},
48+
},
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceMonitoringSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
55+
ac := m.(*client.Client)
56+
result, err := ac.GetMonitoringSystems()
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
monitors := flattenMonitoringSystem(&result)
61+
if err := d.Set("monitors", monitors); err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
if len(result) == 0 {
66+
d.SetId("")
67+
return nil
68+
}
69+
d.SetId(result[0].Name)
70+
return nil
71+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package aquasec
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAquasecMonitoringSystemDataSourceAny(t *testing.T) {
12+
t.Skip()
13+
t.Parallel()
14+
15+
name := "Prometheus"
16+
msType := "prometheus"
17+
token := "tf-acc-token"
18+
enabled := true
19+
interval := 30
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
Providers: testAccProviders,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccMonitoringSystemDataSourceAny(name, msType, token, enabled, interval),
27+
Check: resource.ComposeTestCheckFunc(
28+
testAccCheckAquasecMonitoringSystemDataSourceExists("data.aquasec_monitoring_systems.test_ms"),
29+
resource.TestCheckResourceAttr("data.aquasec_monitoring_systems.test_ms", "monitors.0.name", name),
30+
resource.TestCheckResourceAttr("data.aquasec_monitoring_systems.test_ms", "monitors.0.type", msType),
31+
resource.TestCheckResourceAttr("data.aquasec_monitoring_systems.test_ms", "monitors.0.token", token),
32+
resource.TestCheckResourceAttr("data.aquasec_monitoring_systems.test_ms", "monitors.0.enabled", fmt.Sprintf("%t", enabled)),
33+
resource.TestCheckResourceAttr("data.aquasec_monitoring_systems.test_ms", "monitors.0.interval", fmt.Sprintf("%d", interval))),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccMonitoringSystemDataSourceAny(name, msType, token string, enabled bool, interval int) string {
40+
return fmt.Sprintf(`
41+
resource "aquasec_monitoring_system" "any" {
42+
name = "%s"
43+
type = "%s"
44+
token = "%s"
45+
enabled = %t
46+
interval = %d
47+
}
48+
49+
data "aquasec_monitoring_systems" "test_ms" {
50+
depends_on = [
51+
aquasec_monitoring_system.any
52+
]
53+
}
54+
`, name, msType, token, enabled, interval)
55+
}
56+
57+
func testAccCheckAquasecMonitoringSystemDataSourceExists(n string) resource.TestCheckFunc {
58+
return func(s *terraform.State) error {
59+
rs, ok := s.RootModule().Resources[n]
60+
if !ok {
61+
return NewNotFoundErrorf("%s in state", n)
62+
}
63+
if rs.Primary.ID == "" {
64+
return NewNotFoundErrorf("ID for %s in state", n)
65+
}
66+
return nil
67+
}
68+
}

aquasec/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func Provider(v string) *schema.Provider {
145145
"aquasec_scanner_group": resourceScannerGroup(),
146146
"aquasec_log_management": resourceLogManagement(),
147147
"aquasec_serverless_application": resourceServerlessApplication(),
148+
"aquasec_monitoring_system": resourceMonitoringSystem(),
148149
},
149150
DataSourcesMap: map[string]*schema.Resource{
150151
"aquasec_users": dataSourceUsers(),
@@ -182,6 +183,7 @@ func Provider(v string) *schema.Provider {
182183
"aquasec_vmware_assurance_policy": dataVmwareAssurancePolicy(),
183184
"aquasec_log_managements": dataLogManagement(),
184185
"aquasec_serverless_applications": dataSourceServerlessApplication(),
186+
"aquasec_monitoring_systems": dataSourceMonitoringSystem(),
185187
},
186188
ConfigureContextFunc: providerConfigure,
187189
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package aquasec
2+
3+
import (
4+
"context"
5+
6+
"github.com/aquasecurity/terraform-provider-aquasec/client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func resourceMonitoringSystem() *schema.Resource {
12+
return &schema.Resource{
13+
CreateContext: resourceMonitoringSystemCreate,
14+
ReadContext: resourceMonitoringSystemRead,
15+
UpdateContext: resourceMonitoringSYstemUpdate,
16+
DeleteContext: resourceMonitoringSystemDelete,
17+
Importer: &schema.ResourceImporter{
18+
StateContext: schema.ImportStatePassthroughContext,
19+
},
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Description: "The name of the monitoring system.",
24+
Optional: true,
25+
Default: "Prometheus",
26+
},
27+
"type": {
28+
Type: schema.TypeString,
29+
Description: "The type of the monitoring system.",
30+
Required: true,
31+
},
32+
"token": {
33+
Type: schema.TypeString,
34+
Description: "The authentication token for the monitoring system.",
35+
Optional: true,
36+
Sensitive: true,
37+
},
38+
"enabled": {
39+
Type: schema.TypeBool,
40+
Description: "Indicates whether the monitoring system is enabled.",
41+
Required: true,
42+
},
43+
"interval": {
44+
Type: schema.TypeInt,
45+
Description: "The interval in minutes for monitoring checks.",
46+
Optional: true,
47+
},
48+
},
49+
}
50+
}
51+
52+
func resourceMonitoringSystemCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
53+
ac := m.(*client.Client)
54+
name := d.Get("name").(string)
55+
enabled := d.Get("enabled").(bool)
56+
interval := d.Get("interval").(int)
57+
typeMonSys := d.Get("type").(string)
58+
var tokenPtr *string
59+
if v, ok := d.GetOk("token"); ok {
60+
s := v.(string)
61+
if s != "" {
62+
tokenPtr = &s
63+
}
64+
}
65+
66+
monitoringSystem := client.MonitoringSystem{
67+
Name: name,
68+
Enabled: enabled,
69+
Interval: interval,
70+
Token: tokenPtr,
71+
Type: typeMonSys,
72+
}
73+
74+
err := ac.CreateMonitoringSystem(monitoringSystem)
75+
if err != nil {
76+
return diag.FromErr(err)
77+
}
78+
79+
d.SetId(name)
80+
return resourceMonitoringSystemRead(ctx, d, m)
81+
}
82+
83+
func resourceMonitoringSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
84+
ac := m.(*client.Client)
85+
name := d.Id()
86+
if name == "" {
87+
if v, ok := d.GetOk("name"); ok {
88+
name = v.(string)
89+
}
90+
}
91+
92+
monitor, err := ac.GetMonitoringSystem(name)
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
97+
if monitor == nil {
98+
d.SetId("")
99+
return nil
100+
}
101+
_ = d.Set("name", monitor.Name)
102+
_ = d.Set("type", monitor.Type)
103+
_ = d.Set("enabled", monitor.Enabled)
104+
_ = d.Set("interval", monitor.Interval)
105+
if monitor.Token != nil {
106+
_ = d.Set("token", *monitor.Token)
107+
} else {
108+
_ = d.Set("token", "")
109+
}
110+
d.SetId(monitor.Name)
111+
return nil
112+
}
113+
114+
func resourceMonitoringSYstemUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
115+
ac := m.(*client.Client)
116+
oldName := d.Id()
117+
118+
if d.HasChanges("interval", "enabled", "token", "type") {
119+
enabled := d.Get("enabled").(bool)
120+
interval := d.Get("interval").(int)
121+
msType := d.Get("type").(string)
122+
var tokenPtr *string
123+
if v, ok := d.GetOk("token"); ok {
124+
s := v.(string)
125+
if s != "" {
126+
tokenPtr = &s
127+
}
128+
}
129+
130+
monitor := client.MonitoringSystem{
131+
Name: oldName,
132+
Enabled: enabled,
133+
Token: tokenPtr,
134+
Interval: interval,
135+
Type: msType,
136+
}
137+
err := ac.UpdateMonitoringSystem(monitor)
138+
if err != nil {
139+
return diag.FromErr(err)
140+
}
141+
}
142+
return resourceMonitoringSystemRead(ctx, d, m)
143+
}
144+
func resourceMonitoringSystemDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
145+
ac := m.(*client.Client)
146+
name := d.Get("name").(string)
147+
enabled := false
148+
interval := d.Get("interval").(int)
149+
msType := d.Get("type").(string)
150+
151+
monitoringSystem := client.MonitoringSystem{
152+
Name: name,
153+
Enabled: enabled,
154+
Interval: interval,
155+
Type: msType,
156+
}
157+
err := ac.DeleteMonitoringSystem(monitoringSystem)
158+
if err != nil {
159+
return diag.FromErr(err)
160+
}
161+
162+
d.SetId("")
163+
return nil
164+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package aquasec
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAccAquasecMonitoringSystem(t *testing.T) {
12+
t.Skip()
13+
t.Parallel()
14+
15+
name := "Prometheus"
16+
msType := "prometheus"
17+
token := "tf-acc-test-token"
18+
enabled := true
19+
interval := 30
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
Providers: testAccProviders,
24+
CheckDestroy: CheckDestroy("aquasec_monitoring_system.prom_mon"),
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccMonitoringSystemResourceConfig(name, msType, token, enabled, interval),
28+
Check: resource.ComposeTestCheckFunc(
29+
testAccCheckAquasecMonitoringSystemExists("aquasec_monitoring_system.prom_mon"),
30+
resource.TestCheckResourceAttr("aquasec_monitoring_system.prom_mon", "name", name),
31+
resource.TestCheckResourceAttr("aquasec_monitoring_system.prom_mon", "type", msType),
32+
resource.TestCheckResourceAttr("aquasec_monitoring_system.prom_mon", "enabled", fmt.Sprintf("%t", enabled)),
33+
resource.TestCheckResourceAttr("aquasec_monitoring_system.prom_mon", "interval", fmt.Sprintf("%d", interval)),
34+
),
35+
},
36+
{
37+
ResourceName: "aquasec_monitoring_system.prom_mon",
38+
ImportState: true,
39+
ImportStateVerify: true,
40+
// If token isn't returned by Read or is write-only, ignore it during import verification.
41+
ImportStateVerifyIgnore: []string{"token", "last_updated"},
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccMonitoringSystemResourceConfig(name, msType, token string, enabled bool, interval int) string {
48+
return fmt.Sprintf(`
49+
resource "aquasec_monitoring_system" "prom_mon" {
50+
name = "%s"
51+
type = "%s"
52+
token = "%s"
53+
enabled = %t
54+
interval = %d
55+
}
56+
`, name, msType, token, enabled, interval)
57+
}
58+
59+
func testAccCheckAquasecMonitoringSystemExists(n string) resource.TestCheckFunc {
60+
return func(s *terraform.State) error {
61+
rs, ok := s.RootModule().Resources[n]
62+
if !ok {
63+
return NewNotFoundErrorf("%s in state", n)
64+
}
65+
if rs.Primary.ID == "" {
66+
return NewNotFoundErrorf("ID for %s in state", n)
67+
}
68+
return nil
69+
}
70+
}

0 commit comments

Comments
 (0)