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
Next Next commit
Add Data Source for EBS default encryption
  • Loading branch information
jukie committed Jun 10, 2019
commit 61e74deb95063b6edda96128b891d1b03fe32896
35 changes: 35 additions & 0 deletions aws/data_source_aws_ebs_encryption_by_default.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 dataSourceAwsEbsEncryptionByDefault() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEbsEncryptionByDefaultRead,

Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceAwsEbsEncryptionByDefaultRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

res, err := conn.GetEbsEncryptionByDefault(&ec2.GetEbsEncryptionByDefaultInput{})
if err != nil {
return fmt.Errorf("Error reading default EBS encryption toggle: %q", err)
}

d.SetId(time.Now().UTC().String())
d.Set("enabled", res.EbsEncryptionByDefault)

return nil
}
57 changes: 57 additions & 0 deletions aws/data_source_aws_ebs_encryption_by_default_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"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"strconv"
"testing"
)

func TestAccDataSourceAwsEBSEncryptionByDefault_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEBSEncryptionByDefaultConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceAwsEBSEncryptionByDefault("data.aws_ebs_encryption_by_default.current"),
),
},
},
})
}

func testAccCheckDataSourceAwsEBSEncryptionByDefault(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.GetEbsEncryptionByDefault(&ec2.GetEbsEncryptionByDefaultInput{})
if err != nil {
return fmt.Errorf("Error reading default EBS encryption toggle: %q", err)
}

attr, _ := strconv.ParseBool(rs.Primary.Attributes["enabled"])

if attr != *actual.EbsEncryptionByDefault {
return fmt.Errorf("EBS encryption by default is not in expected state (%t)", *actual.EbsEncryptionByDefault)
}

return nil
}
}

const testAccDataSourceAwsEBSEncryptionByDefaultConfig = `
data "aws_ebs_encryption_by_default" "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_encryption_by_default": dataSourceAwsEbsEncryptionByDefault(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
Expand Down