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

tailscale: use V2 client for acl data source #422

Merged
merged 2 commits into from
Aug 23, 2024
Merged
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
4 changes: 2 additions & 2 deletions tailscale/data_source_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func dataSourceACL() *schema.Resource {
}

func dataSourceACLRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V1
client := m.(*Clients).V2

acl, err := client.RawACL(ctx)
acl, err := client.PolicyFile().Raw(ctx)
if err != nil {
return diagnosticsError(err, "Failed to fetch ACL")
}
Expand Down
48 changes: 48 additions & 0 deletions tailscale/data_source_acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tailscale_test

import (
"context"
"fmt"
"testing"

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

"github.com/tailscale/hujson"
"github.com/tailscale/terraform-provider-tailscale/tailscale"
)

func TestAccTailscaleACL(t *testing.T) {
resourceName := "data.tailscale_acl.acl"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories(t),
Steps: []resource.TestStep{
{
Config: `data "tailscale_acl" "acl" {}`,
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*tailscale.Clients).V2
acl, err := client.PolicyFile().Raw(context.Background())
if err != nil {
return fmt.Errorf("unable to get ACL: %s", err)
}

huj, err := hujson.Parse([]byte(acl))
if err != nil {
return fmt.Errorf("Failed to parse ACL as HuJSON: %s", err)
}
expected := huj.String()

rs := s.RootModule().Resources[resourceName].Primary
actual := rs.Attributes["hujson"]
if err := assertEqual(expected, actual, "wrong ACL"); err != nil {
return err
}

return nil
},
},
},
})
}
10 changes: 10 additions & 0 deletions tailscale/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -191,3 +192,12 @@ func checkPropertiesMatch(resourceName string, s *terraform.State, expected map[

return nil
}

// assertEqual compares the expected and actual using [cmp.Diff] and reports an
// error if they're not equal.
func assertEqual(want, got any, errorMessage string) error {
if diff := cmp.Diff(want, got); diff != "" {
return fmt.Errorf("%s (-want +got): %s", errorMessage, diff)
}
return nil
}
5 changes: 2 additions & 3 deletions tailscale/resource_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -186,8 +185,8 @@ func TestAccACL(t *testing.T) {
return err
}

if diff := cmp.Diff(expected, actual); diff != "" {
return fmt.Errorf("diff found (-got, +want): %s", diff)
if err := assertEqual(expected, actual, "wrong ACL"); err != nil {
return err
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions tailscale/resource_dns_nameservers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package tailscale_test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -53,8 +51,8 @@ func TestAccTailscaleDNSNameservers(t *testing.T) {
return err
}

if diff := cmp.Diff(actual, expected); diff != "" {
return fmt.Errorf("wrong nameservers: (-got+want) \n%s", diff)
if err := assertEqual(expected, actual, "wrong nameservers"); err != nil {
return err
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions tailscale/resource_dns_preferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package tailscale_test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -48,8 +46,8 @@ func TestAccTailscaleDNSPreferences(t *testing.T) {
return err
}

if diff := cmp.Diff(expected, actual); diff != "" {
return fmt.Errorf("diff found (-got, +want): %s", diff)
if err := assertEqual(expected, actual, "wrong DNS preferences"); err != nil {
return err
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions tailscale/resource_dns_search_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package tailscale_test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -53,8 +51,8 @@ func TestAccTailscaleDNSSearchPaths(t *testing.T) {
return err
}

if diff := cmp.Diff(actual, expected); diff != "" {
return fmt.Errorf("wrong dns search paths: (-got+want) \n%s", diff)
if err := assertEqual(expected, actual, "wrong DNS search paths"); err != nil {
return err
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions tailscale/resource_tailnet_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -212,8 +211,8 @@ func TestAccTailscaleTailnetKey(t *testing.T) {
// don't compare IDs
actual.ID = ""

if diff := cmp.Diff(expected, actual); diff != "" {
return fmt.Errorf("diff found (-got, +want): %s", diff)
if err := assertEqual(expected, actual, "wrong key"); err != nil {
return err
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions tailscale/resource_tailnet_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package tailscale_test

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand Down Expand Up @@ -42,8 +40,8 @@ func TestAccTailscaleTailnetSettings(t *testing.T) {
return err
}

if diff := cmp.Diff(expected, actual); diff != "" {
return fmt.Errorf("diff found (-got, +want): %s", diff)
if err := assertEqual(expected, actual, "wrong Tailnet settings"); err != nil {
return err
}

return nil
Expand Down
Loading