github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ns1/resource_datafeed_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 TestAccDataFeed_basic(t *testing.T) { 15 var dataFeed data.Feed 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckDataFeedDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccDataFeedBasic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckDataFeedExists("ns1_datafeed.foobar", "ns1_datasource.api", &dataFeed), 25 testAccCheckDataFeedName(&dataFeed, "terraform test"), 26 testAccCheckDataFeedConfig(&dataFeed, "label", "exampledc2"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccDataFeed_updated(t *testing.T) { 34 var dataFeed data.Feed 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckDataFeedDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccDataFeedBasic, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckDataFeedExists("ns1_datafeed.foobar", "ns1_datasource.api", &dataFeed), 44 testAccCheckDataFeedName(&dataFeed, "terraform test"), 45 testAccCheckDataFeedConfig(&dataFeed, "label", "exampledc2"), 46 ), 47 }, 48 resource.TestStep{ 49 Config: testAccDataFeedUpdated, 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckDataFeedExists("ns1_datafeed.foobar", "ns1_datasource.api", &dataFeed), 52 testAccCheckDataFeedName(&dataFeed, "terraform test"), 53 testAccCheckDataFeedConfig(&dataFeed, "label", "exampledc3"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func testAccCheckDataFeedExists(n string, dsrc string, dataFeed *data.Feed) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 rs, ok := s.RootModule().Resources[n] 63 ds, ok := s.RootModule().Resources[dsrc] 64 65 if !ok { 66 return fmt.Errorf("Not found: %s", n) 67 } 68 69 if rs.Primary.ID == "" { 70 return fmt.Errorf("NoID is set") 71 } 72 73 if ds.Primary.ID == "" { 74 return fmt.Errorf("NoID is set for the datasource") 75 } 76 77 client := testAccProvider.Meta().(*ns1.Client) 78 79 foundFeed, _, err := client.DataFeeds.Get(ds.Primary.Attributes["id"], rs.Primary.Attributes["id"]) 80 81 p := rs.Primary 82 83 if err != nil { 84 return err 85 } 86 87 if foundFeed.Name != p.Attributes["name"] { 88 return fmt.Errorf("DataFeed not found") 89 } 90 91 *dataFeed = *foundFeed 92 93 return nil 94 } 95 } 96 97 func testAccCheckDataFeedDestroy(s *terraform.State) error { 98 client := testAccProvider.Meta().(*ns1.Client) 99 100 var dataFeedID string 101 var dataSourceID string 102 103 for _, rs := range s.RootModule().Resources { 104 105 if rs.Type == "ns1_datasource" { 106 dataSourceID = rs.Primary.Attributes["id"] 107 } 108 109 if rs.Type == "ns1_datafeed" { 110 dataFeedID = rs.Primary.Attributes["id"] 111 } 112 } 113 114 df, _, _ := client.DataFeeds.Get(dataSourceID, dataFeedID) 115 116 if df != nil { 117 return fmt.Errorf("DataFeed still exists: %#v", df) 118 } 119 120 return nil 121 } 122 123 func testAccCheckDataFeedName(dataFeed *data.Feed, expected string) resource.TestCheckFunc { 124 return func(s *terraform.State) error { 125 if dataFeed.Name != expected { 126 return fmt.Errorf("Name: got: %#v want: %#v", dataFeed.Name, expected) 127 } 128 129 return nil 130 } 131 } 132 133 func testAccCheckDataFeedConfig(dataFeed *data.Feed, key, expected string) resource.TestCheckFunc { 134 return func(s *terraform.State) error { 135 136 if dataFeed.Config[key] != expected { 137 return fmt.Errorf("Config[%s]: got: %#v, want: %s", key, dataFeed.Config[key], expected) 138 } 139 140 return nil 141 } 142 } 143 144 const testAccDataFeedBasic = ` 145 resource "ns1_datasource" "api" { 146 name = "terraform test" 147 sourcetype = "nsone_v1" 148 } 149 150 resource "ns1_datafeed" "foobar" { 151 name = "terraform test" 152 source_id = "${ns1_datasource.api.id}" 153 config { 154 label = "exampledc2" 155 } 156 }` 157 158 const testAccDataFeedUpdated = ` 159 resource "ns1_datasource" "api" { 160 name = "terraform test" 161 sourcetype = "nsone_v1" 162 } 163 164 resource "ns1_datafeed" "foobar" { 165 name = "terraform test" 166 source_id = "${ns1_datasource.api.id}" 167 config { 168 label = "exampledc3" 169 } 170 }`