@@ -8,16 +8,19 @@ import (
8
8
"fmt"
9
9
10
10
"github.com/aws/aws-sdk-go/aws"
11
+ "github.com/aws/aws-sdk-go/service/rds"
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
14
14
"github.com/hashicorp/terraform-provider-aws/internal/conns"
15
15
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
16
+ tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
16
17
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
17
18
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
19
+ "github.com/hashicorp/terraform-provider-aws/names"
18
20
)
19
21
20
- // @SDKDataSource("aws_db_instance")
22
+ // @SDKDataSource("aws_db_instance", name="DB Instance")
23
+ // @Tags
21
24
func DataSourceInstance () * schema.Resource {
22
25
return & schema.Resource {
23
26
ReadWithoutTimeout : dataSourceInstanceRead ,
@@ -60,9 +63,9 @@ func DataSourceInstance() *schema.Resource {
60
63
Computed : true ,
61
64
},
62
65
"db_instance_identifier" : {
63
- Type : schema .TypeString ,
64
- Required : true ,
65
- ValidateFunc : validation . StringIsNotEmpty ,
66
+ Type : schema .TypeString ,
67
+ Optional : true ,
68
+ Computed : true ,
66
69
},
67
70
"db_instance_port" : {
68
71
Type : schema .TypeInt ,
@@ -199,7 +202,7 @@ func DataSourceInstance() *schema.Resource {
199
202
Type : schema .TypeString ,
200
203
Computed : true ,
201
204
},
202
- "tags" : tftags .TagsSchemaComputed (),
205
+ names . AttrTags : tftags .TagsSchemaComputed (),
203
206
"timezone" : {
204
207
Type : schema .TypeString ,
205
208
Computed : true ,
@@ -216,74 +219,104 @@ func DataSourceInstance() *schema.Resource {
216
219
func dataSourceInstanceRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
217
220
var diags diag.Diagnostics
218
221
conn := meta .(* conns.AWSClient ).RDSConn (ctx )
219
- ignoreTagsConfig := meta .(* conns.AWSClient ).IgnoreTagsConfig
220
222
221
- v , err := findDBInstanceByIDSDKv1 (ctx , conn , d .Get ("db_instance_identifier" ).(string ))
222
- if err != nil {
223
- return diag .FromErr (tfresource .SingularDataSourceFindError ("RDS DB Instance" , err ))
223
+ var instance * rds.DBInstance
224
+
225
+ filter := tfslices .PredicateTrue [* rds.DBInstance ]()
226
+ if tags := getTagsIn (ctx ); len (tags ) > 0 {
227
+ filter = func (v * rds.DBInstance ) bool {
228
+ return KeyValueTags (ctx , v .TagList ).ContainsAll (KeyValueTags (ctx , tags ))
229
+ }
224
230
}
225
231
226
- d .SetId (aws .StringValue (v .DBInstanceIdentifier ))
227
- d .Set ("allocated_storage" , v .AllocatedStorage )
228
- d .Set ("auto_minor_version_upgrade" , v .AutoMinorVersionUpgrade )
229
- d .Set ("availability_zone" , v .AvailabilityZone )
230
- d .Set ("backup_retention_period" , v .BackupRetentionPeriod )
231
- d .Set ("ca_cert_identifier" , v .CACertificateIdentifier )
232
- d .Set ("db_cluster_identifier" , v .DBClusterIdentifier )
233
- d .Set ("db_instance_arn" , v .DBInstanceArn )
234
- d .Set ("db_instance_class" , v .DBInstanceClass )
235
- d .Set ("db_instance_port" , v .DbInstancePort )
236
- d .Set ("db_name" , v .DBName )
237
- var parameterGroupNames []string
238
- for _ , v := range v .DBParameterGroups {
239
- parameterGroupNames = append (parameterGroupNames , aws .StringValue (v .DBParameterGroupName ))
232
+ if v , ok := d .GetOk ("db_instance_identifier" ); ok {
233
+ id := v .(string )
234
+ output , err := findDBInstanceByIDSDKv1 (ctx , conn , id )
235
+
236
+ if err != nil {
237
+ return sdkdiag .AppendErrorf (diags , "reading RDS DB Instance (%s): %s" , id , err )
238
+ }
239
+
240
+ if ! filter (output ) {
241
+ return sdkdiag .AppendErrorf (diags , "Your query returned no results. Please change your search criteria and try again." )
242
+ }
243
+
244
+ instance = output
245
+ } else {
246
+ input := & rds.DescribeDBInstancesInput {}
247
+ instances , err := findDBInstancesSDKv1 (ctx , conn , input , filter )
248
+
249
+ if err != nil {
250
+ return sdkdiag .AppendErrorf (diags , "reading RDS DB Instances: %s" , err )
251
+ }
252
+
253
+ output , err := tfresource .AssertSinglePtrResult (instances )
254
+
255
+ if err != nil {
256
+ return sdkdiag .AppendFromErr (diags , tfresource .SingularDataSourceFindError ("RDS DB Instance" , err ))
257
+ }
258
+
259
+ instance = output
240
260
}
261
+
262
+ d .SetId (aws .StringValue (instance .DBInstanceIdentifier ))
263
+ d .Set ("allocated_storage" , instance .AllocatedStorage )
264
+ d .Set ("auto_minor_version_upgrade" , instance .AutoMinorVersionUpgrade )
265
+ d .Set ("availability_zone" , instance .AvailabilityZone )
266
+ d .Set ("backup_retention_period" , instance .BackupRetentionPeriod )
267
+ d .Set ("ca_cert_identifier" , instance .CACertificateIdentifier )
268
+ d .Set ("db_cluster_identifier" , instance .DBClusterIdentifier )
269
+ d .Set ("db_instance_arn" , instance .DBInstanceArn )
270
+ d .Set ("db_instance_class" , instance .DBInstanceClass )
271
+ d .Set ("db_instance_port" , instance .DbInstancePort )
272
+ d .Set ("db_name" , instance .DBName )
273
+ parameterGroupNames := tfslices .ApplyToAll (instance .DBParameterGroups , func (v * rds.DBParameterGroupStatus ) string {
274
+ return aws .StringValue (v .DBParameterGroupName )
275
+ })
241
276
d .Set ("db_parameter_groups" , parameterGroupNames )
242
- if v .DBSubnetGroup != nil {
243
- d .Set ("db_subnet_group" , v .DBSubnetGroup .DBSubnetGroupName )
277
+ if instance .DBSubnetGroup != nil {
278
+ d .Set ("db_subnet_group" , instance .DBSubnetGroup .DBSubnetGroupName )
244
279
} else {
245
280
d .Set ("db_subnet_group" , "" )
246
281
}
247
- d .Set ("enabled_cloudwatch_logs_exports" , aws .StringValueSlice (v .EnabledCloudwatchLogsExports ))
248
- d .Set ("engine" , v .Engine )
249
- d .Set ("engine_version" , v .EngineVersion )
250
- d .Set ("iops" , v .Iops )
251
- d .Set ("kms_key_id" , v .KmsKeyId )
252
- d .Set ("license_model" , v .LicenseModel )
253
- d .Set ("master_username" , v .MasterUsername )
254
- if v .MasterUserSecret != nil {
255
- if err := d .Set ("master_user_secret" , []interface {}{flattenManagedMasterUserSecret (v .MasterUserSecret )}); err != nil {
282
+ d .Set ("enabled_cloudwatch_logs_exports" , aws .StringValueSlice (instance .EnabledCloudwatchLogsExports ))
283
+ d .Set ("engine" , instance .Engine )
284
+ d .Set ("engine_version" , instance .EngineVersion )
285
+ d .Set ("iops" , instance .Iops )
286
+ d .Set ("kms_key_id" , instance .KmsKeyId )
287
+ d .Set ("license_model" , instance .LicenseModel )
288
+ d .Set ("master_username" , instance .MasterUsername )
289
+ if instance .MasterUserSecret != nil {
290
+ if err := d .Set ("master_user_secret" , []interface {}{flattenManagedMasterUserSecret (instance .MasterUserSecret )}); err != nil {
256
291
return sdkdiag .AppendErrorf (diags , "setting master_user_secret: %s" , err )
257
292
}
258
293
}
259
- d .Set ("max_allocated_storage" , v .MaxAllocatedStorage )
260
- d .Set ("monitoring_interval" , v .MonitoringInterval )
261
- d .Set ("monitoring_role_arn" , v .MonitoringRoleArn )
262
- d .Set ("multi_az" , v .MultiAZ )
263
- d .Set ("network_type" , v .NetworkType )
264
- var optionGroupNames []string
265
- for _ , v := range v .OptionGroupMemberships {
266
- optionGroupNames = append (optionGroupNames , aws .StringValue (v .OptionGroupName ))
267
- }
294
+ d .Set ("max_allocated_storage" , instance .MaxAllocatedStorage )
295
+ d .Set ("monitoring_interval" , instance .MonitoringInterval )
296
+ d .Set ("monitoring_role_arn" , instance .MonitoringRoleArn )
297
+ d .Set ("multi_az" , instance .MultiAZ )
298
+ d .Set ("network_type" , instance .NetworkType )
299
+ optionGroupNames := tfslices .ApplyToAll (instance .OptionGroupMemberships , func (v * rds.OptionGroupMembership ) string {
300
+ return aws .StringValue (v .OptionGroupName )
301
+ })
268
302
d .Set ("option_group_memberships" , optionGroupNames )
269
- d .Set ("preferred_backup_window" , v .PreferredBackupWindow )
270
- d .Set ("preferred_maintenance_window" , v .PreferredMaintenanceWindow )
271
- d .Set ("publicly_accessible" , v .PubliclyAccessible )
272
- d .Set ("replicate_source_db" , v .ReadReplicaSourceDBInstanceIdentifier )
273
- d .Set ("resource_id" , v .DbiResourceId )
274
- d .Set ("storage_encrypted" , v .StorageEncrypted )
275
- d .Set ("storage_throughput" , v .StorageThroughput )
276
- d .Set ("storage_type" , v .StorageType )
277
- d .Set ("timezone" , v .Timezone )
278
- var vpcSecurityGroupIDs []string
279
- for _ , v := range v .VpcSecurityGroups {
280
- vpcSecurityGroupIDs = append (vpcSecurityGroupIDs , aws .StringValue (v .VpcSecurityGroupId ))
281
- }
303
+ d .Set ("preferred_backup_window" , instance .PreferredBackupWindow )
304
+ d .Set ("preferred_maintenance_window" , instance .PreferredMaintenanceWindow )
305
+ d .Set ("publicly_accessible" , instance .PubliclyAccessible )
306
+ d .Set ("replicate_source_db" , instance .ReadReplicaSourceDBInstanceIdentifier )
307
+ d .Set ("resource_id" , instance .DbiResourceId )
308
+ d .Set ("storage_encrypted" , instance .StorageEncrypted )
309
+ d .Set ("storage_throughput" , instance .StorageThroughput )
310
+ d .Set ("storage_type" , instance .StorageType )
311
+ d .Set ("timezone" , instance .Timezone )
312
+ vpcSecurityGroupIDs := tfslices .ApplyToAll (instance .VpcSecurityGroups , func (v * rds.VpcSecurityGroupMembership ) string {
313
+ return aws .StringValue (v .VpcSecurityGroupId )
314
+ })
282
315
d .Set ("vpc_security_groups" , vpcSecurityGroupIDs )
283
316
284
317
// Per AWS SDK Go docs:
285
318
// The endpoint might not be shown for instances whose status is creating.
286
- if dbEndpoint := v .Endpoint ; dbEndpoint != nil {
319
+ if dbEndpoint := instance .Endpoint ; dbEndpoint != nil {
287
320
d .Set ("address" , dbEndpoint .Address )
288
321
d .Set ("endpoint" , fmt .Sprintf ("%s:%d" , aws .StringValue (dbEndpoint .Address ), aws .Int64Value (dbEndpoint .Port )))
289
322
d .Set ("hosted_zone_id" , dbEndpoint .HostedZoneId )
@@ -295,11 +328,7 @@ func dataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, meta in
295
328
d .Set ("port" , nil )
296
329
}
297
330
298
- tags := KeyValueTags (ctx , v .TagList )
299
-
300
- if err := d .Set ("tags" , tags .IgnoreAWS ().IgnoreConfig (ignoreTagsConfig ).Map ()); err != nil {
301
- return sdkdiag .AppendErrorf (diags , "setting tags: %s" , err )
302
- }
331
+ setTagsOut (ctx , instance .TagList )
303
332
304
333
return diags
305
334
}
0 commit comments