2015-01-10 23:02:19 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-01-26 18:54:07 +01:00
|
|
|
"github.com/rackspace/gophercloud"
|
|
|
|
"github.com/rackspace/gophercloud/openstack"
|
2015-01-10 23:02:19 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceNetworkingNetworkV2() *schema.Resource {
|
2015-01-10 23:02:19 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceNetworkingNetworkV2Create,
|
|
|
|
Read: resourceNetworkingNetworkV2Read,
|
|
|
|
Update: resourceNetworkingNetworkV2Update,
|
|
|
|
Delete: resourceNetworkingNetworkV2Delete,
|
2015-01-10 23:02:19 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-01-26 18:54:07 +01:00
|
|
|
"region": &schema.Schema{
|
2015-01-26 23:45:05 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-01-26 18:54:07 +01:00
|
|
|
},
|
2015-01-10 23:02:19 +01:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"admin_state_up": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"shared": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-01-14 19:56:52 +01:00
|
|
|
ForceNew: true,
|
2015-01-10 23:02:19 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-10 23:02:19 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-26 18:54:07 +01:00
|
|
|
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-10 23:02:19 +01:00
|
|
|
|
|
|
|
createOpts := networks.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
asuRaw := d.Get("admin_state_up").(string)
|
|
|
|
if asuRaw != "" {
|
|
|
|
asu, err := strconv.ParseBool(asuRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("admin_state_up, if provided, must be either 'true' or 'false'")
|
|
|
|
}
|
|
|
|
createOpts.AdminStateUp = &asu
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedRaw := d.Get("shared").(string)
|
|
|
|
if sharedRaw != "" {
|
|
|
|
shared, err := strconv.ParseBool(sharedRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("shared, if provided, must be either 'true' or 'false': %v", err)
|
|
|
|
}
|
|
|
|
createOpts.Shared = &shared
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Requesting network creation")
|
2015-01-26 18:54:07 +01:00
|
|
|
n, err := networks.Create(networkingClient, createOpts).Extract()
|
2015-01-10 23:02:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack Neutron network: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] Network ID: %s", n.ID)
|
|
|
|
|
|
|
|
d.SetId(n.ID)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceNetworkingNetworkV2Read(d, meta)
|
2015-01-10 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-10 23:02:19 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-26 18:54:07 +01:00
|
|
|
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-10 23:02:19 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
n, err := networks.Get(networkingClient, d.Id()).Extract()
|
2015-01-10 23:02:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving OpenStack Neutron Network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retreived Network %s: %+v", d.Id(), n)
|
|
|
|
|
2015-01-26 19:58:19 +01:00
|
|
|
d.Set("region", d.Get("region").(string))
|
|
|
|
|
2015-01-10 23:02:19 +01:00
|
|
|
if _, exists := d.GetOk("name"); exists {
|
|
|
|
if d.HasChange("name") {
|
|
|
|
d.Set("name", n.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.Set("name", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := d.GetOk("admin_state_up"); exists {
|
|
|
|
if d.HasChange("admin_state_up") {
|
|
|
|
d.Set("admin_state_up", strconv.FormatBool(n.AdminStateUp))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.Set("admin_state_up", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := d.GetOk("shared"); exists {
|
|
|
|
if d.HasChange("shared") {
|
|
|
|
d.Set("shared", strconv.FormatBool(n.Shared))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.Set("shared", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := d.GetOk("tenant_id"); exists {
|
|
|
|
if d.HasChange("tenant_id") {
|
|
|
|
d.Set("tenant_id", n.TenantID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.Set("tenant_id", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceNetworkingNetworkV2Update(d *schema.ResourceData, meta interface{}) error {
|
2015-01-10 23:02:19 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-26 18:54:07 +01:00
|
|
|
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-10 23:02:19 +01:00
|
|
|
|
|
|
|
var updateOpts networks.UpdateOpts
|
|
|
|
if d.HasChange("name") {
|
|
|
|
updateOpts.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
if d.HasChange("admin_state_up") {
|
|
|
|
asuRaw := d.Get("admin_state_up").(string)
|
|
|
|
if asuRaw != "" {
|
|
|
|
asu, err := strconv.ParseBool(asuRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("admin_state_up, if provided, must be either 'true' or 'false'")
|
|
|
|
}
|
|
|
|
updateOpts.AdminStateUp = &asu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.HasChange("shared") {
|
|
|
|
sharedRaw := d.Get("shared").(string)
|
|
|
|
if sharedRaw != "" {
|
|
|
|
shared, err := strconv.ParseBool(sharedRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("shared, if provided, must be either 'true' or 'false': %v", err)
|
|
|
|
}
|
|
|
|
updateOpts.Shared = &shared
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:56:52 +01:00
|
|
|
log.Printf("[DEBUG] Updating Network %s with options: %+v", d.Id(), updateOpts)
|
2015-01-10 23:02:19 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err = networks.Update(networkingClient, d.Id(), updateOpts).Extract()
|
2015-01-14 19:56:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack Neutron Network: %s", err)
|
2015-01-10 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceNetworkingNetworkV2Read(d, meta)
|
2015-01-10 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceNetworkingNetworkV2Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-10 23:02:19 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-26 18:54:07 +01:00
|
|
|
networkingClient, err := openstack.NewNetworkV2(config.osClient, gophercloud.EndpointOpts{
|
|
|
|
Region: d.Get("region").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-10 23:02:19 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
err = networks.Delete(networkingClient, d.Id()).ExtractErr()
|
2015-01-10 23:02:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack Neutron Network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|