-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathresource_databricks_mws_credentials_mws_test.go
138 lines (128 loc) · 5 KB
/
resource_databricks_mws_credentials_mws_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package databricks
import (
"errors"
"fmt"
"os"
"testing"
"github.com/databrickslabs/databricks-terraform/client/model"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
func TestAccMWSCredentials(t *testing.T) {
var MWSCredentials model.MWSCredentials
// generate a random name for each tokenInfo test run, to avoid
// collisions from multiple concurrent tests.
// the acctest package includes many helpers such as RandStringFromCharSet
// See https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/acctest
//scope := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
mwsAcctID := os.Getenv("DATABRICKS_MWS_ACCT_ID")
mwsHost := os.Getenv("DATABRICKS_MWS_HOST")
awsAcctID := "999999999999"
credentialsName := "test-mws-credentials-tf"
roleName := "terraform-creds-role"
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testMWSCredentialsResourceDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testMWSCredentialsCreate(mwsAcctID, mwsHost, awsAcctID, roleName, credentialsName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the tokenInfo object
testMWSCredentialsResourceExists("databricks_mws_credentials.my_e2_credentials", &MWSCredentials, t),
// verify local values
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "account_id", mwsAcctID),
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "credentials_name", credentialsName),
),
Destroy: false,
},
{
// use a dynamic configuration with the random name from above
Config: testMWSCredentialsCreate(mwsAcctID, mwsHost, awsAcctID, roleName, credentialsName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the tokenInfo object
testMWSCredentialsResourceExists("databricks_mws_credentials.my_e2_credentials", &MWSCredentials, t),
// verify local values
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "account_id", mwsAcctID),
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "credentials_name", credentialsName),
),
ExpectNonEmptyPlan: false,
Destroy: false,
},
{
PreConfig: func() {
conn := getMWSClient()
err := conn.MWSCredentials().Delete(MWSCredentials.AccountID, MWSCredentials.CredentialsID)
if err != nil {
panic(err)
}
},
// use a dynamic configuration with the random name from above
Config: testMWSCredentialsCreate(mwsAcctID, mwsHost, awsAcctID, roleName, credentialsName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// verify local values
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "account_id", mwsAcctID),
resource.TestCheckResourceAttr("databricks_mws_credentials.my_e2_credentials", "credentials_name", credentialsName),
),
Destroy: false,
},
},
})
}
func testMWSCredentialsResourceDestroy(s *terraform.State) error {
client := getMWSClient()
for _, rs := range s.RootModule().Resources {
if rs.Type != "databricks_mws_credentials" {
continue
}
packagedMWSIds, err := unpackMWSAccountID(rs.Primary.ID)
if err != nil {
return err
}
_, err = client.MWSCredentials().Read(packagedMWSIds.MwsAcctID, packagedMWSIds.ResourceID)
if err != nil {
return nil
}
return errors.New("resource Scim Group is not cleaned up")
}
return nil
}
// testAccCheckTokenResourceExists queries the API and retrieves the matching Widget.
func testMWSCredentialsResourceExists(n string, mwsCreds *model.MWSCredentials, t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
// find the corresponding state object
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
// retrieve the configured client from the test setup
conn := getMWSClient()
packagedMWSIds, err := unpackMWSAccountID(rs.Primary.ID)
if err != nil {
return err
}
resp, err := conn.MWSCredentials().Read(packagedMWSIds.MwsAcctID, packagedMWSIds.ResourceID)
if err != nil {
return err
}
// If no error, assign the response Widget attribute to the widget pointer
*mwsCreds = resp
return nil
}
}
func testMWSCredentialsCreate(mwsAcctID, mwsHost, awsAcctID, roleName, credentialsName string) string {
return fmt.Sprintf(`
provider "databricks" {
host = "%s"
basic_auth {}
}
resource "databricks_mws_credentials" "my_e2_credentials" {
account_id = "%s"
credentials_name = "%s"
role_arn = "arn:aws:iam::%s:role/%s"
}
`, mwsHost, mwsAcctID, credentialsName, awsAcctID, roleName)
}