Skip to content

Commit

Permalink
New Resource: aws_xray_encryption_config (#13600)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSXrayEncryptionConfig_basic (941.85s)
```
  • Loading branch information
DrFaust92 authored Aug 27, 2020
1 parent 0b5172e commit 581768a
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 0 deletions.
25 changes: 25 additions & 0 deletions aws/internal/service/xray/waiter/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package waiter

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

const (
EncryptionConfigStatusUnknown = "Unknown"
)

// EncryptionConfigStatus fetches the Encryption Config and its Status
func EncryptionConfigStatus(conn *xray.XRay) resource.StateRefreshFunc {
return func() (interface{}, string, error) {

output, _ := conn.GetEncryptionConfig(&xray.GetEncryptionConfigInput{})

if output == nil || output.EncryptionConfig == nil {
return output, EncryptionConfigStatusUnknown, nil
}

return output, aws.StringValue(output.EncryptionConfig.Status), nil
}
}
30 changes: 30 additions & 0 deletions aws/internal/service/xray/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package waiter

import (
"time"

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

const (
EncryptionConfigAvailableTimeout = 15 * time.Minute
)

// EncryptionConfigAvailable waits for a EncryptionConfig to return Available
func EncryptionConfigAvailable(conn *xray.XRay) (*xray.EncryptionConfig, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{xray.EncryptionStatusUpdating},
Target: []string{xray.EncryptionStatusActive},
Refresh: EncryptionConfigStatus(conn),
Timeout: EncryptionConfigAvailableTimeout,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*xray.EncryptionConfig); ok {
return v, err
}

return nil, err
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ func Provider() *schema.Provider {
"aws_pinpoint_event_stream": resourceAwsPinpointEventStream(),
"aws_pinpoint_gcm_channel": resourceAwsPinpointGCMChannel(),
"aws_pinpoint_sms_channel": resourceAwsPinpointSMSChannel(),
"aws_xray_encryption_config": resourceAwsXrayEncryptionConfig(),
"aws_xray_group": resourceAwsXrayGroup(),
"aws_xray_sampling_rule": resourceAwsXraySamplingRule(),
"aws_workspaces_ip_group": resourceAwsWorkspacesIpGroup(),
Expand Down
80 changes: 80 additions & 0 deletions aws/resource_aws_xray_encryption_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/xray"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/xray/waiter"
)

func resourceAwsXrayEncryptionConfig() *schema.Resource {
return &schema.Resource{
Create: resourceAwsXrayEncryptionConfigPut,
Read: resourceAwsXrayEncryptionConfigRead,
Update: resourceAwsXrayEncryptionConfigPut,
Delete: schema.Noop,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
xray.EncryptionTypeKms,
xray.EncryptionTypeNone,
}, false),
},
},
}
}

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

input := &xray.PutEncryptionConfigInput{
Type: aws.String(d.Get("type").(string)),
}

if v, ok := d.GetOk("key_id"); ok {
input.KeyId = aws.String(v.(string))
}

_, err := conn.PutEncryptionConfig(input)
if err != nil {
return fmt.Errorf("error creating XRay Encryption Config: %w", err)
}

d.SetId(meta.(*AWSClient).region)

if _, err := waiter.EncryptionConfigAvailable(conn); err != nil {
return fmt.Errorf("error waiting for Xray Encryption Config (%s) to Available: %w", d.Id(), err)
}

return resourceAwsXrayEncryptionConfigRead(d, meta)
}

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

config, err := conn.GetEncryptionConfig(&xray.GetEncryptionConfigInput{})

if err != nil {
return fmt.Errorf("error reading XRay Encryption Config: %w", err)
}

d.Set("key_id", config.EncryptionConfig.KeyId)
d.Set("type", config.EncryptionConfig.Type)

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

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/xray"
"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 TestAccAWSXrayEncryptionConfig_basic(t *testing.T) {
var EncryptionConfig xray.EncryptionConfig
resourceName := "aws_xray_encryption_config.test"
keyResourceName := "aws_kms_key.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSXrayEncryptionConfigBasicConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayEncryptionConfigExists(resourceName, &EncryptionConfig),
resource.TestCheckResourceAttr(resourceName, "type", "NONE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSXrayEncryptionConfigWithKeyConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayEncryptionConfigExists(resourceName, &EncryptionConfig),
resource.TestCheckResourceAttr(resourceName, "type", "KMS"),
resource.TestCheckResourceAttrPair(resourceName, "key_id", keyResourceName, "arn"),
),
},
{
Config: testAccAWSXrayEncryptionConfigBasicConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayEncryptionConfigExists(resourceName, &EncryptionConfig),
resource.TestCheckResourceAttr(resourceName, "type", "NONE"),
),
},
},
})
}

func testAccCheckXrayEncryptionConfigExists(n string, EncryptionConfig *xray.EncryptionConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No XRay Encryption Config ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).xrayconn

config, err := conn.GetEncryptionConfig(&xray.GetEncryptionConfigInput{})

if err != nil {
return err
}

*EncryptionConfig = *config.EncryptionConfig

return nil
}
}

func testAccAWSXrayEncryptionConfigBasicConfig() string {
return fmt.Sprintf(`
resource "aws_xray_encryption_config" "test" {
type = "NONE"
}
`)
}

func testAccAWSXrayEncryptionConfigWithKeyConfig() string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = "Terraform acc test %s"
deletion_window_in_days = 7
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
POLICY
}
resource "aws_xray_encryption_config" "test" {
type = "KMS"
key_id = aws_kms_key.test.arn
}
`, acctest.RandString(8))
}
72 changes: 72 additions & 0 deletions website/docs/r/xray_encryption_config.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
subcategory: "XRay"
layout: "aws"
page_title: "AWS: aws_xray_encryption_config"
description: |-
Creates and manages an AWS XRay Encryption Config.
---

# Resource: aws_xray_encryption_config

Creates and manages an AWS XRay Encryption Config.

~> **NOTE:** Removing this resource from Terraform has no effect to the encryption configuration within X-Ray.

## Example Usage

```hcl
resource "aws_xray_encryption_config" "example" {
type = "NONE"
}
```

## Example Usage with KMS Key

```hcl
resource "aws_kms_key" "example" {
description = "Some Key"
deletion_window_in_days = 7
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
POLICY
}
resource "aws_xray_encryption_config" "example" {
type = "KMS"
key_id = aws_kms_key.example.arn
}
```

## Argument Reference

* `type` - (Required) The type of encryption. Set to `KMS` to use your own key for encryption. Set to `NONE` for default encryption.
* `key_id` - (Optional) An AWS KMS customer master key (CMK) ARN.

## Attributes Reference

In addition to the arguments above, the following attributes are exported:

* `id` - Region name.

## Import

XRay Encryption Config can be imported using the region name, e.g.

```
$ terraform import aws_xray_encryption_config.example us-west-2
```

0 comments on commit 581768a

Please sign in to comment.