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