opentofu/builtin/providers/cloudstack/resource_cloudstack_security_group_test.go
Sander van Harmelen 1619a8138f provider/cloudstack: enhance security groups and rules (#9645)
* govendor: update go-cloudstack dependency

* Separate security groups and rules

This commit separates the creation and management of security groups and security group rules.

It extends the `icmp` options so you can supply `icmp_type` and `icmp_code` to enbale more specific configs.

And it adds lifecycle management of security group rules, so that security groups do not have to be recreated when rules are added or removed.

This is particulary helpful since the `cloudstack_instance` cannot update a security group without having to recreate the instance.

In CloudStack >= 4.9.0 it is possible to update security groups of existing instances, but as that is just added to the latest version it seems a bit too soon to start using this (causing backwards incompatibility issues for people or service providers running older versions).

* Add and update documentation

* Add acceptance tests
2016-10-27 11:10:15 +02:00

101 lines
2.4 KiB
Go

package cloudstack
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/xanzy/go-cloudstack/cloudstack"
)
func TestAccCloudStackSecurityGroup_basic(t *testing.T) {
var sg cloudstack.SecurityGroup
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackSecurityGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackSecurityGroup_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackSecurityGroupExists(
"cloudstack_security_group.foo", &sg),
testAccCheckCloudStackSecurityGroupBasicAttributes(&sg),
),
},
},
})
}
func testAccCheckCloudStackSecurityGroupExists(
n string, sg *cloudstack.SecurityGroup) 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 security group ID is set")
}
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
resp, _, err := cs.SecurityGroup.GetSecurityGroupByID(rs.Primary.ID)
if err != nil {
return err
}
if resp.Id != rs.Primary.ID {
return fmt.Errorf("Network ACL not found")
}
*sg = *resp
return nil
}
}
func testAccCheckCloudStackSecurityGroupBasicAttributes(
sg *cloudstack.SecurityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
if sg.Name != "terraform-security-group" {
return fmt.Errorf("Bad name: %s", sg.Name)
}
if sg.Description != "terraform-security-group-text" {
return fmt.Errorf("Bad description: %s", sg.Description)
}
return nil
}
}
func testAccCheckCloudStackSecurityGroupDestroy(s *terraform.State) error {
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_security_group" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No security group ID is set")
}
_, _, err := cs.SecurityGroup.GetSecurityGroupByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Security group list %s still exists", rs.Primary.ID)
}
}
return nil
}
var testAccCloudStackSecurityGroup_basic = fmt.Sprintf(`
resource "cloudstack_security_group" "foo" {
name = "terraform-security-group"
description = "terraform-security-group-text"
}`)