From 21a2d9c9123ab9cdcf0c00157918c881e198d15b Mon Sep 17 00:00:00 2001 From: LePhanFF <69166576+LePhanFF@users.noreply.github.com> Date: Tue, 15 Aug 2023 20:01:09 -0700 Subject: [PATCH 1/4] adding NetGateway Support --- internal/pritunl/route.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/pritunl/route.go b/internal/pritunl/route.go index 8a271c3..9a5077f 100644 --- a/internal/pritunl/route.go +++ b/internal/pritunl/route.go @@ -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 } From 390d0862c94733ac00c667a7a7f9c3a2bf054fbd Mon Sep 17 00:00:00 2001 From: LePhanFF <69166576+LePhanFF@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:16:17 -0700 Subject: [PATCH 2/4] update usage --- internal/provider/resource_server.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/provider/resource_server.go b/internal/provider/resource_server.go index 9113e3f..e7315d4 100644 --- a/internal/provider/resource_server.go +++ b/internal/provider/resource_server.go @@ -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, @@ -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 } @@ -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 } From 2bd7d9583ff950cbeba9e6109a697251bf598f57 Mon Sep 17 00:00:00 2001 From: LePhanFF <69166576+LePhanFF@users.noreply.github.com> Date: Wed, 23 Aug 2023 21:14:42 -0700 Subject: [PATCH 3/4] added unit testing --- examples/provider/provider.tf | 8 ++++++++ internal/provider/resource_server_test.go | 15 +++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index a993816..737df8f 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -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 + } + } diff --git a/internal/provider/resource_server_test.go b/internal/provider/resource_server_test.go index 3503f0b..4baba90 100644 --- a/internal/provider/resource_server_test.go +++ b/internal/provider/resource_server_test.go @@ -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) { @@ -465,7 +466,7 @@ 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" @@ -479,8 +480,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 { From 123e230fdb7536dda87160644eaaaa46816c138a Mon Sep 17 00:00:00 2001 From: LePhanFF <69166576+LePhanFF@users.noreply.github.com> Date: Wed, 23 Aug 2023 21:28:38 -0700 Subject: [PATCH 4/4] update --- internal/provider/resource_server_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/provider/resource_server_test.go b/internal/provider/resource_server_test.go index 4baba90..b457825 100644 --- a/internal/provider/resource_server_test.go +++ b/internal/provider/resource_server_test.go @@ -212,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{ @@ -220,20 +221,25 @@ 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) @@ -241,6 +247,9 @@ func TestGetServer_with_a_few_attached_routes(t *testing.T) { 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 },