|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/uuid" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/mitchellh/mapstructure" |
| 10 | +) |
| 11 | + |
| 12 | +type Monitoring struct { |
| 13 | + Threshold int |
| 14 | + MemoryThreshold int |
| 15 | + DiskThreshold int |
| 16 | + Disks []string |
| 17 | + Enabled bool |
| 18 | + MemoryEnabled bool |
| 19 | + DiskEnabled bool |
| 20 | + AgentID string |
| 21 | + Validation []Validation |
| 22 | +} |
| 23 | + |
| 24 | +func monitoringDataSource() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + SchemaVersion: 1, |
| 27 | + |
| 28 | + Description: "Use this data source to configure editable options for workspaces.", |
| 29 | + ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 30 | + rd.SetId(uuid.NewString()) |
| 31 | + |
| 32 | + fixedValidation, err := fixValidationResourceData(rd.GetRawConfig(), rd.Get("validation")) |
| 33 | + if err != nil { |
| 34 | + return diag.FromErr(err) |
| 35 | + } |
| 36 | + |
| 37 | + err = rd.Set("validation", fixedValidation) |
| 38 | + if err != nil { |
| 39 | + return diag.FromErr(err) |
| 40 | + } |
| 41 | + |
| 42 | + var monitoring Monitoring |
| 43 | + err = mapstructure.Decode(struct { |
| 44 | + Threshold interface{} |
| 45 | + MemoryThreshold interface{} |
| 46 | + DiskThreshold interface{} |
| 47 | + Disks interface{} |
| 48 | + Enabled interface{} |
| 49 | + MemoryEnabled interface{} |
| 50 | + DiskEnabled interface{} |
| 51 | + AgentID interface{} |
| 52 | + Validation interface{} |
| 53 | + }{ |
| 54 | + Threshold: rd.Get("threshold"), |
| 55 | + MemoryThreshold: rd.Get("memory_threshold"), |
| 56 | + DiskThreshold: rd.Get("disk_threshold"), |
| 57 | + Disks: rd.Get("disks"), |
| 58 | + Enabled: rd.Get("enabled"), |
| 59 | + MemoryEnabled: rd.Get("memory_enabled"), |
| 60 | + DiskEnabled: rd.Get("disk_enabled"), |
| 61 | + AgentID: rd.Get("agent_id"), |
| 62 | + Validation: fixedValidation, |
| 63 | + }, &monitoring) |
| 64 | + if err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + }, |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "threshold": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Optional: true, |
| 74 | + Description: "The threshold for the monitoring module.", |
| 75 | + }, |
| 76 | + "memory_threshold": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Optional: true, |
| 79 | + Description: "The memory threshold for the monitoring module.", |
| 80 | + }, |
| 81 | + "disk_threshold": { |
| 82 | + Type: schema.TypeInt, |
| 83 | + Optional: true, |
| 84 | + Description: "The disk threshold for the monitoring module.", |
| 85 | + }, |
| 86 | + "disks": { |
| 87 | + Type: schema.TypeList, |
| 88 | + Optional: true, |
| 89 | + MaxItems: 10, |
| 90 | + Description: "The disks to monitor.", |
| 91 | + Elem: &schema.Schema{ |
| 92 | + Type: schema.TypeString, |
| 93 | + }, |
| 94 | + }, |
| 95 | + "enabled": { |
| 96 | + Type: schema.TypeBool, |
| 97 | + Optional: true, |
| 98 | + Description: "Whether the monitoring module is enabled.", |
| 99 | + }, |
| 100 | + "memory_enabled": { |
| 101 | + Type: schema.TypeBool, |
| 102 | + Optional: true, |
| 103 | + Description: "Whether the memory monitoring module is enabled.", |
| 104 | + }, |
| 105 | + "disk_enabled": { |
| 106 | + Type: schema.TypeBool, |
| 107 | + Optional: true, |
| 108 | + Description: "Whether the disk monitoring module is enabled.", |
| 109 | + }, |
| 110 | + "agent_id": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Description: "The ID of the agent to use for the monitoring module.", |
| 113 | + ForceNew: true, |
| 114 | + Optional: false, |
| 115 | + }, |
| 116 | + "validation": { |
| 117 | + Type: schema.TypeList, |
| 118 | + MaxItems: 1, |
| 119 | + Optional: true, |
| 120 | + Description: "Validate the input of a parameter.", |
| 121 | + Elem: &schema.Resource{ |
| 122 | + Schema: map[string]*schema.Schema{ |
| 123 | + "min": { |
| 124 | + Type: schema.TypeInt, |
| 125 | + Optional: true, |
| 126 | + Description: "The minimum of a number parameter.", |
| 127 | + }, |
| 128 | + "min_disabled": { |
| 129 | + Type: schema.TypeBool, |
| 130 | + Computed: true, |
| 131 | + Description: "Helper field to check if min is present", |
| 132 | + }, |
| 133 | + "max": { |
| 134 | + Type: schema.TypeInt, |
| 135 | + Optional: true, |
| 136 | + Description: "The maximum of a number parameter.", |
| 137 | + }, |
| 138 | + "max_disabled": { |
| 139 | + Type: schema.TypeBool, |
| 140 | + Computed: true, |
| 141 | + Description: "Helper field to check if max is present", |
| 142 | + }, |
| 143 | + "monotonic": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Optional: true, |
| 146 | + Description: "Number monotonicity, either increasing or decreasing.", |
| 147 | + }, |
| 148 | + "regex": { |
| 149 | + Type: schema.TypeString, |
| 150 | + ConflictsWith: []string{"validation.0.min", "validation.0.max", "validation.0.monotonic"}, |
| 151 | + Description: "A regex for the input parameter to match against.", |
| 152 | + Optional: true, |
| 153 | + }, |
| 154 | + "error": { |
| 155 | + Type: schema.TypeString, |
| 156 | + Optional: true, |
| 157 | + Description: "An error message to display if the value breaks the validation rules. The following placeholders are supported: {max}, {min}, and {value}.", |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | +} |
0 commit comments