|
| 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 | +} |
0 commit comments