github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/statuscake/resource_statuscaketest.go (about)

     1  package statuscake
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"log"
     8  
     9  	"github.com/DreamItGetIT/statuscake"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceStatusCakeTest() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: CreateTest,
    16  		Update: UpdateTest,
    17  		Delete: DeleteTest,
    18  		Read:   ReadTest,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"test_id": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  
    26  			"website_name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"website_url": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  			},
    35  
    36  			"check_rate": &schema.Schema{
    37  				Type:     schema.TypeInt,
    38  				Optional: true,
    39  				Default:  300,
    40  			},
    41  
    42  			"test_type": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  			},
    46  
    47  			"paused": &schema.Schema{
    48  				Type:     schema.TypeBool,
    49  				Optional: true,
    50  				Default:  false,
    51  			},
    52  			"timeout": &schema.Schema{
    53  				Type:     schema.TypeInt,
    54  				Computed: true,
    55  			},
    56  		},
    57  	}
    58  }
    59  
    60  func CreateTest(d *schema.ResourceData, meta interface{}) error {
    61  	client := meta.(*statuscake.Client)
    62  
    63  	newTest := &statuscake.Test{
    64  		WebsiteName: d.Get("website_name").(string),
    65  		WebsiteURL:  d.Get("website_url").(string),
    66  		TestType:    d.Get("test_type").(string),
    67  		CheckRate:   d.Get("check_rate").(int),
    68  	}
    69  
    70  	log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
    71  
    72  	response, err := client.Tests().Update(newTest)
    73  	if err != nil {
    74  		return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
    75  	}
    76  
    77  	d.Set("test_id", fmt.Sprintf("%d", response.TestID))
    78  	d.SetId(fmt.Sprintf("%d", response.TestID))
    79  
    80  	return ReadTest(d, meta)
    81  }
    82  
    83  func UpdateTest(d *schema.ResourceData, meta interface{}) error {
    84  	client := meta.(*statuscake.Client)
    85  
    86  	params := getStatusCakeTestInput(d)
    87  
    88  	log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
    89  	_, err := client.Tests().Update(params)
    90  	if err != nil {
    91  		return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
    92  	}
    93  	return nil
    94  }
    95  
    96  func DeleteTest(d *schema.ResourceData, meta interface{}) error {
    97  	client := meta.(*statuscake.Client)
    98  
    99  	testId, parseErr := strconv.Atoi(d.Id())
   100  	if parseErr != nil {
   101  		return parseErr
   102  	}
   103  	log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
   104  	err := client.Tests().Delete(testId)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func ReadTest(d *schema.ResourceData, meta interface{}) error {
   113  	client := meta.(*statuscake.Client)
   114  
   115  	testId, parseErr := strconv.Atoi(d.Id())
   116  	if parseErr != nil {
   117  		return parseErr
   118  	}
   119  	testResp, err := client.Tests().Detail(testId)
   120  	if err != nil {
   121  		return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
   122  	}
   123  	d.Set("check_rate", testResp.CheckRate)
   124  
   125  	return nil
   126  }
   127  
   128  func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
   129  	testId, parseErr := strconv.Atoi(d.Id())
   130  	if parseErr != nil {
   131  		log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
   132  	}
   133  	test := &statuscake.Test{
   134  		TestID: testId,
   135  	}
   136  	if v, ok := d.GetOk("website_name"); ok {
   137  		test.WebsiteName = v.(string)
   138  	}
   139  	if v, ok := d.GetOk("website_url"); ok {
   140  		test.WebsiteURL = v.(string)
   141  	}
   142  	if v, ok := d.GetOk("check_rate"); ok {
   143  		test.CheckRate = v.(int)
   144  	}
   145  	if v, ok := d.GetOk("test_type"); ok {
   146  		test.TestType = v.(string)
   147  	}
   148  	if v, ok := d.GetOk("paused"); ok {
   149  		test.Paused = v.(bool)
   150  	}
   151  	if v, ok := d.GetOk("timeout"); ok {
   152  		test.Timeout = v.(int)
   153  	}
   154  	return test
   155  }