github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/vcd/resource_vcd_snat_test.go (about)

     1  package vcd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/hmrc/vmware-govcd"
    11  )
    12  
    13  func TestAccVcdSNAT_Basic(t *testing.T) {
    14  	if v := os.Getenv("VCD_EXTERNAL_IP"); v == "" {
    15  		t.Skip("Environment variable VCD_EXTERNAL_IP must be set to run SNAT tests")
    16  		return
    17  	}
    18  
    19  	var e govcd.EdgeGateway
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckVcdSNATDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: fmt.Sprintf(testAccCheckVcdSnat_basic, os.Getenv("VCD_EDGE_GATWEWAY"), os.Getenv("VCD_EXTERNAL_IP")),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckVcdSNATExists("vcd_snat.bar", &e),
    30  					resource.TestCheckResourceAttr(
    31  						"vcd_snat.bar", "external_ip", os.Getenv("VCD_EXTERNAL_IP")),
    32  					resource.TestCheckResourceAttr(
    33  						"vcd_snat.bar", "internal_ip", "10.10.102.0/24"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func testAccCheckVcdSNATExists(n string, gateway *govcd.EdgeGateway) resource.TestCheckFunc {
    41  	return func(s *terraform.State) error {
    42  		rs, ok := s.RootModule().Resources[n]
    43  		if !ok {
    44  			return fmt.Errorf("Not found: %s", n)
    45  		}
    46  
    47  		//return fmt.Errorf("Check this: %#v", rs.Primary)
    48  
    49  		if rs.Primary.ID == "" {
    50  			return fmt.Errorf("No SNAT ID is set")
    51  		}
    52  
    53  		conn := testAccProvider.Meta().(*VCDClient)
    54  
    55  		gatewayName := rs.Primary.Attributes["edge_gateway"]
    56  		edgeGateway, err := conn.OrgVdc.FindEdgeGateway(gatewayName)
    57  
    58  		if err != nil {
    59  			return fmt.Errorf("Could not find edge gateway")
    60  		}
    61  
    62  		var found bool
    63  		for _, v := range edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.NatService.NatRule {
    64  			if v.RuleType == "SNAT" &&
    65  				v.GatewayNatRule.OriginalIP == "10.10.102.0/24" &&
    66  				v.GatewayNatRule.OriginalPort == "" &&
    67  				v.GatewayNatRule.TranslatedIP == os.Getenv("VCD_EXTERNAL_IP") {
    68  				found = true
    69  			}
    70  		}
    71  		if !found {
    72  			return fmt.Errorf("SNAT rule was not found")
    73  		}
    74  
    75  		*gateway = edgeGateway
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckVcdSNATDestroy(s *terraform.State) error {
    82  	conn := testAccProvider.Meta().(*VCDClient)
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "vcd_snat" {
    85  			continue
    86  		}
    87  
    88  		gatewayName := rs.Primary.Attributes["edge_gateway"]
    89  		edgeGateway, err := conn.OrgVdc.FindEdgeGateway(gatewayName)
    90  
    91  		if err != nil {
    92  			return fmt.Errorf("Could not find edge gateway")
    93  		}
    94  
    95  		var found bool
    96  		for _, v := range edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.NatService.NatRule {
    97  			if v.RuleType == "SNAT" &&
    98  				v.GatewayNatRule.OriginalIP == "10.10.102.0/24" &&
    99  				v.GatewayNatRule.OriginalPort == "" &&
   100  				v.GatewayNatRule.TranslatedIP == os.Getenv("VCD_EXTERNAL_IP") {
   101  				found = true
   102  			}
   103  		}
   104  
   105  		if found {
   106  			return fmt.Errorf("SNAT rule still exists.")
   107  		}
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  const testAccCheckVcdSnat_basic = `
   114  resource "vcd_snat" "bar" {
   115  	edge_gateway = "%s"
   116  	external_ip = "%s"
   117  	internal_ip = "10.10.102.0/24"
   118  }
   119  `