github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/vcd/resource_vcd_dnat_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 TestAccVcdDNAT_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 DNAT 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: testAccCheckVcdDNATDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: fmt.Sprintf(testAccCheckVcdDnat_basic, os.Getenv("VCD_EDGE_GATWEWAY"), os.Getenv("VCD_EXTERNAL_IP")),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckVcdDNATExists("vcd_dnat.bar", &e),
    30  					resource.TestCheckResourceAttr(
    31  						"vcd_dnat.bar", "external_ip", os.Getenv("VCD_EXTERNAL_IP")),
    32  					resource.TestCheckResourceAttr(
    33  						"vcd_dnat.bar", "port", "77"),
    34  					resource.TestCheckResourceAttr(
    35  						"vcd_dnat.bar", "internal_ip", "10.10.102.60"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckVcdDNATExists(n string, gateway *govcd.EdgeGateway) resource.TestCheckFunc {
    43  	return func(s *terraform.State) error {
    44  		rs, ok := s.RootModule().Resources[n]
    45  		if !ok {
    46  			return fmt.Errorf("Not found: %s", n)
    47  		}
    48  
    49  		if rs.Primary.ID == "" {
    50  			return fmt.Errorf("No DNAT 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 == "DNAT" &&
    65  				v.GatewayNatRule.OriginalIP == os.Getenv("VCD_EXTERNAL_IP") &&
    66  				v.GatewayNatRule.OriginalPort == "77" &&
    67  				v.GatewayNatRule.TranslatedIP == "10.10.102.60" {
    68  				found = true
    69  			}
    70  		}
    71  		if !found {
    72  			return fmt.Errorf("DNAT rule was not found")
    73  		}
    74  
    75  		*gateway = edgeGateway
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckVcdDNATDestroy(s *terraform.State) error {
    82  	conn := testAccProvider.Meta().(*VCDClient)
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "vcd_dnat" {
    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 == "DNAT" &&
    98  				v.GatewayNatRule.OriginalIP == os.Getenv("VCD_EXTERNAL_IP") &&
    99  				v.GatewayNatRule.OriginalPort == "77" &&
   100  				v.GatewayNatRule.TranslatedIP == "10.10.102.60" {
   101  				found = true
   102  			}
   103  		}
   104  
   105  		if found {
   106  			return fmt.Errorf("DNAT rule still exists.")
   107  		}
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  const testAccCheckVcdDnat_basic = `
   114  resource "vcd_dnat" "bar" {
   115  	edge_gateway = "%s"
   116  	external_ip = "%s"
   117  	port = 77
   118  	internal_ip = "10.10.102.60"
   119  }
   120  `