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

[New Data Source]: aws_describe_connector #38213

Merged
merged 9 commits into from
Jul 2, 2024
156 changes: 156 additions & 0 deletions internal/service/transfer/connector_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package transfer

import (
"context"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/service/transfer"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Connector")
func newDataSourceConnector(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceConnector{}, nil
}

const (
DSNameConnector = "Connector Data Source"
)

type dataSourceConnector struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceConnector) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
resp.TypeName = "aws_transfer_connector"
}

func (d *dataSourceConnector) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Connector object was expanded
"access_role": schema.StringAttribute{
Computed: true,
},
names.AttrARN: schema.StringAttribute{
Computed: true,
},
"as2_config": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[dsAs2Config](ctx),
Computed: true,
},
names.AttrID: schema.StringAttribute{
CustomType: fwtypes.RegexpType,
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexache.MustCompile(`c-([0-9a-f]{17})`),
""),
stringvalidator.LengthAtMost(19),
stringvalidator.LengthAtLeast(19),
},
},
"logging_role": schema.StringAttribute{
Computed: true,
},
"security_policy_name": schema.StringAttribute{
Computed: true,
},
"service_managed_egress_ip_addresses": schema.ListAttribute{
CustomType: fwtypes.ListOfStringType,
Computed: true,
},
"sftp_config": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[dsSftpConfig](ctx),
Computed: true,
},
"tags": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[dsTags](ctx),
Computed: true,
},
ThomasZalewski marked this conversation as resolved.
Show resolved Hide resolved
"url": schema.StringAttribute{
Computed: true,
},
},
}
}

// TIP: ==== ASSIGN CRUD METHODS ====
ThomasZalewski marked this conversation as resolved.
Show resolved Hide resolved
// Data sources only have a read method.
func (d *dataSourceConnector) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().TransferClient(ctx)

var data dsConnectorData
var describeConnectorInput transfer.DescribeConnectorInput
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if !data.ConnectorId.IsNull() || !data.ConnectorId.IsUnknown() {
describeConnectorInput.ConnectorId = data.ConnectorId.ValueStringPointer()
}

description, findConnectorError := conn.DescribeConnector(ctx, &describeConnectorInput)

if findConnectorError != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Transfer, create.ErrActionReading, DSNameConnector, data.SecurityPolicyName.String(), findConnectorError),
findConnectorError.Error(),
)
return
}

resp.Diagnostics.Append(flex.Flatten(ctx, description.Connector, &data)...)
if resp.Diagnostics.HasError() {
return
}

ThomasZalewski marked this conversation as resolved.
Show resolved Hide resolved
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

}

Check failure on line 121 in internal/service/transfer/connector_data_source.go

View workflow job for this annotation

GitHub Actions / 2 of 2

unnecessary trailing newline (whitespace)

type dsConnectorData struct {
Arn types.String `tfsdk:"arn"`
AccessRole types.String `tfsdk:"access_role"`
As2Config fwtypes.ListNestedObjectValueOf[dsAs2Config] `tfsdk:"as2_config"`
ConnectorId fwtypes.Regexp `tfsdk:"id"`
LoggingRole types.String `tfsdk:"logging_role"`
SecurityPolicyName types.String `tfsdk:"security_policy_name"`
ServiceManagedEgressIpAddresses fwtypes.ListValueOf[types.String] `tfsdk:"service_managed_egress_ip_addresses"`
SftpConfig fwtypes.ListNestedObjectValueOf[dsSftpConfig] `tfsdk:"sftp_config"`
Tags fwtypes.ListNestedObjectValueOf[dsTags] `tfsdk:"tags"`
ThomasZalewski marked this conversation as resolved.
Show resolved Hide resolved
Url types.String `tfsdk:"url"`
}

type dsAs2Config struct {
BasicAuthSecretId types.String `tfsdk:"basic_auth_secret_id"`
Compression types.String `tfsdk:"compression"`
EncryptionAlgorithm types.String `tfsdk:"encryption_algorithm"`
LocalProfileId types.String `tfsdk:"local_profile_id"`
MdnResponse types.String `tfsdk:"mdn_response"`
MdnSigningAlgorithm types.String `tfsdk:"mdn_signing_algorithm"`
MessageSubject types.String `tfsdk:"message_subject"`
PartnerProfileId types.String `tfsdk:"partner_profile_id"`
SigningAlgorithm types.String `tfsdk:"singing_algorithm"`
}

type dsSftpConfig struct {
TrustedHostKeys fwtypes.ListValueOf[types.String] `tfsdk:"trusted_host_keys"`
UserSecretId types.String `tfsdk:"user_secret_id"`
}

type dsTags struct {
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
}
124 changes: 124 additions & 0 deletions internal/service/transfer/connector_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package transfer_test

