From e588d9423ab947e50ce6648c405f745601b42c7e Mon Sep 17 00:00:00 2001 From: Volodymyr Manilo Date: Tue, 20 Feb 2024 19:41:45 +0100 Subject: [PATCH] added config for service-account, service-key and connector --- .../test/acctests/resource/config-builder.go | 15 + .../acctests/resource/config-connector.go | 72 +++++ .../test/acctests/resource/config-group.go | 4 +- .../resource/config-remote-network.go | 8 +- .../resource/config-service-account.go | 52 ++++ .../acctests/resource/config-service-key.go | 78 ++++++ .../test/acctests/resource/config-user.go | 2 +- .../test/acctests/resource/connector_test.go | 141 ++++------ .../acctests/resource/service-account_test.go | 26 +- .../acctests/resource/service-key_test.go | 263 ++++++------------ 10 files changed, 376 insertions(+), 285 deletions(-) create mode 100644 twingate/internal/test/acctests/resource/config-connector.go create mode 100644 twingate/internal/test/acctests/resource/config-service-account.go create mode 100644 twingate/internal/test/acctests/resource/config-service-key.go diff --git a/twingate/internal/test/acctests/resource/config-builder.go b/twingate/internal/test/acctests/resource/config-builder.go index 33c1ab58..e0bb385f 100644 --- a/twingate/internal/test/acctests/resource/config-builder.go +++ b/twingate/internal/test/acctests/resource/config-builder.go @@ -20,6 +20,21 @@ func collectResourceIDs[T TerraformResource](resources ...T) []string { return ids } +func optionalInt(val any) *int { + if val == nil { + return nil + } + + switch t := val.(type) { + case int: + return &t + case *int: + return t + default: + return nil + } +} + func optionalString(val any) *string { if val == nil { return nil diff --git a/twingate/internal/test/acctests/resource/config-connector.go b/twingate/internal/test/acctests/resource/config-connector.go new file mode 100644 index 00000000..2d3a9804 --- /dev/null +++ b/twingate/internal/test/acctests/resource/config-connector.go @@ -0,0 +1,72 @@ +package resource + +import ( + "fmt" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/attr" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test/acctests" + "strings" +) + +type Connector struct { + ResourceName string + RemoteNetworkID string + Name *string + StatusUpdatesEnabled *bool +} + +func NewConnector() *Connector { + return &Connector{ + ResourceName: test.RandomResourceName(), + } +} + +func (r *Connector) optionalAttributes() string { + var optional []string + + if r.Name != nil { + optional = append(optional, fmt.Sprintf(`name = "%s"`, *r.Name)) + } + + if r.StatusUpdatesEnabled != nil { + optional = append(optional, fmt.Sprintf(`status_updates_enabled = %v`, *r.StatusUpdatesEnabled)) + } + + return strings.Join(optional, "\n") +} + +func (r *Connector) TerraformResource() string { + return acctests.TerraformConnector(r.ResourceName) +} + +func (r *Connector) String() string { + return acctests.Nprintf(` + resource "twingate_connector" "${terraform_resource}" { + remote_network_id = ${remote_network_id} + + ${optional_attributes} + } + `, map[string]any{ + "terraform_resource": r.ResourceName, + "remote_network_id": r.RemoteNetworkID, + "optional_attributes": r.optionalAttributes(), + }) +} + +func (r *Connector) Set(values ...any) *Connector { + for i := 0; i < len(values); i += 2 { + key := values[i].(string) + val := values[i+1] + + switch key { + case attr.Name: + r.Name = optionalString(val) + case attr.RemoteNetworkID: + r.RemoteNetworkID = val.(string) + case attr.StatusUpdatesEnabled: + r.StatusUpdatesEnabled = optionalBool(val) + } + } + + return r +} diff --git a/twingate/internal/test/acctests/resource/config-group.go b/twingate/internal/test/acctests/resource/config-group.go index 05af9d53..5e4c6501 100644 --- a/twingate/internal/test/acctests/resource/config-group.go +++ b/twingate/internal/test/acctests/resource/config-group.go @@ -21,8 +21,8 @@ type Group struct { func NewGroup() *Group { return &Group{ - ResourceName: test.RandomGroupName(), - Name: test.RandomName(), + ResourceName: test.RandomResourceName(), + Name: test.RandomGroupName(), } } diff --git a/twingate/internal/test/acctests/resource/config-remote-network.go b/twingate/internal/test/acctests/resource/config-remote-network.go index c3a56573..21c38bc5 100644 --- a/twingate/internal/test/acctests/resource/config-remote-network.go +++ b/twingate/internal/test/acctests/resource/config-remote-network.go @@ -16,8 +16,8 @@ type RemoteNetwork struct { func NewRemoteNetwork() *RemoteNetwork { return &RemoteNetwork{ - ResourceName: test.RandomNetworkName(), - Name: test.RandomName(), + ResourceName: test.RandomResourceName(), + Name: test.RandomNetworkName(), } } @@ -35,6 +35,10 @@ func (r *RemoteNetwork) TerraformResource() string { return acctests.TerraformRemoteNetwork(r.ResourceName) } +func (r *RemoteNetwork) TerraformResourceID() string { + return r.TerraformResource() + ".id" +} + func (r *RemoteNetwork) String() string { return acctests.Nprintf(` resource "twingate_remote_network" "${terraform_resource}" { diff --git a/twingate/internal/test/acctests/resource/config-service-account.go b/twingate/internal/test/acctests/resource/config-service-account.go new file mode 100644 index 00000000..bcebc23a --- /dev/null +++ b/twingate/internal/test/acctests/resource/config-service-account.go @@ -0,0 +1,52 @@ +package resource + +import ( + "github.com/Twingate/terraform-provider-twingate/twingate/internal/attr" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test/acctests" +) + +type ServiceAccount struct { + ResourceName string + Name string +} + +func NewServiceAccount() *ServiceAccount { + return &ServiceAccount{ + ResourceName: test.RandomResourceName(), + Name: test.RandomServiceAccountName(), + } +} + +func (r *ServiceAccount) TerraformResource() string { + return acctests.TerraformServiceAccount(r.ResourceName) +} + +func (r *ServiceAccount) TerraformResourceID() string { + return r.TerraformResource() + ".id" +} + +func (r *ServiceAccount) String() string { + return acctests.Nprintf(` + resource "twingate_service_account" "${terraform_resource}" { + name = "${name}" + } + `, map[string]any{ + "terraform_resource": r.ResourceName, + "name": r.Name, + }) +} + +func (r *ServiceAccount) Set(values ...any) *ServiceAccount { + for i := 0; i < len(values); i += 2 { + key := values[i].(string) + val := values[i+1] + + switch key { + case attr.Name: + r.Name = val.(string) + } + } + + return r +} diff --git a/twingate/internal/test/acctests/resource/config-service-key.go b/twingate/internal/test/acctests/resource/config-service-key.go new file mode 100644 index 00000000..2d1afd1c --- /dev/null +++ b/twingate/internal/test/acctests/resource/config-service-key.go @@ -0,0 +1,78 @@ +package resource + +import ( + "fmt" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/attr" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test" + "github.com/Twingate/terraform-provider-twingate/twingate/internal/test/acctests" + "strings" +) + +type ServiceAccountKey struct { + ResourceName string + ServiceAccountID string + + ExpirationTime *int + Name *string +} + +func NewServiceAccountKey(serviceAccountID string) *ServiceAccountKey { + return &ServiceAccountKey{ + ResourceName: test.RandomResourceName(), + ServiceAccountID: serviceAccountID, + } +} + +func (r *ServiceAccountKey) TerraformResource() string { + return acctests.TerraformServiceKey(r.ResourceName) +} + +func (r *ServiceAccountKey) TerraformResourceID() string { + return r.TerraformResource() + ".id" +} + +func (r *ServiceAccountKey) String() string { + return acctests.Nprintf(` + resource "twingate_service_account_key" "${terraform_resource}" { + service_account_id = ${service_account_id} + + ${optional_attributes} + } + `, map[string]any{ + "terraform_resource": r.ResourceName, + "service_account_id": r.ServiceAccountID, + "optional_attributes": r.optionalAttributes(), + }) +} + +func (r *ServiceAccountKey) Set(values ...any) *ServiceAccountKey { + for i := 0; i < len(values); i += 2 { + key := values[i].(string) + val := values[i+1] + + switch key { + case attr.Name: + r.Name = optionalString(val) + case attr.ExpirationTime: + r.ExpirationTime = optionalInt(val) + case attr.ServiceAccountID: + r.ServiceAccountID = val.(string) + } + } + + return r +} + +func (r *ServiceAccountKey) optionalAttributes() string { + var optional []string + + if r.Name != nil { + optional = append(optional, fmt.Sprintf(`name = "%s"`, *r.Name)) + } + + if r.ExpirationTime != nil { + optional = append(optional, fmt.Sprintf(`expiration_time = %v`, *r.ExpirationTime)) + } + + return strings.Join(optional, "\n") +} diff --git a/twingate/internal/test/acctests/resource/config-user.go b/twingate/internal/test/acctests/resource/config-user.go index 7f281b02..47ad3a5e 100644 --- a/twingate/internal/test/acctests/resource/config-user.go +++ b/twingate/internal/test/acctests/resource/config-user.go @@ -19,7 +19,7 @@ type User struct { } func NewUser(terraformResourceName ...string) *User { - resourceName := test.RandomUserName() + resourceName := test.RandomResourceName() if len(terraformResourceName) > 0 { resourceName = terraformResourceName[0] } diff --git a/twingate/internal/test/acctests/resource/connector_test.go b/twingate/internal/test/acctests/resource/connector_test.go index f43af5f4..f54142d8 100644 --- a/twingate/internal/test/acctests/resource/connector_test.go +++ b/twingate/internal/test/acctests/resource/connector_test.go @@ -16,8 +16,9 @@ import ( func TestAccRemoteConnectorCreate(t *testing.T) { t.Parallel() - connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -25,9 +26,9 @@ func TestAccRemoteConnectorCreate(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnector(connectorName, connectorName, test.RandomName()), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), sdk.TestCheckResourceAttrSet(theResource, attr.Name), ), }, @@ -38,8 +39,9 @@ func TestAccRemoteConnectorCreate(t *testing.T) { func TestAccRemoteConnectorWithCustomName(t *testing.T) { t.Parallel() - connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.Name, test.RandomConnectorName(), attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -47,10 +49,10 @@ func TestAccRemoteConnectorWithCustomName(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnectorWithName(connectorName, test.RandomName(), connectorName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), - sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), + sdk.TestCheckResourceAttr(theResource, attr.Name, *connector.Name), ), }, }, @@ -60,8 +62,9 @@ func TestAccRemoteConnectorWithCustomName(t *testing.T) { func TestAccRemoteConnectorImport(t *testing.T) { t.Parallel() - connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.Name, test.RandomConnectorName(), attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -69,10 +72,10 @@ func TestAccRemoteConnectorImport(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnectorWithName(connectorName, test.RandomName(), connectorName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), - sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), + sdk.TestCheckResourceAttr(theResource, attr.Name, *connector.Name), ), }, { @@ -80,7 +83,7 @@ func TestAccRemoteConnectorImport(t *testing.T) { ImportStateVerify: true, ResourceName: theResource, ImportStateCheck: acctests.CheckImportState(map[string]string{ - attr.Name: connectorName, + attr.Name: *connector.Name, }), }, }, @@ -90,12 +93,10 @@ func TestAccRemoteConnectorImport(t *testing.T) { func TestAccRemoteConnectorNotAllowedToChangeRemoteNetworkId(t *testing.T) { t.Parallel() - terraformConnectorName := test.RandomConnectorName() - terraformRemoteNetworkName1 := test.RandomNetworkName() - terraformRemoteNetworkName2 := test.RandomNetworkName() - theResource := acctests.TerraformConnector(terraformConnectorName) - remoteNetworkName1 := test.RandomName() - remoteNetworkName2 := test.RandomName() + network1 := NewRemoteNetwork() + network2 := NewRemoteNetwork() + connector := NewConnector() + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -103,13 +104,13 @@ func TestAccRemoteConnectorNotAllowedToChangeRemoteNetworkId(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnector(terraformRemoteNetworkName1, terraformConnectorName, remoteNetworkName1), + Config: configBuilder(network1, network2, connector.Set(attr.RemoteNetworkID, network1.TerraformResourceID())), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformRemoteNetworkName1)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network1.TerraformResource()), ), }, { - Config: configConnector(terraformRemoteNetworkName2, terraformConnectorName, remoteNetworkName2), + Config: configBuilder(network1, network2, connector.Set(attr.RemoteNetworkID, network2.TerraformResourceID())), ExpectError: regexp.MustCompile(resource.ErrNotAllowChangeRemoteNetworkID.Error()), }, }, @@ -119,9 +120,9 @@ func TestAccRemoteConnectorNotAllowedToChangeRemoteNetworkId(t *testing.T) { func TestAccTwingateConnectorReCreateAfterDeletion(t *testing.T) { t.Parallel() - connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) - networkName := test.RandomName() + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -129,17 +130,17 @@ func TestAccTwingateConnectorReCreateAfterDeletion(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnector(connectorName, connectorName, networkName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), acctests.DeleteTwingateResource(theResource, resource.TwingateConnector), ), ExpectNonEmptyPlan: true, }, { - Config: configConnector(connectorName, connectorName, networkName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), ), }, }, @@ -206,9 +207,10 @@ func TestAccRemoteConnectorUpdateName(t *testing.T) { t.Parallel() connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) - networkName := test.RandomName() - connectorNewName := test.RandomConnectorName() + + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -216,16 +218,16 @@ func TestAccRemoteConnectorUpdateName(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnector(connectorName, connectorName, networkName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), sdk.TestCheckResourceAttrSet(theResource, attr.Name), ), }, { - Config: configConnectorWithName(connectorName, networkName, connectorNewName), + Config: configBuilder(network, connector.Set(attr.Name, connectorName)), Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, attr.Name, connectorNewName), + sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), ), }, }, @@ -235,9 +237,9 @@ func TestAccRemoteConnectorUpdateName(t *testing.T) { func TestAccRemoteConnectorCreateWithNotificationStatus(t *testing.T) { t.Parallel() - connectorName := test.RandomConnectorName() - theResource := acctests.TerraformConnector(connectorName) - networkName := test.RandomName() + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -245,22 +247,22 @@ func TestAccRemoteConnectorCreateWithNotificationStatus(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: configConnector(connectorName, connectorName, networkName), + Config: configBuilder(network, connector), Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + checkTwingateConnectorSetWithRemoteNetwork(theResource, network.TerraformResource()), sdk.TestCheckResourceAttrSet(theResource, attr.Name), ), }, { // expecting no changes, as by default notifications enabled PlanOnly: true, - Config: configConnectorWithNotificationStatus(connectorName, connectorName, networkName, true), + Config: configBuilder(network, connector.Set(attr.StatusUpdatesEnabled, true)), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "true"), ), }, { - Config: configConnectorWithNotificationStatus(connectorName, connectorName, networkName, false), + Config: configBuilder(network, connector.Set(attr.StatusUpdatesEnabled, false)), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), ), @@ -268,7 +270,7 @@ func TestAccRemoteConnectorCreateWithNotificationStatus(t *testing.T) { { // expecting no changes, when user removes `status_updates_enabled` field from terraform PlanOnly: true, - Config: configConnector(connectorName, connectorName, networkName), + Config: configBuilder(network, connector.Set(attr.StatusUpdatesEnabled, nil)), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), ), @@ -277,28 +279,12 @@ func TestAccRemoteConnectorCreateWithNotificationStatus(t *testing.T) { }) } -func configConnectorWithNotificationStatus(terraformRemoteNetworkName, terraformConnectorName, remoteNetworkName string, notificationStatus bool) string { - return acctests.Nprintf(` - ${remote_network} - - resource "twingate_connector" "${connector_resource}" { - remote_network_id = twingate_remote_network.${remote_network_resource}.id - status_updates_enabled = ${notification_status} - } - `, - map[string]any{ - "remote_network": configRemoteNetwork(terraformRemoteNetworkName, remoteNetworkName), - "connector_resource": terraformConnectorName, - "remote_network_resource": terraformRemoteNetworkName, - "notification_status": notificationStatus, - }) - -} - func TestAccRemoteConnectorCreateWithNotificationStatusFalse(t *testing.T) { - const terraformResourceName = "test_c8" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() + t.Parallel() + + network := NewRemoteNetwork() + connector := NewConnector().Set(attr.RemoteNetworkID, network.TerraformResourceID()) + theResource := connector.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -306,7 +292,7 @@ func TestAccRemoteConnectorCreateWithNotificationStatusFalse(t *testing.T) { CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, Steps: []sdk.TestStep{ { - Config: terraformResourceTwingateConnectorWithNotificationStatus(terraformResourceName, terraformResourceName, remoteNetworkName, false), + Config: configBuilder(network, connector.Set(attr.StatusUpdatesEnabled, false)), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), ), @@ -314,22 +300,3 @@ func TestAccRemoteConnectorCreateWithNotificationStatusFalse(t *testing.T) { }, }) } - -func terraformResourceTwingateConnectorWithNotificationStatus(terraformRemoteNetworkName, terraformConnectorName, remoteNetworkName string, notificationStatus bool) string { - return fmt.Sprintf(` - %s - - resource "twingate_connector" "%s" { - remote_network_id = twingate_remote_network.%s.id - status_updates_enabled = %v - } - `, terraformResourceRemoteNetwork(terraformRemoteNetworkName, remoteNetworkName), terraformConnectorName, terraformRemoteNetworkName, notificationStatus) -} - -func terraformResourceRemoteNetwork(terraformResourceName, name string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" - } - `, terraformResourceName, name) -} diff --git a/twingate/internal/test/acctests/resource/service-account_test.go b/twingate/internal/test/acctests/resource/service-account_test.go index 5b126566..07d9e133 100644 --- a/twingate/internal/test/acctests/resource/service-account_test.go +++ b/twingate/internal/test/acctests/resource/service-account_test.go @@ -26,8 +26,8 @@ func configServiceAccount(resourceName, serviceAccountName string) string { func TestAccTwingateServiceAccountCreateUpdate(t *testing.T) { t.Parallel() - resourceName := test.RandomServiceAccountName() - theResource := acctests.TerraformServiceAccount(resourceName) + serviceAccount := NewServiceAccount() + theResource := serviceAccount.TerraformResource() name1 := test.RandomName() name2 := test.RandomName() @@ -37,14 +37,14 @@ func TestAccTwingateServiceAccountCreateUpdate(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceAccount(resourceName, name1), + Config: configBuilder(serviceAccount.Set(attr.Name, name1)), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, attr.Name, name1), ), }, { - Config: configServiceAccount(resourceName, name2), + Config: configBuilder(serviceAccount.Set(attr.Name, name2)), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, attr.Name, name2), @@ -57,9 +57,8 @@ func TestAccTwingateServiceAccountCreateUpdate(t *testing.T) { func TestAccTwingateServiceAccountDelete(t *testing.T) { t.Parallel() - resourceName := test.RandomServiceAccountName() - theResource := acctests.TerraformServiceAccount(resourceName) - name := test.RandomName() + serviceAccount := NewServiceAccount() + theResource := serviceAccount.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -67,11 +66,11 @@ func TestAccTwingateServiceAccountDelete(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceAccount(resourceName, name), + Config: configBuilder(serviceAccount), Destroy: true, }, { - Config: configServiceAccount(resourceName, name), + Config: configBuilder(serviceAccount), ConfigPlanChecks: sdk.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction(theResource, plancheck.ResourceActionCreate), @@ -85,9 +84,8 @@ func TestAccTwingateServiceAccountDelete(t *testing.T) { func TestAccTwingateServiceAccountReCreateAfterDeletion(t *testing.T) { t.Parallel() - resourceName := test.RandomServiceAccountName() - theResource := acctests.TerraformServiceAccount(resourceName) - name := test.RandomName() + serviceAccount := NewServiceAccount() + theResource := serviceAccount.TerraformResource() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -95,7 +93,7 @@ func TestAccTwingateServiceAccountReCreateAfterDeletion(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceAccount(resourceName, name), + Config: configBuilder(serviceAccount), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), acctests.DeleteTwingateResource(theResource, resource.TwingateServiceAccount), @@ -104,7 +102,7 @@ func TestAccTwingateServiceAccountReCreateAfterDeletion(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: configServiceAccount(resourceName, name), + Config: configBuilder(serviceAccount), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), diff --git a/twingate/internal/test/acctests/resource/service-key_test.go b/twingate/internal/test/acctests/resource/service-key_test.go index f8d58aee..2a098783 100644 --- a/twingate/internal/test/acctests/resource/service-key_test.go +++ b/twingate/internal/test/acctests/resource/service-key_test.go @@ -16,55 +16,6 @@ import ( var ErrEmptyValue = errors.New("empty value") -func configServiceKey(resourceName, serviceAccountName string) string { - return acctests.Nprintf(` - ${service_account} - - resource "twingate_service_account_key" "${service_account_key_resource}" { - service_account_id = twingate_service_account.${service_account_resource}.id - } - `, - map[string]any{ - "service_account": configServiceAccount(resourceName, serviceAccountName), - "service_account_key_resource": resourceName, - "service_account_resource": resourceName, - }) -} - -func configServiceKeyWithName(resourceName, serviceAccountName, serviceKeyName string) string { - return acctests.Nprintf(` - ${service_account} - - resource "twingate_service_account_key" "${service_account_key_resource}" { - service_account_id = twingate_service_account.${service_account_resource}.id - name = "${name}" - } - `, - map[string]any{ - "service_account": configServiceAccount(resourceName, serviceAccountName), - "service_account_key_resource": resourceName, - "service_account_resource": resourceName, - "name": serviceKeyName, - }) -} - -func configServiceKeyWithExpiration(resourceName, serviceAccountName string, expirationTime int) string { - return acctests.Nprintf(` - ${service_account} - - resource "twingate_service_account_key" "${service_account_key_resource}" { - service_account_id = twingate_service_account.${service_account_resource}.id - expiration_time = ${expiration_time} - } - `, - map[string]any{ - "service_account": configServiceAccount(resourceName, serviceAccountName), - "service_account_key_resource": resourceName, - "service_account_resource": resourceName, - "expiration_time": expirationTime, - }) -} - func nonEmptyValue(value string) error { if value != "" { return nil @@ -76,10 +27,8 @@ func nonEmptyValue(value string) error { func TestAccTwingateServiceKeyCreateUpdate(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -87,21 +36,17 @@ func TestAccTwingateServiceKeyCreateUpdate(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceAccount, serviceKey), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttr(serviceAccount.TerraformResource(), attr.Name, serviceAccount.Name), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceAccount, serviceKey), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttr(serviceAccount.TerraformResource(), attr.Name, serviceAccount.Name), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, }, @@ -111,10 +56,9 @@ func TestAccTwingateServiceKeyCreateUpdate(t *testing.T) { func TestAccTwingateServiceKeyCreateUpdateWithName(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) + name1 := test.RandomName() name2 := test.RandomName() @@ -124,23 +68,19 @@ func TestAccTwingateServiceKeyCreateUpdateWithName(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKeyWithName(terraformResourceName, serviceAccountName, name1), + Config: configBuilder(serviceKey.Set(attr.Name, name1), serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.Name, name1), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttr(serviceAccount.TerraformResource(), attr.Name, serviceAccount.Name), + sdk.TestCheckResourceAttr(serviceKey.TerraformResource(), attr.Name, name1), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, { - Config: configServiceKeyWithName(terraformResourceName, serviceAccountName, name2), + Config: configBuilder(serviceKey.Set(attr.Name, name2), serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.Name, name2), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttr(serviceAccount.TerraformResource(), attr.Name, serviceAccount.Name), + sdk.TestCheckResourceAttr(serviceKey.TerraformResource(), attr.Name, name2), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), acctests.WaitTestFunc(), ), }, @@ -151,9 +91,8 @@ func TestAccTwingateServiceKeyCreateUpdateWithName(t *testing.T) { func TestAccTwingateServiceKeyWontReCreateAfterInactive(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) resourceID := new(string) @@ -163,23 +102,23 @@ func TestAccTwingateServiceKeyWontReCreateAfterInactive(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.GetTwingateResourceID(serviceKey, &resourceID), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - acctests.RevokeTwingateServiceKey(serviceKey), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + acctests.GetTwingateResourceID(serviceKey.TerraformResource(), &resourceID), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), + acctests.RevokeTwingateServiceKey(serviceKey.TerraformResource()), acctests.WaitTestFunc(), - acctests.CheckTwingateServiceKeyStatus(serviceKey, model.StatusRevoked), + acctests.CheckTwingateServiceKeyStatus(serviceKey.TerraformResource(), model.StatusRevoked), ), }, { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "false"), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttr(serviceKey.TerraformResource(), attr.IsActive, "false"), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.ID, func(value string) error { if *resourceID == "" { return errors.New("failed to fetch resource id") } @@ -199,9 +138,8 @@ func TestAccTwingateServiceKeyWontReCreateAfterInactive(t *testing.T) { func TestAccTwingateServiceKeyDelete(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -209,14 +147,14 @@ func TestAccTwingateServiceKeyDelete(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), Destroy: true, }, { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), ConfigPlanChecks: sdk.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ - plancheck.ExpectResourceAction(serviceKey, plancheck.ResourceActionCreate), + plancheck.ExpectResourceAction(serviceKey.TerraformResource(), plancheck.ResourceActionCreate), }, }, }, @@ -227,9 +165,8 @@ func TestAccTwingateServiceKeyDelete(t *testing.T) { func TestAccTwingateServiceKeyReCreateAfterDeletion(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -237,19 +174,19 @@ func TestAccTwingateServiceKeyReCreateAfterDeletion(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.RevokeTwingateServiceKey(serviceKey), - acctests.DeleteTwingateResource(serviceKey, resource.TwingateServiceAccountKey), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + acctests.RevokeTwingateServiceKey(serviceKey.TerraformResource()), + acctests.DeleteTwingateResource(serviceKey.TerraformResource(), resource.TwingateServiceAccountKey), ), ExpectNonEmptyPlan: true, }, { - Config: configServiceKey(terraformResourceName, serviceAccountName), + Config: configBuilder(serviceKey, serviceAccount), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, }, @@ -259,8 +196,8 @@ func TestAccTwingateServiceKeyReCreateAfterDeletion(t *testing.T) { func TestAccTwingateServiceKeyCreateWithInvalidExpiration(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -268,11 +205,11 @@ func TestAccTwingateServiceKeyCreateWithInvalidExpiration(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, -1), + Config: configBuilder(serviceAccount, serviceKey.Set(attr.ExpirationTime, -1)), ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), }, { - Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 366), + Config: configBuilder(serviceAccount, serviceKey.Set(attr.ExpirationTime, 366)), ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), }, }, @@ -282,10 +219,8 @@ func TestAccTwingateServiceKeyCreateWithInvalidExpiration(t *testing.T) { func TestAccTwingateServiceKeyCreateWithExpiration(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -293,13 +228,13 @@ func TestAccTwingateServiceKeyCreateWithExpiration(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 365), + Config: configBuilder(serviceAccount, serviceKey.Set(attr.ExpirationTime, 365)), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "true"), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.CheckTwingateResourceExists(serviceAccount.TerraformResource()), + sdk.TestCheckResourceAttr(serviceAccount.TerraformResource(), attr.Name, serviceAccount.Name), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttr(serviceKey.TerraformResource(), attr.IsActive, "true"), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, }, @@ -309,9 +244,8 @@ func TestAccTwingateServiceKeyCreateWithExpiration(t *testing.T) { func TestAccTwingateServiceKeyReCreateAfterChangingExpirationTime(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) + serviceAccount := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount.TerraformResourceID()) resourceID := new(string) @@ -321,18 +255,18 @@ func TestAccTwingateServiceKeyReCreateAfterChangingExpirationTime(t *testing.T) CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 1), + Config: configBuilder(serviceAccount, serviceKey.Set(attr.ExpirationTime, 1)), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.GetTwingateResourceID(serviceKey, &resourceID), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + acctests.GetTwingateResourceID(serviceKey.TerraformResource(), &resourceID), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), ), }, { - Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 2), + Config: configBuilder(serviceAccount, serviceKey.Set(attr.ExpirationTime, 2)), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.ID, func(value string) error { if *resourceID == "" { return errors.New("failed to fetch resource id") } @@ -352,14 +286,9 @@ func TestAccTwingateServiceKeyReCreateAfterChangingExpirationTime(t *testing.T) func TestAccTwingateServiceKeyAndServiceAccountLifecycle(t *testing.T) { t.Parallel() - serviceAccountName := test.RandomName() - serviceAccountNameV2 := test.RandomName() - terraformServiceAccountName := test.TerraformRandName("test_acc") - terraformServiceAccountNameV2 := test.TerraformRandName("test_acc_v2") - terraformServiceAccountKeyName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformServiceAccountName) - serviceAccountV2 := acctests.TerraformServiceAccount(terraformServiceAccountNameV2) - serviceKey := acctests.TerraformServiceKey(terraformServiceAccountKeyName) + serviceAccount1 := NewServiceAccount() + serviceAccount2 := NewServiceAccount() + serviceKey := NewServiceAccountKey(serviceAccount1.TerraformResourceID()) serviceKeyResourceID := new(string) serviceAccountResourceID := new(string) @@ -370,26 +299,26 @@ func TestAccTwingateServiceKeyAndServiceAccountLifecycle(t *testing.T) { CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, Steps: []sdk.TestStep{ { - Config: configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountName), + Config: configBuilder(serviceKey, serviceAccount1, serviceAccount2), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - acctests.GetTwingateResourceID(serviceKey, &serviceKeyResourceID), - acctests.GetTwingateResourceID(serviceKey, &serviceAccountResourceID), + acctests.CheckTwingateResourceExists(serviceAccount1.TerraformResource()), + sdk.TestCheckResourceAttr(serviceAccount1.TerraformResource(), attr.Name, serviceAccount1.Name), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), + acctests.GetTwingateResourceID(serviceKey.TerraformResource(), &serviceKeyResourceID), + acctests.GetTwingateResourceID(serviceKey.TerraformResource(), &serviceAccountResourceID), ), }, { - Config: configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountNameV2), + Config: configBuilder(serviceKey.Set(attr.ServiceAccountID, serviceAccount2.TerraformResourceID()), serviceAccount1, serviceAccount2), Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccountV2), - sdk.TestCheckResourceAttr(serviceAccountV2, attr.Name, serviceAccountNameV2), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.CheckTwingateResourceExists(serviceAccount2.TerraformResource()), + sdk.TestCheckResourceAttr(serviceAccount2.TerraformResource(), attr.Name, serviceAccount2.Name), + acctests.CheckTwingateResourceExists(serviceKey.TerraformResource()), + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.Token, nonEmptyValue), // test resources were re-created - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + sdk.TestCheckResourceAttrWith(serviceKey.TerraformResource(), attr.ID, func(value string) error { if *serviceKeyResourceID == "" { return errors.New("failed to fetch service_key resource id") } @@ -401,7 +330,7 @@ func TestAccTwingateServiceKeyAndServiceAccountLifecycle(t *testing.T) { return nil }), - sdk.TestCheckResourceAttrWith(serviceAccountV2, attr.ID, func(value string) error { + sdk.TestCheckResourceAttrWith(serviceAccount2.TerraformResource(), attr.ID, func(value string) error { if *serviceAccountResourceID == "" { return errors.New("failed to fetch service_account resource id") } @@ -417,27 +346,3 @@ func TestAccTwingateServiceKeyAndServiceAccountLifecycle(t *testing.T) { }, }) } - -func configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, serviceAccount string) string { - return acctests.Nprintf(` - resource "twingate_service_account" "${service_account_resource_1}" { - name = "${name_1}" - } - - resource "twingate_service_account" "${service_account_resource_2}" { - name = "${name_2}" - } - - resource "twingate_service_account_key" "${service_account_key_resource}" { - service_account_id = twingate_service_account.${service_account_resource}.id - } - `, - map[string]any{ - "service_account_resource_1": terraformServiceAccountName, - "name_1": serviceAccountName, - "service_account_resource_2": terraformServiceAccountNameV2, - "name_2": serviceAccountNameV2, - "service_account_key_resource": terraformServiceAccountKeyName, - "service_account_resource": serviceAccount, - }) -}