@@ -6,6 +6,7 @@ package vault
6
6
import (
7
7
"fmt"
8
8
"strconv"
9
+ "strings"
9
10
"testing"
10
11
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -22,6 +23,13 @@ func TestAccAWSAuthBackendSTSRole_import(t *testing.T) {
22
23
accountID := strconv .Itoa (acctest .RandInt ())
23
24
arn := acctest .RandomWithPrefix ("arn:aws:iam::" + accountID + ":role/test-role" )
24
25
externalID := "external-id"
26
+
27
+ importStateVerifyIgnore := make ([]string , 0 )
28
+ // Ignore external_id if Vault version is < 1.17.0.
29
+ if ! provider .IsAPISupported (testProvider .Meta (), provider .VaultVersion117 ) {
30
+ importStateVerifyIgnore = append (importStateVerifyIgnore , consts .FieldExternalID )
31
+ }
32
+
25
33
resource .Test (t , resource.TestCase {
26
34
PreCheck : func () { testutil .TestAccPreCheck (t ) },
27
35
ProviderFactories : providerFactories ,
@@ -32,9 +40,10 @@ func TestAccAWSAuthBackendSTSRole_import(t *testing.T) {
32
40
Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , arn ),
33
41
},
34
42
{
35
- ResourceName : "vault_aws_auth_backend_sts_role.role" ,
36
- ImportState : true ,
37
- ImportStateVerify : true ,
43
+ ResourceName : "vault_aws_auth_backend_sts_role.role" ,
44
+ ImportState : true ,
45
+ ImportStateVerify : true ,
46
+ ImportStateVerifyIgnore : importStateVerifyIgnore ,
38
47
},
39
48
},
40
49
})
@@ -56,13 +65,18 @@ func TestAccAWSAuthBackendSTSRole_basic(t *testing.T) {
56
65
Config : testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , arn , "" ),
57
66
Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , arn ),
58
67
},
68
+ {
69
+ // Update ARN.
70
+ Config : testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , updatedArn , "" ),
71
+ Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , updatedArn ),
72
+ },
59
73
{
60
74
// Add external ID.
61
- Config : testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , arn , externalID ),
62
- Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , arn ),
75
+ Config : testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , updatedArn , externalID ),
76
+ Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , updatedArn ),
63
77
},
64
78
{
65
- // Update ARN and external ID.
79
+ // Update external ID.
66
80
Config : testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , updatedArn , updatedExternalID ),
67
81
Check : testAccAWSAuthBackendSTSRoleCheck_attrs (backend , accountID , updatedArn ),
68
82
},
@@ -130,9 +144,13 @@ func testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, stsRole string)
130
144
}
131
145
132
146
attrs := map [string ]string {
133
- "sts_role" : "sts_role" ,
134
- consts .FieldExternalID : consts .FieldExternalID ,
147
+ "sts_role" : "sts_role" ,
135
148
}
149
+ // Only check external_id if Vault version is >= 1.17.0
150
+ if provider .IsAPISupported (testProvider .Meta (), provider .VaultVersion117 ) {
151
+ attrs [consts .FieldExternalID ] = consts .FieldExternalID
152
+ }
153
+
136
154
for stateAttr , apiAttr := range attrs {
137
155
if resp .Data [apiAttr ] == nil && instanceState .Attributes [stateAttr ] == "" {
138
156
continue
@@ -146,30 +164,27 @@ func testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, stsRole string)
146
164
}
147
165
148
166
func testAccAWSAuthBackendSTSRoleConfig_basic (backend , accountID , stsRole , externalID string ) string {
149
- roleResource := fmt .Sprintf (`
150
- resource "vault_aws_auth_backend_sts_role" "role" {
151
- backend = vault_auth_backend.aws.path
152
- account_id = "%s"
153
- sts_role = "%s"
154
- }
155
- ` , accountID , stsRole )
167
+ backendResource := fmt .Sprintf (`
168
+ resource "vault_auth_backend" "aws" {
169
+ type = "aws"
170
+ path = "%s"
171
+ }` , backend )
156
172
173
+ roleResourceOptionalFields := ""
157
174
if externalID != "" {
158
- roleResource = fmt .Sprintf (`
175
+ roleResourceOptionalFields += fmt .Sprintf (`
176
+ external_id = "%s"` , externalID )
177
+ }
178
+
179
+ roleResource := fmt .Sprintf (`
159
180
resource "vault_aws_auth_backend_sts_role" "role" {
160
181
backend = vault_auth_backend.aws.path
161
182
account_id = "%s"
162
- sts_role = "%s"
163
- external_id = "%s"
183
+ sts_role = "%s"%s
164
184
}
165
- ` , accountID , stsRole , externalID )
166
- }
185
+ ` , accountID , stsRole , roleResourceOptionalFields )
167
186
168
- return fmt .Sprintf (`
169
- resource "vault_auth_backend" "aws" {
170
- type = "aws"
171
- path = "%s"
172
- }
173
- %s
174
- ` , backend , roleResource )
187
+ resources := []string {backendResource , roleResource }
188
+
189
+ return strings .Join (resources , "\n " )
175
190
}
0 commit comments