import (
"fmt"
"testing"

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccTransferConnectorDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_transfer_connector.test"
resourceName := "aws_transfer_connector.test"
url := "http://www.example.com"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.TransferEndpointID)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.TransferServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckConnectorDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccConnectorDataSourceConfig_basic(rName, url),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "access_role", resourceName, "access_role"),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(dataSourceName, "as2_config.#", resourceName, "as2_config.#"),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrID, resourceName, names.AttrID),
//resource.TestCheckResourceAttrPair(dataSourceName, "logging_role", resourceName, "logging_role"),
//resource.TestCheckResourceAttrPair(dataSourceName, "security_policy_name", resourceName, "security_policy_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "service_managed_egress_ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "sftp_config.#", resourceName, "sftp_config.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.#", resourceName, "tags.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "url", resourceName, "url"),
),
},
},
})
}

func testAccConnectorDataSourceConfig_basic(rName, url string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %[1]q
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "transfer.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
}

resource "aws_iam_role_policy" "test" {
name = %[1]q
role = aws_iam_role.test.id

policy = <<POLICY
{
"Version":"2012-10-17",
"Statement":[{
"Sid":"AllowFullAccesstoS3",
"Effect":"Allow",
"Action":[
"s3:*"
],
"Resource":"*"
}]
}
POLICY
}
resource "aws_transfer_profile" "local" {
as2_id = %[1]q
profile_type = "LOCAL"
}

resource "aws_transfer_profile" "partner" {
as2_id = %[1]q
profile_type = "PARTNER"
}

resource "aws_transfer_connector" "test" {
access_role = aws_iam_role.test.arn

as2_config {
compression = "DISABLED"
encryption_algorithm = "AES128_CBC"
message_subject = %[1]q
local_profile_id = aws_transfer_profile.local.profile_id
mdn_response = "NONE"
mdn_signing_algorithm = "NONE"
partner_profile_id = aws_transfer_profile.partner.profile_id
signing_algorithm = "NONE"
}

url = %[2]q
}
data "aws_transfer_connector" "test" {
id = aws_transfer_connector.test.id
}


`, rName, url)
}
7 changes: 6 additions & 1 deletion internal/service/transfer/service_package_gen.go

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

65 changes: 65 additions & 0 deletions website/docs/d/transfer_connector.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
subcategory: "Transfer Family"
layout: "aws"
page_title: "AWS: aws_transfer_connector"
description: |-
Terraform data source for managing an AWS Transfer Family Connector.
---
<!---
TIP: A few guiding principles for writing documentation:
1. Use simple language while avoiding jargon and figures of speech.
2. Focus on brevity and clarity to keep a reader's attention.
3. Use active voice and present tense whenever you can.
4. Document your feature as it exists now; do not mention the future or past if you can help it.
5. Use accessible and inclusive language.
--->

# Data Source: aws_transfer_connector

Terraform data source for managing an AWS Transfer Family Connector.

## Example Usage

### Basic Usage

```terraform
data "aws_transfer_connector" "test" {
id = "c-xxxxxxxxxxxxxx"
}
```

## Argument Reference

The following arguments are required:

* `id` - (Required) Unique identifier for connector

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `access_role` - ARN of the AWS Identity and Access Managment role.
* `arn` - ARN of the Connector.
* `as2_config` - Structure containing the parameters for an AS2 connector object. Contains the following attributes:
* `basic_auth_secret_id` - Basic authentication for AS2 connector API. Returns a null value if not set.
* `Compression` - Specifies whether AS2 file is compressed. Will be ZLIB or DISABLED
ThomasZalewski marked this conversation as resolved.
Show resolved Hide resolved
* `encryption_algorithm` - Algorithm used to encrypt file. Will be AES128_CBC or AES192_CBC or AES256_CBC or DES_EDE3_CBC or NONE.
* `local_profile_id` - Unique identifier for AS2 local profile.
* `mdn_response` - Used for outbound requests to tell if response is asynchronous or not. Will be either SYNC or NONE.
* `mdn_signing_algorithm` - Signing algorithm for MDN response. Will be SHA256 or SHA384 or SHA512 or SHA1 or NONE or DEFAULT.
* `message_subject` - Subject HTTP header attribute in outbound AS2 messages to the connector.
* `partner_profile_id` - Unique identifier used by connector for partner profile.
* `signing_algorithm` - Algorithm used for signing AS2 messages sent with the connector.
* `logging_role` - ARN of the IAM role that allows a connector to turn on CLoudwatch logging for Amazon S3 events.
* `security_policy_name` - Name of security policy.
* `service_managed_egress_ip_addresses` - List of egress Ip addresses.
* `sftp_config` - Object containing the following attributes:
* `trusted_host_keys` - List of the public portions of the host keys that are used to identify the servers the connector is connected to.
* `user_secret_id` - Identifer for the secret in AWS Secrets Manager that contains the SFTP user's private key, and/or password.
* `tags` - Object containing the following attributes:
* `key` - Name of the tag.
* `value` - Values associated with the tags key.
* `url` - URL of the partner's AS2 or SFTP endpoint.



Loading