Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding certificate_data_source to dms #30498

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/30498.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_dms_certificate
```
150 changes: 150 additions & 0 deletions internal/service/dms/certificate_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package dms

import (
"context"
"regexp"

"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-plugin-sdk/v2/helper/validation"
"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/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_dms_certificate")
func DataSourceCertificate() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceCertificateRead,

Schema: map[string]*schema.Schema{
"certificate_arn": {
Type: schema.TypeString,
Computed: true,
},
"certificate_creation_date": {
Type: schema.TypeString,
Computed: true,
},
"certificate_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 255),
validation.StringMatch(regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-]+$"), "must start with a letter, only contain alphanumeric characters and hyphens"),
validation.StringDoesNotMatch(regexp.MustCompile(`--`), "cannot contain two consecutive hyphens"),
validation.StringDoesNotMatch(regexp.MustCompile(`-$`), "cannot end in a hyphen"),
),
},
"certificate_owner": {
Type: schema.TypeString,
Computed: true,
},
"certificate_pem": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"certificate_wallet": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"key_length": {
Type: schema.TypeInt,
Computed: true,
},
"signing_algorithm": {
Type: schema.TypeString,
Computed: true,
},
"valid_from_date": {
Type: schema.TypeString,
Computed: true,
},
"valid_to_date": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

const (
DSNameCertificate = "Certificate Data Source"
)

func dataSourceCertificateRead(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

certificateID := d.Get("certificate_id").(string)

out, err := FindCertificateByID(ctx, conn, certificateID)

if err != nil {
create.DiagError(names.DMS, create.ErrActionReading, DSNameCertificate, d.Id(), err)
}

d.SetId(aws.StringValue(out.CertificateIdentifier))

d.Set("certificate_id", out.CertificateIdentifier)
d.Set("certificate_arn", out.CertificateArn)
d.Set("certificate_pem", out.CertificatePem)

if out.CertificateWallet != nil && len(out.CertificateWallet) != 0 {
d.Set("certificate_wallet", verify.Base64Encode(out.CertificateWallet))
}

d.Set("key_length", out.KeyLength)
d.Set("signing_algorithm", out.SigningAlgorithm)

from_date := out.ValidFromDate.String()
d.Set("valid_from_date", from_date)
to_date := out.ValidToDate.String()
d.Set("valid_to_date", to_date)

tags, err := ListTags(ctx, conn, aws.StringValue(out.CertificateArn))

if err != nil {
return create.DiagError(names.DMS, create.ErrActionReading, DSNameCertificate, 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, DSNameCertificate, d.Id(), err)
}

return nil
}

func FindCertificateByID(ctx context.Context, conn *dms.DatabaseMigrationService, id string) (*dms.Certificate, error) {
input := &dms.DescribeCertificatesInput{
Filters: []*dms.Filter{
{
Name: aws.String("certificate-id"),
Values: []*string{aws.String(id)},
},
},
}
response, err := conn.DescribeCertificatesWithContext(ctx, input)

if err != nil {
return nil, err
}

if response == nil || len(response.Certificates) == 0 || response.Certificates[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return response.Certificates[0], nil
}
46 changes: 46 additions & 0 deletions internal/service/dms/certificate_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 TestAccDMSCertificateDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_dms_certificate.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, dms.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(ctx, t),
CheckDestroy: testAccCheckCertificateDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccCertificateDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCertificateExists(ctx, dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "certificate_id"),
),
},
},
})
}

func testAccCertificateDataSourceConfig_basic(certId string) string {
return fmt.Sprintf(`
resource "aws_dms_certificate" "test" {
certificate_id = "%[1]s"
certificate_pem = "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6DfOgJs7MMftd1KeWqTO+uW134QlTt7+jwI8Jq0uyKCu/O2kJhVtH/Ryog14tGl\n+wLcuIPLbwJI9CwZX4WMBrq4DnYss+6F47i8NCc+Z3MAiG4vtq9ytBmaod0dj2bI\ng4/Lac0e00dql9RnqENh1+dF0V+QgTJCoPkMqDNAlSB8vOodBW81UAb2z12t+IFi\n3X9J3WtCK2+T5brXL6itzewWJ2ALvX3QpmZx7fMHJ3tE+SjjyivE1BbOlzYHx83t\nTeYnm7pS9un7A/UzTDHbs7hPUezLek+H3xTPAnnq\n-----END CERTIFICATE-----\n"
}

data "aws_dms_certificate" "test" {
certificate_id = aws_dms_certificate.test.certificate_id
}
`, certId)
}
7 changes: 6 additions & 1 deletion internal/service/dms/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions website/docs/d/dms_certificate.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "DMS (Database Migration)"
layout: "aws"
page_title: "AWS: aws_dms_certificate"
description: |-
Terraform data source for managing an AWS DMS (Database Migration) Certificate.
---

# Data Source: aws_dms_certificate

Terraform data source for managing an AWS DMS (Database Migration) Certificate.

## Example Usage

### Basic Usage

```terraform
data "aws_dms_certificate" "example" {
certificate_id = aws_dms_certificate.test.certificate_id
}
```

## Argument Reference

The following arguments are required:

* `certificate_id` - (Required) A customer-assigned name for the certificate. Identifiers must begin with a letter and must contain only ASCII letters, digits, and hyphens. They can't end with a hyphen or contain two consecutive hyphens.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `certificate_creation_date` - The date that the certificate was created.
* `certificate_pem` - The contents of a .pem file, which contains an X.509 certificate.
* `certificate_owner` - The owner of the certificate.
* `certificate_arn` - The Amazon Resource Name (ARN) for the certificate.
* `certificate_wallet` - The owner of the certificate.
* `key_length` - The key length of the cryptographic algorithm being used.
* `signing_algorithm` - The algorithm for the certificate.
* `valid_from_date` - The beginning date that the certificate is valid.
* `valid_to_date` - The final date that the certificate is valid.