|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + "log" |
| 8 | +) |
| 9 | + |
| 10 | +func resourceSumologicOTCollector() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: resourceSumologicOTCollectorRead, |
| 13 | + Delete: resourceSumologicOTCollectorDelete, |
| 14 | + Update: resourceSumologicOTCollectorUpdate, |
| 15 | + Importer: &schema.ResourceImporter{ |
| 16 | + State: schema.ImportStatePassthrough, |
| 17 | + }, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + |
| 21 | + "created_at": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + |
| 26 | + "modified_by": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + |
| 31 | + "created_by": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + |
| 36 | + "description": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Optional: true, |
| 39 | + }, |
| 40 | + |
| 41 | + "alive": { |
| 42 | + Type: schema.TypeBool, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + |
| 46 | + "is_remotely_managed": { |
| 47 | + Type: schema.TypeBool, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + |
| 51 | + "name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + }, |
| 55 | + "time_zone": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + }, |
| 59 | + |
| 60 | + "modified_at": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + |
| 65 | + "category": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + }, |
| 69 | + |
| 70 | + "ephemeral": { |
| 71 | + Type: schema.TypeBool, |
| 72 | + Optional: true, |
| 73 | + }, |
| 74 | + |
| 75 | + "tags": { |
| 76 | + Type: schema.TypeMap, |
| 77 | + Elem: &schema.Schema{ |
| 78 | + Type: schema.TypeString, |
| 79 | + }, |
| 80 | + Optional: true, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func resourceSumologicOTCollectorRead(d *schema.ResourceData, meta interface{}) error { |
| 87 | + c := meta.(*Client) |
| 88 | + |
| 89 | + id := d.Id() |
| 90 | + otCollector, err := c.GetOTCollector(id) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + if otCollector == nil { |
| 96 | + log.Printf("[WARN] OTCollector not found, removing from state: %v - %v", id, err) |
| 97 | + d.SetId("") |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + if err := d.Set("tags", otCollector.Tags); err != nil { |
| 102 | + return fmt.Errorf("error setting fields for resource %s: %s", d.Id(), err) |
| 103 | + } |
| 104 | + |
| 105 | + d.Set("alive", otCollector.IsAlive) |
| 106 | + d.Set("created_at", otCollector.CreatedAt) |
| 107 | + d.Set("description", otCollector.Description) |
| 108 | + d.Set("time_zone", otCollector.TimeZone) |
| 109 | + d.Set("category", otCollector.Category) |
| 110 | + d.Set("modified_at", otCollector.ModifiedAt) |
| 111 | + d.Set("is_remotely_managed", otCollector.IsRemotelyManaged) |
| 112 | + d.Set("created_by", otCollector.CreatedBy) |
| 113 | + d.Set("modified_by", otCollector.ModifiedBy) |
| 114 | + d.Set("ephemeral", otCollector.Ephemeral) |
| 115 | + d.Set("name", otCollector.Name) |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceSumologicOTCollectorDelete(d *schema.ResourceData, meta interface{}) error { |
| 121 | + c := meta.(*Client) |
| 122 | + |
| 123 | + return c.DeleteOTCollector(d.Id()) |
| 124 | +} |
| 125 | + |
| 126 | +func resourceSumologicOTCollectorUpdate(d *schema.ResourceData, meta interface{}) error { |
| 127 | + return errors.New("Terraform does not support OTel collector updates") |
| 128 | +} |
| 129 | + |
| 130 | +func resourceToOTCollector(d *schema.ResourceData) OTCollector { |
| 131 | + |
| 132 | + return OTCollector{ |
| 133 | + IsAlive: d.Get("alive").(bool), |
| 134 | + CreatedBy: d.Get("created_by").(string), |
| 135 | + Ephemeral: d.Get("ephemeral").(bool), |
| 136 | + CreatedAt: d.Get("created_at").(string), |
| 137 | + Description: d.Get("description").(string), |
| 138 | + ModifiedBy: d.Get("modified_by").(string), |
| 139 | + ModifiedAt: d.Get("modified_at").(string), |
| 140 | + Category: d.Get("category").(string), |
| 141 | + IsRemotelyManaged: d.Get("is_remotely_managed").(bool), |
| 142 | + TimeZone: d.Get("time_zone").(string), |
| 143 | + ID: d.Id(), |
| 144 | + Name: d.Get("name").(string), |
| 145 | + Tags: d.Get("fields").(map[string]interface{}), |
| 146 | + } |
| 147 | +} |
0 commit comments