github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_network_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 TestAccCloudStackNetwork_basic(t *testing.T) { 13 var network cloudstack.Network 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackNetworkDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackNetwork_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackNetworkExists( 24 "cloudstack_network.foo", &network), 25 testAccCheckCloudStackNetworkBasicAttributes(&network), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccCloudStackNetwork_vpc(t *testing.T) { 33 var network cloudstack.Network 34 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckCloudStackNetworkDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccCloudStackNetwork_vpc, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckCloudStackNetworkExists( 44 "cloudstack_network.foo", &network), 45 testAccCheckCloudStackNetworkVPCAttributes(&network), 46 resource.TestCheckResourceAttr( 47 "cloudstack_network.foo", "vpc", "terraform-vpc"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func testAccCheckCloudStackNetworkExists( 55 n string, network *cloudstack.Network) resource.TestCheckFunc { 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No network ID is set") 64 } 65 66 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 67 ntwrk, _, err := cs.Network.GetNetworkByID(rs.Primary.ID) 68 69 if err != nil { 70 return err 71 } 72 73 if ntwrk.Id != rs.Primary.ID { 74 return fmt.Errorf("Network not found") 75 } 76 77 *network = *ntwrk 78 79 return nil 80 } 81 } 82 83 func testAccCheckCloudStackNetworkBasicAttributes( 84 network *cloudstack.Network) resource.TestCheckFunc { 85 return func(s *terraform.State) error { 86 87 if network.Name != "terraform-network" { 88 return fmt.Errorf("Bad name: %s", network.Name) 89 } 90 91 if network.Displaytext != "terraform-network" { 92 return fmt.Errorf("Bad display name: %s", network.Displaytext) 93 } 94 95 if network.Cidr != CLOUDSTACK_NETWORK_2_CIDR { 96 return fmt.Errorf("Bad service offering: %s", network.Cidr) 97 } 98 99 if network.Networkofferingname != CLOUDSTACK_NETWORK_2_OFFERING { 100 return fmt.Errorf("Bad template: %s", network.Networkofferingname) 101 } 102 103 return nil 104 } 105 } 106 107 func testAccCheckCloudStackNetworkVPCAttributes( 108 network *cloudstack.Network) resource.TestCheckFunc { 109 return func(s *terraform.State) error { 110 111 if network.Name != "terraform-network" { 112 return fmt.Errorf("Bad name: %s", network.Name) 113 } 114 115 if network.Displaytext != "terraform-network" { 116 return fmt.Errorf("Bad display name: %s", network.Displaytext) 117 } 118 119 if network.Cidr != CLOUDSTACK_VPC_NETWORK_CIDR { 120 return fmt.Errorf("Bad CIDR: %s", network.Cidr) 121 } 122 123 if network.Networkofferingname != CLOUDSTACK_VPC_NETWORK_OFFERING { 124 return fmt.Errorf("Bad network offering: %s", network.Networkofferingname) 125 } 126 127 return nil 128 } 129 } 130 131 func testAccCheckCloudStackNetworkDestroy(s *terraform.State) error { 132 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 133 134 for _, rs := range s.RootModule().Resources { 135 if rs.Type != "cloudstack_network" { 136 continue 137 } 138 139 if rs.Primary.ID == "" { 140 return fmt.Errorf("No network ID is set") 141 } 142 143 _, _, err := cs.Network.GetNetworkByID(rs.Primary.ID) 144 if err == nil { 145 return fmt.Errorf("Network %s still exists", rs.Primary.ID) 146 } 147 } 148 149 return nil 150 } 151 152 var testAccCloudStackNetwork_basic = fmt.Sprintf(` 153 resource "cloudstack_network" "foo" { 154 name = "terraform-network" 155 cidr = "%s" 156 network_offering = "%s" 157 zone = "%s" 158 }`, 159 CLOUDSTACK_NETWORK_2_CIDR, 160 CLOUDSTACK_NETWORK_2_OFFERING, 161 CLOUDSTACK_ZONE) 162 163 var testAccCloudStackNetwork_vpc = fmt.Sprintf(` 164 resource "cloudstack_vpc" "foobar" { 165 name = "terraform-vpc" 166 cidr = "%s" 167 vpc_offering = "%s" 168 zone = "%s" 169 } 170 171 resource "cloudstack_network" "foo" { 172 name = "terraform-network" 173 cidr = "%s" 174 network_offering = "%s" 175 vpc = "${cloudstack_vpc.foobar.name}" 176 zone = "${cloudstack_vpc.foobar.zone}" 177 }`, 178 CLOUDSTACK_VPC_CIDR_1, 179 CLOUDSTACK_VPC_OFFERING, 180 CLOUDSTACK_ZONE, 181 CLOUDSTACK_VPC_NETWORK_CIDR, 182 CLOUDSTACK_VPC_NETWORK_OFFERING)