Skip to content

Commit a8154ea

Browse files
Add conformance tests for fabric credential resource and fix issues (#370)
* Add conformance tests for fabric credential resource and fix issues * Reuse existing global variable for version
1 parent 269c2dc commit a8154ea

2 files changed

Lines changed: 157 additions & 76 deletions

File tree

pkg/framework/objects/fabric_credential/resource_acceptance_test.go

Lines changed: 144 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,62 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
910
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
1011
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
1112
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1213
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1314
"github.com/hashicorp/terraform-plugin-testing/terraform"
1415
)
1516

17+
func TestConformanceBasicConfig(t *testing.T) {
18+
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
19+
user := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
20+
password := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
21+
22+
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
24+
CheckDestroy: testAccCheckDbtCloudFabricCredentialDestroy,
25+
Steps: []resource.TestStep{
26+
acctest_helper.MakeExternalProviderTestStep(getBasicConfigTestStep(projectName, user, password), acctest_config.LAST_VERSION_BEFORE_FRAMEWORK_MIGRATION),
27+
acctest_helper.MakeCurrentProviderNoOpTestStep(getBasicConfigTestStep(projectName, user, password)),
28+
},
29+
})
30+
}
31+
32+
func TestConformanceModifyConfig(t *testing.T) {
33+
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
34+
clientId := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
35+
tenantId := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
36+
clientSecret := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
37+
38+
// MODIFY: test that running commands in SDKv2 and then the same commands in Framework generates a NoOp plan
39+
resource.Test(t, resource.TestCase{
40+
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
41+
CheckDestroy: testAccCheckDbtCloudFabricCredentialDestroy,
42+
Steps: []resource.TestStep{
43+
acctest_helper.MakeExternalProviderTestStep(getModifyConfigTestStep(projectName, clientId, tenantId, clientSecret), acctest_config.LAST_VERSION_BEFORE_FRAMEWORK_MIGRATION),
44+
acctest_helper.MakeCurrentProviderNoOpTestStep(getModifyConfigTestStep(projectName, clientId, tenantId, clientSecret)),
45+
},
46+
})
47+
}
48+
49+
func TestConformanceImportConfig(t *testing.T) {
50+
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
51+
user := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
52+
password := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
53+
54+
// Import: test that running an import in SDKv2 and then the same thing in Framework generates a NoOp plan
55+
resource.Test(t, resource.TestCase{
56+
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
57+
CheckDestroy: testAccCheckDbtCloudFabricCredentialDestroy,
58+
Steps: []resource.TestStep{
59+
acctest_helper.MakeExternalProviderTestStep(getBasicConfigTestStep(projectName, user, password), acctest_config.LAST_VERSION_BEFORE_FRAMEWORK_MIGRATION),
60+
acctest_helper.MakeCurrentProviderNoOpTestStep(getBasicConfigTestStep(projectName, user, password)),
61+
},
62+
})
63+
}
64+
1665
func TestAccDbtCloudFabricCredentialResource(t *testing.T) {
1766

1867
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
@@ -22,89 +71,108 @@ func TestAccDbtCloudFabricCredentialResource(t *testing.T) {
2271
tenantId := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
2372
clientSecret := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
2473

74+
var basicConfigTestStep = getBasicConfigTestStep(projectName, user, password)
75+
76+
var modifyConfigTestStep = getModifyConfigTestStep(projectName, clientId, tenantId, clientSecret)
77+
78+
var importConfigTestStep = getImportConfigTestStep()
79+
2580
resource.Test(t, resource.TestCase{
2681
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
2782
ProtoV6ProviderFactories: acctest_helper.TestAccProtoV6ProviderFactories,
2883
CheckDestroy: testAccCheckDbtCloudFabricCredentialDestroy,
2984
Steps: []resource.TestStep{
30-
{
31-
Config: testAccDbtCloudFabricCredentialResourceUserPassConfig(
32-
projectName,
33-
user,
34-
password,
35-
),
36-
Check: resource.ComposeTestCheckFunc(
37-
testAccCheckDbtCloudFabricCredentialExists(
38-
"dbtcloud_fabric_credential.test_credential",
39-
),
40-
resource.TestCheckResourceAttr(
41-
"dbtcloud_fabric_credential.test_credential",
42-
"user",
43-
user,
44-
),
45-
resource.TestCheckResourceAttr(
46-
"dbtcloud_fabric_credential.test_credential",
47-
"schema",
48-
"my_schema",
49-
),
50-
resource.TestCheckResourceAttr(
51-
"dbtcloud_fabric_credential.test_credential",
52-
"schema_authorization",
53-
"sp",
54-
),
55-
),
56-
},
57-
// RENAME
58-
// MODIFY
59-
{
60-
Config: testAccDbtCloudFabricCredentialResourceServicePrincipalConfig(
61-
projectName, clientId, tenantId, clientSecret,
62-
),
63-
Check: resource.ComposeTestCheckFunc(
64-
testAccCheckDbtCloudFabricCredentialExists(
65-
"dbtcloud_fabric_credential.test_credential",
66-
),
67-
resource.TestCheckResourceAttr(
68-
"dbtcloud_fabric_credential.test_credential",
69-
"client_id",
70-
clientId,
71-
),
72-
resource.TestCheckResourceAttr(
73-
"dbtcloud_fabric_credential.test_credential",
74-
"tenant_id",
75-
tenantId,
76-
),
77-
),
78-
},
79-
// IMPORT
80-
{
81-
ResourceName: "dbtcloud_fabric_credential.test_credential",
82-
ImportState: true,
83-
ImportStateVerify: true,
84-
ImportStateVerifyIgnore: []string{"password", "client_secret", "schema_authorization", "user"},
85-
ImportStateCheck: func(s []*terraform.InstanceState) error {
86-
if len(s) != 1 {
87-
return fmt.Errorf("expected 1 state, got %d", len(s))
88-
}
89-
state := s[0]
90-
if state.Attributes["adapter_id"] == "" {
91-
return fmt.Errorf("missing adapter_id in import state")
92-
}
93-
if state.Attributes["client_id"] == "" {
94-
return fmt.Errorf("missing client_id in import state")
95-
}
96-
if state.Attributes["tenant_id"] == "" {
97-
return fmt.Errorf("missing tenant_id in import state")
98-
}
99-
if state.Attributes["schema"] == "" {
100-
return fmt.Errorf("missing schema in import state")
101-
}
102-
103-
return nil
104-
},
105-
},
85+
basicConfigTestStep,
86+
modifyConfigTestStep,
87+
importConfigTestStep,
10688
},
10789
})
90+
91+
}
92+
93+
func getBasicConfigTestStep(projectName, user, password string) resource.TestStep {
94+
return resource.TestStep{
95+
Config: testAccDbtCloudFabricCredentialResourceUserPassConfig(
96+
projectName,
97+
user,
98+
password,
99+
),
100+
Check: resource.ComposeTestCheckFunc(
101+
testAccCheckDbtCloudFabricCredentialExists(
102+
"dbtcloud_fabric_credential.test_credential",
103+
),
104+
resource.TestCheckResourceAttr(
105+
"dbtcloud_fabric_credential.test_credential",
106+
"user",
107+
user,
108+
),
109+
resource.TestCheckResourceAttr(
110+
"dbtcloud_fabric_credential.test_credential",
111+
"schema",
112+
"my_schema",
113+
),
114+
resource.TestCheckResourceAttr(
115+
"dbtcloud_fabric_credential.test_credential",
116+
"schema_authorization",
117+
"sp",
118+
),
119+
),
120+
}
121+
}
122+
123+
func getModifyConfigTestStep(projectName, clientId, tenantId, clientSecret string) resource.TestStep {
124+
return resource.TestStep{
125+
Config: testAccDbtCloudFabricCredentialResourceServicePrincipalConfig(
126+
projectName,
127+
clientId,
128+
tenantId,
129+
clientSecret,
130+
),
131+
Check: resource.ComposeTestCheckFunc(
132+
testAccCheckDbtCloudFabricCredentialExists(
133+
"dbtcloud_fabric_credential.test_credential",
134+
),
135+
resource.TestCheckResourceAttr(
136+
"dbtcloud_fabric_credential.test_credential",
137+
"client_id",
138+
clientId,
139+
),
140+
resource.TestCheckResourceAttr(
141+
"dbtcloud_fabric_credential.test_credential",
142+
"tenant_id",
143+
tenantId,
144+
),
145+
),
146+
}
147+
}
148+
149+
func getImportConfigTestStep() resource.TestStep {
150+
return resource.TestStep{
151+
ResourceName: "dbtcloud_fabric_credential.test_credential",
152+
ImportState: true,
153+
ImportStateVerify: true,
154+
ImportStateVerifyIgnore: []string{"password", "client_secret", "schema_authorization", "user"},
155+
ImportStateCheck: func(s []*terraform.InstanceState) error {
156+
if len(s) != 1 {
157+
return fmt.Errorf("expected 1 state, got %d", len(s))
158+
}
159+
state := s[0]
160+
if state.Attributes["adapter_id"] == "" {
161+
return fmt.Errorf("missing adapter_id in import state")
162+
}
163+
if state.Attributes["client_id"] == "" {
164+
return fmt.Errorf("missing client_id in import state")
165+
}
166+
if state.Attributes["tenant_id"] == "" {
167+
return fmt.Errorf("missing tenant_id in import state")
168+
}
169+
if state.Attributes["schema"] == "" {
170+
return fmt.Errorf("missing schema in import state")
171+
}
172+
173+
return nil
174+
},
175+
}
108176
}
109177

110178
func testAccDbtCloudFabricCredentialResourceUserPassConfig(

pkg/framework/objects/fabric_credential/schema.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
resource_schema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
99
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
11+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1213
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1314
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -43,6 +44,8 @@ var resourceSchema = resource_schema.Schema{
4344
},
4445
"user": resource_schema.StringAttribute{
4546
Optional: true,
47+
Computed: true,
48+
Default: stringdefault.StaticString(""),
4649
Description: "The username of the Fabric account to connect to. Only used when connection with AD user/pass",
4750
Validators: []validator.String{
4851
conflictingFieldsValidator{
@@ -53,6 +56,8 @@ var resourceSchema = resource_schema.Schema{
5356
"password": resource_schema.StringAttribute{
5457
Optional: true,
5558
Sensitive: true,
59+
Computed: true,
60+
Default: stringdefault.StaticString(""),
5661
Description: "The password for the account to connect to. Only used when connection with AD user/pass",
5762
Validators: []validator.String{
5863
conflictingFieldsValidator{
@@ -62,6 +67,8 @@ var resourceSchema = resource_schema.Schema{
6267
},
6368
"tenant_id": resource_schema.StringAttribute{
6469
Optional: true,
70+
Computed: true,
71+
Default: stringdefault.StaticString(""),
6572
Description: "The tenant ID of the Azure Active Directory instance. This is only used when connecting to Azure SQL with a service principal.",
6673
Validators: []validator.String{
6774
conflictingFieldsValidator{
@@ -70,7 +77,9 @@ var resourceSchema = resource_schema.Schema{
7077
},
7178
},
7279
"client_id": resource_schema.StringAttribute{
80+
Computed: true,
7381
Optional: true,
82+
Default: stringdefault.StaticString(""),
7483
Description: "The client ID of the Azure Active Directory service principal. This is only used when connecting to Azure SQL with an AAD service principal.",
7584
Validators: []validator.String{
7685
conflictingFieldsValidator{
@@ -80,7 +89,9 @@ var resourceSchema = resource_schema.Schema{
8089
},
8190
"client_secret": resource_schema.StringAttribute{
8291
Optional: true,
92+
Computed: true,
8393
Sensitive: true,
94+
Default: stringdefault.StaticString(""),
8495
Description: "The client secret of the Azure Active Directory service principal. This is only used when connecting to Azure SQL with an AAD service principal.",
8596
Validators: []validator.String{
8697
conflictingFieldsValidator{
@@ -94,6 +105,8 @@ var resourceSchema = resource_schema.Schema{
94105
},
95106
"schema_authorization": resource_schema.StringAttribute{
96107
Optional: true,
108+
Computed: true,
109+
Default: stringdefault.StaticString(""),
97110
Description: "Optionally set this to the principal who should own the schemas created by dbt",
98111
},
99112
},

0 commit comments

Comments
 (0)