github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccNetworkingV2Subnet_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "name", "subnet_1"),
    31  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"),
    32  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "true"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccNetworkingV2Subnet_enableDHCP(t *testing.T) {
    40  	var subnet subnets.Subnet
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccNetworkingV2Subnet_enableDHCP,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    51  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "true"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestAccNetworkingV2Subnet_disableDHCP(t *testing.T) {
    59  	var subnet subnets.Subnet
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccNetworkingV2Subnet_disableDHCP,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    70  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "false"),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccNetworkingV2Subnet_noGateway(t *testing.T) {
    78  	var subnet subnets.Subnet
    79  
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
    84  		Steps: []resource.TestStep{
    85  			resource.TestStep{
    86  				Config: testAccNetworkingV2Subnet_noGateway,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    89  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", ""),
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  func TestAccNetworkingV2Subnet_impliedGateway(t *testing.T) {
    97  	var subnet subnets.Subnet
    98  
    99  	resource.Test(t, resource.TestCase{
   100  		PreCheck:     func() { testAccPreCheck(t) },
   101  		Providers:    testAccProviders,
   102  		CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
   103  		Steps: []resource.TestStep{
   104  			resource.TestStep{
   105  				Config: testAccNetworkingV2Subnet_impliedGateway,
   106  				Check: resource.ComposeTestCheckFunc(
   107  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
   108  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"),
   109  				),
   110  			},
   111  		},
   112  	})
   113  }
   114  
   115  func testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error {
   116  	config := testAccProvider.Meta().(*Config)
   117  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   118  	if err != nil {
   119  		return fmt.Errorf("(testAccCheckNetworkingV2SubnetDestroy) Error creating OpenStack networking client: %s", err)
   120  	}
   121  
   122  	for _, rs := range s.RootModule().Resources {
   123  		if rs.Type != "openstack_networking_subnet_v2" {
   124  			continue
   125  		}
   126  
   127  		_, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
   128  		if err == nil {
   129  			return fmt.Errorf("Subnet still exists")
   130  		}
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func testAccCheckNetworkingV2SubnetExists(t *testing.T, n string, subnet *subnets.Subnet) resource.TestCheckFunc {
   137  	return func(s *terraform.State) error {
   138  		rs, ok := s.RootModule().Resources[n]
   139  		if !ok {
   140  			return fmt.Errorf("Not found: %s", n)
   141  		}
   142  
   143  		if rs.Primary.ID == "" {
   144  			return fmt.Errorf("No ID is set")
   145  		}
   146  
   147  		config := testAccProvider.Meta().(*Config)
   148  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   149  		if err != nil {
   150  			return fmt.Errorf("(testAccCheckNetworkingV2SubnetExists) Error creating OpenStack networking client: %s", err)
   151  		}
   152  
   153  		found, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
   154  		if err != nil {
   155  			return err
   156  		}
   157  
   158  		if found.ID != rs.Primary.ID {
   159  			return fmt.Errorf("Subnet not found")
   160  		}
   161  
   162  		*subnet = *found
   163  
   164  		return nil
   165  	}
   166  }
   167  
   168  var testAccNetworkingV2Subnet_basic = fmt.Sprintf(`
   169    resource "openstack_networking_network_v2" "network_1" {
   170      name = "network_1"
   171      admin_state_up = "true"
   172    }
   173  
   174    resource "openstack_networking_subnet_v2" "subnet_1" {
   175      network_id = "${openstack_networking_network_v2.network_1.id}"
   176      cidr = "192.168.199.0/24"
   177  		allocation_pools {
   178  			start = "192.168.199.100"
   179  			end = "192.168.199.200"
   180  		}
   181    }`)
   182  
   183  var testAccNetworkingV2Subnet_update = fmt.Sprintf(`
   184    resource "openstack_networking_network_v2" "network_1" {
   185      name = "network_1"
   186      admin_state_up = "true"
   187    }
   188  
   189    resource "openstack_networking_subnet_v2" "subnet_1" {
   190      name = "subnet_1"
   191      network_id = "${openstack_networking_network_v2.network_1.id}"
   192      cidr = "192.168.199.0/24"
   193      gateway_ip = "192.168.199.1"
   194   		allocation_pools {
   195  			start = "192.168.199.100"
   196  			end = "192.168.199.200"
   197  		}
   198   }`)
   199  
   200  var testAccNetworkingV2Subnet_enableDHCP = fmt.Sprintf(`
   201    resource "openstack_networking_network_v2" "network_1" {
   202      name = "network_1"
   203      admin_state_up = "true"
   204    }
   205  
   206    resource "openstack_networking_subnet_v2" "subnet_1" {
   207      name = "tf-test-subnet"
   208      network_id = "${openstack_networking_network_v2.network_1.id}"
   209      cidr = "192.168.199.0/24"
   210      gateway_ip = "192.168.199.1"
   211      enable_dhcp = true
   212    }`)
   213  
   214  var testAccNetworkingV2Subnet_disableDHCP = fmt.Sprintf(`
   215    resource "openstack_networking_network_v2" "network_1" {
   216      name = "network_1"
   217      admin_state_up = "true"
   218    }
   219  
   220    resource "openstack_networking_subnet_v2" "subnet_1" {
   221      name = "tf-test-subnet"
   222      network_id = "${openstack_networking_network_v2.network_1.id}"
   223      cidr = "192.168.199.0/24"
   224      enable_dhcp = false
   225    }`)
   226  
   227  var testAccNetworkingV2Subnet_noGateway = fmt.Sprintf(`
   228    resource "openstack_networking_network_v2" "network_1" {
   229      name = "network_1"
   230      admin_state_up = "true"
   231    }
   232    resource "openstack_networking_subnet_v2" "subnet_1" {
   233      name = "tf-test-subnet"
   234      network_id = "${openstack_networking_network_v2.network_1.id}"
   235      cidr = "192.168.199.0/24"
   236  		no_gateway = true
   237    }`)
   238  
   239  var testAccNetworkingV2Subnet_impliedGateway = fmt.Sprintf(`
   240    resource "openstack_networking_network_v2" "network_1" {
   241      name = "network_1"
   242      admin_state_up = "true"
   243    }
   244    resource "openstack_networking_subnet_v2" "subnet_1" {
   245      name = "tf-test-subnet"
   246      network_id = "${openstack_networking_network_v2.network_1.id}"
   247      cidr = "192.168.199.0/24"
   248    }`)