Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable setting main route table #437

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Provider() *schema.Provider {
"outscale_ca": resourceOutscaleOAPICa(),
"outscale_api_access_rule": resourceOutscaleOAPIApiAccessRule(),
"outscale_api_access_policy": resourceOutscaleOAPIApiAccessPolicy(),
"outscale_main_route_table_link": resourceLinkMainRouteTable(),
},
DataSourcesMap: map[string]*schema.Resource{
"outscale_vm": dataSourceOutscaleOAPIVM(),
Expand Down
182 changes: 182 additions & 0 deletions outscale/resource_outscale_main_route_table_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package outscale

import (
"context"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func resourceLinkMainRouteTable() *schema.Resource {
return &schema.Resource{
Create: resourceLinkMainRouteTableCreate,
Read: resourceLinkMainRouteTableRead,
Delete: resourceLinkMainRouteTableDelete,
Schema: map[string]*schema.Schema{
"net_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"link_route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"default_route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"main": {
Type: schema.TypeBool,
Computed: true,
},
"request_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceLinkMainRouteTableCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI
netID := d.Get("net_id").(string)

routeTable, err := readMainLinkRouteTable(meta.(*OutscaleClient), netID)
if err != nil {
return err
}
linkRouteTable := routeTable.GetLinkRouteTables()
oldLinkRouteTableId := linkRouteTable[0].GetLinkRouteTableId()
defaultRouteTableId := linkRouteTable[0].GetRouteTableId()

updateRequest := oscgo.UpdateRouteTableLinkRequest{
RouteTableId: d.Get("route_table_id").(string),
}
updateRequest.SetLinkRouteTableId(oldLinkRouteTableId)
resp := oscgo.UpdateRouteTableLinkResponse{}
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.RouteTableApi.UpdateRouteTableLink(
context.Background()).UpdateRouteTableLinkRequest(updateRequest).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})
if err != nil {
return err
}

if err := d.Set("default_route_table_id", defaultRouteTableId); err != nil {
return err
}
d.SetId(resp.GetLinkRouteTableId())

return resourceOutscaleOAPILinkRouteTableRead(d, meta)
}

func resourceLinkMainRouteTableRead(d *schema.ResourceData, meta interface{}) error {
netID := d.Get("net_id").(string)
routeTable, err := readMainLinkRouteTable(meta.(*OutscaleClient), netID)
if err != nil {
return err
}
linkRTable := routeTable.GetLinkRouteTables()
if linkRTable == nil {
utils.LogManuallyDeleted("RouteTableLink", d.Id())
d.SetId("")
return nil
}

if err := d.Set("net_id", linkRTable[0].GetNetId()); err != nil {
return err
}
if linkRTable[0].GetSubnetId() != "" {
if err := d.Set("subnet_id", linkRTable[0].GetSubnetId()); err != nil {
return err
}
}
if err := d.Set("link_route_table_id", linkRTable[0].GetLinkRouteTableId()); err != nil {
return err
}
if err := d.Set("main", linkRTable[0].GetMain()); err != nil {
return err
}
if err := d.Set("route_table_id", linkRTable[0].GetRouteTableId()); err != nil {
return err
}

return nil
}

func resourceLinkMainRouteTableDelete(d *schema.ResourceData, meta interface{}) error {
var err error
conn := meta.(*OutscaleClient).OSCAPI

updateRequest := oscgo.UpdateRouteTableLinkRequest{
LinkRouteTableId: d.Get("link_route_table_id").(string),
RouteTableId: d.Get("default_route_table_id").(string),
}

err = resource.Retry(5*time.Minute, func() *resource.RetryError {
_, httpResp, err := conn.RouteTableApi.UpdateRouteTableLink(
context.Background()).UpdateRouteTableLinkRequest(updateRequest).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
return nil
})

if err != nil {
return fmt.Errorf("Error deleting link route table: %s", err)
}

return nil
}

func readMainLinkRouteTable(meta *OutscaleClient, netID string) (oscgo.RouteTable, error) {
conn := meta.OSCAPI

var resp oscgo.ReadRouteTablesResponse
var err error
var routeTable oscgo.RouteTable

rtbRequest := oscgo.ReadRouteTablesRequest{}
rtbRequest.Filters = &oscgo.FiltersRouteTable{
NetIds: &[]string{netID},
LinkRouteTableMain: &[]bool{true}[0],
}
err = resource.Retry(15*time.Minute, func() *resource.RetryError {
var err error
rp, httpResp, err := conn.RouteTableApi.ReadRouteTables(
context.Background()).ReadRouteTablesRequest(rtbRequest).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return routeTable, err
}
if len(resp.GetRouteTables()) == 0 {
return routeTable, nil
}

return resp.GetRouteTables()[0], nil
}
144 changes: 144 additions & 0 deletions outscale/resource_outscale_main_route_table_link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package outscale

import (
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)

func TestAccNet_WithLinkMainRouteTable_basic(t *testing.T) {
t.Parallel()
var v oscgo.RouteTable
resourceName := "outscale_main_route_table_link.main"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLinkMainRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccLinkMainRouteTableConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckLinkMainRouteTableExists(
resourceName, &v),
resource.TestCheckResourceAttrSet(resourceName, "link_route_table_id"),
resource.TestCheckResourceAttrSet(resourceName, "main"),
resource.TestCheckResourceAttr(resourceName, "main", "true"),
),
},
},
})
}

func testAccCheckLinkMainRouteTableDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI

for _, rs := range s.RootModule().Resources {
if rs.Type != "outscale_main_route_table_link" {
continue
}
params := oscgo.ReadRouteTablesRequest{
Filters: &oscgo.FiltersRouteTable{
RouteTableIds: &[]string{rs.Primary.Attributes["route_table_id"]},
},
}
var resp oscgo.ReadRouteTablesResponse
var err error
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.RouteTableApi.ReadRouteTables(context.Background()).ReadRouteTablesRequest(params).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
if strings.Contains(fmt.Sprint(err), "InvalidRouteTableID.NotFound") {
return nil
}
return err
}

if len(resp.GetRouteTables()) > 0 {
return fmt.Errorf(
"RouteTable: %s has LinkRouteTables", resp.GetRouteTables()[0].GetRouteTableId())
}
}
return nil
}

func testAccCheckLinkMainRouteTableExists(n string, v *oscgo.RouteTable) 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")
}

conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI

params := oscgo.ReadRouteTablesRequest{
Filters: &oscgo.FiltersRouteTable{
RouteTableIds: &[]string{rs.Primary.Attributes["route_table_id"]},
},
}
var resp oscgo.ReadRouteTablesResponse
var err error
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.RouteTableApi.ReadRouteTables(context.Background()).ReadRouteTablesRequest(params).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}
if len(resp.GetRouteTables()) == 0 {
return fmt.Errorf("RouteTable not found")
}

*v = resp.GetRouteTables()[0]
if len(v.GetLinkRouteTables()) == 0 {
return fmt.Errorf("RouteTable: %s has no LinkRouteTables", v.GetRouteTableId())
}

return nil
}
}

const testAccLinkMainRouteTableConfig = `
resource "outscale_net" "main_net" {
ip_range = "10.1.0.0/16"
tags {
key = "Name"
value = "testacc-mainRTable-link"
}
}

resource "outscale_subnet" "mainSubnet" {
net_id = outscale_net.main_net.net_id
ip_range = "10.1.1.0/24"
}

resource "outscale_route_table" "mainRTable" {
net_id = outscale_net.main_net.net_id
}

resource "outscale_main_route_table_link" "main" {
net_id = outscale_net.main_net.net_id
route_table_id = outscale_route_table.mainRTable.id
}
`
26 changes: 6 additions & 20 deletions outscale/resource_outscale_route_table_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -57,16 +55,13 @@ func resourceOutscaleOAPILinkRouteTable() *schema.Resource {

func resourceOutscaleOAPILinkRouteTableCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI
subnetID := d.Get("subnet_id").(string)
routeTableID := d.Get("route_table_id").(string)
log.Printf("[INFO] Creating route table link: %s => %s", subnetID, routeTableID)
linkRouteTableOpts := oscgo.LinkRouteTableRequest{
RouteTableId: routeTableID,
SubnetId: subnetID,
}

var resp oscgo.LinkRouteTableResponse
var err error

linkRouteTableOpts := oscgo.LinkRouteTableRequest{
RouteTableId: d.Get("route_table_id").(string),
SubnetId: d.Get("subnet_id").(string),
}
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
var err error
rp, httpResp, err := conn.RouteTableApi.LinkRouteTable(context.Background()).LinkRouteTableRequest(linkRouteTableOpts).Execute()
Expand Down Expand Up @@ -120,25 +115,16 @@ func resourceOutscaleOAPILinkRouteTableRead(d *schema.ResourceData, meta interfa
func resourceOutscaleOAPILinkRouteTableDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

log.Printf("[INFO] Deleting link route table: %s", d.Id())

var err error
var statusCode int
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, httpResp, err := conn.RouteTableApi.UnlinkRouteTable(context.Background()).UnlinkRouteTableRequest(oscgo.UnlinkRouteTableRequest{
LinkRouteTableId: d.Id(),
}).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
statusCode = httpResp.StatusCode
return nil
})

if err != nil {
if statusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("Error deleting link route table: %s", err)
}

Expand Down
Loading
Loading