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 Glue Data sources - Glue Connection and Glue Data Catalog Encryption Settings #18802

Closed
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions .changelog/18802.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_glue_connection
```

```release-note:new-data-source
aws_glue_connection
```
62 changes: 62 additions & 0 deletions aws/data_source_aws_glue_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aws

import (
"context"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/glue"
"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"
)

func dataSourceAwsGlueConnection() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAwsGlueConnectionRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"catalog_id": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"connection_type": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsGlueConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).glueconn
catalogID, connectionName, err := decodeGlueConnectionID(d.Id())
input := &glue.GetConnectionInput{
CatalogId: aws.String(catalogID),
Name: aws.String(connectionName),
}
output, err := conn.GetConnection(input)
if err != nil {
if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
return diag.Errorf("error Glue Connection (%s) not found", d.Id())
}
return diag.Errorf("error reading Glue Connection (%s): %s", d.Id(), err)
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
}
d.Set("catalog_id", catalogID)
d.Set("creation_time", aws.TimeValue(output.Connection.CreationTime).Format(time.RFC3339))
d.Set("connection_type", output.Connection.ConnectionType)
d.Set("name", connectionName)
return nil
}
61 changes: 61 additions & 0 deletions aws/data_source_aws_glue_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/glue"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceAwsGlueConnection_basic(t *testing.T) {
resourceName := "aws_glue_connection.test"
datasourceName := "data.aws_glue_connection.test"
rName := fmt.Sprintf("tf-testacc-glue-connection-%s", acctest.RandString(13))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, glue.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsGlueConnectionConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsGlueConnectionCheck(datasourceName),
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as above resource

resource.TestCheckResourceAttrPair(datasourceName, "catalog_id", resourceName, "catalog_id"),
resource.TestCheckResourceAttrPair(datasourceName, "creation_time", resourceName, "creation_time"),
resource.TestCheckResourceAttrPair(datasourceName, "connection_type", resourceName, "connection_type"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
),
Copy link
Collaborator

Choose a reason for hiding this comment

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

lets also add connection_properties check here as we added it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have connection_properties as part of the schema in the data source. It doesn't appear for every type of connection so I thought it might be best to leave it out.

},
},
})
}

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

return nil
}
}

func testAccDataSourceAwsGlueConnectionConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_glue_connection" "test" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

due to faling tests, lets change this to something that works on the resource accaptenece tests:

this is the basic config for connection resource tests

resource "aws_glue_connection" "test" {
  connection_properties = {
    JDBC_CONNECTION_URL = "jdbc:mysql://terraformacctesting.com/testdatabase"
    PASSWORD            = "testpassword"
    USERNAME            = "testusername"
  }

  name = "%s"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thank you. Just pushed a commit with this test.

name = %[1]q
connection_type = "NETWORK"
connection_properties = {}

}

data "aws_glue_connection" "test" {
id = aws_glue_connection.test.id
}
`, rName)
}
56 changes: 56 additions & 0 deletions aws/data_source_aws_glue_data_catalog_encryption_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package aws

import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/glue"
"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"
)

func dataSourceAwsGlueDataCatalogEncryptionSettings() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAwsGlueDataCatalogEncryptionSettingsRead,
Schema: map[string]*schema.Schema{
"id": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

id attribute is implicit dont think we need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it also implicitly required? The goal here is to make id a required argument, so just want to make sure that behavior would remain the same if I take this out.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right, my mistake i see this is the convention in data sources to keep "id" explicit. ill try to run tests soon. code wise looks good

Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"connection_password_encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"connection_password_kms_key_arn": {
Type: schema.TypeString,
Computed: true,
},
"encryption_mode": {
Type: schema.TypeString,
Computed: true,
},
"encryption_kms_key_arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsGlueDataCatalogEncryptionSettingsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).glueconn
input := &glue.GetDataCatalogEncryptionSettingsInput{
CatalogId: aws.String(d.Id()),
}
out, err := conn.GetDataCatalogEncryptionSettings(input)
if err != nil {
return diag.Errorf("Error reading Glue Data Catalog Encryption Settings: %s", err)
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
}
d.SetId(d.Id())
d.Set("connection_password_encrypted", out.DataCatalogEncryptionSettings.ConnectionPasswordEncryption.ReturnConnectionPasswordEncrypted)
d.Set("connection_password_kms_key_arn", out.DataCatalogEncryptionSettings.ConnectionPasswordEncryption.AwsKmsKeyId)
d.Set("encryption_mode", out.DataCatalogEncryptionSettings.EncryptionAtRest.CatalogEncryptionMode)
d.Set("connection_password_encrypted", out.DataCatalogEncryptionSettings.EncryptionAtRest.SseAwsKmsKeyId)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/glue"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, glue.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsGlueDataCatalogEncryptionSettingsConfig(),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsGlueDataCatalogEncryptionSettingsCheck(datasourceName),
Copy link
Collaborator

Choose a reason for hiding this comment

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

from what i see in other datasource tests we don't need this check here. (the data source is not creating anything so there is nothing to check here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I based this from data_source_aws_kms_key_test.go. Could you point me to another file whose structure I should copy?

resource.TestCheckResourceAttrPair(datasourceName, "connection_password_encrypted", resourceName, "connection_password_encrypted"),
resource.TestCheckResourceAttrPair(datasourceName, "connection_password_kms_key_arn", resourceName, "connection_password_kms_key_arn"),
resource.TestCheckResourceAttrPair(datasourceName, "encryption_mode", resourceName, "encryption_mode"),
resource.TestCheckResourceAttrPair(datasourceName, "connection_password_encrypted", resourceName, "connection_password_encrypted"),
),
},
},
})
}

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

return nil
}
}

func testAccDataSourceAwsGlueDataCatalogEncryptionSettingsConfig() string {
return `
resource "aws_glue_data_catalog_encryption_settings" "test" {
data_catalog_encryption_settings {
connection_password_encryption {
return_connection_password_encrypted = false
}

encryption_at_rest {
catalog_encryption_mode = "DISABLED"
}
}
}

data "aws_glue_data_catalog_encryption_settings" "test" {
id = aws_glue_data_catalog_encryption_settings.test.id
}
`
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func Provider() *schema.Provider {
"aws_elasticache_replication_group": dataSourceAwsElasticacheReplicationGroup(),
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
"aws_glue_connection": dataSourceAwsGlueConnection(),
"aws_glue_data_catalog_encryption_settings": dataSourceAwsGlueDataCatalogEncryptionSettings(),
"aws_glue_script": dataSourceAwsGlueScript(),
"aws_guardduty_detector": dataSourceAwsGuarddutyDetector(),
"aws_iam_account_alias": dataSourceAwsIamAccountAlias(),
Expand Down