Skip to content

Commit

Permalink
static routing entries for routers
Browse files Browse the repository at this point in the history
  • Loading branch information
drebes committed Apr 19, 2016
1 parent 483d081 commit 65987a4
Show file tree
Hide file tree
Showing 5 changed files with 623 additions and 0 deletions.
5 changes: 5 additions & 0 deletions builtin/providers/openstack/provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package openstack

import (
"github.com/hashicorp/terraform/helper/mutexkv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// This is a global MutexKV for use within this plugin.
var osMutexKV = mutexkv.NewMutexKV()

// Provider returns a schema.Provider for OpenStack.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Expand Down Expand Up @@ -96,6 +100,7 @@ func Provider() terraform.ResourceProvider {
"openstack_networking_port_v2": resourceNetworkingPortV2(),
"openstack_networking_router_v2": resourceNetworkingRouterV2(),
"openstack_networking_router_interface_v2": resourceNetworkingRouterInterfaceV2(),
"openstack_networking_router_route_v2": resourceNetworkingRouterRouteV2(),
"openstack_objectstorage_container_v1": resourceObjectStorageContainerV1(),
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package openstack

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"

"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
)

func resourceNetworkingRouterRouteV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingRouterRouteV2Create,
Read: resourceNetworkingRouterRouteV2Read,
Delete: resourceNetworkingRouterRouteV2Delete,

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
},
"router_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_cidr": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"next_hop": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceNetworkingRouterRouteV2Create(d *schema.ResourceData, meta interface{}) error {

routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)

var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)

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, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
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)
}

var updateOpts routers.UpdateOpts
var routeExists bool = false

var rts []routers.Route = n.Routes
for _, r := range rts {

if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
routeExists = true
break
}
}

if !routeExists {

if destCidr != "" && nextHop != "" {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Adding route %s", r)
rts = append(rts, r)
}

updateOpts.Routes = rts

log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)

_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
}
d.SetId(fmt.Sprintf("%s-route-%s-%s", routerId, destCidr, nextHop))

} else {
log.Printf("[DEBUG] Router %s has route already", routerId)
}

return resourceNetworkingRouterRouteV2Read(d, meta)
}

func resourceNetworkingRouterRouteV2Read(d *schema.ResourceData, meta interface{}) error {

routerId := d.Get("router_id").(string)

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, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
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] Retrieved Router %s: %+v", routerId, n)

var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)

d.Set("next_hop", "")
d.Set("destination_cidr", "")

for _, r := range n.Routes {

if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
d.Set("destination_cidr", destCidr)
d.Set("next_hop", nextHop)
break
}
}

return nil
}

func resourceNetworkingRouterRouteV2Delete(d *schema.ResourceData, meta interface{}) error {

routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)

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, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
if !ok {
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}

if httpError.Actual == 404 {
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}

var updateOpts routers.UpdateOpts

var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)

var oldRts []routers.Route = n.Routes
var newRts []routers.Route

for _, r := range oldRts {

if r.DestinationCIDR != destCidr || r.NextHop != nextHop {
newRts = append(newRts, r)
}
}

if len(oldRts) != len(newRts) {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Deleting route %s", r)
updateOpts.Routes = newRts

log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)

_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
}
} else {
return fmt.Errorf("Route did not exist already")
}

return nil
}
Loading

0 comments on commit 65987a4

Please sign in to comment.