|
| 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 resourceSumologicCSEContextAction() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Create: resourceSumologicCSEContextActionCreate, |
| 12 | + Read: resourceSumologicCSEContextActionRead, |
| 13 | + Delete: resourceSumologicCSEContextActionDelete, |
| 14 | + Update: resourceSumologicCSEContextActionUpdate, |
| 15 | + Importer: &schema.ResourceImporter{ |
| 16 | + State: schema.ImportStatePassthrough, |
| 17 | + }, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "name": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + }, |
| 24 | + "type": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + ValidateFunc: validation.All( |
| 28 | + validation.StringIsNotEmpty, |
| 29 | + validation.StringInSlice([]string{"URL", "QUERY"}, false)), |
| 30 | + }, |
| 31 | + "template": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Optional: true, |
| 34 | + }, |
| 35 | + "ioc_types": { |
| 36 | + Type: schema.TypeList, |
| 37 | + Required: true, |
| 38 | + Elem: &schema.Schema{ |
| 39 | + Type: schema.TypeString, |
| 40 | + ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringInSlice([]string{"ASN", "DOMAIN", "HASH", "IP_ADDRESS", "MAC_ADDRESS", "PORT", "RECORD_PROPERTY", "URL"}, false)), |
| 41 | + }, |
| 42 | + }, |
| 43 | + "entity_types": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Optional: true, |
| 46 | + Elem: &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + ValidateFunc: validation.StringIsNotEmpty, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "record_fields": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Optional: true, |
| 54 | + Elem: &schema.Schema{ |
| 55 | + Type: schema.TypeString, |
| 56 | + ValidateFunc: validation.StringIsNotEmpty, |
| 57 | + }, |
| 58 | + }, |
| 59 | + "all_record_fields": { |
| 60 | + Type: schema.TypeBool, |
| 61 | + Optional: true, |
| 62 | + Default: true, |
| 63 | + }, |
| 64 | + "enabled": { |
| 65 | + Type: schema.TypeBool, |
| 66 | + Optional: true, |
| 67 | + Default: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func resourceSumologicCSEContextActionRead(d *schema.ResourceData, meta interface{}) error { |
| 74 | + c := meta.(*Client) |
| 75 | + |
| 76 | + var CSEContextAction *CSEContextAction |
| 77 | + id := d.Id() |
| 78 | + |
| 79 | + CSEContextAction, err := c.GetCSEContextAction(id) |
| 80 | + if err != nil { |
| 81 | + log.Printf("[WARN] CSE Context Action not found when looking by id: %s, err: %v", id, err) |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + if CSEContextAction == nil { |
| 86 | + log.Printf("[WARN] CSE Context Action not found, removing from state: %v - %v", id, err) |
| 87 | + d.SetId("") |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + d.Set("name", CSEContextAction.Name) |
| 92 | + d.Set("type", CSEContextAction.Type) |
| 93 | + d.Set("template", CSEContextAction.Template) |
| 94 | + d.Set("ioc_types", CSEContextAction.IocTypes) |
| 95 | + d.Set("entity_types", CSEContextAction.EntityTypes) |
| 96 | + d.Set("record_fields", CSEContextAction.RecordFields) |
| 97 | + d.Set("all_record_fields", CSEContextAction.AllRecordFields) |
| 98 | + d.Set("enabled", CSEContextAction.Enabled) |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func resourceSumologicCSEContextActionDelete(d *schema.ResourceData, meta interface{}) error { |
| 104 | + c := meta.(*Client) |
| 105 | + |
| 106 | + return c.DeleteCSEContextAction(d.Id()) |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +func resourceSumologicCSEContextActionCreate(d *schema.ResourceData, meta interface{}) error { |
| 111 | + c := meta.(*Client) |
| 112 | + |
| 113 | + if d.Id() == "" { |
| 114 | + id, err := c.CreateCSEContextAction(CSEContextAction{ |
| 115 | + Name: d.Get("name").(string), |
| 116 | + Type: d.Get("type").(string), |
| 117 | + Template: d.Get("template").(string), |
| 118 | + IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})), |
| 119 | + EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})), |
| 120 | + RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})), |
| 121 | + AllRecordFields: d.Get("all_record_fields").(bool), |
| 122 | + Enabled: d.Get("enabled").(bool), |
| 123 | + }) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + d.SetId(id) |
| 130 | + } |
| 131 | + |
| 132 | + return resourceSumologicCSEContextActionRead(d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceSumologicCSEContextActionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 136 | + CSEContextAction, err := resourceToCSEContextAction(d) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + c := meta.(*Client) |
| 141 | + if err = c.UpdateCSEContextAction(CSEContextAction); err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + return resourceSumologicCSEContextActionRead(d, meta) |
| 146 | +} |
| 147 | + |
| 148 | +func resourceToCSEContextAction(d *schema.ResourceData) (CSEContextAction, error) { |
| 149 | + id := d.Id() |
| 150 | + if id == "" { |
| 151 | + return CSEContextAction{}, nil |
| 152 | + } |
| 153 | + |
| 154 | + return CSEContextAction{ |
| 155 | + ID: id, |
| 156 | + Name: d.Get("name").(string), |
| 157 | + Type: d.Get("type").(string), |
| 158 | + Template: d.Get("template").(string), |
| 159 | + IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})), |
| 160 | + EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})), |
| 161 | + RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})), |
| 162 | + AllRecordFields: d.Get("all_record_fields").(bool), |
| 163 | + Enabled: d.Get("enabled").(bool), |
| 164 | + }, nil |
| 165 | +} |
0 commit comments