|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Sumo Logic and manual |
| 8 | +// changes will be clobbered when the file is regenerated. Do not submit |
| 9 | +// changes to this file. |
| 10 | +// |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | +package sumologic |
| 13 | + |
| 14 | +import ( |
| 15 | + "fmt" |
| 16 | + "log" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 20 | +) |
| 21 | + |
| 22 | +func resourceSumologicLookupTable() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Create: resourceSumologicLookupTableCreate, |
| 25 | + Read: resourceSumologicLookupTableRead, |
| 26 | + Update: resourceSumologicLookupTableUpdate, |
| 27 | + Delete: resourceSumologicLookupTableDelete, |
| 28 | + Importer: &schema.ResourceImporter{ |
| 29 | + State: schema.ImportStatePassthrough, |
| 30 | + }, |
| 31 | + |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + |
| 34 | + "primary_keys": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Optional: true, |
| 37 | + Description: "The primary key field names.", |
| 38 | + Elem: &schema.Schema{ |
| 39 | + Type: schema.TypeString, |
| 40 | + }, |
| 41 | + ForceNew: true, |
| 42 | + }, |
| 43 | + |
| 44 | + "parent_folder_id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + ForceNew: true, |
| 48 | + }, |
| 49 | + |
| 50 | + "description": { |
| 51 | + Type: schema.TypeString, |
| 52 | + ValidateFunc: validation.StringLenBetween(0, 1000), |
| 53 | + Required: true, |
| 54 | + }, |
| 55 | + |
| 56 | + "size_limit_action": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + ValidateFunc: validation.StringInSlice([]string{"StopIncomingMessages", "DeleteOldData"}, false), |
| 60 | + Default: "StopIncomingMessages", |
| 61 | + }, |
| 62 | + |
| 63 | + "fields": { |
| 64 | + Type: schema.TypeList, |
| 65 | + Optional: true, |
| 66 | + Description: "The list of fields in the lookup table.", |
| 67 | + Elem: &schema.Resource{ |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + |
| 70 | + "field_name": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Required: true, |
| 73 | + ForceNew: false, |
| 74 | + }, |
| 75 | + |
| 76 | + "field_type": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Required: true, |
| 79 | + ForceNew: false, |
| 80 | + ValidateFunc: validation.StringInSlice([]string{"boolean", "int", "long", "double", "string"}, false), |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + ForceNew: true, |
| 85 | + }, |
| 86 | + |
| 87 | + "ttl": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Optional: true, |
| 90 | + Default: 0, |
| 91 | + }, |
| 92 | + |
| 93 | + "name": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Optional: true, |
| 96 | + ForceNew: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func resourceSumologicLookupTableCreate(d *schema.ResourceData, meta interface{}) error { |
| 103 | + c := meta.(*Client) |
| 104 | + |
| 105 | + if d.Id() == "" { |
| 106 | + lookupTable := resourceToLookupTable(d) |
| 107 | + id, err := c.CreateLookupTable(lookupTable) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + d.SetId(id) |
| 113 | + } |
| 114 | + |
| 115 | + log.Printf("created lookup: %+v\n", d) |
| 116 | + log.Printf("lookup id: %v\n", d.Id()) |
| 117 | + return resourceSumologicLookupTableRead(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceSumologicLookupTableRead(d *schema.ResourceData, meta interface{}) error { |
| 121 | + c := meta.(*Client) |
| 122 | + |
| 123 | + id := d.Id() |
| 124 | + lookupTable, err := c.GetLookupTable(id) |
| 125 | + log.Printf("##DEBUG## read lookup: %+v\n", lookupTable) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if lookupTable == nil { |
| 131 | + log.Printf("[WARN] LookupTable not found, removing from state: %v - %v", id, err) |
| 132 | + d.SetId("") |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + d.Set("name", lookupTable.Name) |
| 137 | + if err := d.Set("fields", fieldsToList(lookupTable.Fields)); err != nil { |
| 138 | + return fmt.Errorf("error setting fields for resource %s: %s", d.Id(), err) |
| 139 | + } |
| 140 | + d.Set("ttl", lookupTable.Ttl) |
| 141 | + d.Set("primary_keys", lookupTable.PrimaryKeys) |
| 142 | + d.Set("parent_folder_id", lookupTable.ParentFolderId) |
| 143 | + d.Set("size_limit_action", lookupTable.SizeLimitAction) |
| 144 | + d.Set("description", lookupTable.Description) |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func resourceSumologicLookupTableDelete(d *schema.ResourceData, meta interface{}) error { |
| 150 | + c := meta.(*Client) |
| 151 | + |
| 152 | + log.Printf("##DEBUG## resourceSumologicLookupTableDelete: %s", d.Id()) |
| 153 | + return c.DeleteLookupTable(d.Id()) |
| 154 | +} |
| 155 | + |
| 156 | +func resourceSumologicLookupTableUpdate(d *schema.ResourceData, meta interface{}) error { |
| 157 | + c := meta.(*Client) |
| 158 | + |
| 159 | + lookupTable := resourceToLookupTable(d) |
| 160 | + err := c.UpdateLookupTable(lookupTable) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + return resourceSumologicLookupTableRead(d, meta) |
| 166 | +} |
| 167 | + |
| 168 | +func resourceToLookupTable(d *schema.ResourceData) LookupTable { |
| 169 | + |
| 170 | + fieldsData := d.Get("fields").([]interface{}) |
| 171 | + var fields []LookupTableField |
| 172 | + for _, data := range fieldsData { |
| 173 | + fields = append(fields, resourceToLookupTableField([]interface{}{data})) |
| 174 | + } |
| 175 | + |
| 176 | + primaryKeysData := d.Get("primary_keys").([]interface{}) |
| 177 | + var primaryKeys []string |
| 178 | + for _, data := range primaryKeysData { |
| 179 | + primaryKeys = append(primaryKeys, data.(string)) |
| 180 | + } |
| 181 | + |
| 182 | + return LookupTable{ |
| 183 | + Name: d.Get("name").(string), |
| 184 | + ID: d.Id(), |
| 185 | + Fields: fields, |
| 186 | + Description: d.Get("description").(string), |
| 187 | + Ttl: d.Get("ttl").(int), |
| 188 | + SizeLimitAction: d.Get("size_limit_action").(string), |
| 189 | + PrimaryKeys: primaryKeys, |
| 190 | + ParentFolderId: d.Get("parent_folder_id").(string), |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func resourceToLookupTableField(data interface{}) LookupTableField { |
| 195 | + |
| 196 | + lookupTableFieldSlice := data.([]interface{}) |
| 197 | + lookupTableField := LookupTableField{} |
| 198 | + if len(lookupTableFieldSlice) > 0 { |
| 199 | + lookupTableFieldObj := lookupTableFieldSlice[0].(map[string]interface{}) |
| 200 | + lookupTableField.FieldName = lookupTableFieldObj["field_name"].(string) |
| 201 | + lookupTableField.FieldType = lookupTableFieldObj["field_type"].(string) |
| 202 | + } |
| 203 | + |
| 204 | + return lookupTableField |
| 205 | +} |
| 206 | + |
| 207 | +func fieldsToList(lookupTableField []LookupTableField) []map[string]interface{} { |
| 208 | + var s []map[string]interface{} |
| 209 | + |
| 210 | + for _, t := range lookupTableField { |
| 211 | + mapping := map[string]interface{}{ |
| 212 | + "field_name": t.FieldName, |
| 213 | + "field_type": t.FieldType, |
| 214 | + } |
| 215 | + s = append(s, mapping) |
| 216 | + } |
| 217 | + |
| 218 | + return s |
| 219 | +} |
0 commit comments