github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/icinga2/resource_icinga2_hostgroup_test.go (about) 1 package icinga2 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/lrsmith/go-icinga2-api/iapi" 10 ) 11 12 func TestAccCreateBasicHostGroup(t *testing.T) { 13 14 var testAccCreateBasicHostGroup = fmt.Sprintf(` 15 resource "icinga2_hostgroup" "tf-hg-1" { 16 name = "terraform-hostgroup-1" 17 display_name = "Terraform Test HostGroup" 18 }`) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccCreateBasicHostGroup, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckHostgroupExists("icinga2_hostgroup.tf-hg-1"), 28 testAccCheckResourceState("icinga2_hostgroup.tf-hg-1", "name", "terraform-hostgroup-1"), 29 testAccCheckResourceState("icinga2_hostgroup.tf-hg-1", "display_name", "Terraform Test HostGroup"), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func testAccCheckHostgroupExists(rn string) resource.TestCheckFunc { 37 return func(s *terraform.State) error { 38 resource, ok := s.RootModule().Resources[rn] 39 if !ok { 40 return fmt.Errorf("Hostgroup resource not found: %s", rn) 41 } 42 43 if resource.Primary.ID == "" { 44 return fmt.Errorf("Hostgroup resource id not set") 45 } 46 47 client := testAccProvider.Meta().(*iapi.Server) 48 _, err := client.GetHostgroup(resource.Primary.ID) 49 if err != nil { 50 return fmt.Errorf("Error getting getting hostgroup: %s", err) 51 } 52 53 return nil 54 } 55 }