github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  
    26  			"website_name": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"website_url": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  			},
    35  
    36  			"contact_id": {
    37  				Type:     schema.TypeInt,
    38  				Optional: true,
    39  			},
    40  
    41  			"check_rate": {
    42  				Type:     schema.TypeInt,
    43  				Optional: true,
    44  				Default:  300,
    45  			},
    46  
    47  			"test_type": {
    48  				Type:     schema.TypeString,
    49  				Required: true,
    50  			},
    51  
    52  			"paused": {
    53  				Type:     schema.TypeBool,
    54  				Optional: true,
    55  				Default:  false,
    56  			},
    57  
    58  			"timeout": {
    59  				Type:     schema.TypeInt,
    60  				Optional: true,
    61  				Default:  40,
    62  			},
    63  
    64  			"confirmations": {
    65  				Type:     schema.TypeInt,
    66  				Optional: true,
    67  			},
    68  
    69  			"port": {
    70  				Type:     schema.TypeInt,
    71  				Optional: true,
    72  			},
    73  		},
    74  	}
    75  }
    76  
    77  func CreateTest(d *schema.ResourceData, meta interface{}) error {
    78  	client := meta.(*statuscake.Client)
    79  
    80  	newTest := &statuscake.Test{
    81  		WebsiteName:  d.Get("website_name").(string),
    82  		WebsiteURL:   d.Get("website_url").(string),
    83  		CheckRate:    d.Get("check_rate").(int),
    84  		TestType:     d.Get("test_type").(string),
    85  		Paused:       d.Get("paused").(bool),
    86  		Timeout:      d.Get("timeout").(int),
    87  		ContactID:    d.Get("contact_id").(int),
    88  		Confirmation: d.Get("confirmations").(int),
    89  		Port:         d.Get("port").(int),
    90  	}
    91  
    92  	log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
    93  
    94  	response, err := client.Tests().Update(newTest)
    95  	if err != nil {
    96  		return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
    97  	}
    98  
    99  	d.Set("test_id", fmt.Sprintf("%d", response.TestID))
   100  	d.SetId(fmt.Sprintf("%d", response.TestID))
   101  
   102  	return ReadTest(d, meta)
   103  }
   104  
   105  func UpdateTest(d *schema.ResourceData, meta interface{}) error {
   106  	client := meta.(*statuscake.Client)
   107  
   108  	params := getStatusCakeTestInput(d)
   109  
   110  	log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
   111  	_, err := client.Tests().Update(params)
   112  	if err != nil {
   113  		return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
   114  	}
   115  	return nil
   116  }
   117  
   118  func DeleteTest(d *schema.ResourceData, meta interface{}) error {
   119  	client := meta.(*statuscake.Client)
   120  
   121  	testId, parseErr := strconv.Atoi(d.Id())
   122  	if parseErr != nil {
   123  		return parseErr
   124  	}
   125  	log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
   126  	err := client.Tests().Delete(testId)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func ReadTest(d *schema.ResourceData, meta interface{}) error {
   135  	client := meta.(*statuscake.Client)
   136  
   137  	testId, parseErr := strconv.Atoi(d.Id())
   138  	if parseErr != nil {
   139  		return parseErr
   140  	}
   141  	testResp, err := client.Tests().Detail(testId)
   142  	if err != nil {
   143  		return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
   144  	}
   145  	d.Set("website_name", testResp.WebsiteName)
   146  	d.Set("website_url", testResp.WebsiteURL)
   147  	d.Set("check_rate", testResp.CheckRate)
   148  	d.Set("test_type", testResp.TestType)
   149  	d.Set("paused", testResp.Paused)
   150  	d.Set("timeout", testResp.Timeout)
   151  	d.Set("contact_id", testResp.ContactID)
   152  	d.Set("confirmations", testResp.Confirmation)
   153  	d.Set("port", testResp.Port)
   154  
   155  	return nil
   156  }
   157  
   158  func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
   159  	testId, parseErr := strconv.Atoi(d.Id())
   160  	if parseErr != nil {
   161  		log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
   162  	}
   163  	test := &statuscake.Test{
   164  		TestID: testId,
   165  	}
   166  	if v, ok := d.GetOk("website_name"); ok {
   167  		test.WebsiteName = v.(string)
   168  	}
   169  	if v, ok := d.GetOk("website_url"); ok {
   170  		test.WebsiteURL = v.(string)
   171  	}
   172  	if v, ok := d.GetOk("check_rate"); ok {
   173  		test.CheckRate = v.(int)
   174  	}
   175  	if v, ok := d.GetOk("contact_id"); ok {
   176  		test.ContactID = v.(int)
   177  	}
   178  	if v, ok := d.GetOk("test_type"); ok {
   179  		test.TestType = v.(string)
   180  	}
   181  	if v, ok := d.GetOk("paused"); ok {
   182  		test.Paused = v.(bool)
   183  	}
   184  	if v, ok := d.GetOk("timeout"); ok {
   185  		test.Timeout = v.(int)
   186  	}
   187  	if v, ok := d.GetOk("contact_id"); ok {
   188  		test.ContactID = v.(int)
   189  	}
   190  	if v, ok := d.GetOk("confirmations"); ok {
   191  		test.Confirmation = v.(int)
   192  	}
   193  	if v, ok := d.GetOk("port"); ok {
   194  		test.Port = v.(int)
   195  	}
   196  
   197  	defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
   198  		"408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
   199  		"504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
   200  		"598, 599"
   201  
   202  	test.StatusCodes = defaultStatusCodes
   203  
   204  	return test
   205  }