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

Add new chime_voice_connector_streaming resource #20933

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/20933.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_chime_voice_connector_streaming
```
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ func Provider() *schema.Provider {
"aws_chime_voice_connector": resourceAwsChimeVoiceConnector(),
"aws_chime_voice_connector_group": resourceAwsChimeVoiceConnectorGroup(),
"aws_chime_voice_connector_logging": resourceAwsChimeVoiceConnectorLogging(),
"aws_chime_voice_connector_streaming": resourceAwsChimeVoiceConnectorStreaming(),
"aws_chime_voice_connector_origination": resourceAwsChimeVoiceConnectorOrigination(),
"aws_chime_voice_connector_termination": resourceAwsChimeVoiceConnectorTermination(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
Expand Down
190 changes: 190 additions & 0 deletions aws/resource_aws_chime_voice_connector_streaming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package aws

import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/chime"
"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 resourceAwsChimeVoiceConnectorStreaming() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAwsChimeVoiceConnectorStreamingCreate,
ReadWithoutTimeout: resourceAwsChimeVoiceConnectorStreamingRead,
UpdateWithoutTimeout: resourceAwsChimeVoiceConnectorStreamingUpdate,
DeleteWithoutTimeout: resourceAwsChimeVoiceConnectorStreamingDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"data_retention": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(0),
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"streaming_notification_targets": {
Type: schema.TypeSet,
MinItems: 1,
MaxItems: 3,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(chime.NotificationTarget_Values(), false),
},
},
"voice_connector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsChimeVoiceConnectorStreamingCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).chimeconn

vcId := d.Get("voice_connector_id").(string)
input := &chime.PutVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(vcId),
}

config := &chime.StreamingConfiguration{
DataRetentionInHours: aws.Int64(int64(d.Get("data_retention").(int))),
Disabled: aws.Bool(d.Get("disabled").(bool)),
}

if v, ok := d.GetOk("streaming_notification_targets"); ok && v.(*schema.Set).Len() > 0 {
config.StreamingNotificationTargets = expandStreamingNotificationTargets(v.(*schema.Set).List())
}

input.StreamingConfiguration = config

if _, err := conn.PutVoiceConnectorStreamingConfigurationWithContext(ctx, input); err != nil {
return diag.Errorf("error creating Chime Voice Connector (%s) streaming configuration: %s", vcId, err)
}

d.SetId(vcId)

return resourceAwsChimeVoiceConnectorStreamingRead(ctx, d, meta)
}

func resourceAwsChimeVoiceConnectorStreamingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).chimeconn

input := &chime.GetVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(d.Id()),
}

resp, err := conn.GetVoiceConnectorStreamingConfigurationWithContext(ctx, input)
if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Chime Voice Connector (%s) streaming not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.Errorf("error getting Chime Voice Connector (%s) streaming: %s", d.Id(), err)
}

if resp == nil || resp.StreamingConfiguration == nil {
return diag.Errorf("error getting Chime Voice Connector (%s) streaming: empty response", d.Id())
}

d.Set("disabled", resp.StreamingConfiguration.Disabled)
d.Set("data_retention", resp.StreamingConfiguration.DataRetentionInHours)
d.Set("voice_connector_id", d.Id())

if err := d.Set("streaming_notification_targets", flattenStreamingNotificationTargets(resp.StreamingConfiguration.StreamingNotificationTargets)); err != nil {
return diag.Errorf("error setting Chime Voice Connector streaming configuration targets (%s): %s", d.Id(), err)
}

return nil
}

func resourceAwsChimeVoiceConnectorStreamingUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).chimeconn

vcId := d.Get("voice_connector_id").(string)

if d.HasChanges("data_retention", "disabled", "streaming_notification_targets") {
input := &chime.PutVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(vcId),
}

config := &chime.StreamingConfiguration{
DataRetentionInHours: aws.Int64(int64(d.Get("data_retention").(int))),
Disabled: aws.Bool(d.Get("disabled").(bool)),
}

if v, ok := d.GetOk("streaming_notification_targets"); ok && v.(*schema.Set).Len() > 0 {
config.StreamingNotificationTargets = expandStreamingNotificationTargets(v.(*schema.Set).List())
}

input.StreamingConfiguration = config

if _, err := conn.PutVoiceConnectorStreamingConfigurationWithContext(ctx, input); err != nil {
if isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] error getting Chime Voice Connector (%s) streaming configuration", d.Id())
d.SetId("")
return nil
}
return diag.Errorf("error updating Chime Voice Connector (%s) streaming configuration: %s", d.Id(), err)
}
}

return resourceAwsChimeVoiceConnectorStreamingRead(ctx, d, meta)
}

func resourceAwsChimeVoiceConnectorStreamingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).chimeconn

input := &chime.DeleteVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(d.Id()),
}

_, err := conn.DeleteVoiceConnectorStreamingConfigurationWithContext(ctx, input)

if isAWSErr(err, chime.ErrCodeNotFoundException, "") {
return nil
}

if err != nil {
return diag.Errorf("error deleting Chime Voice Connector (%s) streaming configuration: %s", d.Id(), err)
}

return nil
}

func expandStreamingNotificationTargets(data []interface{}) []*chime.StreamingNotificationTarget {
var streamingTargets []*chime.StreamingNotificationTarget

for _, item := range data {
streamingTargets = append(streamingTargets, &chime.StreamingNotificationTarget{
NotificationTarget: aws.String(item.(string)),
})
}

return streamingTargets
}

func flattenStreamingNotificationTargets(targets []*chime.StreamingNotificationTarget) []*string {
var rawTargets []*string

for _, t := range targets {
rawTargets = append(rawTargets, t.NotificationTarget)
}

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

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/chime"
"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 TestAccAWSChimeVoiceConnectorStreaming_basic(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_streaming.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorStreamingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorStreamingConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorStreamingExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "data_retention", "5"),
resource.TestCheckResourceAttr(resourceName, "disabled", "false"),
resource.TestCheckResourceAttr(resourceName, "streaming_notification_targets.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSChimeVoiceConnectorStreaming_disappears(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_streaming.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorStreamingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorStreamingConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorStreamingExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsChimeVoiceConnectorStreaming(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSChimeVoiceConnectorStreaming_update(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_streaming.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorStreamingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorStreamingConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorStreamingExists(resourceName),
),
},
{
Config: testAccAWSChimeVoiceConnectorStreamingUpdated(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorStreamingExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "data_retention", "2"),
resource.TestCheckResourceAttr(resourceName, "disabled", "false"),
resource.TestCheckResourceAttr(resourceName, "streaming_notification_targets.#", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSChimeVoiceConnectorStreamingConfig(name string) string {
return fmt.Sprintf(`
resource "aws_chime_voice_connector" "chime" {
name = "vc-%[1]s"
require_encryption = true
}

resource "aws_chime_voice_connector_streaming" "test" {
voice_connector_id = aws_chime_voice_connector.chime.id

disabled = false
data_retention = 5
streaming_notification_targets = ["SQS"]
}
`, name)
}

func testAccAWSChimeVoiceConnectorStreamingUpdated(name string) string {
return fmt.Sprintf(`
resource "aws_chime_voice_connector" "chime" {
name = "vc-%[1]s"
require_encryption = true
}

resource "aws_chime_voice_connector_streaming" "test" {
voice_connector_id = aws_chime_voice_connector.chime.id

disabled = false
data_retention = 2
streaming_notification_targets = ["SQS", "SNS"]
}
`, name)
}

func testAccCheckAWSChimeVoiceConnectorStreamingExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("not found: %s", name)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no Chime Voice Connector streaming configuration ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).chimeconn
input := &chime.GetVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(rs.Primary.ID),
}

resp, err := conn.GetVoiceConnectorStreamingConfiguration(input)
if err != nil {
return err
}

if resp == nil || resp.StreamingConfiguration == nil {
return fmt.Errorf("no Chime Voice Connector Streaming configuration (%s) found", rs.Primary.ID)
}

return nil
}
}

func testAccCheckAWSChimeVoiceConnectorStreamingDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_chime_voice_connector_termination" {
continue
}
conn := testAccProvider.Meta().(*AWSClient).chimeconn
input := &chime.GetVoiceConnectorStreamingConfigurationInput{
VoiceConnectorId: aws.String(rs.Primary.ID),
}
resp, err := conn.GetVoiceConnectorStreamingConfiguration(input)

if isAWSErr(err, chime.ErrCodeNotFoundException, "") {
continue
}

if err != nil {
return err
}

if resp != nil && resp.StreamingConfiguration != nil {
return fmt.Errorf("error Chime Voice Connector streaming configuration still exists")
}
}

return nil
}
Loading