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 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/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
```
10 changes: 10 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,16 @@ func (c *Config) Client() (interface{}, error) {
}
})

client.chimeconn.Handlers.Retry.PushBack(func(r *request.Request) {
// When calling CreateVoiceConnector across multiple resources,
// the API can randomly return a BadRequestException without explanation
if r.Operation.Name == "CreateVoiceConnector" {
if tfawserr.ErrMessageContains(r.Error, chime.ErrCodeBadRequestException, "Service received a bad request") {
r.Retryable = aws.Bool(true)
}
}
})

client.cloudhsmv2conn.Handlers.Retry.PushBack(func(r *request.Request) {
if tfawserr.ErrMessageContains(r.Error, cloudhsmv2.ErrCodeCloudHsmInternalFailureException, "request was rejected because of an AWS CloudHSM internal failure") {
r.Retryable = aws.Bool(true)
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,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
196 changes: 196 additions & 0 deletions aws/resource_aws_chime_voice_connector_termination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package aws

import (
"context"
"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{
CreateWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationCreate,
ReadWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationRead,
UpdateWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationUpdate,
DeleteWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationDelete,

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

Schema: map[string]*schema.Schema{
"calling_regions": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(2, 2),
},
},
"cidr_allow_list": {
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.IntAtLeast(1),
},
"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 resourceAwsChimeVoiceConnectorTerminationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).chimeconn

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

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

termination := &chime.Termination{
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(vcId)

return resourceAwsChimeVoiceConnectorTerminationRead(ctx, d, meta)
}

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

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 (%s) termination not found, removing from state", d.Id())
d.SetId("")
return nil
}

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

if resp == nil || resp.Termination == nil {
return diag.Errorf("error getting Chime Voice Connector (%s) termination: 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", d.Id(), 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", d.Id(), err)
}

d.Set("voice_connector_id", d.Id())

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") {
termination := &chime.Termination{
CallingRegions: expandStringSet(d.Get("calling_regions").(*schema.Set)),
CidrAllowedList: expandStringSet(d.Get("cidr_allow_list").(*schema.Set)),
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(d.Id()),
Termination: termination,
}

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

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

return resourceAwsChimeVoiceConnectorTerminationRead(ctx, d, meta)
}

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

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

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

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

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

return nil
}
Loading