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