diff --git a/.changelog/20667.txt b/.changelog/20667.txt new file mode 100644 index 00000000000..b84ccdb1e70 --- /dev/null +++ b/.changelog/20667.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_chime_voice_connector_termination +``` \ No newline at end of file diff --git a/aws/config.go b/aws/config.go index ff09b292064..9fe0fe6e3e3 100644 --- a/aws/config.go +++ b/aws/config.go @@ -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) diff --git a/aws/provider.go b/aws/provider.go index 93e42452494..5fd89096b8a 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/aws/resource_aws_chime_voice_connector_termination.go b/aws/resource_aws_chime_voice_connector_termination.go new file mode 100644 index 00000000000..9ae2aff05e1 --- /dev/null +++ b/aws/resource_aws_chime_voice_connector_termination.go @@ -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), + }, + }, + "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 +} diff --git a/aws/resource_aws_chime_voice_connector_termination_test.go b/aws/resource_aws_chime_voice_connector_termination_test.go new file mode 100644 index 00000000000..0d650ff0d91 --- /dev/null +++ b/aws/resource_aws_chime_voice_connector_termination_test.go @@ -0,0 +1,187 @@ +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) { + 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: testAccCheckAWSChimeVoiceConnectorTerminationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSChimeVoiceConnectorTerminationConfig(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSChimeVoiceConnectorTerminationExists(resourceName), + 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: resourceName, + 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: testAccCheckAWSChimeVoiceConnectorTerminationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSChimeVoiceConnectorTerminationConfig(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSChimeVoiceConnectorTerminationExists(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsChimeVoiceConnectorTermination(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccAWSChimeVoiceConnectorTermination_update(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: testAccCheckAWSChimeVoiceConnectorTerminationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSChimeVoiceConnectorTerminationConfig(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSChimeVoiceConnectorTerminationExists(resourceName), + ), + }, + { + Config: testAccAWSChimeVoiceConnectorTerminationUpdated(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSChimeVoiceConnectorTerminationExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "cps_limit", "1"), + resource.TestCheckResourceAttr(resourceName, "calling_regions.#", "3"), + resource.TestCheckTypeSetElemAttr(resourceName, "cidr_allow_list.*", "100.35.78.97/32"), + resource.TestCheckResourceAttr(resourceName, "disabled", "false"), + resource.TestCheckResourceAttr(resourceName, "default_phone_number", ""), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +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.ID), + } + + resp, err := conn.GetVoiceConnectorTermination(input) + if err != nil { + return err + } + + if resp == nil || resp.Termination == nil { + return fmt.Errorf("Chime Voice Connector Termintation (%s) not found", rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckAWSChimeVoiceConnectorTerminationDestroy(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.GetVoiceConnectorTerminationInput{ + VoiceConnectorId: aws.String(rs.Primary.ID), + } + resp, err := conn.GetVoiceConnectorTermination(input) + + if isAWSErr(err, chime.ErrCodeNotFoundException, "") { + continue + } + + if err != nil { + return err + } + + if resp != nil && resp.Termination != nil { + return fmt.Errorf("error Chime Voice Connector Termination still exists") + } + } + + return nil +} diff --git a/website/docs/r/chime_voice_connector_termination.html.markdown b/website/docs/r/chime_voice_connector_termination.html.markdown new file mode 100644 index 00000000000..cab606cc72a --- /dev/null +++ b/website/docs/r/chime_voice_connector_termination.html.markdown @@ -0,0 +1,53 @@ +--- +subcategory: "Chime" +layout: "aws" +page_title: "AWS: aws_chime_voice_connector_termination" +description: |- + Enable Termination settings to control outbound calling from your SIP infrastructure. +--- + +# Resource: aws_chime_voice_connector_termination + +Enable Termination settings to control outbound calling from your SIP infrastructure. + +## Example Usage + +```terraform +resource "aws_chime_voice_connector" "default" { + name = "vc-name-test" + require_encryption = true +} + +resource "aws_chime_voice_connector_termination" "default" { + disabled = false + cps_limit = 1 + cidr_allow_list = ["50.35.78.96/31"] + calling_regions = ["US", "CA"] + voice_connector_id = aws_chime_voice_connector.default.id +} +``` + +## Argument Reference + +The following arguments are supported: + +* `voice_connector_id` - (Required) The Amazon Chime Voice Connector ID. +* `cidr_allow_list` - (Required) The IP addresses allowed to make calls, in CIDR format. +* `calling_regions` - (Required) The countries to which calls are allowed, in ISO 3166-1 alpha-2 format. +* `disabled` - (Optional) When termination settings are disabled, outbound calls can not be made. +* `default_phone_number` - (Optional) The default caller ID phone number. +* `cps_limit` - (Optional) The limit on calls per second. Max value based on account service quota. Default value of `1`. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The Amazon Chime Voice Connector ID. + +## Import + +Chime Voice Connector Termination can be imported using the `voice_connector_id`, e.g. + +``` +$ terraform import aws_chime_voice_connector_termination.default abcdef1ghij2klmno3pqr4 +``` \ No newline at end of file