2015-01-08 18:47:10 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2015-01-30 05:54:07 +01:00
|
|
|
"bytes"
|
2015-01-08 18:47:10 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-07-16 05:26:41 +02:00
|
|
|
"time"
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-01-30 05:54:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2015-07-16 05:26:41 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-01-08 18:47:10 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-02-17 18:48:23 +01:00
|
|
|
"github.com/rackspace/gophercloud"
|
2015-01-08 18:47:10 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeSecGroupV2() *schema.Resource {
|
2015-01-08 18:47:10 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceComputeSecGroupV2Create,
|
|
|
|
Read: resourceComputeSecGroupV2Read,
|
|
|
|
Update: resourceComputeSecGroupV2Update,
|
|
|
|
Delete: resourceComputeSecGroupV2Delete,
|
2015-01-08 18:47:10 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-01-26 18:54:07 +01:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
2015-01-26 18:54:07 +01:00
|
|
|
},
|
2015-01-08 18:47:10 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-02-01 01:22:19 +01:00
|
|
|
"rule": &schema.Schema{
|
2015-11-11 06:43:59 +01:00
|
|
|
Type: schema.TypeSet,
|
2015-01-30 05:54:07 +01:00
|
|
|
Optional: true,
|
2015-11-11 06:43:59 +01:00
|
|
|
Computed: true,
|
2015-01-30 05:54:07 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-02-13 01:25:45 +01:00
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-01-30 05:54:07 +01:00
|
|
|
"from_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
2015-02-13 01:25:45 +01:00
|
|
|
ForceNew: false,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
|
|
|
"to_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
2015-02-13 01:25:45 +01:00
|
|
|
ForceNew: false,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
|
|
|
"ip_protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-02-13 01:25:45 +01:00
|
|
|
ForceNew: false,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
|
|
|
"cidr": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-02-13 01:25:45 +01:00
|
|
|
ForceNew: false,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
|
|
|
"from_group_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-02-13 01:25:45 +01:00
|
|
|
ForceNew: false,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
2015-02-16 22:04:08 +01:00
|
|
|
"self": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-03-04 05:41:15 +01:00
|
|
|
Default: false,
|
2015-02-16 22:04:08 +01:00
|
|
|
ForceNew: false,
|
|
|
|
},
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
|
|
|
},
|
2015-11-11 06:43:59 +01:00
|
|
|
Set: secgroupRuleV2Hash,
|
2015-01-30 05:54:07 +01:00
|
|
|
},
|
2015-01-08 18:47:10 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeSecGroupV2Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 18:47:10 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-12-30 19:22:24 +01:00
|
|
|
// Before creating the security group, make sure all rules are valid.
|
|
|
|
if err := checkSecGroupV2RulesForErrors(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all rules are valid, proceed with creating the security gruop.
|
2015-01-08 18:47:10 +01:00
|
|
|
createOpts := secgroups.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
}
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-01-26 18:54:07 +01:00
|
|
|
sg, err := secgroups.Create(computeClient, createOpts).Extract()
|
2015-01-08 18:47:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack security group: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(sg.ID)
|
|
|
|
|
2015-12-30 19:22:24 +01:00
|
|
|
// Now that the security group has been created, iterate through each rule and create it
|
2015-01-30 05:54:07 +01:00
|
|
|
createRuleOptsList := resourceSecGroupRulesV2(d)
|
|
|
|
for _, createRuleOpts := range createRuleOptsList {
|
|
|
|
_, err := secgroups.CreateRule(computeClient, createRuleOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack security group rule: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceComputeSecGroupV2Read(d, meta)
|
2015-01-08 18:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeSecGroupV2Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 18:47:10 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
sg, err := secgroups.Get(computeClient, d.Id()).Extract()
|
2015-01-08 18:47:10 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "security group")
|
2015-01-08 18:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", sg.Name)
|
|
|
|
d.Set("description", sg.Description)
|
2015-11-11 06:43:59 +01:00
|
|
|
|
|
|
|
rtm, err := rulesToMap(computeClient, d, sg.Rules)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-04 05:41:15 +01:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] rulesToMap(sg.Rules): %+v", rtm)
|
|
|
|
d.Set("rule", rtm)
|
2015-01-08 18:47:10 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeSecGroupV2Update(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 18:47:10 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-01-26 20:59:38 +01:00
|
|
|
updateOpts := secgroups.UpdateOpts{
|
2015-01-26 23:45:05 +01:00
|
|
|
Name: d.Get("name").(string),
|
2015-01-26 20:59:38 +01:00
|
|
|
Description: d.Get("description").(string),
|
2015-01-08 18:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
log.Printf("[DEBUG] Updating Security Group (%s) with options: %+v", d.Id(), updateOpts)
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err = secgroups.Update(computeClient, d.Id(), updateOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack security group (%s): %s", d.Id(), err)
|
2015-01-08 18:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 01:22:19 +01:00
|
|
|
if d.HasChange("rule") {
|
|
|
|
oldSGRaw, newSGRaw := d.GetChange("rule")
|
2015-11-11 06:43:59 +01:00
|
|
|
oldSGRSet, newSGRSet := oldSGRaw.(*schema.Set), newSGRaw.(*schema.Set)
|
2015-01-30 05:54:07 +01:00
|
|
|
secgrouprulesToAdd := newSGRSet.Difference(oldSGRSet)
|
|
|
|
secgrouprulesToRemove := oldSGRSet.Difference(newSGRSet)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Security group rules to add: %v", secgrouprulesToAdd)
|
2015-02-13 01:25:45 +01:00
|
|
|
log.Printf("[DEBUG] Security groups rules to remove: %v", secgrouprulesToRemove)
|
2015-01-30 05:54:07 +01:00
|
|
|
|
|
|
|
for _, rawRule := range secgrouprulesToAdd.List() {
|
2015-02-13 01:25:45 +01:00
|
|
|
createRuleOpts := resourceSecGroupRuleCreateOptsV2(d, rawRule)
|
2015-01-30 05:54:07 +01:00
|
|
|
rule, err := secgroups.CreateRule(computeClient, createRuleOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error adding rule to OpenStack security group (%s): %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Added rule (%s) to OpenStack security group (%s) ", rule.ID, d.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range secgrouprulesToRemove.List() {
|
2015-02-13 01:25:45 +01:00
|
|
|
rule := resourceSecGroupRuleV2(d, r)
|
|
|
|
err := secgroups.DeleteRule(computeClient, rule.ID).ExtractErr()
|
2015-01-30 05:54:07 +01:00
|
|
|
if err != nil {
|
2015-02-17 18:48:23 +01:00
|
|
|
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-13 01:25:45 +01:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error removing rule (%s) from OpenStack security group (%s): %s", rule.ID, d.Id(), err)
|
|
|
|
}
|
|
|
|
if errCode.Actual == 404 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error removing rule (%s) from OpenStack security group (%s)", rule.ID, d.Id())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Removed rule (%s) from OpenStack security group (%s): %s", rule.ID, d.Id(), err)
|
2015-01-30 05:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceComputeSecGroupV2Read(d, meta)
|
2015-01-08 18:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceComputeSecGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-08 18:47:10 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
computeClient, err := config.computeV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
2015-01-08 18:47:10 +01:00
|
|
|
|
2015-07-16 05:26:41 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ACTIVE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"DELETED"},
|
2015-07-16 05:26:41 +02:00
|
|
|
Refresh: SecGroupV2StateRefreshFunc(computeClient, d),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
2015-01-08 18:47:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack security group: %s", err)
|
|
|
|
}
|
2015-07-16 05:26:41 +02:00
|
|
|
|
2015-01-08 18:47:10 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-30 05:54:07 +01:00
|
|
|
|
|
|
|
func resourceSecGroupRulesV2(d *schema.ResourceData) []secgroups.CreateRuleOpts {
|
2015-11-11 06:43:59 +01:00
|
|
|
rawRules := d.Get("rule").(*schema.Set).List()
|
2015-02-13 01:25:45 +01:00
|
|
|
createRuleOptsList := make([]secgroups.CreateRuleOpts, len(rawRules))
|
2015-11-11 06:43:59 +01:00
|
|
|
for i, rawRule := range rawRules {
|
|
|
|
createRuleOptsList[i] = resourceSecGroupRuleCreateOptsV2(d, rawRule)
|
2015-01-30 05:54:07 +01:00
|
|
|
}
|
|
|
|
return createRuleOptsList
|
|
|
|
}
|
|
|
|
|
2015-11-11 06:43:59 +01:00
|
|
|
func resourceSecGroupRuleCreateOptsV2(d *schema.ResourceData, rawRule interface{}) secgroups.CreateRuleOpts {
|
|
|
|
rawRuleMap := rawRule.(map[string]interface{})
|
|
|
|
groupId := rawRuleMap["from_group_id"].(string)
|
|
|
|
if rawRuleMap["self"].(bool) {
|
2015-02-16 22:04:08 +01:00
|
|
|
groupId = d.Id()
|
|
|
|
}
|
2015-02-01 01:22:19 +01:00
|
|
|
return secgroups.CreateRuleOpts{
|
2015-01-30 05:54:07 +01:00
|
|
|
ParentGroupID: d.Id(),
|
2015-11-11 06:43:59 +01:00
|
|
|
FromPort: rawRuleMap["from_port"].(int),
|
|
|
|
ToPort: rawRuleMap["to_port"].(int),
|
|
|
|
IPProtocol: rawRuleMap["ip_protocol"].(string),
|
|
|
|
CIDR: rawRuleMap["cidr"].(string),
|
2015-02-16 22:04:08 +01:00
|
|
|
FromGroupID: groupId,
|
2015-01-30 05:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-13 01:25:45 +01:00
|
|
|
|
2015-12-30 19:22:24 +01:00
|
|
|
func checkSecGroupV2RulesForErrors(d *schema.ResourceData) error {
|
|
|
|
rawRules := d.Get("rule").(*schema.Set).List()
|
|
|
|
for _, rawRule := range rawRules {
|
|
|
|
rawRuleMap := rawRule.(map[string]interface{})
|
|
|
|
|
|
|
|
// only one of cidr, from_group_id, or self can be set
|
|
|
|
cidr := rawRuleMap["cidr"].(string)
|
|
|
|
groupId := rawRuleMap["from_group_id"].(string)
|
|
|
|
self := rawRuleMap["self"].(bool)
|
|
|
|
errorMessage := fmt.Errorf("Only one of cidr, from_group_id, or self can be set.")
|
|
|
|
|
|
|
|
// if cidr is set, from_group_id and self cannot be set
|
|
|
|
if cidr != "" {
|
|
|
|
if groupId != "" || self {
|
|
|
|
return errorMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if from_group_id is set, cidr and self cannot be set
|
|
|
|
if groupId != "" {
|
|
|
|
if cidr != "" || self {
|
|
|
|
return errorMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if self is set, cidr and from_group_id cannot be set
|
|
|
|
if self {
|
|
|
|
if cidr != "" || groupId != "" {
|
|
|
|
return errorMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-11 06:43:59 +01:00
|
|
|
func resourceSecGroupRuleV2(d *schema.ResourceData, rawRule interface{}) secgroups.Rule {
|
|
|
|
rawRuleMap := rawRule.(map[string]interface{})
|
2015-02-13 01:25:45 +01:00
|
|
|
return secgroups.Rule{
|
2015-11-11 06:43:59 +01:00
|
|
|
ID: rawRuleMap["id"].(string),
|
2015-02-13 01:25:45 +01:00
|
|
|
ParentGroupID: d.Id(),
|
2015-11-11 06:43:59 +01:00
|
|
|
FromPort: rawRuleMap["from_port"].(int),
|
|
|
|
ToPort: rawRuleMap["to_port"].(int),
|
|
|
|
IPProtocol: rawRuleMap["ip_protocol"].(string),
|
|
|
|
IPRange: secgroups.IPRange{CIDR: rawRuleMap["cidr"].(string)},
|
2015-02-13 01:25:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 06:43:59 +01:00
|
|
|
func rulesToMap(computeClient *gophercloud.ServiceClient, d *schema.ResourceData, sgrs []secgroups.Rule) ([]map[string]interface{}, error) {
|
2015-02-13 01:25:45 +01:00
|
|
|
sgrMap := make([]map[string]interface{}, len(sgrs))
|
|
|
|
for i, sgr := range sgrs {
|
2015-11-11 06:43:59 +01:00
|
|
|
groupId := ""
|
|
|
|
self := false
|
|
|
|
if sgr.Group.Name != "" {
|
|
|
|
if sgr.Group.Name == d.Get("name").(string) {
|
|
|
|
self = true
|
|
|
|
} else {
|
|
|
|
// Since Nova only returns the secgroup Name (and not the ID) for the group attribute,
|
|
|
|
// we need to look up all security groups and match the name.
|
|
|
|
// Nevermind that Nova wants the ID when setting the Group *and* that multiple groups
|
|
|
|
// with the same name can exist...
|
|
|
|
allPages, err := secgroups.List(computeClient).AllPages()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
securityGroups, err := secgroups.ExtractSecurityGroups(allPages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sg := range securityGroups {
|
|
|
|
if sg.Name == sgr.Group.Name {
|
|
|
|
groupId = sg.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 01:25:45 +01:00
|
|
|
sgrMap[i] = map[string]interface{}{
|
2015-11-11 06:43:59 +01:00
|
|
|
"id": sgr.ID,
|
|
|
|
"from_port": sgr.FromPort,
|
|
|
|
"to_port": sgr.ToPort,
|
|
|
|
"ip_protocol": sgr.IPProtocol,
|
|
|
|
"cidr": sgr.IPRange.CIDR,
|
|
|
|
"self": self,
|
|
|
|
"from_group_id": groupId,
|
2015-02-13 01:25:45 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 06:43:59 +01:00
|
|
|
return sgrMap, nil
|
2015-02-13 01:25:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func secgroupRuleV2Hash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["from_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["to_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["ip_protocol"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["cidr"].(string)))
|
2015-11-11 06:43:59 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["from_group_id"].(string)))
|
2015-11-14 09:19:22 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%t-", m["self"].(bool)))
|
2015-02-13 01:25:45 +01:00
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
2015-07-16 05:26:41 +02:00
|
|
|
|
|
|
|
func SecGroupV2StateRefreshFunc(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] Attempting to delete Security Group %s.\n", d.Id())
|
|
|
|
|
|
|
|
err := secgroups.Delete(computeClient, d.Id()).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := secgroups.Get(computeClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
|
|
|
err = CheckDeleted(d, err, "Security Group")
|
|
|
|
if err != nil {
|
|
|
|
return s, "", err
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Successfully deleted Security Group %s", d.Id())
|
|
|
|
return s, "DELETED", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Security Group %s still active.\n", d.Id())
|
|
|
|
return s, "ACTIVE", nil
|
|
|
|
}
|
|
|
|
}
|