github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/azure/resource_azure_security_group_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management"
     8  	"github.com/Azure/azure-sdk-for-go/management/networksecuritygroup"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureSecurityGroup_basic(t *testing.T) {
    14  	var group networksecuritygroup.SecurityGroupResponse
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAzureSecurityGroupDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAzureSecurityGroupConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAzureSecurityGroupExists(
    25  						"azure_security_group.foo", &group),
    26  					resource.TestCheckResourceAttr(
    27  						"azure_security_group.foo", "name", "terraform-security-group"),
    28  					resource.TestCheckResourceAttr(
    29  						"azure_security_group.foo", "location", "West US"),
    30  					resource.TestCheckResourceAttr(
    31  						"azure_security_group.foo", "label", "terraform testing security group"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAzureSecurityGroupExists(
    39  	n string,
    40  	group *networksecuritygroup.SecurityGroupResponse) resource.TestCheckFunc {
    41  	return func(s *terraform.State) error {
    42  		rs, ok := s.RootModule().Resources[n]
    43  		if !ok {
    44  			return fmt.Errorf("Not found: %s", n)
    45  		}
    46  
    47  		if rs.Primary.ID == "" {
    48  			return fmt.Errorf("No Network Security Group ID is set")
    49  		}
    50  
    51  		secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
    52  		sg, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID)
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		if sg.Name != rs.Primary.ID {
    58  			return fmt.Errorf("Security Group not found")
    59  		}
    60  
    61  		*group = sg
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccCheckAzureSecurityGroupDestroy(s *terraform.State) error {
    68  	secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
    69  
    70  	for _, rs := range s.RootModule().Resources {
    71  		if rs.Type != "azure_security_group" {
    72  			continue
    73  		}
    74  
    75  		if rs.Primary.ID == "" {
    76  			return fmt.Errorf("No Network Security Group ID is set")
    77  		}
    78  
    79  		_, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID)
    80  		if err == nil {
    81  			return fmt.Errorf("Network Security Group %s still exists", rs.Primary.ID)
    82  		}
    83  
    84  		if !management.IsResourceNotFoundError(err) {
    85  			return err
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  const testAccAzureSecurityGroupConfigTemplate = `
    93  resource "azure_security_group" "%s" {
    94      name = "%s"
    95      location = "West US"
    96      label = "terraform testing security group"
    97  }`
    98  
    99  var testAccAzureSecurityGroupConfig = fmt.Sprintf(
   100  	testAccAzureSecurityGroupConfigTemplate,
   101  	"foo", "terraform-security-group",
   102  )