|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 6 | + "log" |
| 7 | +) |
| 8 | + |
| 9 | +func resourceSumologicCSEOutlierRule() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Create: resourceSumologicCSEOutlierRuleCreate, |
| 12 | + Read: resourceSumologicCSEOutlierRuleRead, |
| 13 | + Delete: resourceSumologicCSEOutlierRuleDelete, |
| 14 | + Update: resourceSumologicCSEOutlierRuleUpdate, |
| 15 | + Importer: &schema.ResourceImporter{ |
| 16 | + State: schema.ImportStatePassthrough, |
| 17 | + }, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "aggregation_functions": { |
| 21 | + Type: schema.TypeList, |
| 22 | + Required: true, |
| 23 | + MaxItems: 1, |
| 24 | + Elem: &schema.Resource{ |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "function": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "arguments": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Required: true, |
| 37 | + Elem: &schema.Schema{ |
| 38 | + Type: schema.TypeString, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + |
| 45 | + "baseline_window_size": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + }, |
| 49 | + "description_expression": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + "deviation_threshold": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + "enabled": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Required: true, |
| 60 | + }, |
| 61 | + "entity_selectors": getEntitySelectorsSchema(), |
| 62 | + "floor_value": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Required: true, |
| 65 | + }, |
| 66 | + "group_by_fields": { |
| 67 | + Type: schema.TypeList, |
| 68 | + Optional: true, |
| 69 | + Elem: &schema.Schema{ |
| 70 | + Type: schema.TypeString, |
| 71 | + }, |
| 72 | + }, |
| 73 | + "is_prototype": { |
| 74 | + Type: schema.TypeBool, |
| 75 | + Optional: true, |
| 76 | + }, |
| 77 | + "match_expression": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Required: true, |
| 80 | + }, |
| 81 | + "name": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Required: true, |
| 84 | + }, |
| 85 | + "name_expression": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Required: true, |
| 88 | + }, |
| 89 | + "retention_window_size": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Required: true, |
| 92 | + }, |
| 93 | + "severity": { |
| 94 | + Type: schema.TypeInt, |
| 95 | + Required: true, |
| 96 | + }, |
| 97 | + "summary_expression": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Optional: true, |
| 100 | + }, |
| 101 | + "tags": { |
| 102 | + Type: schema.TypeList, |
| 103 | + Optional: true, |
| 104 | + Elem: &schema.Schema{ |
| 105 | + Type: schema.TypeString, |
| 106 | + ValidateFunc: validation.StringIsNotEmpty, |
| 107 | + }, |
| 108 | + }, |
| 109 | + "window_size": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Required: true, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func resourceSumologicCSEOutlierRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 118 | + c := meta.(*Client) |
| 119 | + |
| 120 | + var CSEOutlierRuleGet *CSEOutlierRule |
| 121 | + id := d.Id() |
| 122 | + |
| 123 | + CSEOutlierRuleGet, err := c.GetCSEOutlierRule(id) |
| 124 | + if err != nil { |
| 125 | + log.Printf("[WARN] CSE Outlier Rule not found when looking by id: %s, err: %v", id, err) |
| 126 | + } |
| 127 | + |
| 128 | + if CSEOutlierRuleGet == nil { |
| 129 | + log.Printf("[WARN] CSE Outlier Rule not found, removing from state: %v - %v", id, err) |
| 130 | + d.SetId("") |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + d.Set("aggregate_function", aggregationFunctionsArrayToResource(CSEOutlierRuleGet.AggregationFunctions)) |
| 135 | + d.Set("baseline_window_size", CSEOutlierRuleGet.BaselineWindowSize) |
| 136 | + d.Set("description_expression", CSEOutlierRuleGet.DescriptionExpression) |
| 137 | + d.Set("deviation_threshold", CSEOutlierRuleGet.DeviationThreshold) |
| 138 | + d.Set("enabled", CSEOutlierRuleGet.Enabled) |
| 139 | + d.Set("entity_selectors", entitySelectorArrayToResource(CSEOutlierRuleGet.EntitySelectors)) |
| 140 | + d.Set("floor_value", CSEOutlierRuleGet.FloorValue) |
| 141 | + d.Set("group_by_fields", CSEOutlierRuleGet.GroupByFields) |
| 142 | + d.Set("is_prototype", CSEOutlierRuleGet.IsPrototype) |
| 143 | + d.Set("match_expression", CSEOutlierRuleGet.MatchExpression) |
| 144 | + d.Set("name", CSEOutlierRuleGet.Name) |
| 145 | + d.Set("name_expression", CSEOutlierRuleGet.NameExpression) |
| 146 | + d.Set("retention_window_size", CSEOutlierRuleGet.RetentionWindowSize) |
| 147 | + d.Set("severity", CSEOutlierRuleGet.Severity) |
| 148 | + d.Set("summary_expression", CSEOutlierRuleGet.SummaryExpression) |
| 149 | + d.Set("tags", CSEOutlierRuleGet.Tags) |
| 150 | + d.Set("window_size", CSEOutlierRuleGet.WindowSizeName) |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func resourceSumologicCSEOutlierRuleDelete(d *schema.ResourceData, meta interface{}) error { |
| 156 | + c := meta.(*Client) |
| 157 | + |
| 158 | + return c.DeleteCSEOutlierRule(d.Id()) |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | +func resourceSumologicCSEOutlierRuleCreate(d *schema.ResourceData, meta interface{}) error { |
| 163 | + c := meta.(*Client) |
| 164 | + |
| 165 | + if d.Id() == "" { |
| 166 | + id, err := c.CreateCSEOutlierRule(CSEOutlierRule{ |
| 167 | + AggregationFunctions: resourceToAggregationFunctionsArray(d.Get("aggregation_functions").([]interface{})), |
| 168 | + BaselineWindowSize: d.Get("baseline_window_size").(string), |
| 169 | + DescriptionExpression: d.Get("description_expression").(string), |
| 170 | + DeviationThreshold: d.Get("deviation_threshold").(int), |
| 171 | + Enabled: d.Get("enabled").(bool), |
| 172 | + EntitySelectors: resourceToEntitySelectorArray(d.Get("entity_selectors").([]interface{})), |
| 173 | + FloorValue: d.Get("floor_value").(int), |
| 174 | + GroupByFields: resourceToStringArray(d.Get("group_by_fields").([]interface{})), |
| 175 | + IsPrototype: d.Get("is_prototype").(bool), |
| 176 | + MatchExpression: d.Get("match_expression").(string), |
| 177 | + Name: d.Get("name").(string), |
| 178 | + NameExpression: d.Get("name_expression").(string), |
| 179 | + RetentionWindowSize: d.Get("retention_window_size").(string), |
| 180 | + Severity: d.Get("severity").(int), |
| 181 | + SummaryExpression: d.Get("summary_expression").(string), |
| 182 | + Tags: resourceToStringArray(d.Get("tags").([]interface{})), |
| 183 | + WindowSize: windowSizeField(d.Get("window_size").(string)), |
| 184 | + }) |
| 185 | + |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + d.SetId(id) |
| 190 | + } |
| 191 | + |
| 192 | + return resourceSumologicCSEOutlierRuleRead(d, meta) |
| 193 | +} |
| 194 | + |
| 195 | +func resourceSumologicCSEOutlierRuleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 196 | + CSEOutlierRule, err := resourceToCSEOutlierRule(d) |
| 197 | + if err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + |
| 201 | + c := meta.(*Client) |
| 202 | + if err = c.UpdateCSEOutlierRule(CSEOutlierRule); err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + |
| 206 | + return resourceSumologicCSEOutlierRuleRead(d, meta) |
| 207 | +} |
| 208 | + |
| 209 | +func resourceToCSEOutlierRule(d *schema.ResourceData) (CSEOutlierRule, error) { |
| 210 | + id := d.Id() |
| 211 | + if id == "" { |
| 212 | + return CSEOutlierRule{}, nil |
| 213 | + } |
| 214 | + |
| 215 | + return CSEOutlierRule{ |
| 216 | + ID: id, |
| 217 | + AggregationFunctions: resourceToAggregationFunctionsArray(d.Get("aggregation_functions").([]interface{})), |
| 218 | + BaselineWindowSize: d.Get("baseline_window_size").(string), |
| 219 | + DescriptionExpression: d.Get("description_expression").(string), |
| 220 | + DeviationThreshold: d.Get("deviation_threshold").(int), |
| 221 | + Enabled: d.Get("enabled").(bool), |
| 222 | + EntitySelectors: resourceToEntitySelectorArray(d.Get("entity_selectors").([]interface{})), |
| 223 | + FloorValue: d.Get("floor_value").(int), |
| 224 | + GroupByFields: resourceToStringArray(d.Get("group_by_fields").([]interface{})), |
| 225 | + IsPrototype: d.Get("is_prototype").(bool), |
| 226 | + MatchExpression: d.Get("match_expression").(string), |
| 227 | + Name: d.Get("name").(string), |
| 228 | + NameExpression: d.Get("name_expression").(string), |
| 229 | + RetentionWindowSize: d.Get("retention_window_size").(string), |
| 230 | + Severity: d.Get("severity").(int), |
| 231 | + SummaryExpression: d.Get("summary_expression").(string), |
| 232 | + Tags: resourceToStringArray(d.Get("tags").([]interface{})), |
| 233 | + WindowSize: windowSizeField(d.Get("window_size").(string)), |
| 234 | + }, nil |
| 235 | +} |
0 commit comments