2015-02-09 23:37:39 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-02-17 18:48:23 +01:00
|
|
|
"github.com/rackspace/gophercloud"
|
2015-02-09 23:37:39 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceNetworkingRouterV2() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceNetworkingRouterV2Create,
|
|
|
|
Read: resourceNetworkingRouterV2Read,
|
|
|
|
Update: resourceNetworkingRouterV2Update,
|
|
|
|
Delete: resourceNetworkingRouterV2Delete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-04-11 06:11:34 +02:00
|
|
|
DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
|
2015-02-09 23:37:39 +01:00
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"admin_state_up": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"external_gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-05-10 01:04:45 +02:00
|
|
|
Computed: true,
|
2015-02-09 23:37:39 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkingRouterV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts := routers.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
|
|
|
|
}
|
|
|
|
|
|
|
|
externalGateway := d.Get("external_gateway").(string)
|
|
|
|
if externalGateway != "" {
|
|
|
|
gatewayInfo := routers.GatewayInfo{
|
|
|
|
NetworkID: externalGateway,
|
|
|
|
}
|
|
|
|
createOpts.GatewayInfo = &gatewayInfo
|
|
|
|
}
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2015-02-09 23:37:39 +01:00
|
|
|
n, err := routers.Create(networkingClient, createOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack Neutron router: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] Router ID: %s", n.ID)
|
|
|
|
|
|
|
|
d.SetId(n.ID)
|
|
|
|
|
|
|
|
return resourceNetworkingRouterV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkingRouterV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := routers.Get(networkingClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
2015-02-17 18:48:23 +01:00
|
|
|
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-09 23:37:39 +01:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if httpError.Actual == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retreived Router %s: %+v", d.Id(), n)
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
d.Set("name", n.Name)
|
|
|
|
d.Set("admin_state_up", strconv.FormatBool(n.AdminStateUp))
|
|
|
|
d.Set("tenant_id", n.TenantID)
|
|
|
|
d.Set("external_gateway", n.GatewayInfo.NetworkID)
|
2015-02-09 23:37:39 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkingRouterV2Update(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateOpts routers.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating Router %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
|
|
|
_, err = routers.Update(networkingClient, d.Id(), updateOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceNetworkingRouterV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceNetworkingRouterV2Delete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = routers.Delete(networkingClient, d.Id()).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack Neutron Router: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|