Skip to content

Commit

Permalink
Enable to set a route table as Main
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Mar 15, 2024
1 parent 01be441 commit e9e1f3f
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 0 deletions.
1 change: 1 addition & 0 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Provider() terraform.ResourceProvider {
"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
183 changes: 183 additions & 0 deletions outscale/resource_outscale_main_route_table_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package outscale

import (
"context"
"fmt"
"time"

oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/terraform-providers/terraform-provider-outscale/utils"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

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
}
145 changes: 145 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,145 @@
package outscale

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

oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/terraform-providers/terraform-provider-outscale/utils"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

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
}
`

0 comments on commit e9e1f3f

Please sign in to comment.