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/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 } 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 } diff --git a/internal/provider/resource_server_test.go b/internal/provider/resource_server_test.go index 3503f0b..b457825 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) { @@ -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{ @@ -219,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) @@ -240,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 }, @@ -465,7 +475,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 +489,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 {