-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cloudstack_security_group resource
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
builtin/providers/cloudstack/resource_cloudstack_security_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package cloudstack | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/xanzy/go-cloudstack/cloudstack" | ||
) | ||
|
||
func resourceCloudStackSecurityGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudStackSecurityGroupCreate, | ||
Read: resourceCloudStackSecurityGroupRead, | ||
Delete: resourceCloudStackSecurityGroupDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudStackSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||
cs := meta.(*cloudstack.CloudStackClient) | ||
|
||
name := d.Get("name").(string) | ||
|
||
// Create a new parameter struct | ||
p := cs.SecurityGroup.NewCreateSecurityGroupParams(name) | ||
|
||
// Set the description | ||
if description, ok := d.GetOk("description"); ok { | ||
p.SetDescription(description.(string)) | ||
} else { | ||
p.SetDescription(name) | ||
} | ||
|
||
// If there is a project supplied, we retrieve and set the project id | ||
if err := setProjectid(p, cs, d); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating security group %s", name) | ||
r, err := cs.SecurityGroup.CreateSecurityGroup(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Security group %s successfully created with ID: %s", name, r.SecurityGroup.Id) | ||
d.SetId(r.SecurityGroup.Id) | ||
|
||
return resourceCloudStackSecurityGroupRead(d, meta) | ||
} | ||
|
||
func resourceCloudStackSecurityGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
cs := meta.(*cloudstack.CloudStackClient) | ||
|
||
log.Printf("[DEBUG] Retrieving security group %s (ID=%s)", d.Get("name").(string), d.Id()) | ||
|
||
// Get the security group details | ||
ag, count, err := cs.SecurityGroup.GetSecurityGroupByID( | ||
d.Id(), | ||
cloudstack.WithProject(d.Get("project").(string)), | ||
) | ||
if err != nil { | ||
if count == 0 { | ||
log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string)) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
log.Printf("[DEBUG] Found %v groups matching", count) | ||
|
||
return err | ||
} | ||
|
||
// Update the config | ||
d.Set("name", ag.Name) | ||
d.Set("description", ag.Description) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudStackSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
cs := meta.(*cloudstack.CloudStackClient) | ||
|
||
// Create a new parameter struct | ||
p := cs.SecurityGroup.NewDeleteSecurityGroupParams() | ||
p.SetId(d.Id()) | ||
|
||
// If there is a project supplied, we retrieve and set the project id | ||
if err := setProjectid(p, cs, d); err != nil { | ||
return err | ||
} | ||
|
||
// Delete the security group | ||
_, err := cs.SecurityGroup.DeleteSecurityGroup(p) | ||
if err != nil { | ||
// This is a very poor way to be told the ID does no longer exist :( | ||
if strings.Contains(err.Error(), fmt.Sprintf( | ||
"Invalid parameter id value=%s due to incorrect long value format, "+ | ||
"or entity does not exist", d.Id())) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error deleting security group: %s", err) | ||
} | ||
|
||
return nil | ||
} |