diff --git a/internal/service/dms/replication_instance_data_source.go b/internal/service/dms/replication_instance_data_source.go new file mode 100644 index 000000000000..3b60d3cc60a5 --- /dev/null +++ b/internal/service/dms/replication_instance_data_source.go @@ -0,0 +1,165 @@ +package dms + +import ( + "context" + + "github.com/aws/aws-sdk-go/aws" + dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/create" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" +) + +// @SDKDataSource("aws_dms_replication_instance") +func DataSourceReplicationInstance() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceReplicationInstanceRead, + + Schema: map[string]*schema.Schema{ + "allocated_storage": { + Type: schema.TypeInt, + Computed: true, + }, + "allow_major_version_upgrade": { + Type: schema.TypeBool, + Computed: true, + }, + "apply_immediately": { + Type: schema.TypeBool, + Computed: true, + }, + "auto_minor_version_upgrade": { + Type: schema.TypeBool, + Computed: true, + }, + "availability_zone": { + Type: schema.TypeString, + Computed: true, + }, + "engine_version": { + Type: schema.TypeString, + Computed: true, + }, + "kms_key_arn": { + Type: schema.TypeString, + Computed: true, + }, + "multi_az": { + Type: schema.TypeBool, + Computed: true, + }, + "preferred_maintenance_window": { + Type: schema.TypeString, + Computed: true, + }, + "publicly_accessible": { + Type: schema.TypeBool, + Computed: true, + }, + "replication_instance_arn": { + Type: schema.TypeString, + Computed: true, + }, + "replication_instance_class": { + Type: schema.TypeString, + Computed: true, + }, + "replication_instance_id": { + Type: schema.TypeString, + Required: true, + }, + "replication_instance_private_ips": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "replication_instance_public_ips": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "replication_subnet_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "vpc_security_group_ids": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Computed: true, + }, + "tags": tftags.TagsSchemaComputed(), + }, + } +} + +const ( + DSNameReplicationInstance = "Replication Instance Data Source" +) + +func dataSourceReplicationInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).DMSConn() + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + rID := d.Get("replication_instance_id").(string) + + instance, err := FindReplicationInstanceByID(ctx, conn, rID) + if err != nil { + return create.DiagError(names.DMS, create.ErrActionReading, DSNameReplicationTask, d.Id(), err) + } + + d.SetId(aws.StringValue(instance.ReplicationInstanceIdentifier)) + + d.Set("allocated_storage", instance.AllocatedStorage) + d.Set("auto_minor_version_upgrade", instance.AutoMinorVersionUpgrade) + d.Set("availability_zone", instance.AvailabilityZone) + d.Set("engine_version", instance.EngineVersion) + d.Set("kms_key_arn", instance.KmsKeyId) + d.Set("multi_az", instance.MultiAZ) + d.Set("preferred_maintenance_window", instance.PreferredMaintenanceWindow) + d.Set("publicly_accessible", instance.PubliclyAccessible) + d.Set("replication_instance_arn", instance.ReplicationInstanceArn) + d.Set("replication_instance_class", instance.ReplicationInstanceClass) + d.Set("replication_instance_id", instance.ReplicationInstanceIdentifier) + + tags, err := ListTags(ctx, conn, aws.StringValue(instance.ReplicationInstanceArn)) + + if err != nil { + return create.DiagError(names.DMS, create.ErrActionReading, DSNameReplicationInstance, d.Id(), err) + } + + tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + + //lintignore:AWSR002 + if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { + return create.DiagError(names.DMS, create.ErrActionSetting, DSNameReplicationInstance, d.Id(), err) + } + + return nil +} + +func FindReplicationInstanceByID(ctx context.Context, conn *dms.DatabaseMigrationService, id string) (*dms.ReplicationInstance, error) { + input := &dms.DescribeReplicationInstancesInput{ + Filters: []*dms.Filter{ + { + Name: aws.String("replication-instance-id"), + Values: []*string{aws.String(id)}, + }, + }, + } + response, err := conn.DescribeReplicationInstancesWithContext(ctx, input) + if err != nil { + return nil, err + } + + if response == nil || len(response.ReplicationInstances) == 0 || response.ReplicationInstances[0] == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return response.ReplicationInstances[0], nil +} diff --git a/internal/service/dms/replication_instance_data_source_test.go b/internal/service/dms/replication_instance_data_source_test.go new file mode 100644 index 000000000000..3bc1135d2aac --- /dev/null +++ b/internal/service/dms/replication_instance_data_source_test.go @@ -0,0 +1,49 @@ +package dms_test + +import ( + "fmt" + "testing" + + dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccDMSReplicationInstanceDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + replicationInstanceClass := "dms.c4.large" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_dms_replication_instance.test" + dataSourceName := "data.aws_dms_replication_instance.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, dms.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(ctx, t), + CheckDestroy: testAccCheckReplicationInstanceDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccReplicationInstanceDataSourceConfig_basic(rName, replicationInstanceClass), + Check: resource.ComposeTestCheckFunc( + testAccCheckReplicationInstanceExists(ctx, dataSourceName), + resource.TestCheckResourceAttrPair(dataSourceName, "replication_instance_id", resourceName, "replication_instance_id"), + ), + }, + }, + }) +} + +func testAccReplicationInstanceDataSourceConfig_basic(rName, replicationInstanceClass string) string { + return fmt.Sprintf(` +resource "aws_dms_replication_instance" "test" { + apply_immediately = true + replication_instance_class = %q + replication_instance_id = %q +} + +data "aws_dms_replication_instance" "test" { + replication_instance_id = aws_dms_replication_instance.test.replication_instance_id +} +`, replicationInstanceClass, rName) +} diff --git a/internal/service/dms/service_package_gen.go b/internal/service/dms/service_package_gen.go index 1d3439b33f04..250b23ed9540 100644 --- a/internal/service/dms/service_package_gen.go +++ b/internal/service/dms/service_package_gen.go @@ -29,6 +29,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceEndpoint, TypeName: "aws_dms_endpoint", }, + { + Factory: DataSourceReplicationInstance, + TypeName: "aws_dms_replication_instance", + }, { Factory: DataSourceReplicationSubnetGroup, TypeName: "aws_dms_replication_subnet_group", diff --git a/website/docs/d/dms_replication_instance.html.markdown b/website/docs/d/dms_replication_instance.html.markdown new file mode 100644 index 000000000000..892f3fd9eed8 --- /dev/null +++ b/website/docs/d/dms_replication_instance.html.markdown @@ -0,0 +1,57 @@ +--- +subcategory: "DMS (Database Migration)" +layout: "aws" +page_title: "AWS: aws_dms_certificate" +description: |- + Terraform data source for managing an AWS DMS (Database Migration) Replication Instance. +--- + +# Data Source: aws_dms_replication_instance + +Terraform data source for managing an AWS DMS (Database Migration) Replication Instance. + +## Example Usage + +```terraform +data "aws_dms_replication_instance" "test" { + replication_instance_id = aws_dms_replication_instance.test.replication_instance_id +} +``` + +## Argument Reference + +The following arguments are required: + +* `replication_instance_id` - (Required) The replication instance identifier. This parameter is stored as a lowercase string. + + - Must contain from 1 to 63 alphanumeric characters or hyphens. + - First character must be a letter. + - Cannot end with a hyphen + - Cannot contain two consecutive hyphens. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `allocated_storage` - (Default: 50, Min: 5, Max: 6144) The amount of storage (in gigabytes) to be initially allocated for the replication instance. +* `allow_major_version_upgrade` - (Default: false) Indicates that major version upgrades are allowed. +* `apply_immediately` - (Default: false) Indicates whether the changes should be applied immediately or during the next maintenance window. Only used when updating an existing resource. +* `auto_minor_version_upgrade` - (Default: false) Indicates that minor engine upgrades will be applied automatically to the replication instance during the maintenance window. +* `availability_zone` - The EC2 Availability Zone that the replication instance will be created in. +* `engine_version` - The engine version number of the replication instance. +* `kms_key_arn` - The Amazon Resource Name (ARN) for the KMS key that will be used to encrypt the connection parameters. If you do not specify a value for `kms_key_arn`, then AWS DMS will use your default encryption key. AWS KMS creates the default encryption key for your AWS account. Your AWS account has a different default encryption key for each AWS region. +* `multi_az` - Specifies if the replication instance is a multi-az deployment. You cannot set the `availability_zone` parameter if the `multi_az` parameter is set to `true`. +* `preferred_maintenance_window` - The weekly time range during which system maintenance can occur, in Universal Coordinated Time (UTC). + + - Default: A 30-minute window selected at random from an 8-hour block of time per region, occurring on a random day of the week. + - Format: `ddd:hh24:mi-ddd:hh24:mi` + - Valid Days: `mon, tue, wed, thu, fri, sat, sun` + - Constraints: Minimum 30-minute window. + +* `publicly_accessible` - (Default: false) Specifies the accessibility options for the replication instance. A value of true represents an instance with a public IP address. A value of false represents an instance with a private IP address. +* `replication_instance_arn` - The Amazon Resource Name (ARN) of the replication instance. +* `replication_instance_class` - The compute and memory capacity of the replication instance as specified by the replication instance class. See [AWS DMS User Guide](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_ReplicationInstance.Types.html) for available instance sizes and advice on which one to choose. +* `replication_instance_private_ips` - A list of the private IP addresses of the replication instance. +* `replication_instance_public_ips` - A list of the public IP addresses of the replication instance. +* `replication_subnet_group_id` - (Optional) A subnet group to associate with the replication instance. +* `vpc_security_group_ids` - (Optional) A list of VPC security group IDs to be used with the replication instance. The VPC security groups must work with the VPC containing the replication instance.