|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceSumologicLocalWindowsEventLogSource() *schema.Resource { |
| 13 | + LocalWindowsEventLogSource := resourceSumologicSource() |
| 14 | + LocalWindowsEventLogSource.Create = resourceSumologicLocalWindowsEventLogSourceCreate |
| 15 | + LocalWindowsEventLogSource.Read = resourceSumologicLocalWindowsEventLogSourceRead |
| 16 | + LocalWindowsEventLogSource.Update = resourceSumologicLocalWindowsEventLogSourceUpdate |
| 17 | + LocalWindowsEventLogSource.Importer = &schema.ResourceImporter{ |
| 18 | + State: resourceSumologicSourceImport, |
| 19 | + } |
| 20 | + |
| 21 | + // Windows Event Log specific fields |
| 22 | + LocalWindowsEventLogSource.Schema["log_names"] = &schema.Schema{ |
| 23 | + Type: schema.TypeList, |
| 24 | + Required: true, |
| 25 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 26 | + Description: "List of Windows log types to collect (e.g., Security, Application, System)", |
| 27 | + } |
| 28 | + |
| 29 | + LocalWindowsEventLogSource.Schema["render_messages"] = &schema.Schema{ |
| 30 | + Type: schema.TypeBool, |
| 31 | + Optional: true, |
| 32 | + Default: true, |
| 33 | + Description: "When using legacy format, indicates if full event messages are collected", |
| 34 | + } |
| 35 | + |
| 36 | + LocalWindowsEventLogSource.Schema["event_format"] = &schema.Schema{ |
| 37 | + Type: schema.TypeInt, |
| 38 | + Optional: true, |
| 39 | + Default: 0, |
| 40 | + ValidateFunc: validation.IntInSlice([]int{0, 1}), |
| 41 | + Description: "0 for legacy format (XML), 1 for JSON format", |
| 42 | + } |
| 43 | + |
| 44 | + LocalWindowsEventLogSource.Schema["event_message"] = &schema.Schema{ |
| 45 | + Type: schema.TypeInt, |
| 46 | + Optional: true, |
| 47 | + ValidateFunc: validation.IntInSlice([]int{0, 1, 2}), |
| 48 | + Description: "0 for complete message, 1 for message title, 2 for metadata only. Required if event_format is 0", |
| 49 | + } |
| 50 | + |
| 51 | + LocalWindowsEventLogSource.Schema["deny_list"] = &schema.Schema{ |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Comma-separated list of event IDs to deny", |
| 55 | + } |
| 56 | + |
| 57 | + LocalWindowsEventLogSource.Schema["allow_list"] = &schema.Schema{ |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + Description: "Comma-separated list of event IDs to allow", |
| 61 | + } |
| 62 | + |
| 63 | + return LocalWindowsEventLogSource |
| 64 | +} |
| 65 | + |
| 66 | +func resourceSumologicLocalWindowsEventLogSourceCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + c := meta.(*Client) |
| 68 | + |
| 69 | + if d.Id() == "" { |
| 70 | + source := resourceToLocalWindowsEventLogSource(d) |
| 71 | + collectorID := d.Get("collector_id").(int) |
| 72 | + |
| 73 | + id, err := c.CreateLocalWindowsEventLogSource(source, collectorID) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + d.SetId(strconv.Itoa(id)) |
| 79 | + } |
| 80 | + |
| 81 | + return resourceSumologicLocalWindowsEventLogSourceRead(d, meta) |
| 82 | +} |
| 83 | + |
| 84 | +func resourceSumologicLocalWindowsEventLogSourceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + c := meta.(*Client) |
| 86 | + |
| 87 | + source := resourceToLocalWindowsEventLogSource(d) |
| 88 | + |
| 89 | + err := c.UpdateLocalWindowsEventLogSource(source, d.Get("collector_id").(int)) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + return resourceSumologicLocalWindowsEventLogSourceRead(d, meta) |
| 96 | +} |
| 97 | + |
| 98 | +func resourceToLocalWindowsEventLogSource(d *schema.ResourceData) LocalWindowsEventLogSource { |
| 99 | + |
| 100 | + source := resourceToSource(d) |
| 101 | + source.Type = "LocalWindowsEventLog" |
| 102 | + |
| 103 | + LocalWindowsEventLogSource := LocalWindowsEventLogSource{ |
| 104 | + Source: source, |
| 105 | + LogNames: d.Get("log_names").([]interface{}), |
| 106 | + RenderMessages: d.Get("render_messages").(bool), |
| 107 | + EventFormat: d.Get("event_format").(int), |
| 108 | + } |
| 109 | + |
| 110 | + // Handle optional deny_list |
| 111 | + if DenyList, ok := d.GetOk("deny_list"); ok { |
| 112 | + LocalWindowsEventLogSource.DenyList = DenyList.(string) |
| 113 | + } |
| 114 | + |
| 115 | + // Handle optional allow_list |
| 116 | + if AllowList, ok := d.GetOk("allow_list"); ok { |
| 117 | + LocalWindowsEventLogSource.AllowList = AllowList.(string) |
| 118 | + } |
| 119 | + |
| 120 | + // Handle optional event_message field |
| 121 | + if eventMessage, ok := d.GetOk("event_message"); ok { |
| 122 | + eventMessageInt := eventMessage.(int) |
| 123 | + LocalWindowsEventLogSource.EventMessage = &eventMessageInt |
| 124 | + } |
| 125 | + |
| 126 | + return LocalWindowsEventLogSource |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +func resourceSumologicLocalWindowsEventLogSourceRead(d *schema.ResourceData, meta interface{}) error { |
| 131 | + c := meta.(*Client) |
| 132 | + |
| 133 | + id, _ := strconv.Atoi(d.Id()) |
| 134 | + source, err := c.GetLocalWindowsEventLogSource(d.Get("collector_id").(int), id) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + if source == nil { |
| 141 | + log.Printf("[WARN] Local Windows Event Log source not found, removing from state: %v - %v", id, err) |
| 142 | + d.SetId("") |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + if err := resourceSumologicSourceRead(d, source.Source); err != nil { |
| 147 | + return fmt.Errorf("%s", err) |
| 148 | + } |
| 149 | + d.Set("log_names", source.LogNames) |
| 150 | + d.Set("render_messages", source.RenderMessages) |
| 151 | + d.Set("event_format", source.EventFormat) |
| 152 | + d.Set("deny_list", source.DenyList) |
| 153 | + d.Set("allow_list", source.AllowList) |
| 154 | + d.Set("event_message", source.EventMessage) |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments