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

Data sources for EBS default encryption #8884

Merged
merged 4 commits into from
Jun 26, 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
Prev Previous commit
Next Next commit
Add Data Source for EBS default encryption KMS key
  • Loading branch information
jukie committed Jun 10, 2019
commit 20369f823e1c00c53f876150328fff0d289da6ba
35 changes: 35 additions & 0 deletions aws/data_source_aws_ebs_default_kms_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package aws

import (
"fmt"
"time"

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

func dataSourceAwsEbsDefaultKmsKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEbsDefaultKmsKeyRead,

Schema: map[string]*schema.Schema{
"key_id": {
jukie marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsEbsDefaultKmsKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

res, err := conn.GetEbsDefaultKmsKeyId(&ec2.GetEbsDefaultKmsKeyIdInput{})
if err != nil {
return fmt.Errorf("Error reading EBS default KMS key: %q", err)
}

d.SetId(time.Now().UTC().String())
d.Set("key_id", *res.KmsKeyId)
jukie marked this conversation as resolved.
Show resolved Hide resolved

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

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"testing"

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

func TestAccDataSourceAwsEBSDefaultKmsKey_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEBSDefaultKmsKeyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceAwsEBSDefaultKmsKey("data.aws_ebs_default_kms_key.current"),
),
},
},
})
}

func testAccCheckDataSourceAwsEBSDefaultKmsKey(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

actual, err := conn.GetEbsDefaultKmsKeyId(&ec2.GetEbsDefaultKmsKeyIdInput{})
if err != nil {
return fmt.Errorf("Error reading EBS default KMS key: %q", err)
}

attr := rs.Primary.Attributes["key_id"]
jukie marked this conversation as resolved.
Show resolved Hide resolved

if attr != *actual.KmsKeyId {
return fmt.Errorf("EBS default KMS key is not the expected value (%v)", actual.KmsKeyId)
}

return nil
}
}

const testAccDataSourceAwsEBSDefaultKmsKeyConfig = `
data "aws_ebs_default_kms_key" "current" { }
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider {
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dx_gateway": dataSourceAwsDxGateway(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_default_kms_key": dataSourceAwsEbsDefaultKmsKey(),
"aws_ebs_encryption_by_default": dataSourceAwsEbsEncryptionByDefault(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
Expand Down