Add router interface resource
This commit is contained in:
parent
32d0e36709
commit
e9abf04e4b
|
@ -59,18 +59,19 @@ func Provider() terraform.ResourceProvider {
|
|||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"openstack_blockstorage_volume_v1": resourceBlockStorageVolumeV1(),
|
||||
"openstack_compute_instance_v2": resourceComputeInstanceV2(),
|
||||
"openstack_compute_keypair_v2": resourceComputeKeypairV2(),
|
||||
"openstack_compute_secgroup_v2": resourceComputeSecGroupV2(),
|
||||
"openstack_lb_monitor_v1": resourceLBMonitorV1(),
|
||||
"openstack_lb_pool_v1": resourceLBPoolV1(),
|
||||
"openstack_lb_vip_v1": resourceLBVipV1(),
|
||||
"openstack_networking_network_v2": resourceNetworkingNetworkV2(),
|
||||
"openstack_networking_subnet_v2": resourceNetworkingSubnetV2(),
|
||||
"openstack_networking_floatingip_v2": resourceNetworkingFloatingIPV2(),
|
||||
"openstack_networking_router_v2": resourceNetworkingRouterV2(),
|
||||
"openstack_objectstorage_container_v1": resourceObjectStorageContainerV1(),
|
||||
"openstack_blockstorage_volume_v1": resourceBlockStorageVolumeV1(),
|
||||
"openstack_compute_instance_v2": resourceComputeInstanceV2(),
|
||||
"openstack_compute_keypair_v2": resourceComputeKeypairV2(),
|
||||
"openstack_compute_secgroup_v2": resourceComputeSecGroupV2(),
|
||||
"openstack_lb_monitor_v1": resourceLBMonitorV1(),
|
||||
"openstack_lb_pool_v1": resourceLBPoolV1(),
|
||||
"openstack_lb_vip_v1": resourceLBVipV1(),
|
||||
"openstack_networking_network_v2": resourceNetworkingNetworkV2(),
|
||||
"openstack_networking_subnet_v2": resourceNetworkingSubnetV2(),
|
||||
"openstack_networking_floatingip_v2": resourceNetworkingFloatingIPV2(),
|
||||
"openstack_networking_router_v2": resourceNetworkingRouterV2(),
|
||||
"openstack_networking_router_interface_v2": resourceNetworkingRouterInterfaceV2(),
|
||||
"openstack_objectstorage_container_v1": resourceObjectStorageContainerV1(),
|
||||
},
|
||||
|
||||
ConfigureFunc: configureProvider,
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
package openstack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/racker/perigee"
|
||||
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
|
||||
"github.com/rackspace/gophercloud/openstack/networking/v2/ports"
|
||||
)
|
||||
|
||||
func resourceNetworkingRouterInterfaceV2() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceNetworkingRouterInterfaceV2Create,
|
||||
Read: resourceNetworkingRouterInterfaceV2Read,
|
||||
Delete: resourceNetworkingRouterInterfaceV2Delete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"region": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
|
||||
},
|
||||
"router_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"subnet_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceNetworkingRouterInterfaceV2Create(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.InterfaceOpts{
|
||||
SubnetID: d.Get("subnet_id").(string),
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Requesting router interface creation")
|
||||
n, err := routers.AddInterface(networkingClient, d.Get("router_id").(string), createOpts).Extract()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating OpenStack Neutron router interface: %s", err)
|
||||
}
|
||||
log.Printf("[INFO] Router interface Port ID: %s", n.PortID)
|
||||
|
||||
d.SetId(n.PortID)
|
||||
|
||||
return resourceNetworkingRouterInterfaceV2Read(d, meta)
|
||||
}
|
||||
|
||||
func resourceNetworkingRouterInterfaceV2Read(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 := ports.Get(networkingClient, d.Id()).Extract()
|
||||
if err != nil {
|
||||
httpError, ok := err.(*perigee.UnexpectedResponseCodeError)
|
||||
if !ok {
|
||||
return fmt.Errorf("Error retrieving OpenStack Neutron Router Interface: %s", err)
|
||||
}
|
||||
|
||||
if httpError.Actual == 404 {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("Error retrieving OpenStack Neutron Router Interface: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Retreived Router Interface %s: %+v", d.Id(), n)
|
||||
|
||||
d.Set("region", d.Get("region").(string))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceNetworkingRouterInterfaceV2Delete(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)
|
||||
}
|
||||
|
||||
removeOpts := routers.InterfaceOpts{
|
||||
SubnetID: d.Get("subnet_id").(string),
|
||||
}
|
||||
|
||||
_, err = routers.RemoveInterface(networkingClient, d.Get("router_id").(string), removeOpts).Extract()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error deleting OpenStack Neutron Router Interface: %s", err)
|
||||
}
|
||||
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package openstack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
|
||||
"github.com/rackspace/gophercloud/openstack/networking/v2/ports"
|
||||
)
|
||||
|
||||
func TestAccNetworkingV2RouterInterface_basic(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckNetworkingV2RouterInterfaceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccNetworkingV2RouterInterface_basic,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckNetworkingV2RouterInterfaceExists(t, "openstack_networking_router_interface_v2.int_1"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckNetworkingV2RouterInterfaceDestroy(s *terraform.State) error {
|
||||
config := testAccProvider.Meta().(*Config)
|
||||
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("(testAccCheckNetworkingV2RouterInterfaceDestroy) Error creating OpenStack networking client: %s", err)
|
||||
}
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "openstack_networking_router_interface_v2" {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := ports.Get(networkingClient, rs.Primary.ID).Extract()
|
||||
if err == nil {
|
||||
return fmt.Errorf("Router interface still exists")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckNetworkingV2RouterInterfaceExists(t *testing.T, n string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found: %s", n)
|
||||
}
|
||||
|
||||
if rs.Primary.ID == "" {
|
||||
return fmt.Errorf("No ID is set")
|
||||
}
|
||||
|
||||
config := testAccProvider.Meta().(*Config)
|
||||
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("(testAccCheckNetworkingV2RouterInterfaceExists) Error creating OpenStack networking client: %s", err)
|
||||
}
|
||||
|
||||
found, err := ports.Get(networkingClient, rs.Primary.ID).Extract()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if found.ID != rs.Primary.ID {
|
||||
return fmt.Errorf("Router interface not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var testAccNetworkingV2RouterInterface_basic = fmt.Sprintf(`
|
||||
resource "openstack_networking_router_v2" "router_1" {
|
||||
name = "router_1"
|
||||
region = "%s"
|
||||
admin_state_up = "true"
|
||||
}
|
||||
|
||||
resource "openstack_networking_router_interface_v2" "int_1" {
|
||||
subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
|
||||
router_id = "${openstack_networking_router_v2.router_1.id}"
|
||||
region = "%s"
|
||||
}
|
||||
|
||||
resource "openstack_networking_network_v2" "network_1" {
|
||||
region = "%s"
|
||||
name = "network_1"
|
||||
admin_state_up = "true"
|
||||
}
|
||||
|
||||
resource "openstack_networking_subnet_v2" "subnet_1" {
|
||||
region = "%s"
|
||||
network_id = "${openstack_networking_network_v2.network_1.id}"
|
||||
cidr = "192.168.199.0/24"
|
||||
ip_version = 4
|
||||
}`, OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
|
Loading…
Reference in New Issue