terraform/builtin/providers/openstack/resource_openstack_compute_...

126 lines
3.4 KiB
Go
Raw Normal View History

2015-01-08 18:47:10 +01:00
package openstack
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
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{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
2015-01-27 02:28:01 +01:00
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
},
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-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)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2015-01-08 18:47:10 +01:00
createOpts := secgroups.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
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-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)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2015-01-08 18:47:10 +01:00
sg, err := secgroups.Get(computeClient, d.Id()).Extract()
2015-01-08 18:47:10 +01:00
if err != nil {
return fmt.Errorf("Error retrieving OpenStack security group: %s", err)
}
2015-01-26 19:58:19 +01:00
d.Set("region", d.Get("region").(string))
2015-01-08 18:47:10 +01:00
d.Set("name", sg.Name)
d.Set("description", sg.Description)
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)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2015-01-08 18:47:10 +01:00
updateOpts := secgroups.UpdateOpts{
2015-01-26 23:45:05 +01:00
Name: d.Get("name").(string),
Description: d.Get("description").(string),
2015-01-08 18:47:10 +01:00
}
log.Printf("[DEBUG] Updating Security Group (%s) with options: %+v", d.Id(), updateOpts)
2015-01-08 18:47:10 +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-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)
computeClient, err := openstack.NewComputeV2(config.osClient, gophercloud.EndpointOpts{
Region: d.Get("region").(string),
})
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2015-01-08 18:47:10 +01:00
err = secgroups.Delete(computeClient, d.Id()).ExtractErr()
2015-01-08 18:47:10 +01:00
if err != nil {
return fmt.Errorf("Error deleting OpenStack security group: %s", err)
}
d.SetId("")
return nil
}