|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + validArgumentTypes = []string{ |
| 12 | + "String", |
| 13 | + "Any", |
| 14 | + "Number", |
| 15 | + "Keyword", |
| 16 | + } |
| 17 | +) |
| 18 | + |
| 19 | +func resourceSumologicMacro() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + Create: resourceSumologicMacroCreate, |
| 22 | + Read: resourceSumologicMacroRead, |
| 23 | + Delete: resourceSumologicMacroDelete, |
| 24 | + Update: resourceSumologicMacroUpdate, |
| 25 | + Importer: &schema.ResourceImporter{ |
| 26 | + State: schema.ImportStatePassthrough, |
| 27 | + }, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + ValidateFunc: validation.StringLenBetween(1, 128), |
| 35 | + }, |
| 36 | + "description": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Optional: true, |
| 39 | + }, |
| 40 | + "definition": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + "enabled": { |
| 45 | + Type: schema.TypeBool, |
| 46 | + Optional: true, |
| 47 | + Default: true, |
| 48 | + }, |
| 49 | + "argument": { |
| 50 | + Type: schema.TypeList, |
| 51 | + Optional: true, |
| 52 | + Elem: &schema.Resource{ |
| 53 | + Schema: getArgumentsSchema(), |
| 54 | + }, |
| 55 | + }, |
| 56 | + "argument_validation": { |
| 57 | + Type: schema.TypeList, |
| 58 | + Optional: true, |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: getArgumentValidationsSchema(), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func getArgumentsSchema() map[string]*schema.Schema { |
| 68 | + return map[string]*schema.Schema{ |
| 69 | + "name": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Required: true, |
| 72 | + }, |
| 73 | + "type": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Optional: true, |
| 76 | + ValidateFunc: validation.StringInSlice(validArgumentTypes, false), |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func getArgumentValidationsSchema() map[string]*schema.Schema { |
| 82 | + return map[string]*schema.Schema{ |
| 83 | + "eval_expression": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Required: true, |
| 86 | + }, |
| 87 | + "error_message": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Optional: true, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func resourceToMacro(d *schema.ResourceData) Macro { |
| 95 | + var arguments []Argument |
| 96 | + if val, ok := d.GetOk("argument"); ok { |
| 97 | + tfArguments := val.([]interface{}) |
| 98 | + for _, tfArgument := range tfArguments { |
| 99 | + argument := getArgument(tfArgument.(map[string]interface{})) |
| 100 | + arguments = append(arguments, argument) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + var argumentValidations []Argumentvalidation |
| 105 | + if val, ok := d.GetOk("argument_validation"); ok { |
| 106 | + tfArgumentValidations := val.([]interface{}) |
| 107 | + for _, tfArgumentValidation := range tfArgumentValidations { |
| 108 | + argumentValidation := getArgumentValidation(tfArgumentValidation.(map[string]interface{})) |
| 109 | + argumentValidations = append(argumentValidations, argumentValidation) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return Macro{ |
| 114 | + ID: d.Id(), |
| 115 | + Name: d.Get("name").(string), |
| 116 | + Description: d.Get("description").(string), |
| 117 | + Definition: d.Get("definition").(string), |
| 118 | + Enabled: d.Get("enabled").(bool), |
| 119 | + Arguments: arguments, |
| 120 | + ArgumentValidations: argumentValidations, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func getArgument(tfArgument map[string]interface{}) Argument { |
| 125 | + var argument Argument |
| 126 | + |
| 127 | + if val, ok := tfArgument["name"]; ok { |
| 128 | + argument.Name = val.(string) |
| 129 | + } |
| 130 | + |
| 131 | + if val, ok := tfArgument["type"]; ok { |
| 132 | + argument.Type = val.(string) |
| 133 | + } |
| 134 | + return argument |
| 135 | +} |
| 136 | + |
| 137 | +func getArgumentValidation(tfArgumentValidation map[string]interface{}) Argumentvalidation { |
| 138 | + var argumentValidation Argumentvalidation |
| 139 | + |
| 140 | + if val, ok := tfArgumentValidation["eval_expression"]; ok { |
| 141 | + argumentValidation.EvalExpression = val.(string) |
| 142 | + } |
| 143 | + |
| 144 | + if val, ok := tfArgumentValidation["error_message"]; ok { |
| 145 | + argumentValidation.ErrorMessage = val.(string) |
| 146 | + } |
| 147 | + return argumentValidation |
| 148 | +} |
| 149 | + |
| 150 | +func setMacro(d *schema.ResourceData, macro *Macro) error { |
| 151 | + if err := d.Set("name", macro.Name); err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + if err := d.Set("description", macro.Description); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + if err := d.Set("definition", macro.Definition); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + if err := d.Set("enabled", macro.Enabled); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + arguments := getTerraformArguments(macro.Arguments) |
| 167 | + if err := d.Set("argument", arguments); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + argumentValidations := getTerraformArgumentValidations(macro.ArgumentValidations) |
| 172 | + if err := d.Set("argument_validation", argumentValidations); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + log.Println("=====================================================================") |
| 177 | + log.Printf("name: %s\n", d.Get("name")) |
| 178 | + log.Printf("description: %s\n", d.Get("description")) |
| 179 | + log.Printf("definition: %s\n", d.Get("definition")) |
| 180 | + log.Printf("enabled: %s\n", d.Get("enabled")) |
| 181 | + log.Printf("arguments: %+v\n", d.Get("arguments")) |
| 182 | + log.Printf("argumentValidations: %+v\n", d.Get("argument_validation")) |
| 183 | + log.Println("=====================================================================") |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func getTerraformArguments(arguments []Argument) []map[string]interface{} { |
| 188 | + tfArguments := make([]map[string]interface{}, len(arguments)) |
| 189 | + |
| 190 | + for i, argument := range arguments { |
| 191 | + tfArguments[i] = make(map[string]interface{}) |
| 192 | + tfArguments[i]["name"] = argument.Name |
| 193 | + tfArguments[i]["type"] = argument.Type |
| 194 | + } |
| 195 | + return tfArguments |
| 196 | +} |
| 197 | + |
| 198 | +func getTerraformArgumentValidations(argumentValidations []Argumentvalidation) []map[string]interface{} { |
| 199 | + tfArgumentValidations := make([]map[string]interface{}, len(argumentValidations)) |
| 200 | + |
| 201 | + for i, argumentValidation := range argumentValidations { |
| 202 | + tfArgumentValidations[i] = make(map[string]interface{}) |
| 203 | + tfArgumentValidations[i]["eval_expression"] = argumentValidation.EvalExpression |
| 204 | + tfArgumentValidations[i]["error_message"] = argumentValidation.ErrorMessage |
| 205 | + } |
| 206 | + return tfArgumentValidations |
| 207 | +} |
| 208 | + |
| 209 | +func resourceSumologicMacroCreate(d *schema.ResourceData, meta interface{}) error { |
| 210 | + c := meta.(*Client) |
| 211 | + if d.Id() == "" { |
| 212 | + macro := resourceToMacro(d) |
| 213 | + log.Println("=====================================================================") |
| 214 | + log.Printf("Creating macro: %+v\n", macro) |
| 215 | + log.Println("=====================================================================") |
| 216 | + |
| 217 | + createdMacro, err := c.CreateMacro(macro) |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + d.SetId(createdMacro.ID) |
| 222 | + } |
| 223 | + |
| 224 | + return resourceSumologicMacroRead(d, meta) |
| 225 | +} |
| 226 | + |
| 227 | +func resourceSumologicMacroRead(d *schema.ResourceData, meta interface{}) error { |
| 228 | + c := meta.(*Client) |
| 229 | + |
| 230 | + id := d.Id() |
| 231 | + macro, err := c.GetMacro(id) |
| 232 | + log.Println("=====================================================================") |
| 233 | + log.Printf("Read macro: %+v\n", macro) |
| 234 | + log.Println("=====================================================================") |
| 235 | + if err != nil { |
| 236 | + return err |
| 237 | + } |
| 238 | + |
| 239 | + if macro == nil { |
| 240 | + log.Printf("[WARN] Macro not found, removing from state: %v - %v", id, err) |
| 241 | + d.SetId("") |
| 242 | + return nil |
| 243 | + } |
| 244 | + |
| 245 | + err = setMacro(d, macro) |
| 246 | + return err |
| 247 | +} |
| 248 | + |
| 249 | +func resourceSumologicMacroDelete(d *schema.ResourceData, meta interface{}) error { |
| 250 | + c := meta.(*Client) |
| 251 | + log.Printf("Deleting macro: %+v\n", d.Id()) |
| 252 | + return c.DeleteMacro(d.Id()) |
| 253 | +} |
| 254 | + |
| 255 | +func resourceSumologicMacroUpdate(d *schema.ResourceData, meta interface{}) error { |
| 256 | + macro := resourceToMacro(d) |
| 257 | + log.Println("=====================================================================") |
| 258 | + log.Printf("Updating macro: %+v\n", macro) |
| 259 | + log.Println("=====================================================================") |
| 260 | + |
| 261 | + c := meta.(*Client) |
| 262 | + err := c.UpdateMacro(macro) |
| 263 | + |
| 264 | + if err != nil { |
| 265 | + return err |
| 266 | + } |
| 267 | + |
| 268 | + return resourceSumologicMacroRead(d, meta) |
| 269 | +} |
0 commit comments