github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/cloudstack/resource_cloudstack_port_forward_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/xanzy/go-cloudstack/cloudstack"
    11  )
    12  
    13  func TestAccCloudStackPortForward_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCloudStackPortForward_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
    23  					resource.TestCheckResourceAttr(
    24  						"cloudstack_port_forward.foo", "ip_address_id", CLOUDSTACK_PUBLIC_IPADDRESS),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_port_forward.foo", "forward.#", "1"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccCloudStackPortForward_update(t *testing.T) {
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccCloudStackPortForward_basic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
    43  					resource.TestCheckResourceAttr(
    44  						"cloudstack_port_forward.foo", "ip_address_id", CLOUDSTACK_PUBLIC_IPADDRESS),
    45  					resource.TestCheckResourceAttr(
    46  						"cloudstack_port_forward.foo", "forward.#", "1"),
    47  				),
    48  			},
    49  
    50  			resource.TestStep{
    51  				Config: testAccCloudStackPortForward_update,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
    54  					resource.TestCheckResourceAttr(
    55  						"cloudstack_port_forward.foo", "ip_address_id", CLOUDSTACK_PUBLIC_IPADDRESS),
    56  					resource.TestCheckResourceAttr(
    57  						"cloudstack_port_forward.foo", "forward.#", "2"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func testAccCheckCloudStackPortForwardsExist(n string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rs, ok := s.RootModule().Resources[n]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", n)
    69  		}
    70  
    71  		if rs.Primary.ID == "" {
    72  			return fmt.Errorf("No port forward ID is set")
    73  		}
    74  
    75  		for k, id := range rs.Primary.Attributes {
    76  			if !strings.Contains(k, "uuid") {
    77  				continue
    78  			}
    79  
    80  			cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    81  			_, count, err := cs.Firewall.GetPortForwardingRuleByID(id)
    82  
    83  			if err != nil {
    84  				return err
    85  			}
    86  
    87  			if count == 0 {
    88  				return fmt.Errorf("Port forward for %s not found", k)
    89  			}
    90  		}
    91  
    92  		return nil
    93  	}
    94  }
    95  
    96  func testAccCheckCloudStackPortForwardDestroy(s *terraform.State) error {
    97  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    98  
    99  	for _, rs := range s.RootModule().Resources {
   100  		if rs.Type != "cloudstack_port_forward" {
   101  			continue
   102  		}
   103  
   104  		if rs.Primary.ID == "" {
   105  			return fmt.Errorf("No port forward ID is set")
   106  		}
   107  
   108  		for k, id := range rs.Primary.Attributes {
   109  			if !strings.Contains(k, "uuid") {
   110  				continue
   111  			}
   112  
   113  			_, _, err := cs.Firewall.GetPortForwardingRuleByID(id)
   114  			if err == nil {
   115  				return fmt.Errorf("Port forward %s still exists", rs.Primary.ID)
   116  			}
   117  		}
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  var testAccCloudStackPortForward_basic = fmt.Sprintf(`
   124  resource "cloudstack_instance" "foobar" {
   125    name = "terraform-test"
   126    service_offering= "%s"
   127    network_id = "%s"
   128    template = "%s"
   129    zone = "%s"
   130    expunge = true
   131  }
   132  
   133  resource "cloudstack_port_forward" "foo" {
   134    ip_address_id = "%s"
   135  
   136    forward {
   137      protocol = "tcp"
   138      private_port = 443
   139      public_port = 8443
   140      virtual_machine_id = "${cloudstack_instance.foobar.id}"
   141    }
   142  }`,
   143  	CLOUDSTACK_SERVICE_OFFERING_1,
   144  	CLOUDSTACK_NETWORK_1,
   145  	CLOUDSTACK_TEMPLATE,
   146  	CLOUDSTACK_ZONE,
   147  	CLOUDSTACK_PUBLIC_IPADDRESS)
   148  
   149  var testAccCloudStackPortForward_update = fmt.Sprintf(`
   150  resource "cloudstack_instance" "foobar" {
   151    name = "terraform-test"
   152    service_offering= "%s"
   153    network_id = "%s"
   154    template = "%s"
   155    zone = "%s"
   156    expunge = true
   157  }
   158  
   159  resource "cloudstack_port_forward" "foo" {
   160    ip_address_id = "%s"
   161  
   162    forward {
   163      protocol = "tcp"
   164      private_port = 443
   165      public_port = 8443
   166      virtual_machine_id = "${cloudstack_instance.foobar.id}"
   167    }
   168  
   169    forward {
   170      protocol = "tcp"
   171      private_port = 80
   172      public_port = 8080
   173      virtual_machine_id = "${cloudstack_instance.foobar.id}"
   174    }
   175  }`,
   176  	CLOUDSTACK_SERVICE_OFFERING_1,
   177  	CLOUDSTACK_NETWORK_1,
   178  	CLOUDSTACK_TEMPLATE,
   179  	CLOUDSTACK_ZONE,
   180  	CLOUDSTACK_PUBLIC_IPADDRESS)