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 Datasource: aws_transfer_server #7977

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions aws/data_source_aws_transfer_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/transfer"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsTransferServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsTransferServerRead,
Schema: map[string]*schema.Schema{
"server_id": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
},
"invocation_role": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since invocation_role and the below attributes cannot be configured to lookup the Transfer server, Optional: true should be removed from them 👍

},
"url": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"identity_provider_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"logging_role": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}

func dataSourceAwsTransferServerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).transferconn

serverID := d.Get("server_id").(string)
input := &transfer.DescribeServerInput{
ServerId: aws.String(serverID),
}

log.Printf("[DEBUG] Describe Transfer Server Option: %#v", input)

resp, err := conn.DescribeServer(input)
if err != nil {
if isAWSErr(err, transfer.ErrCodeResourceNotFoundException, "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, Terraform data sources should always error when their associated resource is not found, so this handling should be removed.

log.Printf("[ERROR] Transfer Server (%s) not found", serverID)
return nil
}
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide error context for operators and code maintainers here, e.g.

Suggested change
return err
return fmt.Errorf("error describing Transfer Server (%s): %s", serverID, err)

}

endpoint := fmt.Sprintf("%s.server.transfer.%s.amazonaws.com", serverID, meta.(*AWSClient).region)

d.SetId(serverID)
d.Set("arn", resp.Server.Arn)
d.Set("endpoint", endpoint)
if resp.Server.IdentityProviderDetails != nil {
d.Set("invocation_role", aws.StringValue(resp.Server.IdentityProviderDetails.InvocationRole))
d.Set("url", aws.StringValue(resp.Server.IdentityProviderDetails.Url))
}
d.Set("identity_provider_type", resp.Server.IdentityProviderType)
d.Set("logging_role", resp.Server.LoggingRole)

return nil
}
272 changes: 272 additions & 0 deletions aws/data_source_aws_transfer_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsTransferServer_basic(t *testing.T) {
resourceName := "aws_transfer_server.test"
datasourceName := "data.aws_transfer_server.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsTransferServerConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsTransferServerCheck(datasourceName, resourceName),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the custom function by using resource.TestCheckResourceAttrPair(), e.g.

Suggested change
testAccDataSourceAwsTransferServerCheck(datasourceName, resourceName),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "endpoint", resourceName, "endpoint"),
resource.TestCheckResourceAttrPair(datasourceName, "identity_provider_type", resourceName, "identity_provider_type"),
resource.TestCheckResourceAttrPair(datasourceName, "invocation_role", resourceName, "invocation_role"),
resource.TestCheckResourceAttrPair(datasourceName, "logging_role", resourceName, "logging_role"),
resource.TestCheckResourceAttrPair(datasourceName, "url", resourceName, "url"),

Along with simplifying the code, we prefer using this upstream function since it also has some special handling in the upcoming Terraform 0.12 Provider SDK to ignore differences between missing values and zero-values. 😄

),
},
},
})
}

func TestAccDataSourceAwsTransferServer_service_managed(t *testing.T) {
rName := acctest.RandString(5)
resourceName := "aws_transfer_server.test"
datasourceName := "data.aws_transfer_server.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsTransferServerConfig_service_managed(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsTransferServerCheck(datasourceName, resourceName),
),
},
},
})
}

func TestAccDataSourceAwsTransferServer_apigateway(t *testing.T) {
rName := acctest.RandString(5)
resourceName := "aws_transfer_server.test"
datasourceName := "data.aws_transfer_server.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsTransferServerConfig_apigateway(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsTransferServerCheck(datasourceName, resourceName),
),
},
},
})
}

func testAccDataSourceAwsTransferServerCheck(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[datasourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", datasourceName)
}

transferServerRs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", resourceName)
}

attrNames := []string{
"arn",
"endpoint",
"invocation_role",
"url",
"identity_provider_type",
"logging_role",
}

for _, attrName := range attrNames {
if rs.Primary.Attributes[attrName] != transferServerRs.Primary.Attributes[attrName] {
return fmt.Errorf(
"%s is %s; want %s",
attrName,
rs.Primary.Attributes[attrName],
transferServerRs.Primary.Attributes[attrName],
)
}
}

return nil
}
}

const testAccDataSourceAwsTransferServerConfig_basic = `
resource "aws_transfer_server" "test" {}

data "aws_transfer_server" "test" {
server_id = "${aws_transfer_server.test.id}"
}
`

func testAccDataSourceAwsTransferServerConfig_service_managed(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = "tf-test-transfer-server-iam-role-%s"

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 = "tf-test-transfer-server-iam-policy-%s"
role = "${aws_iam_role.test.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowFullAccesstoCloudWatchLogs",
"Effect": "Allow",
"Action": [
"logs:*"
],
"Resource": "*"
}
]
}
POLICY
}

resource "aws_transfer_server" "test" {
identity_provider_type = "SERVICE_MANAGED"
logging_role = "${aws_iam_role.test.arn}"
}

data "aws_transfer_server" "test" {
server_id = "${aws_transfer_server.test.id}"
}
`, rName, rName)
}

func testAccDataSourceAwsTransferServerConfig_apigateway(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}

resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "test"
}

resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"
}

resource "aws_api_gateway_method_response" "error" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
status_code = "400"
}

resource "aws_api_gateway_integration" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"

type = "HTTP"
uri = "https://www.google.de"
integration_http_method = "GET"
}

resource "aws_api_gateway_integration_response" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_integration.test.http_method}"
status_code = "${aws_api_gateway_method_response.error.status_code}"
}

resource "aws_api_gateway_deployment" "test" {
depends_on = ["aws_api_gateway_integration.test"]

rest_api_id = "${aws_api_gateway_rest_api.test.id}"
stage_name = "test"
description = "%s"
stage_description = "%s"


variables = {
"a" = "2"
}
}


resource "aws_iam_role" "test" {
name = "tf-test-transfer-server-iam-role-for-apigateway-%s"

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 = "tf-test-transfer-server-iam-policy-%s"
role = "${aws_iam_role.test.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowFullAccesstoCloudWatchLogs",
"Effect": "Allow",
"Action": [
"logs:*"
],
"Resource": "*"
}
]
}
POLICY
}

resource "aws_transfer_server" "test" {
identity_provider_type = "API_GATEWAY"
url = "https://${aws_api_gateway_rest_api.test.id}.execute-api.us-west-2.amazonaws.com${aws_api_gateway_resource.test.path}"
invocation_role = "${aws_iam_role.test.arn}"
logging_role = "${aws_iam_role.test.arn}"
}

data "aws_transfer_server" "test" {
server_id = "${aws_transfer_server.test.id}"
}
`, rName, rName, rName, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func Provider() terraform.ResourceProvider {
"aws_storagegateway_local_disk": dataSourceAwsStorageGatewayLocalDisk(),
"aws_subnet": dataSourceAwsSubnet(),
"aws_subnet_ids": dataSourceAwsSubnetIDs(),
"aws_transfer_server": dataSourceAwsTransferServer(),
"aws_vpcs": dataSourceAwsVpcs(),
"aws_security_group": dataSourceAwsSecurityGroup(),
"aws_security_groups": dataSourceAwsSecurityGroups(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@
<li<%= sidebar_current("docs-aws-datasource-subnet-ids") %>>
<a href="/docs/providers/aws/d/subnet_ids.html">aws_subnet_ids</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-transfer-server") %>>
<a href="/docs/providers/aws/d/transfer_server.html">aws_transfer_server</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-vpc-x") %>>
<a href="/docs/providers/aws/d/vpc.html">aws_vpc</a>
</li>
Expand Down
Loading