Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding NetGateway Support #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the setter of the NetGateway property but didn't see any place where you use this property in the provider. Do you need just to store this property in the state or I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to create net gateway route, would this change set the route type when it invokes the api to create the route?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some places where it could be used, would these changes work, please suggest

}

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
28 changes: 22 additions & 6 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 @@ -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 @@ -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"
Expand All @@ -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 {
Expand Down