2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/networksecuritygroup"
|
2015-05-28 00:50:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureSecurityGroup_basic(t *testing.T) {
|
|
|
|
var group networksecuritygroup.SecurityGroupResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-06-05 16:12:21 +02:00
|
|
|
Config: testAccAzureSecurityGroupConfig,
|
2015-05-28 00:50:45 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureSecurityGroupExists(
|
|
|
|
"azure_security_group.foo", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_security_group.foo", "name", "terraform-security-group"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_security_group.foo", "location", "West US"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-06-05 16:12:21 +02:00
|
|
|
"azure_security_group.foo", "label", "terraform testing security group"),
|
2015-05-28 00:50:45 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureSecurityGroupExists(
|
|
|
|
n string,
|
|
|
|
group *networksecuritygroup.SecurityGroupResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Network Security Group ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
|
|
|
|
sg, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID)
|
2015-05-28 00:50:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sg.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Security Group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*group = sg
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureSecurityGroupDestroy(s *terraform.State) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
secGroupClient := testAccProvider.Meta().(*Client).secGroupClient
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azure_security_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Network Security Group ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
_, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID)
|
2015-06-04 17:11:07 +02:00
|
|
|
if err == nil {
|
2015-06-09 12:38:05 +02:00
|
|
|
return fmt.Errorf("Network Security Group %s still exists", rs.Primary.ID)
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 17:11:07 +02:00
|
|
|
if !management.IsResourceNotFoundError(err) {
|
|
|
|
return err
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
var testAccAzureSecurityGroupConfig = fmt.Sprintf(`
|
2015-05-28 00:50:45 +02:00
|
|
|
resource "azure_security_group" "foo" {
|
2015-06-05 16:12:21 +02:00
|
|
|
name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
2015-06-05 16:12:21 +02:00
|
|
|
label = "terraform testing security group"
|
|
|
|
}`, testAccSecurityGroupName)
|