github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/ns1/resource_notifylist_test.go (about)

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    11  	"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
    12  )
    13  
    14  func TestAccNotifyList_basic(t *testing.T) {
    15  	var nl monitor.NotifyList
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckNotifyListDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccNotifyListBasic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckNotifyListExists("ns1_notifylist.test", &nl),
    25  					testAccCheckNotifyListName(&nl, "terraform test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccNotifyList_updated(t *testing.T) {
    33  	var nl monitor.NotifyList
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckNotifyListDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccNotifyListBasic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckNotifyListExists("ns1_notifylist.test", &nl),
    43  					testAccCheckNotifyListName(&nl, "terraform test"),
    44  				),
    45  			},
    46  			resource.TestStep{
    47  				Config: testAccNotifyListUpdated,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckNotifyListExists("ns1_notifylist.test", &nl),
    50  					testAccCheckNotifyListName(&nl, "terraform test"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func testAccCheckNotifyListState(key, value string) resource.TestCheckFunc {
    58  	return func(s *terraform.State) error {
    59  		rs, ok := s.RootModule().Resources["ns1_notifylist.test"]
    60  		if !ok {
    61  			return fmt.Errorf("Not found: %s", "ns1_notifylist.test")
    62  		}
    63  
    64  		if rs.Primary.ID == "" {
    65  			return fmt.Errorf("No ID is set")
    66  		}
    67  
    68  		p := rs.Primary
    69  		if p.Attributes[key] != value {
    70  			return fmt.Errorf(
    71  				"%s != %s (actual: %s)", key, value, p.Attributes[key])
    72  		}
    73  
    74  		return nil
    75  	}
    76  }
    77  
    78  func testAccCheckNotifyListExists(n string, nl *monitor.NotifyList) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[n]
    81  
    82  		if !ok {
    83  			return fmt.Errorf("Resource not found: %v", n)
    84  		}
    85  
    86  		id := rs.Primary.ID
    87  		if id == "" {
    88  			return fmt.Errorf("ID is not set")
    89  		}
    90  
    91  		client := testAccProvider.Meta().(*ns1.Client)
    92  
    93  		foundNl, _, err := client.Notifications.Get(id)
    94  
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		if foundNl.ID != id {
   100  			return fmt.Errorf("Notify List not found want: %#v, got %#v", id, foundNl)
   101  		}
   102  
   103  		*nl = *foundNl
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckNotifyListDestroy(s *terraform.State) error {
   110  	client := testAccProvider.Meta().(*ns1.Client)
   111  
   112  	for _, rs := range s.RootModule().Resources {
   113  		if rs.Type != "ns1_notifylist" {
   114  			continue
   115  		}
   116  
   117  		nl, _, err := client.Notifications.Get(rs.Primary.Attributes["id"])
   118  
   119  		if err == nil {
   120  			return fmt.Errorf("Notify List still exists %#v: %#v", err, nl)
   121  		}
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  func testAccCheckNotifyListName(nl *monitor.NotifyList, expected string) resource.TestCheckFunc {
   128  	return func(s *terraform.State) error {
   129  		if nl.Name != expected {
   130  			return fmt.Errorf("Name: got: %#v want: %#v", nl.Name, expected)
   131  		}
   132  		return nil
   133  	}
   134  }
   135  
   136  const testAccNotifyListBasic = `
   137  resource "ns1_notifylist" "test" {
   138    name = "terraform test"
   139    notifications = {
   140      type = "webhook"
   141      config = {
   142        url = "http://localhost:9090"
   143      }
   144    }
   145  }
   146  `
   147  
   148  const testAccNotifyListUpdated = `
   149  resource "ns1_notifylist" "test" {
   150    name = "terraform test"
   151    notifications = {
   152      type = "webhook"
   153      config = {
   154        url = "http://localhost:9091"
   155      }
   156    }
   157  }
   158  `