github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/azure/resource_azure_security_group_rule_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAzureSecurityGroupRule(t *testing.T) {
    12  	name := "azure_security_group_rule.foo"
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAzureSecurityGroupRuleDeleted,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAzureSecurityGroupRule,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckAzureSecurityGroupRuleExists(name),
    23  					resource.TestCheckResourceAttr(name, "name", "terraform-secgroup-rule"),
    24  					resource.TestCheckResourceAttr(name, "security_group_name", testAccSecurityGroupName),
    25  					resource.TestCheckResourceAttr(name, "type", "Inbound"),
    26  					resource.TestCheckResourceAttr(name, "action", "Deny"),
    27  					resource.TestCheckResourceAttr(name, "priority", "200"),
    28  					resource.TestCheckResourceAttr(name, "source_address_prefix", "100.0.0.0/32"),
    29  					resource.TestCheckResourceAttr(name, "source_port_range", "1000"),
    30  					resource.TestCheckResourceAttr(name, "destination_address_prefix", "10.0.0.0/32"),
    31  					resource.TestCheckResourceAttr(name, "protocol", "TCP"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAzureSecurityGroupRuleExists(name string) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		resource, ok := s.RootModule().Resources[name]
    41  		if !ok {
    42  			return fmt.Errorf("Azure security group rule not found: %s", name)
    43  		}
    44  
    45  		if resource.Primary.ID == "" {
    46  			return fmt.Errorf("Azure network security group rule ID not set: %s", name)
    47  		}
    48  
    49  		secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
    50  
    51  		secGroup, err := secGroupClient.GetNetworkSecurityGroup(testAccSecurityGroupName)
    52  		if err != nil {
    53  			return fmt.Errorf("Failed getting network security group details: %s", err)
    54  		}
    55  
    56  		for _, rule := range secGroup.Rules {
    57  			if rule.Name == resource.Primary.ID {
    58  				return nil
    59  			}
    60  		}
    61  
    62  		return fmt.Errorf("Azure security group rule doesn't exist: %s", name)
    63  	}
    64  }
    65  
    66  func testAccCheckAzureSecurityGroupRuleDeleted(s *terraform.State) error {
    67  	for _, resource := range s.RootModule().Resources {
    68  		if resource.Type != "azure_security_group_rule" {
    69  			continue
    70  		}
    71  
    72  		if resource.Primary.ID == "" {
    73  			return fmt.Errorf("Azure network security group ID not set.")
    74  		}
    75  
    76  		secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
    77  
    78  		secGroup, err := secGroupClient.GetNetworkSecurityGroup(testAccSecurityGroupName)
    79  		if err != nil {
    80  			return fmt.Errorf("Failed getting network security group details: %s", err)
    81  		}
    82  
    83  		for _, rule := range secGroup.Rules {
    84  			if rule.Name == resource.Primary.ID {
    85  				return fmt.Errorf("Azure network security group rule still exists!")
    86  			}
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  var testAccAzureSecurityGroupRule = testAccAzureSecurityGroupConfig + `
    94  resource "azure_security_group_rule" "foo" {
    95  	name = "terraform-secgroup-rule"
    96  	security_group_name = "${azure_security_group.foo.name}"
    97  	type = "Inbound"
    98  	action = "Deny"
    99  	priority = 200
   100  	source_address_prefix = "100.0.0.0/32"
   101  	source_port_range = "1000"
   102  	destination_address_prefix = "10.0.0.0/32"
   103  	destination_port_range = "1000"
   104  	protocol = "TCP"
   105  }
   106  `