github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/logentries/resource_logentries_log.go (about) 1 package logentries 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 logentries "github.com/logentries/le_goclient" 11 ) 12 13 func resourceLogentriesLog() *schema.Resource { 14 15 return &schema.Resource{ 16 Create: resourceLogentriesLogCreate, 17 Read: resourceLogentriesLogRead, 18 Update: resourceLogentriesLogUpdate, 19 Delete: resourceLogentriesLogDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "token": { 23 Type: schema.TypeString, 24 Computed: true, 25 ForceNew: true, 26 }, 27 "logset_id": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "name": { 33 Type: schema.TypeString, 34 Required: true, 35 }, 36 "filename": { 37 Type: schema.TypeString, 38 Optional: true, 39 }, 40 "retention_period": { 41 Type: schema.TypeString, 42 Optional: true, 43 Default: "ACCOUNT_DEFAULT", 44 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 45 value := v.(string) 46 allowed_values := []string{"1W", "2W", "1M", "2M", "6M", "1Y", "2Y", "UNLIMITED", "ACCOUNT_DEFAULT"} 47 if !sliceContains(value, allowed_values) { 48 errors = append(errors, fmt.Errorf("Invalid retention period: %s (must be one of: %s)", value, allowed_values)) 49 } 50 return 51 }, 52 }, 53 "source": { 54 Type: schema.TypeString, 55 Optional: true, 56 Default: "token", 57 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 58 value := v.(string) 59 allowed_values := []string{"token", "syslog", "agent", "api"} 60 if !sliceContains(value, allowed_values) { 61 errors = append(errors, fmt.Errorf("Invalid log source option: %s (must be one of: %s)", value, allowed_values)) 62 } 63 return 64 }, 65 }, 66 "type": { 67 Type: schema.TypeString, 68 Default: "", 69 Optional: true, 70 }, 71 }, 72 } 73 } 74 75 func resourceLogentriesLogCreate(d *schema.ResourceData, meta interface{}) error { 76 client := meta.(*logentries.Client) 77 retentionPeriod, err := retentionPeriodForEnum(d.Get("retention_period").(string)) 78 if err != nil { 79 return err 80 } 81 res, err := client.Log.Create(logentries.LogCreateRequest{ 82 LogSetKey: d.Get("logset_id").(string), 83 Name: d.Get("name").(string), 84 Retention: strconv.FormatInt(retentionPeriod, 10), 85 Type: d.Get("type").(string), 86 Source: d.Get("source").(string), 87 Filename: d.Get("filename").(string), 88 }) 89 90 if err != nil { 91 return err 92 } 93 94 d.SetId(res.Key) 95 96 return mapLogToSchema(client, res, d) 97 } 98 99 func resourceLogentriesLogRead(d *schema.ResourceData, meta interface{}) error { 100 client := meta.(*logentries.Client) 101 res, err := client.Log.Read(logentries.LogReadRequest{ 102 LogSetKey: d.Get("logset_id").(string), 103 Key: d.Id(), 104 }) 105 if err != nil { 106 if strings.Contains(err.Error(), "not found") { 107 log.Printf("Logentries Log Not Found - Refreshing from State") 108 d.SetId("") 109 return nil 110 } 111 return err 112 } 113 114 if res == nil { 115 d.SetId("") 116 return nil 117 } 118 119 return mapLogToSchema(client, res, d) 120 } 121 122 func resourceLogentriesLogUpdate(d *schema.ResourceData, meta interface{}) error { 123 client := meta.(*logentries.Client) 124 _, err := client.Log.Update(logentries.LogUpdateRequest{ 125 Key: d.Id(), 126 Name: d.Get("name").(string), 127 Retention: d.Get("retention_period").(string), 128 Type: d.Get("type").(string), 129 Source: d.Get("source").(string), 130 Filename: d.Get("filename").(string), 131 }) 132 if err != nil { 133 return err 134 } 135 136 return resourceLogentriesLogRead(d, meta) 137 } 138 139 func resourceLogentriesLogDelete(d *schema.ResourceData, meta interface{}) error { 140 client := meta.(*logentries.Client) 141 err := client.Log.Delete(logentries.LogDeleteRequest{ 142 LogSetKey: d.Get("logset_id").(string), 143 Key: d.Id(), 144 }) 145 return err 146 } 147 148 func mapLogToSchema(client *logentries.Client, log *logentries.Log, d *schema.ResourceData) error { 149 d.Set("token", log.Token) 150 d.Set("name", log.Name) 151 d.Set("filename", log.Filename) 152 retentionEnum, err := enumForRetentionPeriod(log.Retention) 153 if err != nil { 154 return err 155 } 156 d.Set("retention_period", retentionEnum) 157 d.Set("source", log.Source) 158 if log.Type != "" { 159 logTypes, err := client.LogType.ReadDefault(logentries.LogTypeListRequest{}) 160 if err != nil { 161 return err 162 } 163 logType := lookupTypeShortcut(log.Type, logTypes) 164 if logType == "" { 165 logTypes, err = client.LogType.Read(logentries.LogTypeListRequest{}) 166 if err != nil { 167 return err 168 } 169 logType = lookupTypeShortcut(log.Type, logTypes) 170 } 171 d.Set("type", logType) 172 } 173 174 return nil 175 } 176 177 func enumForRetentionPeriod(retentionPeriod int64) (string, error) { 178 switch retentionPeriod { 179 case 604800000: 180 return "1W", nil 181 case 1209600000: 182 return "2W", nil 183 case 2678400000: 184 return "1M", nil 185 case 5356800000: 186 return "2M", nil 187 case 16070400000: 188 return "6M", nil 189 case 31536000000: 190 return "1Y", nil 191 case 63072000000: 192 return "2Y", nil 193 case 0: 194 return "UNLIMITED", nil 195 case -1: 196 return "ACCOUNT_DEFAULT", nil 197 } 198 199 return "", fmt.Errorf("Unknown retention period: %d", retentionPeriod) 200 } 201 202 func retentionPeriodForEnum(retentionPeriodEnum string) (int64, error) { 203 switch retentionPeriodEnum { 204 case "1W": 205 return 604800000, nil 206 case "2W": 207 return 1209600000, nil 208 case "1M": 209 return 2678400000, nil 210 case "2M": 211 return 5356800000, nil 212 case "6M": 213 return 16070400000, nil 214 case "1Y": 215 return 31536000000, nil 216 case "2Y": 217 return 63072000000, nil 218 case "UNLIMITED": 219 return 0, nil 220 case "ACCOUNT_DEFAULT": 221 return -1, nil 222 } 223 224 return 0, fmt.Errorf("Unknown retention period: %s", retentionPeriodEnum) 225 } 226 227 func lookupTypeShortcut(currentLogKey string, logTypes []logentries.LogType) string { 228 for _, logType := range logTypes { 229 if logType.Key == currentLogKey { 230 return logType.Shortcut 231 } 232 } 233 return "" 234 } 235 236 func sliceContains(a string, list []string) bool { 237 for _, b := range list { 238 if b == a { 239 return true 240 } 241 } 242 return false 243 }