github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/rackspace/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", "tf-test-subnet"),
    31  					resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error {
    39  	config := testAccProvider.Meta().(*Config)
    40  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    41  	if err != nil {
    42  		return fmt.Errorf("(testAccCheckNetworkingV2SubnetDestroy) Error creating OpenStack networking client: %s", err)
    43  	}
    44  
    45  	for _, rs := range s.RootModule().Resources {
    46  		if rs.Type != "openstack_networking_subnet_v2" {
    47  			continue
    48  		}
    49  
    50  		_, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
    51  		if err == nil {
    52  			return fmt.Errorf("Subnet still exists")
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func testAccCheckNetworkingV2SubnetExists(t *testing.T, n string, subnet *subnets.Subnet) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", n)
    64  		}
    65  
    66  		if rs.Primary.ID == "" {
    67  			return fmt.Errorf("No ID is set")
    68  		}
    69  
    70  		config := testAccProvider.Meta().(*Config)
    71  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    72  		if err != nil {
    73  			return fmt.Errorf("(testAccCheckNetworkingV2SubnetExists) Error creating OpenStack networking client: %s", err)
    74  		}
    75  
    76  		found, err := subnets.Get(networkingClient, rs.Primary.ID).Extract()
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		if found.ID != rs.Primary.ID {
    82  			return fmt.Errorf("Subnet not found")
    83  		}
    84  
    85  		*subnet = *found
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  var testAccNetworkingV2Subnet_basic = fmt.Sprintf(`
    92    resource "openstack_networking_network_v2" "network_1" {
    93      region = "%s"
    94      name = "network_1"
    95      admin_state_up = "true"
    96    }
    97  
    98    resource "openstack_networking_subnet_v2" "subnet_1" {
    99      region = "%s"
   100      network_id = "${openstack_networking_network_v2.network_1.id}"
   101      cidr = "192.168.199.0/24"
   102    }`, OS_REGION_NAME, OS_REGION_NAME)
   103  
   104  var testAccNetworkingV2Subnet_update = fmt.Sprintf(`
   105    resource "openstack_networking_network_v2" "network_1" {
   106      region = "%s"
   107      name = "network_1"
   108      admin_state_up = "true"
   109    }
   110  
   111    resource "openstack_networking_subnet_v2" "subnet_1" {
   112      region = "%s"
   113      name = "tf-test-subnet"
   114      network_id = "${openstack_networking_network_v2.network_1.id}"
   115      cidr = "192.168.199.0/24"
   116      gateway_ip = "192.168.199.1"
   117    }`, OS_REGION_NAME, OS_REGION_NAME)