Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from OpenVPN/fix/add_route_update
Browse files Browse the repository at this point in the history
properly update routes
  • Loading branch information
vladimir-kozyrev authored Mar 24, 2023
2 parents 493af39 + e660759 commit 898ac8f
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
- main
pull_request:
branches: [ main ]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
Expand Down
26 changes: 18 additions & 8 deletions client/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ const (
)

func (c *Client) CreateRoute(networkId string, route Route) (*Route, error) {

type newRoute struct {
Description string `json:"description"`
Value string `json:"value"`
Type string `json:"type"`
}
routeToCreate := newRoute{
Description: route.Description,
Value: route.Value,
Type: route.Type,
}

var routeToCreate newRoute
routeToCreate.Description = "Managed by Terraform. " + route.Description
routeToCreate.Value = route.Value

routeJson, err := json.Marshal(routeToCreate)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/beta/networks/%s/routes", c.BaseURL, networkId), bytes.NewBuffer(routeJson))
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/api/beta/networks/%s/routes", c.BaseURL, networkId),
bytes.NewBuffer(routeJson),
)
if err != nil {
return nil, err
}
Expand All @@ -51,6 +55,8 @@ func (c *Client) CreateRoute(networkId string, route Route) (*Route, error) {
if err != nil {
return nil, err
}
// The API does not return the route Value, so we set it manually.
r.Value = routeToCreate.Value
return &r, nil
}

Expand Down Expand Up @@ -116,7 +122,11 @@ func (c *Client) UpdateRoute(networkId string, route Route) error {
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/api/beta/networks/%s/routes/%s", c.BaseURL, networkId, route.Id), bytes.NewBuffer(routeJson))
req, err := http.NewRequest(
http.MethodPut,
fmt.Sprintf("%s/api/beta/networks/%s/routes/%s", c.BaseURL, networkId, route.Id),
bytes.NewBuffer(routeJson),
)
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions openvpncloud/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interf
}
defaultRouteWithIdSlice := make([]map[string]interface{}, 1)
defaultRouteWithIdSlice[0] = map[string]interface{}{
"id": defaultRoute.Id,
"id": defaultRoute.Id,
"description": defaultRoute.Description,
"type": defaultRoute.Type,
"value": defaultRoute.Value,
}
d.Set("default_route", defaultRouteWithIdSlice)
return append(diags, diag.Diagnostic{
Expand Down Expand Up @@ -217,8 +220,9 @@ func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, m interfac
} else {
defaultRoute := []map[string]interface{}{
{
"id": configRoute["id"].(string),
"type": route.Type,
"id": configRoute["id"].(string),
"type": route.Type,
"description": route.Description,
},
}
if route.Type == client.RouteTypeIPV4 || route.Type == client.RouteTypeIPV6 {
Expand Down Expand Up @@ -294,7 +298,8 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
defaultRouteWithIdSlice := make([]map[string]interface{}, 1)
defaultRouteWithIdSlice[0] = map[string]interface{}{
"id": defaultRoute.Id,
"id": defaultRoute.Id,
"description": defaultRoute.Description,
}
err = d.Set("default_route", defaultRouteWithIdSlice)
if err != nil {
Expand Down
27 changes: 26 additions & 1 deletion openvpncloud/resource_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func resourceRoute() *schema.Resource {
return &schema.Resource{
Description: "Use `openvpncloud_route` to create a route on an OpenVPN Cloud network.",
CreateContext: resourceRouteCreate,
UpdateContext: resourceRouteCreate,
UpdateContext: resourceRouteUpdate,
ReadContext: resourceRouteRead,
DeleteContext: resourceRouteDelete,
Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -42,6 +42,7 @@ func resourceRoute() *schema.Resource {
"description": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
},
}
Expand Down Expand Up @@ -89,11 +90,35 @@ func resourceRouteRead(ctx context.Context, d *schema.ResourceData, m interface{
} else if r.Type == client.RouteTypeDomain {
d.Set("resourceRouteRead", r.Domain)
}
d.Set("description", r.Description)
d.Set("network_item_id", r.NetworkItemId)
}
return diags
}

func resourceRouteUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)
var diags diag.Diagnostics
if !d.HasChanges("description", "value") {
return diags
}

networkItemId := d.Get("network_item_id").(string)
_, description := d.GetChange("description")
_, value := d.GetChange("value")
r := client.Route{
Id: d.Id(),
Description: description.(string),
Value: value.(string),
}

err := c.UpdateRoute(networkItemId, r)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
return diags
}

func resourceRouteDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)
var diags diag.Diagnostics
Expand Down
132 changes: 132 additions & 0 deletions openvpncloud/resource_route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package openvpncloud

import (
"errors"
"fmt"
"testing"

"github.com/OpenVPN/terraform-provider-openvpn-cloud/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"
)

func TestAccOpenvpncloudRoute_basic(t *testing.T) {
rn := "openvpncloud_route.test"
ip, err := acctest.RandIpAddress("10.0.0.0/8")
require.NoError(t, err)
route := client.Route{
Description: "test" + acctest.RandString(10),
Type: client.RouteTypeIPV4,
Value: ip + "/32",
}
routeChanged := route
routeChanged.Description = acctest.RandStringFromCharSet(10, alphabet)
networkRandString := "test" + acctest.RandString(10)
var routeId string

check := func(r client.Route) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
testAccCheckOpenvpncloudRouteExists(rn, &routeId),
resource.TestCheckResourceAttr(rn, "description", r.Description),
resource.TestCheckResourceAttr(rn, "type", r.Type),
resource.TestCheckResourceAttr(rn, "value", r.Value),
)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckOpenvpncloudRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccOpenvpncloudRouteConfig(route, networkRandString),
Check: check(route),
},
{
Config: testAccOpenvpncloudRouteConfig(routeChanged, networkRandString),
Check: check(routeChanged),
},
{
ResourceName: rn,
ImportState: true,
ImportStateIdFunc: testAccOpenvpncloudRouteImportStateIdFunc(rn),
ImportStateVerify: true,
},
},
})
}

func testAccCheckOpenvpncloudRouteDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*client.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "openvpncloud_route" {
continue
}
routeId := rs.Primary.ID
r, err := client.GetRouteById(routeId)
if err == nil {
return err
}
if r != nil {
return errors.New("route still exists")
}
}
return nil
}

func testAccCheckOpenvpncloudRouteExists(n string, routeID *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 errors.New("no ID is set")
}

client := testAccProvider.Meta().(*client.Client)
_, err := client.GetRouteById(rs.Primary.ID)
if err != nil {
return err
}
return nil
}
}

func testAccOpenvpncloudRouteImportStateIdFunc(n string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return "", fmt.Errorf("not found: %s", n)
}
return rs.Primary.ID, nil
}
}

func testAccOpenvpncloudRouteConfig(r client.Route, networkRandStr string) string {
return fmt.Sprintf(`
provider "openvpncloud" {
base_url = "https://%[1]s.api.openvpn.com"
}
resource "openvpncloud_network" "test" {
name = "%[5]s"
default_connector {
name = "%[5]s"
vpn_region_id = "fi-hel"
}
default_route {
value = "10.1.2.0/24"
type = "IP_V4"
}
}
resource "openvpncloud_route" "test" {
network_item_id = openvpncloud_network.test.id
description = "%[2]s"
value = "%[3]s"
type = "%[4]s"
}
`, testCloudID, r.Description, r.Value, r.Type, networkRandStr)
}

0 comments on commit 898ac8f

Please sign in to comment.