github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/cloudstack/resource_cloudstack_vpc_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func TestAccCloudStackVPC_basic(t *testing.T) { 13 var vpc cloudstack.VPC 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackVPCDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackVPC_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackVPCExists( 24 "cloudstack_vpc.foo", &vpc), 25 testAccCheckCloudStackVPCAttributes(&vpc), 26 resource.TestCheckResourceAttr( 27 "cloudstack_vpc.foo", "vpc_offering", CLOUDSTACK_VPC_OFFERING), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func testAccCheckCloudStackVPCExists( 35 n string, vpc *cloudstack.VPC) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[n] 38 if !ok { 39 return fmt.Errorf("Not found: %s", n) 40 } 41 42 if rs.Primary.ID == "" { 43 return fmt.Errorf("No VPC ID is set") 44 } 45 46 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 47 v, _, err := cs.VPC.GetVPCByID(rs.Primary.ID) 48 49 if err != nil { 50 return err 51 } 52 53 if v.Id != rs.Primary.ID { 54 return fmt.Errorf("VPC not found") 55 } 56 57 *vpc = *v 58 59 return nil 60 } 61 } 62 63 func testAccCheckCloudStackVPCAttributes( 64 vpc *cloudstack.VPC) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 67 if vpc.Name != "terraform-vpc" { 68 return fmt.Errorf("Bad name: %s", vpc.Name) 69 } 70 71 if vpc.Displaytext != "terraform-vpc-text" { 72 return fmt.Errorf("Bad display text: %s", vpc.Displaytext) 73 } 74 75 if vpc.Cidr != CLOUDSTACK_VPC_CIDR_1 { 76 return fmt.Errorf("Bad VPC CIDR: %s", vpc.Cidr) 77 } 78 79 if vpc.Networkdomain != "terraform-domain" { 80 return fmt.Errorf("Bad network domain: %s", vpc.Networkdomain) 81 } 82 83 return nil 84 } 85 } 86 87 func testAccCheckCloudStackVPCDestroy(s *terraform.State) error { 88 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 89 90 for _, rs := range s.RootModule().Resources { 91 if rs.Type != "cloudstack_vpc" { 92 continue 93 } 94 95 if rs.Primary.ID == "" { 96 return fmt.Errorf("No VPC ID is set") 97 } 98 99 _, _, err := cs.VPC.GetVPCByID(rs.Primary.ID) 100 if err == nil { 101 return fmt.Errorf("VPC %s still exists", rs.Primary.ID) 102 } 103 } 104 105 return nil 106 } 107 108 var testAccCloudStackVPC_basic = fmt.Sprintf(` 109 resource "cloudstack_vpc" "foo" { 110 name = "terraform-vpc" 111 display_text = "terraform-vpc-text" 112 cidr = "%s" 113 vpc_offering = "%s" 114 network_domain = "terraform-domain" 115 zone = "%s" 116 }`, 117 CLOUDSTACK_VPC_CIDR_1, 118 CLOUDSTACK_VPC_OFFERING, 119 CLOUDSTACK_ZONE)