github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/digitalocean/resource_digitalocean_tag_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/digitalocean/godo" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccDigitalOceanTag_Basic(t *testing.T) { 13 var tag godo.Tag 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckDigitalOceanTagDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCheckDigitalOceanTagConfig_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckDigitalOceanTagExists("digitalocean_tag.foobar", &tag), 24 testAccCheckDigitalOceanTagAttributes(&tag), 25 resource.TestCheckResourceAttr( 26 "digitalocean_tag.foobar", "name", "foobar"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckDigitalOceanTagDestroy(s *terraform.State) error { 34 client := testAccProvider.Meta().(*godo.Client) 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "digitalocean_tag" { 38 continue 39 } 40 41 // Try to find the key 42 _, _, err := client.Tags.Get(rs.Primary.ID) 43 44 if err == nil { 45 return fmt.Errorf("Tag still exists") 46 } 47 } 48 49 return nil 50 } 51 52 func testAccCheckDigitalOceanTagAttributes(tag *godo.Tag) resource.TestCheckFunc { 53 return func(s *terraform.State) error { 54 55 if tag.Name != "foobar" { 56 return fmt.Errorf("Bad name: %s", tag.Name) 57 } 58 59 return nil 60 } 61 } 62 63 func testAccCheckDigitalOceanTagExists(n string, tag *godo.Tag) resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 rs, ok := s.RootModule().Resources[n] 66 67 if !ok { 68 return fmt.Errorf("Not found: %s", n) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("No Record ID is set") 73 } 74 75 client := testAccProvider.Meta().(*godo.Client) 76 77 // Try to find the tag 78 foundTag, _, err := client.Tags.Get(rs.Primary.ID) 79 80 if err != nil { 81 return err 82 } 83 84 *tag = *foundTag 85 86 return nil 87 } 88 } 89 90 var testAccCheckDigitalOceanTagConfig_basic = fmt.Sprintf(` 91 resource "digitalocean_tag" "foobar" { 92 name = "foobar" 93 }`)