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

F/aws chime voice connector origination #20676

Merged
merged 6 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/20676.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_chime_voice_connector_origination
```
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_origination": resourceAwsChimeVoiceConnectorOrigination(),
"aws_chime_voice_connector_termination": resourceAwsChimeVoiceConnectorTermination(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
Expand Down
211 changes: 211 additions & 0 deletions aws/resource_aws_chime_voice_connector_origination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
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 resourceAwsChimeVoiceConnectorOrigination() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAwsChimeVoiceConnectorOriginationCreate,
ReadWithoutTimeout: resourceAwsChimeVoiceConnectorOriginationRead,
UpdateWithoutTimeout: resourceAwsChimeVoiceConnectorOriginationUpdate,
DeleteWithoutTimeout: resourceAwsChimeVoiceConnectorOriginationDelete,

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

Schema: map[string]*schema.Schema{
"disabled": {
Type: schema.TypeBool,
Optional: true,
},
"route": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
MaxItems: 20,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsIPAddress,
},
"port": {
Type: schema.TypeInt,
Optional: true,
Default: 5060,
ValidateFunc: validation.IsPortNumber,
},
"priority": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 99),
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(chime.OriginationRouteProtocol_Values(), false),
},
"weight": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 99),
},
},
},
},
"voice_connector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

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

input := &chime.PutVoiceConnectorOriginationInput{
VoiceConnectorId: aws.String(vcId),
Origination: &chime.Origination{
Routes: expandOriginationRoutes(d.Get("route").(*schema.Set).List()),
},
}

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

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

d.SetId(vcId)

return resourceAwsChimeVoiceConnectorOriginationRead(ctx, d, meta)
}

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

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

resp, err := conn.GetVoiceConnectorOriginationWithContext(ctx, input)

if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Chime Voice Connector (%s) origination not found, removing from state", d.Id())
d.SetId("")
return nil
}

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

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

d.Set("disabled", resp.Origination.Disabled)
d.Set("voice_connector_id", d.Id())

if err := d.Set("route", flattenOriginationRoutes(resp.Origination.Routes)); err != nil {
return diag.Errorf("error setting Chime Voice Connector (%s) origination routes: %s", d.Id(), err)
}

return nil
}

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

if d.HasChanges("route", "disabled") {
input := &chime.PutVoiceConnectorOriginationInput{
VoiceConnectorId: aws.String(d.Id()),
Origination: &chime.Origination{
Routes: expandOriginationRoutes(d.Get("route").(*schema.Set).List()),
},
}

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

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

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

return resourceAwsChimeVoiceConnectorOriginationRead(ctx, d, meta)
}

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

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

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

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

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

return nil
}

func expandOriginationRoutes(data []interface{}) []*chime.OriginationRoute {
var originationRoutes []*chime.OriginationRoute

for _, rItem := range data {
item := rItem.(map[string]interface{})
originationRoutes = append(originationRoutes, &chime.OriginationRoute{
Host: aws.String(item["host"].(string)),
Port: aws.Int64(int64(item["port"].(int))),
Priority: aws.Int64(int64(item["priority"].(int))),
Protocol: aws.String(item["protocol"].(string)),
Weight: aws.Int64(int64(item["weight"].(int))),
})
}

return originationRoutes
}

func flattenOriginationRoutes(routes []*chime.OriginationRoute) []interface{} {
var rawRoutes []interface{}

for _, route := range routes {
r := map[string]interface{}{
"host": aws.StringValue(route.Host),
"port": aws.Int64Value(route.Port),
"priority": aws.Int64Value(route.Priority),
"protocol": aws.StringValue(route.Protocol),
"weight": aws.Int64Value(route.Weight),
}

rawRoutes = append(rawRoutes, r)
}

return rawRoutes
}
Loading