8
8
9
9
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
10
10
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
11
+ "github.com/hashicorp/terraform-plugin-framework/attr"
11
12
"github.com/hashicorp/terraform-plugin-framework/datasource"
13
+ "github.com/hashicorp/terraform-plugin-framework/diag"
12
14
"github.com/hashicorp/terraform-plugin-framework/provider"
13
15
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
14
16
"github.com/hashicorp/terraform-plugin-framework/providerserver"
@@ -21,6 +23,7 @@ import (
21
23
sdkv2schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22
24
23
25
cstmvalidator "github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/validator"
26
+ "github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
24
27
"github.com/mongodb/terraform-provider-mongodbatlas/version"
25
28
)
26
29
@@ -69,6 +72,18 @@ type tfAssumeRoleModel struct {
69
72
SourceIdentity types.String `tfsdk:"source_identity"`
70
73
}
71
74
75
+ var AssumeRoleType = types.ObjectType {AttrTypes : map [string ]attr.Type {
76
+ "policy_arns" : types.SetType {ElemType : types .StringType },
77
+ "transitive_tag_keys" : types.SetType {ElemType : types .StringType },
78
+ "tags" : types.MapType {ElemType : types .StringType },
79
+ "duration" : types .StringType ,
80
+ "external_id" : types .StringType ,
81
+ "policy" : types .StringType ,
82
+ "role_arn" : types .StringType ,
83
+ "session_name" : types .StringType ,
84
+ "source_identity" : types .StringType ,
85
+ }}
86
+
72
87
func (p * MongodbtlasProvider ) Metadata (ctx context.Context , req provider.MetadataRequest , resp * provider.MetadataResponse ) {
73
88
resp .TypeName = "mongodbatlas"
74
89
resp .Version = version .ProviderVersion
@@ -202,11 +217,7 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
202
217
return
203
218
}
204
219
205
- var assumeRoles []tfAssumeRoleModel
206
- data .AssumeRole .ElementsAs (ctx , & assumeRoles , true )
207
- awsRoleDefined := len (assumeRoles ) > 0
208
-
209
- data = setDefaultValuesWithValidations (& data , awsRoleDefined , resp )
220
+ data = setDefaultValuesWithValidations (ctx , & data , resp )
210
221
if resp .Diagnostics .HasError () {
211
222
return
212
223
}
@@ -218,10 +229,13 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
218
229
RealmBaseURL : data .RealmBaseURL .ValueString (),
219
230
}
220
231
232
+ var assumeRoles []tfAssumeRoleModel
233
+ data .AssumeRole .ElementsAs (ctx , & assumeRoles , true )
234
+ awsRoleDefined := len (assumeRoles ) > 0
221
235
if awsRoleDefined {
222
236
config .AssumeRole = parseTfModel (ctx , & assumeRoles [0 ])
223
237
secret := data .SecretName .ValueString ()
224
- region := data .Region .ValueString ()
238
+ region := util . MongoDBRegionToAWSRegion ( data .Region .ValueString () )
225
239
awsAccessKeyID := data .AwsAccessKeyID .ValueString ()
226
240
awsSecretAccessKey := data .AwsSecretAccessKeyID .ValueString ()
227
241
awsSessionToken := data .AwsSessionToken .ValueString ()
@@ -281,7 +295,7 @@ func parseTfModel(ctx context.Context, tfAssumeRoleModel *tfAssumeRoleModel) *As
281
295
282
296
const MongodbGovCloudURL = "https://cloud.mongodbgov.com"
283
297
284
- func setDefaultValuesWithValidations (data * tfMongodbAtlasProviderModel , awsRoleDefined bool , resp * provider.ConfigureResponse ) tfMongodbAtlasProviderModel {
298
+ func setDefaultValuesWithValidations (ctx context. Context , data * tfMongodbAtlasProviderModel , resp * provider.ConfigureResponse ) tfMongodbAtlasProviderModel {
285
299
if mongodbgovCloud := data .IsMongodbGovCloud .ValueBool (); mongodbgovCloud {
286
300
data .BaseURL = types .StringValue (MongodbGovCloudURL )
287
301
}
@@ -292,6 +306,31 @@ func setDefaultValuesWithValidations(data *tfMongodbAtlasProviderModel, awsRoleD
292
306
}, "" ).(string ))
293
307
}
294
308
309
+ awsRoleDefined := false
310
+ if len (data .AssumeRole .Elements ()) == 0 {
311
+ assumeRoleArn := MultiEnvDefaultFunc ([]string {
312
+ "ASSUME_ROLE_ARN" ,
313
+ "TF_VAR_ASSUME_ROLE_ARN" ,
314
+ }, "" ).(string )
315
+ if assumeRoleArn != "" {
316
+ awsRoleDefined = true
317
+ var diags diag.Diagnostics
318
+ data .AssumeRole , diags = types .ListValueFrom (ctx , AssumeRoleType , []tfAssumeRoleModel {
319
+ {
320
+ Tags : types .MapNull (types .StringType ),
321
+ PolicyARNs : types .SetNull (types .StringType ),
322
+ TransitiveTagKeys : types .SetNull (types .StringType ),
323
+ RoleARN : types .StringValue (assumeRoleArn ),
324
+ },
325
+ })
326
+ if diags .HasError () {
327
+ resp .Diagnostics .Append (diags ... )
328
+ }
329
+ }
330
+ } else {
331
+ awsRoleDefined = true
332
+ }
333
+
295
334
if data .PublicKey .ValueString () == "" {
296
335
data .PublicKey = types .StringValue (MultiEnvDefaultFunc ([]string {
297
336
"MONGODB_ATLAS_PUBLIC_KEY" ,
@@ -353,6 +392,13 @@ func setDefaultValuesWithValidations(data *tfMongodbAtlasProviderModel, awsRoleD
353
392
}, "" ).(string ))
354
393
}
355
394
395
+ if data .SecretName .ValueString () == "" {
396
+ data .SecretName = types .StringValue (MultiEnvDefaultFunc ([]string {
397
+ "SECRET_NAME" ,
398
+ "TF_VAR_SECRET_NAME" ,
399
+ }, "" ).(string ))
400
+ }
401
+
356
402
return * data
357
403
}
358
404
0 commit comments