github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 testAccCheckNetworkTags(&network, "terraform-tag", "true"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccCloudStackNetwork_vpc(t *testing.T) { 34 var network cloudstack.Network 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckCloudStackNetworkDestroy, 40 Steps: []resource.TestStep{ 41 resource.TestStep{ 42 Config: testAccCloudStackNetwork_vpc, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckCloudStackNetworkExists( 45 "cloudstack_network.foo", &network), 46 testAccCheckCloudStackNetworkVPCAttributes(&network), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccCloudStackNetwork_updateACL(t *testing.T) { 54 var network cloudstack.Network 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckCloudStackNetworkDestroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: testAccCloudStackNetwork_acl, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckCloudStackNetworkExists( 65 "cloudstack_network.foo", &network), 66 testAccCheckCloudStackNetworkVPCAttributes(&network), 67 ), 68 }, 69 70 resource.TestStep{ 71 Config: testAccCloudStackNetwork_updateACL, 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckCloudStackNetworkExists( 74 "cloudstack_network.foo", &network), 75 testAccCheckCloudStackNetworkVPCAttributes(&network), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func testAccCheckCloudStackNetworkExists( 83 n string, network *cloudstack.Network) resource.TestCheckFunc { 84 return func(s *terraform.State) error { 85 rs, ok := s.RootModule().Resources[n] 86 if !ok { 87 return fmt.Errorf("Not found: %s", n) 88 } 89 90 if rs.Primary.ID == "" { 91 return fmt.Errorf("No network ID is set") 92 } 93 94 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 95 ntwrk, _, err := cs.Network.GetNetworkByID(rs.Primary.ID) 96 97 if err != nil { 98 return err 99 } 100 101 if ntwrk.Id != rs.Primary.ID { 102 return fmt.Errorf("Network not found") 103 } 104 105 *network = *ntwrk 106 107 return nil 108 } 109 } 110 111 func testAccCheckCloudStackNetworkBasicAttributes( 112 network *cloudstack.Network) resource.TestCheckFunc { 113 return func(s *terraform.State) error { 114 115 if network.Name != "terraform-network" { 116 return fmt.Errorf("Bad name: %s", network.Name) 117 } 118 119 if network.Displaytext != "terraform-network" { 120 return fmt.Errorf("Bad display name: %s", network.Displaytext) 121 } 122 123 if network.Cidr != CLOUDSTACK_NETWORK_2_CIDR { 124 return fmt.Errorf("Bad CIDR: %s", network.Cidr) 125 } 126 127 if network.Networkofferingname != CLOUDSTACK_NETWORK_2_OFFERING { 128 return fmt.Errorf("Bad network offering: %s", network.Networkofferingname) 129 } 130 131 return nil 132 } 133 } 134 135 func testAccCheckNetworkTags( 136 n *cloudstack.Network, key string, value string) resource.TestCheckFunc { 137 return func(s *terraform.State) error { 138 tags := make(map[string]string) 139 for item := range n.Tags { 140 tags[n.Tags[item].Key] = n.Tags[item].Value 141 } 142 return testAccCheckTags(tags, key, value) 143 } 144 } 145 146 func testAccCheckCloudStackNetworkVPCAttributes( 147 network *cloudstack.Network) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 150 if network.Name != "terraform-network" { 151 return fmt.Errorf("Bad name: %s", network.Name) 152 } 153 154 if network.Displaytext != "terraform-network" { 155 return fmt.Errorf("Bad display name: %s", network.Displaytext) 156 } 157 158 if network.Cidr != CLOUDSTACK_VPC_NETWORK_CIDR { 159 return fmt.Errorf("Bad CIDR: %s", network.Cidr) 160 } 161 162 if network.Networkofferingname != CLOUDSTACK_VPC_NETWORK_OFFERING { 163 return fmt.Errorf("Bad network offering: %s", network.Networkofferingname) 164 } 165 166 return nil 167 } 168 } 169 170 func testAccCheckCloudStackNetworkDestroy(s *terraform.State) error { 171 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 172 173 for _, rs := range s.RootModule().Resources { 174 if rs.Type != "cloudstack_network" { 175 continue 176 } 177 178 if rs.Primary.ID == "" { 179 return fmt.Errorf("No network ID is set") 180 } 181 182 _, _, err := cs.Network.GetNetworkByID(rs.Primary.ID) 183 if err == nil { 184 return fmt.Errorf("Network %s still exists", rs.Primary.ID) 185 } 186 } 187 188 return nil 189 } 190 191 var testAccCloudStackNetwork_basic = fmt.Sprintf(` 192 resource "cloudstack_network" "foo" { 193 name = "terraform-network" 194 cidr = "%s" 195 network_offering = "%s" 196 zone = "%s" 197 tags = { 198 terraform-tag = "true" 199 } 200 }`, 201 CLOUDSTACK_NETWORK_2_CIDR, 202 CLOUDSTACK_NETWORK_2_OFFERING, 203 CLOUDSTACK_ZONE) 204 205 var testAccCloudStackNetwork_vpc = fmt.Sprintf(` 206 resource "cloudstack_vpc" "foobar" { 207 name = "terraform-vpc" 208 cidr = "%s" 209 vpc_offering = "%s" 210 zone = "%s" 211 } 212 213 resource "cloudstack_network" "foo" { 214 name = "terraform-network" 215 cidr = "%s" 216 network_offering = "%s" 217 vpc_id = "${cloudstack_vpc.foobar.id}" 218 zone = "${cloudstack_vpc.foobar.zone}" 219 }`, 220 CLOUDSTACK_VPC_CIDR_1, 221 CLOUDSTACK_VPC_OFFERING, 222 CLOUDSTACK_ZONE, 223 CLOUDSTACK_VPC_NETWORK_CIDR, 224 CLOUDSTACK_VPC_NETWORK_OFFERING) 225 226 var testAccCloudStackNetwork_acl = fmt.Sprintf(` 227 resource "cloudstack_vpc" "foobar" { 228 name = "terraform-vpc" 229 cidr = "%s" 230 vpc_offering = "%s" 231 zone = "%s" 232 } 233 234 resource "cloudstack_network_acl" "foo" { 235 name = "foo" 236 vpc_id = "${cloudstack_vpc.foobar.id}" 237 } 238 239 resource "cloudstack_network" "foo" { 240 name = "terraform-network" 241 cidr = "%s" 242 network_offering = "%s" 243 vpc_id = "${cloudstack_vpc.foobar.id}" 244 acl_id = "${cloudstack_network_acl.foo.id}" 245 zone = "${cloudstack_vpc.foobar.zone}" 246 }`, 247 CLOUDSTACK_VPC_CIDR_1, 248 CLOUDSTACK_VPC_OFFERING, 249 CLOUDSTACK_ZONE, 250 CLOUDSTACK_VPC_NETWORK_CIDR, 251 CLOUDSTACK_VPC_NETWORK_OFFERING) 252 253 var testAccCloudStackNetwork_updateACL = fmt.Sprintf(` 254 resource "cloudstack_vpc" "foobar" { 255 name = "terraform-vpc" 256 cidr = "%s" 257 vpc_offering = "%s" 258 zone = "%s" 259 } 260 261 resource "cloudstack_network_acl" "bar" { 262 name = "bar" 263 vpc_id = "${cloudstack_vpc.foobar.id}" 264 } 265 266 resource "cloudstack_network" "foo" { 267 name = "terraform-network" 268 cidr = "%s" 269 network_offering = "%s" 270 vpc_id = "${cloudstack_vpc.foobar.id}" 271 acl_id = "${cloudstack_network_acl.bar.id}" 272 zone = "${cloudstack_vpc.foobar.zone}" 273 }`, 274 CLOUDSTACK_VPC_CIDR_1, 275 CLOUDSTACK_VPC_OFFERING, 276 CLOUDSTACK_ZONE, 277 CLOUDSTACK_VPC_NETWORK_CIDR, 278 CLOUDSTACK_VPC_NETWORK_OFFERING)