Skip to content

Commit

Permalink
tailscale: remove use of client V1
Browse files Browse the repository at this point in the history
Fixes tailscale/corp#21867

Signed-off-by: Percy Wegmann <percy@tailscale.com>
  • Loading branch information
oxtoacart committed Aug 29, 2024
1 parent 2c5046b commit 14682dc
Show file tree
Hide file tree
Showing 30 changed files with 101 additions and 123 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/stretchr/testify v1.9.0
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
github.com/tailscale/tailscale-client-go v1.17.1-0.20240729175651-90a1e935cc19
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240821114300-27aa0bfb4219
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240826162147-08f128738726
golang.org/x/tools v0.24.0
tailscale.com v1.72.0
)
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
github.com/tailscale/tailscale-client-go v1.17.1-0.20240729175651-90a1e935cc19 h1:fRLv1yZH1ueL1cnpLhOnOymoBfMCIviCn0e0VkAjkK4=
github.com/tailscale/tailscale-client-go v1.17.1-0.20240729175651-90a1e935cc19/go.mod h1:jbwJyHniK3nyLttwcDTXnfdDQEnADvc4VMOP8hZWnR0=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240819223802-3a9fb56052db h1:sckiiaymnxDtxozA8jGc2cAU1X/0vEmKULvAP8fTS1o=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240819223802-3a9fb56052db/go.mod h1:i/MSgQ71kdyh1Wdp50XxrIgtsyO4uZ2SZSPd83lGKHM=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240820184537-d8366c1105f5 h1:4KAE8ArCbcmTWCn1jMnhos+G4jMhGAJt76rVAWIO6D4=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240820184537-d8366c1105f5/go.mod h1:i/MSgQ71kdyh1Wdp50XxrIgtsyO4uZ2SZSPd83lGKHM=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240821114300-27aa0bfb4219 h1:j51CReIxNS/7ZrFmn5zm5IYoIBg8Or8BL6G3jFC/ZgY=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240821114300-27aa0bfb4219/go.mod h1:i/MSgQ71kdyh1Wdp50XxrIgtsyO4uZ2SZSPd83lGKHM=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240826162147-08f128738726 h1:LmzY47aAgi6MhlBe8iar0Syp1gODiChMK2UGWplRjG0=
github.com/tailscale/tailscale-client-go/v2 v2.0.0-20240826162147-08f128738726/go.mod h1:i/MSgQ71kdyh1Wdp50XxrIgtsyO4uZ2SZSPd83lGKHM=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
3 changes: 2 additions & 1 deletion tailscale/data_source_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/tailscale/hujson"
tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func dataSourceACL() *schema.Resource {
Expand All @@ -29,7 +30,7 @@ func dataSourceACL() *schema.Resource {
}

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

acl, err := client.PolicyFile().Raw(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion tailscale/data_source_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/tailscale/hujson"
tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func TestAccTailscaleACL(t *testing.T) {
Expand All @@ -21,7 +22,7 @@ func TestAccTailscaleACL(t *testing.T) {
{
Config: `data "tailscale_acl" "acl" {}`,
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
acl, err := client.PolicyFile().Raw(context.Background())
if err != nil {
return fmt.Errorf("unable to get ACL: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion tailscale/data_source_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func dataSourceDevice() *schema.Resource {
}

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

var filter func(d tsclient.Device) bool
var filterDesc string
Expand Down
4 changes: 3 additions & 1 deletion tailscale/data_source_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

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

tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func dataSourceDevices() *schema.Resource {
Expand Down Expand Up @@ -68,7 +70,7 @@ func dataSourceDevices() *schema.Resource {
}

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

devices, err := client.Devices().List(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tailscale/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func dataSourceUser() *schema.Resource {
}

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

id, hasID := d.Get("id").(string)
if !hasID {
Expand Down
2 changes: 1 addition & 1 deletion tailscale/data_source_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func dataSourceUsers() *schema.Resource {
}

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

var userType *tsclient.UserType
if _userType, ok := d.Get("type").(string); ok {
Expand Down
6 changes: 4 additions & 2 deletions tailscale/data_source_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

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

tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func TestAccTailscaleUsers(t *testing.T) {
Expand All @@ -25,7 +27,7 @@ func TestAccTailscaleUsers(t *testing.T) {
{
Config: `data "tailscale_users" "all_users" {}`,
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
users, err := client.Users().List(context.Background(), nil, nil)
if err != nil {
return fmt.Errorf("unable to list users: %s", err)
Expand Down Expand Up @@ -95,7 +97,7 @@ func TestAccTailscaleUsers(t *testing.T) {
{
Config: userDataSources.String(),
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
users, err := client.Users().List(context.Background(), nil, nil)
if err != nil {
return fmt.Errorf("unable to list users: %s", err)
Expand Down
6 changes: 4 additions & 2 deletions tailscale/datasource_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

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

tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func TestAccTailscaleDevices(t *testing.T) {
Expand All @@ -29,7 +31,7 @@ func TestAccTailscaleDevices(t *testing.T) {
{
Config: `data "tailscale_devices" "all_devices" {}`,
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
devices, err := client.Devices().List(context.Background())
if err != nil {
return fmt.Errorf("unable to list devices: %s", err)
Expand Down Expand Up @@ -103,7 +105,7 @@ func TestAccTailscaleDevices(t *testing.T) {
{
Config: devicesDataSources.String(),
Check: func(s *terraform.State) error {
client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
devices, err := client.Devices().List(context.Background())
if err != nil {
return fmt.Errorf("unable to list devices: %s", err)
Expand Down
40 changes: 6 additions & 34 deletions tailscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

tsclientv1 "github.com/tailscale/tailscale-client-go/tailscale"
tsclient "github.com/tailscale/tailscale-client-go/v2"
)

Expand All @@ -23,12 +22,6 @@ var providerVersion = "dev"

type ProviderOption func(p *schema.Provider)

// Clients contains both v1 and v2 Tailscale Clients
type Clients struct {
V1 *tsclientv1.Client
V2 *tsclient.Client
}

// Provider returns the *schema.Provider instance that implements the terraform provider.
func Provider(options ...ProviderOption) *schema.Provider {
// Support both sets of OAuth Env vars for backwards compatibility
Expand Down Expand Up @@ -160,45 +153,24 @@ func providerConfigure(_ context.Context, provider *schema.Provider, d *schema.R
oauthScopes[i] = scope.(string)
}

client, err := tsclientv1.NewClient(
"",
tailnet,
tsclientv1.WithBaseURL(baseURL),
tsclientv1.WithUserAgent(userAgent),
tsclientv1.WithOAuthClientCredentials(oauthClientID, oauthClientSecret, oauthScopes),
)
if err != nil {
return nil, diagnosticsError(err, "failed to initialise client")
}

clientV2 := &tsclient.Client{
client := &tsclient.Client{
BaseURL: parsedBaseURL,
UserAgent: userAgent,
Tailnet: tailnet,
}
clientV2.UseOAuth(oauthClientID, oauthClientSecret, oauthScopes)

return &Clients{client, clientV2}, nil
}
client.UseOAuth(oauthClientID, oauthClientSecret, oauthScopes)

client, err := tsclientv1.NewClient(
apiKey,
tailnet,
tsclientv1.WithBaseURL(baseURL),
tsclientv1.WithUserAgent(userAgent),
)
if err != nil {
return nil, diagnosticsError(err, "failed to initialise client")
return client, nil
}

clientV2 := &tsclient.Client{
client := &tsclient.Client{
BaseURL: parsedBaseURL,
UserAgent: userAgent,
APIKey: apiKey,
Tailnet: tailnet,
}

return &Clients{client, clientV2}, nil
return client, nil
}

func diagnosticsError(err error, message string, args ...interface{}) diag.Diagnostics {
Expand All @@ -215,7 +187,7 @@ func diagnosticsError(err error, message string, args ...interface{}) diag.Diagn
},
}

if details := tsclientv1.ErrorData(err); len(details) > 0 {
if details := tsclient.ErrorData(err); len(details) > 0 {
for _, dt := range details {
for _, e := range dt.Errors {
diags = append(diags, diag.Diagnostic{
Expand Down
10 changes: 5 additions & 5 deletions tailscale/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
tsclient "github.com/tailscale/tailscale-client-go/v2"
)

var testClients *Clients
var testClient *tsclient.Client
var testServer *TestServer
var testAccProvider = Provider()

Expand Down Expand Up @@ -70,13 +70,13 @@ func TestProvider_Implemented(t *testing.T) {
func testProviderFactories(t *testing.T) map[string]func() (*schema.Provider, error) {
t.Helper()

testClients, testServer = NewTestHarness(t)
testClient, testServer = NewTestHarness(t)
return map[string]func() (*schema.Provider, error){
"tailscale": func() (*schema.Provider, error) {
return Provider(func(p *schema.Provider) {
// Set up a test harness for the provider
p.ConfigureContextFunc = func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
return testClients, nil
return testClient, nil
}

// Don't require any of the global configuration
Expand Down Expand Up @@ -139,7 +139,7 @@ func checkResourceRemoteProperties(resourceName string, check func(client *tscli
return fmt.Errorf("resource has no ID set")
}

client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
return check(client, rs)
}
}
Expand All @@ -155,7 +155,7 @@ func checkResourceDestroyed(resourceName string, check func(client *tsclient.Cli
return fmt.Errorf("resource has no ID set")
}

client := testAccProvider.Meta().(*Clients).V2
client := testAccProvider.Meta().(*tsclient.Client)
return check(client, rs)
}
}
Expand Down
9 changes: 5 additions & 4 deletions tailscale/resource_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/tailscale/hujson"
tsclient "github.com/tailscale/tailscale-client-go/v2"
)

const resourceACLDescription = `The acl resource allows you to configure a Tailscale ACL. See https://tailscale.com/kb/1018/acls for more information. Note that this resource will completely overwrite existing ACL contents for a given tailnet.
Expand All @@ -33,7 +34,7 @@ func resourceACL() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: func(ctx context.Context, rd *schema.ResourceDiff, m interface{}) error {
client := m.(*Clients).V2
client := m.(*tsclient.Client)

//if the acl is only known after apply, then acl will be an empty string and validation will fail
if rd.Get("acl").(string) == "" {
Expand Down Expand Up @@ -95,7 +96,7 @@ func resourceACL() *schema.Resource {
}

func resourceACLRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V2
client := m.(*tsclient.Client)
acl, err := client.PolicyFile().Raw(ctx)
if err != nil {
return diagnosticsError(err, "Failed to fetch ACL")
Expand All @@ -108,7 +109,7 @@ func resourceACLRead(ctx context.Context, d *schema.ResourceData, m interface{})
}

func resourceACLCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V2
client := m.(*tsclient.Client)
acl := d.Get("acl").(string)

// Setting the `ts-default` ETag will make this operation succeed only if
Expand All @@ -134,7 +135,7 @@ func resourceACLCreate(ctx context.Context, d *schema.ResourceData, m interface{
}

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

if !d.HasChange("acl") {
return nil
Expand Down
6 changes: 3 additions & 3 deletions tailscale/resource_contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func resourceContacts() *schema.Resource {
}

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

if diagErr := updateContact(ctx, client, d, tsclient.ContactAccount); diagErr != nil {
return diagErr
Expand All @@ -94,7 +94,7 @@ func resourceContactsCreate(ctx context.Context, d *schema.ResourceData, m inter
}

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

contacts, err := client.Contacts().Get(ctx)
if err != nil {
Expand All @@ -117,7 +117,7 @@ func resourceContactsRead(ctx context.Context, d *schema.ResourceData, m interfa
}

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

if d.HasChange("account") {
if diagErr := updateContact(ctx, client, d, tsclient.ContactAccount); diagErr != nil {
Expand Down
8 changes: 5 additions & 3 deletions tailscale/resource_device_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

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

tsclient "github.com/tailscale/tailscale-client-go/v2"
)

func resourceDeviceAuthorization() *schema.Resource {
Expand Down Expand Up @@ -33,7 +35,7 @@ func resourceDeviceAuthorization() *schema.Resource {
}

func resourceDeviceAuthorizationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V2
client := m.(*tsclient.Client)
deviceID := d.Id()

device, err := client.Devices().Get(ctx, deviceID)
Expand All @@ -48,7 +50,7 @@ func resourceDeviceAuthorizationRead(ctx context.Context, d *schema.ResourceData
}

func resourceDeviceAuthorizationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V2
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
authorized := d.Get("authorized").(bool)

Expand All @@ -63,7 +65,7 @@ func resourceDeviceAuthorizationCreate(ctx context.Context, d *schema.ResourceDa
}

func resourceDeviceAuthorizationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Clients).V2
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)

device, err := client.Devices().Get(ctx, deviceID)
Expand Down
Loading

0 comments on commit 14682dc

Please sign in to comment.