github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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  			"contact_id": &schema.Schema{
    57  				Type:     schema.TypeInt,
    58  				Optional: true,
    59  			},
    60  		},
    61  	}
    62  }
    63  
    64  func CreateTest(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*statuscake.Client)
    66  
    67  	newTest := &statuscake.Test{
    68  		WebsiteName: d.Get("website_name").(string),
    69  		WebsiteURL:  d.Get("website_url").(string),
    70  		CheckRate:   d.Get("check_rate").(int),
    71  		TestType:    d.Get("test_type").(string),
    72  		Paused:      d.Get("paused").(bool),
    73  		Timeout:     d.Get("timeout").(int),
    74  		ContactID:   d.Get("contact_id").(int),
    75  	}
    76  
    77  	log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
    78  
    79  	response, err := client.Tests().Update(newTest)
    80  	if err != nil {
    81  		return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
    82  	}
    83  
    84  	d.Set("test_id", fmt.Sprintf("%d", response.TestID))
    85  	d.SetId(fmt.Sprintf("%d", response.TestID))
    86  
    87  	return ReadTest(d, meta)
    88  }
    89  
    90  func UpdateTest(d *schema.ResourceData, meta interface{}) error {
    91  	client := meta.(*statuscake.Client)
    92  
    93  	params := getStatusCakeTestInput(d)
    94  
    95  	log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
    96  	_, err := client.Tests().Update(params)
    97  	if err != nil {
    98  		return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
    99  	}
   100  	return nil
   101  }
   102  
   103  func DeleteTest(d *schema.ResourceData, meta interface{}) error {
   104  	client := meta.(*statuscake.Client)
   105  
   106  	testId, parseErr := strconv.Atoi(d.Id())
   107  	if parseErr != nil {
   108  		return parseErr
   109  	}
   110  	log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
   111  	err := client.Tests().Delete(testId)
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func ReadTest(d *schema.ResourceData, meta interface{}) error {
   120  	client := meta.(*statuscake.Client)
   121  
   122  	testId, parseErr := strconv.Atoi(d.Id())
   123  	if parseErr != nil {
   124  		return parseErr
   125  	}
   126  	testResp, err := client.Tests().Detail(testId)
   127  	if err != nil {
   128  		return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
   129  	}
   130  	d.Set("website_name", testResp.WebsiteName)
   131  	d.Set("website_url", testResp.WebsiteURL)
   132  	d.Set("check_rate", testResp.CheckRate)
   133  	d.Set("test_type", testResp.TestType)
   134  	d.Set("paused", testResp.Paused)
   135  	d.Set("timeout", testResp.Timeout)
   136  	d.Set("contact_id", testResp.ContactID)
   137  
   138  	return nil
   139  }
   140  
   141  func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
   142  	testId, parseErr := strconv.Atoi(d.Id())
   143  	if parseErr != nil {
   144  		log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
   145  	}
   146  	test := &statuscake.Test{
   147  		TestID: testId,
   148  	}
   149  	if v, ok := d.GetOk("website_name"); ok {
   150  		test.WebsiteName = v.(string)
   151  	}
   152  	if v, ok := d.GetOk("website_url"); ok {
   153  		test.WebsiteURL = v.(string)
   154  	}
   155  	if v, ok := d.GetOk("check_rate"); ok {
   156  		test.CheckRate = v.(int)
   157  	}
   158  	if v, ok := d.GetOk("test_type"); ok {
   159  		test.TestType = v.(string)
   160  	}
   161  	if v, ok := d.GetOk("paused"); ok {
   162  		test.Paused = v.(bool)
   163  	}
   164  	if v, ok := d.GetOk("timeout"); ok {
   165  		test.Timeout = v.(int)
   166  	}
   167  	if v, ok := d.GetOk("contact_id"); ok {
   168  		test.ContactID = v.(int)
   169  	}
   170  	return test
   171  }