github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_networking_subnet_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" 11 ) 12 13 func TestAccNetworkingV2Subnet_basic(t *testing.T) { 14 var subnet subnets.Subnet 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccNetworkingV2Subnet_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 25 ), 26 }, 27 resource.TestStep{ 28 Config: testAccNetworkingV2Subnet_update, 29 Check: resource.ComposeTestCheckFunc( 30 resource.TestCheckResourceAttr( 31 "openstack_networking_subnet_v2.subnet_1", "name", "subnet_1"), 32 resource.TestCheckResourceAttr( 33 "openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"), 34 resource.TestCheckResourceAttr( 35 "openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "true"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccNetworkingV2Subnet_enableDHCP(t *testing.T) { 43 var subnet subnets.Subnet 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testAccNetworkingV2Subnet_enableDHCP, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 54 resource.TestCheckResourceAttr( 55 "openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "true"), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func TestAccNetworkingV2Subnet_disableDHCP(t *testing.T) { 63 var subnet subnets.Subnet 64 65 resource.Test(t, resource.TestCase{ 66 PreCheck: func() { testAccPreCheck(t) }, 67 Providers: testAccProviders, 68 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 69 Steps: []resource.TestStep{ 70 resource.TestStep{ 71 Config: testAccNetworkingV2Subnet_disableDHCP, 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 74 resource.TestCheckResourceAttr( 75 "openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "false"), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func TestAccNetworkingV2Subnet_noGateway(t *testing.T) { 83 var subnet subnets.Subnet 84 85 resource.Test(t, resource.TestCase{ 86 PreCheck: func() { testAccPreCheck(t) }, 87 Providers: testAccProviders, 88 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 89 Steps: []resource.TestStep{ 90 resource.TestStep{ 91 Config: testAccNetworkingV2Subnet_noGateway, 92 Check: resource.ComposeTestCheckFunc( 93 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 94 resource.TestCheckResourceAttr( 95 "openstack_networking_subnet_v2.subnet_1", "gateway_ip", ""), 96 ), 97 }, 98 }, 99 }) 100 } 101 102 func TestAccNetworkingV2Subnet_impliedGateway(t *testing.T) { 103 var subnet subnets.Subnet 104 105 resource.Test(t, resource.TestCase{ 106 PreCheck: func() { testAccPreCheck(t) }, 107 Providers: testAccProviders, 108 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 109 Steps: []resource.TestStep{ 110 resource.TestStep{ 111 Config: testAccNetworkingV2Subnet_impliedGateway, 112 Check: resource.ComposeTestCheckFunc( 113 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 114 resource.TestCheckResourceAttr( 115 "openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"), 116 ), 117 }, 118 }, 119 }) 120 } 121 122 func TestAccNetworkingV2Subnet_timeout(t *testing.T) { 123 var subnet subnets.Subnet 124 125 resource.Test(t, resource.TestCase{ 126 PreCheck: func() { testAccPreCheck(t) }, 127 Providers: testAccProviders, 128 CheckDestroy: testAccCheckNetworkingV2SubnetDestroy, 129 Steps: []resource.TestStep{ 130 resource.TestStep{ 131 Config: testAccNetworkingV2Subnet_timeout, 132 Check: resource.ComposeTestCheckFunc( 133 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 134 ), 135 }, 136 }, 137 }) 138 } 139 140 func testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error { 141 config := testAccProvider.Meta().(*Config) 142 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 143 if err != nil { 144 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 145 } 146 147 for _, rs := range s.RootModule().Resources { 148 if rs.Type != "openstack_networking_subnet_v2" { 149 continue 150 } 151 152 _, err := subnets.Get(networkingClient, rs.Primary.ID).Extract() 153 if err == nil { 154 return fmt.Errorf("Subnet still exists") 155 } 156 } 157 158 return nil 159 } 160 161 func testAccCheckNetworkingV2SubnetExists(n string, subnet *subnets.Subnet) resource.TestCheckFunc { 162 return func(s *terraform.State) error { 163 rs, ok := s.RootModule().Resources[n] 164 if !ok { 165 return fmt.Errorf("Not found: %s", n) 166 } 167 168 if rs.Primary.ID == "" { 169 return fmt.Errorf("No ID is set") 170 } 171 172 config := testAccProvider.Meta().(*Config) 173 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 174 if err != nil { 175 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 176 } 177 178 found, err := subnets.Get(networkingClient, rs.Primary.ID).Extract() 179 if err != nil { 180 return err 181 } 182 183 if found.ID != rs.Primary.ID { 184 return fmt.Errorf("Subnet not found") 185 } 186 187 *subnet = *found 188 189 return nil 190 } 191 } 192 193 const testAccNetworkingV2Subnet_basic = ` 194 resource "openstack_networking_network_v2" "network_1" { 195 name = "network_1" 196 admin_state_up = "true" 197 } 198 199 resource "openstack_networking_subnet_v2" "subnet_1" { 200 cidr = "192.168.199.0/24" 201 network_id = "${openstack_networking_network_v2.network_1.id}" 202 203 allocation_pools { 204 start = "192.168.199.100" 205 end = "192.168.199.200" 206 } 207 } 208 ` 209 210 const testAccNetworkingV2Subnet_update = ` 211 resource "openstack_networking_network_v2" "network_1" { 212 name = "network_1" 213 admin_state_up = "true" 214 } 215 216 resource "openstack_networking_subnet_v2" "subnet_1" { 217 name = "subnet_1" 218 cidr = "192.168.199.0/24" 219 gateway_ip = "192.168.199.1" 220 network_id = "${openstack_networking_network_v2.network_1.id}" 221 222 allocation_pools { 223 start = "192.168.199.100" 224 end = "192.168.199.200" 225 } 226 } 227 ` 228 229 const testAccNetworkingV2Subnet_enableDHCP = ` 230 resource "openstack_networking_network_v2" "network_1" { 231 name = "network_1" 232 admin_state_up = "true" 233 } 234 235 resource "openstack_networking_subnet_v2" "subnet_1" { 236 name = "subnet_1" 237 cidr = "192.168.199.0/24" 238 gateway_ip = "192.168.199.1" 239 enable_dhcp = true 240 network_id = "${openstack_networking_network_v2.network_1.id}" 241 } 242 ` 243 244 const testAccNetworkingV2Subnet_disableDHCP = ` 245 resource "openstack_networking_network_v2" "network_1" { 246 name = "network_1" 247 admin_state_up = "true" 248 } 249 250 resource "openstack_networking_subnet_v2" "subnet_1" { 251 name = "subnet_1" 252 cidr = "192.168.199.0/24" 253 enable_dhcp = false 254 network_id = "${openstack_networking_network_v2.network_1.id}" 255 } 256 ` 257 258 const testAccNetworkingV2Subnet_noGateway = ` 259 resource "openstack_networking_network_v2" "network_1" { 260 name = "network_1" 261 admin_state_up = "true" 262 } 263 264 resource "openstack_networking_subnet_v2" "subnet_1" { 265 name = "subnet_1" 266 cidr = "192.168.199.0/24" 267 no_gateway = true 268 network_id = "${openstack_networking_network_v2.network_1.id}" 269 } 270 ` 271 272 const testAccNetworkingV2Subnet_impliedGateway = ` 273 resource "openstack_networking_network_v2" "network_1" { 274 name = "network_1" 275 admin_state_up = "true" 276 } 277 resource "openstack_networking_subnet_v2" "subnet_1" { 278 name = "subnet_1" 279 cidr = "192.168.199.0/24" 280 network_id = "${openstack_networking_network_v2.network_1.id}" 281 } 282 ` 283 284 const testAccNetworkingV2Subnet_timeout = ` 285 resource "openstack_networking_network_v2" "network_1" { 286 name = "network_1" 287 admin_state_up = "true" 288 } 289 290 resource "openstack_networking_subnet_v2" "subnet_1" { 291 cidr = "192.168.199.0/24" 292 network_id = "${openstack_networking_network_v2.network_1.id}" 293 294 allocation_pools { 295 start = "192.168.199.100" 296 end = "192.168.199.200" 297 } 298 299 timeouts { 300 create = "5m" 301 delete = "5m" 302 } 303 } 304 `