github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/ns1/resource_datasource_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/data"
    12  )
    13  
    14  func TestAccDataSource_basic(t *testing.T) {
    15  	var dataSource data.Source
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckDataSourceDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccDataSourceBasic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckDataSourceExists("ns1_datasource.foobar", &dataSource),
    25  					testAccCheckDataSourceName(&dataSource, "terraform test"),
    26  					testAccCheckDataSourceType(&dataSource, "nsone_v1"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccDataSource_updated(t *testing.T) {
    34  	var dataSource data.Source
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckDataSourceDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccDataSourceBasic,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckDataSourceExists("ns1_datasource.foobar", &dataSource),
    44  					testAccCheckDataSourceName(&dataSource, "terraform test"),
    45  					testAccCheckDataSourceType(&dataSource, "nsone_v1"),
    46  				),
    47  			},
    48  			resource.TestStep{
    49  				Config: testAccDataSourceUpdated,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckDataSourceExists("ns1_datasource.foobar", &dataSource),
    52  					testAccCheckDataSourceName(&dataSource, "terraform test"),
    53  					testAccCheckDataSourceType(&dataSource, "nsone_monitoring"),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func testAccCheckDataSourceExists(n string, dataSource *data.Source) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s", n)
    66  		}
    67  
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("NoID is set")
    70  		}
    71  
    72  		client := testAccProvider.Meta().(*ns1.Client)
    73  
    74  		foundSource, _, err := client.DataSources.Get(rs.Primary.Attributes["id"])
    75  
    76  		p := rs.Primary
    77  
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		if foundSource.Name != p.Attributes["name"] {
    83  			return fmt.Errorf("Datasource not found")
    84  		}
    85  
    86  		*dataSource = *foundSource
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccCheckDataSourceDestroy(s *terraform.State) error {
    93  	client := testAccProvider.Meta().(*ns1.Client)
    94  
    95  	for _, rs := range s.RootModule().Resources {
    96  		if rs.Type != "ns1_datasource" {
    97  			continue
    98  		}
    99  
   100  		_, _, err := client.DataSources.Get(rs.Primary.Attributes["id"])
   101  
   102  		if err == nil {
   103  			return fmt.Errorf("Datasource still exists")
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func testAccCheckDataSourceName(dataSource *data.Source, expected string) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		if dataSource.Name != expected {
   113  			return fmt.Errorf("Name: got: %#v want: %#v", dataSource.Name, expected)
   114  		}
   115  
   116  		return nil
   117  	}
   118  }
   119  
   120  func testAccCheckDataSourceType(dataSource *data.Source, expected string) resource.TestCheckFunc {
   121  	return func(s *terraform.State) error {
   122  		if dataSource.Type != expected {
   123  			return fmt.Errorf("Type: got: %#v want: %#v", dataSource.Type, expected)
   124  		}
   125  
   126  		return nil
   127  	}
   128  }
   129  
   130  const testAccDataSourceBasic = `
   131  resource "ns1_datasource" "foobar" {
   132  	name = "terraform test"
   133  	sourcetype = "nsone_v1"
   134  }`
   135  
   136  const testAccDataSourceUpdated = `
   137  resource "ns1_datasource" "foobar" {
   138  	name = "terraform test"
   139  	sourcetype = "nsone_monitoring"
   140  }`