github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/logentries/resource_logentries_log.go (about)

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