Skip to content

Commit

Permalink
Feature/issue 37 route net gateway support (#39)
Browse files Browse the repository at this point in the history
* adding NetGateway Support

* update usage

* added unit testing

* update

* added comment for net_gateway test

* Update resource_server_test.go

update

* adjust test test

* update comment
  • Loading branch information
LePhanFF authored Aug 26, 2023
1 parent d1abdac commit 58108f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
8 changes: 8 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ resource "pritunl_server" "test" {
comment = "Private network #2"
nat = false
}

route {
network = "10.3.0.0/32"
comment = "Private network #2"
nat = false
net_gateway = true
}

}
3 changes: 3 additions & 0 deletions internal/pritunl/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func ConvertMapToRoute(data map[string]interface{}) Route {
if v, ok := data["nat"]; ok {
route.Nat = v.(bool)
}
if v, ok := data["net_gateway"]; ok {
route.NetGateway = v.(bool)
}

return route
}
10 changes: 9 additions & 1 deletion internal/provider/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ func resourceServer() *schema.Resource {
Description: "NAT vpn traffic destined to this network",
Computed: true,
},
"net_gateway": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Description: "Net Gateway vpn traffic destined to this network",
Computed: true,
},
},
},
Required: false,
Expand Down Expand Up @@ -1036,6 +1043,7 @@ func flattenRoutesData(routesList []pritunl.Route) []interface{} {

routeMap["network"] = route.Network
routeMap["nat"] = route.Nat
routeMap["net_gateway"] = route.NetGateway
if route.Comment != "" {
routeMap["comment"] = route.Comment
}
Expand All @@ -1061,7 +1069,7 @@ func matchRoutesWithSchema(routes []pritunl.Route, declaredRoutes []interface{})
declaredRouteMap := declaredRoute.(map[string]interface{})

for key, route := range routesMap {
if route.Network != declaredRouteMap["network"] || route.Nat != declaredRouteMap["nat"] {
if route.Network != declaredRouteMap["network"] || route.Nat != declaredRouteMap["nat"] || route.NetGateway != declaredRouteMap["net_gateway"] {
continue
}

Expand Down
33 changes: 26 additions & 7 deletions internal/provider/resource_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"regexp"
"testing"

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

func TestGetServer_basic(t *testing.T) {
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestGetServer_with_a_few_attached_organizations(t *testing.T) {
}

if _, ok := expectedOrganizationIds[attachedOrganization2Id]; !ok {
return fmt.Errorf("attached organization_id %s doesn't contain in expected organizations list", attachedOrganization1Id)
return fmt.Errorf("attached organization_id %s doesn't contain in expected organizations list", attachedOrganization2Id)
}

return nil
Expand Down Expand Up @@ -211,6 +212,7 @@ func TestGetServer_with_a_few_attached_routes(t *testing.T) {

expectedRoute1Network := "10.2.0.0/24"
expectedRoute2Network := "10.3.0.0/24"
expectedRoute3Network := "10.4.0.0/32"
expectedRouteComment := "tfacc-route"

resource.Test(t, resource.TestCase{
Expand All @@ -219,27 +221,35 @@ func TestGetServer_with_a_few_attached_routes(t *testing.T) {
CheckDestroy: testGetServerDestroy,
Steps: []resource.TestStep{
{
Config: testGetServerSimpleConfigWithAFewAttachedRoutes("tfacc-server1", expectedRoute1Network, expectedRoute2Network),
Config: testGetServerSimpleConfigWithAFewAttachedRoutes("tfacc-server1", expectedRoute1Network, expectedRoute2Network, expectedRoute3Network),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("pritunl_server.test", "name", "tfacc-server1"),

func(s *terraform.State) error {
routeNetwork1 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.0.network"]
routeNetwork2 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.1.network"]
routeNetwork3 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.2.network"]
routeComment1 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.0.comment"]
routeComment2 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.1.comment"]
routeComment3 := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["route.2.comment"]
if routeNetwork1 != expectedRoute1Network {
return fmt.Errorf("first route network is invalid: expected is %s, but actual is %s", expectedRoute1Network, routeNetwork1)
}
if routeNetwork2 != expectedRoute2Network {
return fmt.Errorf("second route network is invalid: expected is %s, but actual is %s", expectedRoute2Network, routeNetwork1)
return fmt.Errorf("second route network is invalid: expected is %s, but actual is %s", expectedRoute2Network, routeNetwork2)
}
if routeNetwork3 != expectedRoute3Network {
return fmt.Errorf("second route network is invalid: expected is %s, but actual is %s", expectedRoute3Network, routeNetwork3)
}
if routeComment1 != expectedRouteComment {
return fmt.Errorf("first route comment is invalid: expected is %s, but actual is %s", expectedRouteComment, routeComment1)
}
if routeComment2 != expectedRouteComment {
return fmt.Errorf("second route comment is invalid: expected is %s, but actual is %s", expectedRouteComment, routeComment2)
}
if routeComment3 != expectedRouteComment {
return fmt.Errorf(" route comment is invalid: expected is %s, but actual is %s", expectedRouteComment, routeComment3)
}
return nil
},

Expand Down Expand Up @@ -434,6 +444,7 @@ resource "pritunl_server" "test" {
}

func testGetServerSimpleConfigWithAFewAttachedOrganization(name, organization1Name, organization2Name string) string {
//testing net-gateway
return fmt.Sprintf(`
resource "pritunl_organization" "test" {
name = "%[2]s"
Expand Down Expand Up @@ -465,7 +476,9 @@ resource "pritunl_server" "test" {
}
`, name, route)
}
func testGetServerSimpleConfigWithAFewAttachedRoutes(name, route1, route2 string) string {

func testGetServerSimpleConfigWithAFewAttachedRoutes(name, route1, route2, route3 string) string {

return fmt.Sprintf(`
resource "pritunl_server" "test" {
name = "%[1]s"
Expand All @@ -479,8 +492,14 @@ resource "pritunl_server" "test" {
network = "%[3]s"
comment = "tfacc-route"
}
route {
network = "%[4]s"
comment = "tfacc-route"
net_gateway = true
}
}
`, name, route1, route2)
`, name, route1, route2, route3)
}

func testGetServerConfig(name, network string, port int) string {
Expand Down

0 comments on commit 58108f6

Please sign in to comment.