github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error {
   123  	config := testAccProvider.Meta().(*Config)
   124  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   125  	if err != nil {
   126  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   127  	}
   128  
   129  	for _, rs := range s.RootModule().Resources {
   130  		if rs.Type != "openstack_networking_subnet_v2" {
   131  			continue
   132  		}
   133  
   134  		_, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
   135  		if err == nil {
   136  			return fmt.Errorf("Subnet still exists")
   137  		}
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func testAccCheckNetworkingV2SubnetExists(n string, subnet *subnets.Subnet) resource.TestCheckFunc {
   144  	return func(s *terraform.State) error {
   145  		rs, ok := s.RootModule().Resources[n]
   146  		if !ok {
   147  			return fmt.Errorf("Not found: %s", n)
   148  		}
   149  
   150  		if rs.Primary.ID == "" {
   151  			return fmt.Errorf("No ID is set")
   152  		}
   153  
   154  		config := testAccProvider.Meta().(*Config)
   155  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   156  		if err != nil {
   157  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   158  		}
   159  
   160  		found, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
   161  		if err != nil {
   162  			return err
   163  		}
   164  
   165  		if found.ID != rs.Primary.ID {
   166  			return fmt.Errorf("Subnet not found")
   167  		}
   168  
   169  		*subnet = *found
   170  
   171  		return nil
   172  	}
   173  }
   174  
   175  const testAccNetworkingV2Subnet_basic = `
   176  resource "openstack_networking_network_v2" "network_1" {
   177    name = "network_1"
   178    admin_state_up = "true"
   179  }
   180  
   181  resource "openstack_networking_subnet_v2" "subnet_1" {
   182    cidr = "192.168.199.0/24"
   183    network_id = "${openstack_networking_network_v2.network_1.id}"
   184  
   185    allocation_pools {
   186      start = "192.168.199.100"
   187      end = "192.168.199.200"
   188    }
   189  }
   190  `
   191  
   192  const testAccNetworkingV2Subnet_update = `
   193  resource "openstack_networking_network_v2" "network_1" {
   194    name = "network_1"
   195    admin_state_up = "true"
   196  }
   197  
   198  resource "openstack_networking_subnet_v2" "subnet_1" {
   199    name = "subnet_1"
   200    cidr = "192.168.199.0/24"
   201    gateway_ip = "192.168.199.1"
   202    network_id = "${openstack_networking_network_v2.network_1.id}"
   203  
   204    allocation_pools {
   205      start = "192.168.199.100"
   206      end = "192.168.199.200"
   207    }
   208  }
   209  `
   210  
   211  const testAccNetworkingV2Subnet_enableDHCP = `
   212  resource "openstack_networking_network_v2" "network_1" {
   213    name = "network_1"
   214    admin_state_up = "true"
   215  }
   216  
   217  resource "openstack_networking_subnet_v2" "subnet_1" {
   218    name = "subnet_1"
   219    cidr = "192.168.199.0/24"
   220    gateway_ip = "192.168.199.1"
   221    enable_dhcp = true
   222    network_id = "${openstack_networking_network_v2.network_1.id}"
   223  }
   224  `
   225  
   226  const testAccNetworkingV2Subnet_disableDHCP = `
   227  resource "openstack_networking_network_v2" "network_1" {
   228    name = "network_1"
   229    admin_state_up = "true"
   230  }
   231  
   232  resource "openstack_networking_subnet_v2" "subnet_1" {
   233    name = "subnet_1"
   234    cidr = "192.168.199.0/24"
   235    enable_dhcp = false
   236    network_id = "${openstack_networking_network_v2.network_1.id}"
   237  }
   238  `
   239  
   240  const testAccNetworkingV2Subnet_noGateway = `
   241  resource "openstack_networking_network_v2" "network_1" {
   242    name = "network_1"
   243    admin_state_up = "true"
   244  }
   245  
   246  resource "openstack_networking_subnet_v2" "subnet_1" {
   247    name = "subnet_1"
   248    cidr = "192.168.199.0/24"
   249    no_gateway = true
   250    network_id = "${openstack_networking_network_v2.network_1.id}"
   251  }
   252  `
   253  
   254  const testAccNetworkingV2Subnet_impliedGateway = `
   255  resource "openstack_networking_network_v2" "network_1" {
   256    name = "network_1"
   257    admin_state_up = "true"
   258  }
   259  resource "openstack_networking_subnet_v2" "subnet_1" {
   260    name = "subnet_1"
   261    cidr = "192.168.199.0/24"
   262    network_id = "${openstack_networking_network_v2.network_1.id}"
   263  }
   264  `