github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_firewall_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 TestAccCloudStackFirewall_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCloudStackFirewallDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCloudStackFirewall_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
    23  					resource.TestCheckResourceAttr(
    24  						"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
    27  					resource.TestCheckResourceAttr(
    28  						"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
    29  					resource.TestCheckResourceAttr(
    30  						"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
    31  					resource.TestCheckResourceAttr(
    32  						"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
    33  					resource.TestCheckResourceAttr(
    34  						"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func TestAccCloudStackFirewall_update(t *testing.T) {
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckCloudStackFirewallDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccCloudStackFirewall_basic,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
    51  					resource.TestCheckResourceAttr(
    52  						"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    53  					resource.TestCheckResourceAttr(
    54  						"cloudstack_firewall.foo", "rule.#", "1"),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
    57  					resource.TestCheckResourceAttr(
    58  						"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
    59  					resource.TestCheckResourceAttr(
    60  						"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
    61  					resource.TestCheckResourceAttr(
    62  						"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
    63  					resource.TestCheckResourceAttr(
    64  						"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
    65  				),
    66  			},
    67  
    68  			resource.TestStep{
    69  				Config: testAccCloudStackFirewall_update,
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
    72  					resource.TestCheckResourceAttr(
    73  						"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
    74  					resource.TestCheckResourceAttr(
    75  						"cloudstack_firewall.foo", "rule.#", "2"),
    76  					resource.TestCheckResourceAttr(
    77  						"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
    78  					resource.TestCheckResourceAttr(
    79  						"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
    80  					resource.TestCheckResourceAttr(
    81  						"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
    82  					resource.TestCheckResourceAttr(
    83  						"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
    84  					resource.TestCheckResourceAttr(
    85  						"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
    86  					resource.TestCheckResourceAttr(
    87  						"cloudstack_firewall.foo", "rule.3779782959.source_cidr", "172.16.100.0/24"),
    88  					resource.TestCheckResourceAttr(
    89  						"cloudstack_firewall.foo", "rule.3779782959.protocol", "tcp"),
    90  					resource.TestCheckResourceAttr(
    91  						"cloudstack_firewall.foo", "rule.3779782959.ports.#", "2"),
    92  					resource.TestCheckResourceAttr(
    93  						"cloudstack_firewall.foo", "rule.3779782959.ports.1889509032", "80"),
    94  					resource.TestCheckResourceAttr(
    95  						"cloudstack_firewall.foo", "rule.3779782959.ports.3638101695", "443"),
    96  				),
    97  			},
    98  		},
    99  	})
   100  }
   101  
   102  func testAccCheckCloudStackFirewallRulesExist(n string) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No firewall ID is set")
   111  		}
   112  
   113  		for k, id := range rs.Primary.Attributes {
   114  			if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.#") {
   115  				continue
   116  			}
   117  
   118  			cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   119  			_, count, err := cs.Firewall.GetFirewallRuleByID(id)
   120  
   121  			if err != nil {
   122  				return err
   123  			}
   124  
   125  			if count == 0 {
   126  				return fmt.Errorf("Firewall rule for %s not found", k)
   127  			}
   128  		}
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  func testAccCheckCloudStackFirewallDestroy(s *terraform.State) error {
   135  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   136  
   137  	for _, rs := range s.RootModule().Resources {
   138  		if rs.Type != "cloudstack_firewall" {
   139  			continue
   140  		}
   141  
   142  		if rs.Primary.ID == "" {
   143  			return fmt.Errorf("No instance ID is set")
   144  		}
   145  
   146  		for k, id := range rs.Primary.Attributes {
   147  			if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.#") {
   148  				continue
   149  			}
   150  
   151  			_, _, err := cs.Firewall.GetFirewallRuleByID(id)
   152  			if err == nil {
   153  				return fmt.Errorf("Firewall rule %s still exists", rs.Primary.ID)
   154  			}
   155  		}
   156  	}
   157  
   158  	return nil
   159  }
   160  
   161  var testAccCloudStackFirewall_basic = fmt.Sprintf(`
   162  resource "cloudstack_firewall" "foo" {
   163    ipaddress = "%s"
   164  
   165    rule {
   166      source_cidr = "10.0.0.0/24"
   167      protocol = "tcp"
   168      ports = ["80", "1000-2000"]
   169    }
   170  }`, CLOUDSTACK_PUBLIC_IPADDRESS)
   171  
   172  var testAccCloudStackFirewall_update = fmt.Sprintf(`
   173  resource "cloudstack_firewall" "foo" {
   174    ipaddress = "%s"
   175  
   176    rule {
   177      source_cidr = "10.0.0.0/24"
   178      protocol = "tcp"
   179      ports = ["80", "1000-2000"]
   180    }
   181  
   182    rule {
   183      source_cidr = "172.16.100.0/24"
   184      protocol = "tcp"
   185      ports = ["80", "443"]
   186    }
   187  }`, CLOUDSTACK_PUBLIC_IPADDRESS)