-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tailscale: use v2 client for device tags
Updates tailscale/corp#21867 Signed-off-by: Percy Wegmann <percy@tailscale.com>
- Loading branch information
Showing
3 changed files
with
115 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,101 @@ | ||
package tailscale_test | ||
|
||
import ( | ||
"net/http" | ||
"context" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/tailscale/tailscale-client-go/tailscale" | ||
tsclient "github.com/tailscale/tailscale-client-go/v2" | ||
"github.com/tailscale/terraform-provider-tailscale/tailscale" | ||
) | ||
|
||
const testDeviceTags = ` | ||
data "tailscale_device" "test_device" { | ||
name = "device.example.com" | ||
wait_for = "60s" | ||
func TestAccTailscaleDeviceTags(t *testing.T) { | ||
const resourceName = "tailscale_device_tags.test_tags" | ||
|
||
const testDeviceTagsCreate = ` | ||
data "tailscale_device" "test_device" { | ||
name = "%s" | ||
wait_for = "60s" | ||
} | ||
resource "tailscale_device_tags" "test_tags" { | ||
device_id = data.tailscale_device.test_device.id | ||
tags = [ | ||
"tag:a", | ||
"tag:b", | ||
] | ||
}` | ||
|
||
const testDeviceTagsUpdate = ` | ||
data "tailscale_device" "test_device" { | ||
name = "%s" | ||
wait_for = "60s" | ||
} | ||
resource "tailscale_device_tags" "test_tags" { | ||
device_id = data.tailscale_device.test_device.id | ||
tags = [ | ||
"tag:b", | ||
"tag:c", | ||
] | ||
}` | ||
|
||
checkProperties := func(expectedTags []string) func(client *tsclient.Client, rs *terraform.ResourceState) error { | ||
return func(client *tsclient.Client, rs *terraform.ResourceState) error { | ||
deviceID := rs.Primary.Attributes["device_id"] | ||
|
||
device, err := client.Devices().Get(context.Background(), deviceID) | ||
if err != nil { | ||
return fmt.Errorf("failed to fetch device: %s", err) | ||
} | ||
|
||
if !reflect.DeepEqual(device.Tags, expectedTags) { | ||
return fmt.Errorf("bad tags: %#v", device.Tags) | ||
} | ||
return nil | ||
} | ||
} | ||
resource "tailscale_device_tags" "test_tags" { | ||
device_id = data.tailscale_device.test_device.id | ||
tags = [ | ||
"a:b", | ||
"b:c", | ||
] | ||
}` | ||
|
||
func TestProvider_TailscaleDeviceTags(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
IsUnitTest: true, | ||
PreCheck: func() { | ||
testServer.ResponseCode = http.StatusOK | ||
testServer.ResponseBody = map[string][]tailscale.Device{ | ||
"devices": { | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { | ||
// Set up ACLs to allow the required tags | ||
client := testAccProvider.Meta().(*tailscale.Clients).V2 | ||
err := client.PolicyFile().Set(context.Background(), ` | ||
{ | ||
Name: "device.example.com", | ||
ID: "123", | ||
}, | ||
"tagOwners": { | ||
"tag:a": ["autogroup:member"], | ||
"tag:b": ["autogroup:member"], | ||
"tag:c": ["autogroup:member"], | ||
}, | ||
}`, "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
}, | ||
} | ||
}, | ||
ProviderFactories: testProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
testResourceCreated("tailscale_device_tags.test_tags", testDeviceTags), | ||
testResourceDestroyed("tailscale_device_tags.test_tags", testDeviceTags), | ||
Config: fmt.Sprintf(testDeviceTagsCreate, os.Getenv("TAILSCALE_TEST_DEVICE_NAME")), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkResourceRemoteProperties(resourceName, checkProperties([]string{"tag:a", "tag:b"})), | ||
resource.TestCheckTypeSetElemAttr(resourceName, "tags.*", "tag:a"), | ||
resource.TestCheckTypeSetElemAttr(resourceName, "tags.*", "tag:b"), | ||
), | ||
}, | ||
{ | ||
Config: fmt.Sprintf(testDeviceTagsUpdate, os.Getenv("TAILSCALE_TEST_DEVICE_NAME")), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkResourceRemoteProperties(resourceName, checkProperties([]string{"tag:b", "tag:c"})), | ||
resource.TestCheckTypeSetElemAttr(resourceName, "tags.*", "tag:b"), | ||
resource.TestCheckTypeSetElemAttr(resourceName, "tags.*", "tag:c"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |