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 resource aws_chime_voice_connector_termination #20667

Merged
merged 15 commits into from
Sep 8, 2021
Merged
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
3 changes: 3 additions & 0 deletions .changelog/20667.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_chime_voice_connector_termination
```
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ func Provider() *schema.Provider {
"aws_budgets_budget_action": resourceAwsBudgetsBudgetAction(),
"aws_chime_voice_connector": resourceAwsChimeVoiceConnector(),
"aws_chime_voice_connector_group": resourceAwsChimeVoiceConnectorGroup(),
"aws_chime_voice_connector_termination": resourceAwsChimeVoiceConnectorTermination(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
"aws_cloudformation_stack_set": resourceAwsCloudFormationStackSet(),
Expand Down
191 changes: 191 additions & 0 deletions aws/resource_aws_chime_voice_connector_termination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package aws

import (
"context"
"fmt"
"log"
"regexp"

"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 resourceAwsChimeVoiceConnectorTermination() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAwsChimeVoiceConnectorTerminationPut,
ReadContext: resourceAwsChimeVoiceConnectorTerminationRead,
UpdateContext: resourceAwsChimeVoiceConnectorTerminationUpdate,
DeleteContext: resourceAwsChimeVoiceConnectorTerminationDelete,
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved

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

Schema: map[string]*schema.Schema{
"calling_regions": {
Type: schema.TypeList,
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(2, 2),
},
},
"cidr_allow_list": {
Type: schema.TypeList,
Copy link
Contributor

Choose a reason for hiding this comment

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

To guard against diffs if the API returns values in a different order

Suggested change
Type: schema.TypeList,
Type: schema.TypeSet,

Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsCIDRNetwork(27, 32),
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
},
},
"cps_limit": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntAtMost(1),
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
},
"default_phone_number": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^\+?[1-9]\d{1,14}$`), "must match ^\\+?[1-9]\\d{1,14}$"),
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
},
"voice_connector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsChimeVoiceConnectorTerminationPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
conn := meta.(*AWSClient).chimeconn

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

input := &chime.PutVoiceConnectorTerminationInput{
VoiceConnectorId: aws.String(vcId),
}

termination := &chime.Termination{
CidrAllowedList: expandStringList(d.Get("cidr_allow_list").([]interface{})),
CallingRegions: expandStringList(d.Get("calling_regions").([]interface{})),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
CidrAllowedList: expandStringList(d.Get("cidr_allow_list").([]interface{})),
CallingRegions: expandStringList(d.Get("calling_regions").([]interface{})),
CidrAllowedList: expandStringSet(d.Get("cidr_allow_list").(*schema.Set)),
CallingRegions: expandStringSet(d.Get("calling_regions").(*schema.Set)),

}

if v, ok := d.GetOk("disabled"); ok {
termination.Disabled = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("cps_limit"); ok {
termination.CpsLimit = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("default_phone_number"); ok {
termination.DefaultPhoneNumber = aws.String(v.(string))
}

input.Termination = termination

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

d.SetId(fmt.Sprintf("termination-%s", vcId))
Copy link
Contributor

Choose a reason for hiding this comment

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

let's just use vcId as the ID here so we can read and re-use i throughout the other CRUD ops

Suggested change
d.SetId(fmt.Sprintf("termination-%s", vcId))
d.SetId(vcId)


return resourceAwsChimeVoiceConnectorTerminationRead(ctx, d, meta)
}

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

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

resp, err := conn.GetVoiceConnectorTerminationWithContext(ctx, input)
if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] error getting Chime Voice Connector (%s) termination: %s", vcId, err)
d.SetId("")
return nil
}

if err != nil || resp.Termination == nil {
return diag.Errorf("error getting Chime Voice Connector (%s) termination: %s", vcId, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

we can re-use the ID once the change is made above e.g.

Suggested change
vcId := d.Get("voice_connector_id").(string)
input := &chime.GetVoiceConnectorTerminationInput{
VoiceConnectorId: aws.String(vcId),
}
resp, err := conn.GetVoiceConnectorTerminationWithContext(ctx, input)
if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] error getting Chime Voice Connector (%s) termination: %s", vcId, err)
d.SetId("")
return nil
}
if err != nil || resp.Termination == nil {
return diag.Errorf("error getting Chime Voice Connector (%s) termination: %s", vcId, err)
}
input := &chime.GetVoiceConnectorTerminationInput{
VoiceConnectorId: aws.String(d.Id()),
}
resp, err := conn.GetVoiceConnectorTerminationWithContext(ctx, input)
if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Chime Voice Connector Termination (%s) not found, removing from state: %s", d.Id(), err)
d.SetId("")
return nil
}
if err != nil {
return diag.Errorf("error getting Chime Voice Connector Termination (%s)": %s, d.Id(), err)
}
if resp == nil || resp.Termination == nil {
return diag.Errorf("error getting Chime Voice Connector Termination (%s): empty response", d.Id())
}


d.Set("cps_limit", resp.Termination.CpsLimit)
d.Set("disabled", resp.Termination.Disabled)
d.Set("default_phone_number", resp.Termination.DefaultPhoneNumber)

if err := d.Set("calling_regions", flattenStringList(resp.Termination.CallingRegions)); err != nil {
return diag.Errorf("error setting termination calling regions (%s): %s", vcId, err)
}
if err := d.Set("cidr_allow_list", flattenStringList(resp.Termination.CidrAllowedList)); err != nil {
return diag.Errorf("error setting termination cidr allow list (%s): %s", vcId, err)
}

return nil
}

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

if d.HasChanges("calling_regions", "cidr_allow_list", "disabled", "cps_limit", "default_phone_number") {
vcId := d.Get("voice_connector_id").(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
vcId := d.Get("voice_connector_id").(string)
vcId := d.Id()

termination := &chime.Termination{
CallingRegions: expandStringList(d.Get("calling_regions").([]interface{})),
CidrAllowedList: expandStringList(d.Get("cidr_allow_list").([]interface{})),
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
CpsLimit: aws.Int64(int64(d.Get("cps_limit").(int))),
}

if v, ok := d.GetOk("default_phone_number"); ok {
termination.DefaultPhoneNumber = aws.String(v.(string))
}

if v, ok := d.GetOk("disabled"); ok {
termination.Disabled = aws.Bool(v.(bool))
}

input := &chime.PutVoiceConnectorTerminationInput{
VoiceConnectorId: aws.String(vcId),
Termination: termination,
}

if _, err := conn.PutVoiceConnectorTerminationWithContext(ctx, input); err != nil {
if isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] error getting Chime Voice Connector (%s) termination: %s", vcId, err)
d.SetId("")
return nil
}

return diag.Errorf("error updating Chime Voice Connector (%s) termination: %s", vcId, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

since we're at the update phase here, we'll want to return all errors instead

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

}

return resourceAwsChimeVoiceConnectorTerminationRead(ctx, d, meta)
}

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

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

if _, err := conn.DeleteVoiceConnectorTerminationWithContext(ctx, input); err != nil {
return diag.Errorf("error deleting Chime Voice Connector (%s) termination (%s): %s", vcId, d.Id(), err)
}
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
157 changes: 157 additions & 0 deletions aws/resource_aws_chime_voice_connector_termination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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 TestAccAWSChimeVoiceConnectorTermination_basic(t *testing.T) {
var vc *chime.VoiceConnector
name := acctest.RandomWithPrefix("tf-acc-test")
vcResourceName := "aws_chime_voice_connector.chime"
resourceName := "aws_chime_voice_connector_termination.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorDestroy,
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorTerminationConfig(name),

Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorExists(vcResourceName, vc),
resource.TestCheckResourceAttr(resourceName, "cps_limit", "1"),
resource.TestCheckResourceAttr(resourceName, "calling_regions.#", "2"),
resource.TestCheckResourceAttr(resourceName, "cidr_allow_list.#", "1"),
resource.TestCheckResourceAttr(resourceName, "disabled", "false"),
),
},
{
ResourceName: vcResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

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

func TestAccAWSChimeVoiceConnectorTermination_update(t *testing.T) {
var vc *chime.VoiceConnector
name := acctest.RandomWithPrefix("tf-acc-test")
vcResourceName := "aws_chime_voice_connector.chime"
resourceName := "aws_chime_voice_connector_termination.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorTerminationConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorExists(vcResourceName, vc),
),
},
{
ResourceName: vcResourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSChimeVoiceConnectorTerminationUpdated(name),
Check: resource.ComposeAggregateTestCheckFunc(
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "cps_limit", "1"),
resource.TestCheckResourceAttr(resourceName, "calling_regions.#", "3"),
),
},
},
})
}

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

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

calling_regions = ["US", "RU"]
cidr_allow_list = ["50.35.78.97/32"]
}
`, name)
}

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

resource "aws_chime_voice_connector_termination" "test" {
voice_connector_id = aws_chime_voice_connector.chime.id
disabled = false
calling_regions = ["US", "RU", "CA"]
cidr_allow_list = ["100.35.78.97/32"]
}
`, name)
}

func testAccCheckAWSChimeVoiceConnectorTerminationExists(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 group ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).chimeconn
input := &chime.GetVoiceConnectorTerminationInput{
VoiceConnectorId: aws.String(rs.Primary.Attributes["voice_connector_id"]),
}

resp, err := conn.GetVoiceConnectorTermination(input)
if err != nil || resp.Termination == nil {
return err
}
aleks1001 marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
}
Loading