github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_port_forward.foo", "forward.1537694805.protocol", "tcp"),
    27  					resource.TestCheckResourceAttr(
    28  						"cloudstack_port_forward.foo", "forward.1537694805.private_port", "443"),
    29  					resource.TestCheckResourceAttr(
    30  						"cloudstack_port_forward.foo", "forward.1537694805.public_port", "8443"),
    31  					resource.TestCheckResourceAttr(
    32  						"cloudstack_port_forward.foo", "forward.1537694805.virtual_machine", "terraform-test"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccCloudStackPortForward_update(t *testing.T) {
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccCloudStackPortForward_basic,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
    49  					resource.TestCheckResourceAttr(
    50  						"cloudstack_port_forward.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    51  					resource.TestCheckResourceAttr(
    52  						"cloudstack_port_forward.foo", "forward.#", "1"),
    53  					resource.TestCheckResourceAttr(
    54  						"cloudstack_port_forward.foo", "forward.1537694805.protocol", "tcp"),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudstack_port_forward.foo", "forward.1537694805.private_port", "443"),
    57  					resource.TestCheckResourceAttr(
    58  						"cloudstack_port_forward.foo", "forward.1537694805.public_port", "8443"),
    59  					resource.TestCheckResourceAttr(
    60  						"cloudstack_port_forward.foo", "forward.1537694805.virtual_machine", "terraform-test"),
    61  				),
    62  			},
    63  
    64  			resource.TestStep{
    65  				Config: testAccCloudStackPortForward_update,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
    68  					resource.TestCheckResourceAttr(
    69  						"cloudstack_port_forward.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    70  					resource.TestCheckResourceAttr(
    71  						"cloudstack_port_forward.foo", "forward.#", "2"),
    72  					resource.TestCheckResourceAttr(
    73  						"cloudstack_port_forward.foo", "forward.8416686.protocol", "tcp"),
    74  					resource.TestCheckResourceAttr(
    75  						"cloudstack_port_forward.foo", "forward.8416686.private_port", "80"),
    76  					resource.TestCheckResourceAttr(
    77  						"cloudstack_port_forward.foo", "forward.8416686.public_port", "8080"),
    78  					resource.TestCheckResourceAttr(
    79  						"cloudstack_port_forward.foo", "forward.8416686.virtual_machine", "terraform-test"),
    80  					resource.TestCheckResourceAttr(
    81  						"cloudstack_port_forward.foo", "forward.1537694805.protocol", "tcp"),
    82  					resource.TestCheckResourceAttr(
    83  						"cloudstack_port_forward.foo", "forward.1537694805.private_port", "443"),
    84  					resource.TestCheckResourceAttr(
    85  						"cloudstack_port_forward.foo", "forward.1537694805.public_port", "8443"),
    86  					resource.TestCheckResourceAttr(
    87  						"cloudstack_port_forward.foo", "forward.1537694805.virtual_machine", "terraform-test"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccCheckCloudStackPortForwardsExist(n string) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		rs, ok := s.RootModule().Resources[n]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", n)
    99  		}
   100  
   101  		if rs.Primary.ID == "" {
   102  			return fmt.Errorf("No port forward ID is set")
   103  		}
   104  
   105  		for k, id := range rs.Primary.Attributes {
   106  			if !strings.Contains(k, "uuid") {
   107  				continue
   108  			}
   109  
   110  			cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   111  			_, count, err := cs.Firewall.GetPortForwardingRuleByID(id)
   112  
   113  			if err != nil {
   114  				return err
   115  			}
   116  
   117  			if count == 0 {
   118  				return fmt.Errorf("Port forward for %s not found", k)
   119  			}
   120  		}
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  func testAccCheckCloudStackPortForwardDestroy(s *terraform.State) error {
   127  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   128  
   129  	for _, rs := range s.RootModule().Resources {
   130  		if rs.Type != "cloudstack_port_forward" {
   131  			continue
   132  		}
   133  
   134  		if rs.Primary.ID == "" {
   135  			return fmt.Errorf("No port forward ID is set")
   136  		}
   137  
   138  		for k, id := range rs.Primary.Attributes {
   139  			if !strings.Contains(k, "uuid") {
   140  				continue
   141  			}
   142  
   143  			_, _, err := cs.Firewall.GetPortForwardingRuleByID(id)
   144  			if err == nil {
   145  				return fmt.Errorf("Port forward %s still exists", rs.Primary.ID)
   146  			}
   147  		}
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  var testAccCloudStackPortForward_basic = fmt.Sprintf(`
   154  resource "cloudstack_instance" "foobar" {
   155    name = "terraform-test"
   156    service_offering= "%s"
   157    network = "%s"
   158    template = "%s"
   159    zone = "%s"
   160    expunge = true
   161  }
   162  
   163  resource "cloudstack_port_forward" "foo" {
   164    ipaddress = "%s"
   165  
   166    forward {
   167      protocol = "tcp"
   168      private_port = 443
   169      public_port = 8443
   170      virtual_machine = "${cloudstack_instance.foobar.name}"
   171    }
   172  }`,
   173  	CLOUDSTACK_SERVICE_OFFERING_1,
   174  	CLOUDSTACK_NETWORK_1,
   175  	CLOUDSTACK_TEMPLATE,
   176  	CLOUDSTACK_ZONE,
   177  	CLOUDSTACK_PUBLIC_IPADDRESS)
   178  
   179  var testAccCloudStackPortForward_update = fmt.Sprintf(`
   180  resource "cloudstack_instance" "foobar" {
   181    name = "terraform-test"
   182    service_offering= "%s"
   183    network = "%s"
   184    template = "%s"
   185    zone = "%s"
   186    expunge = true
   187  }
   188  
   189  resource "cloudstack_port_forward" "foo" {
   190    ipaddress = "%s"
   191  
   192    forward {
   193      protocol = "tcp"
   194      private_port = 443
   195      public_port = 8443
   196      virtual_machine = "${cloudstack_instance.foobar.name}"
   197    }
   198  
   199    forward {
   200      protocol = "tcp"
   201      private_port = 80
   202      public_port = 8080
   203      virtual_machine = "${cloudstack_instance.foobar.name}"
   204    }
   205  }`,
   206  	CLOUDSTACK_SERVICE_OFFERING_1,
   207  	CLOUDSTACK_NETWORK_1,
   208  	CLOUDSTACK_TEMPLATE,
   209  	CLOUDSTACK_ZONE,
   210  	CLOUDSTACK_PUBLIC_IPADDRESS)