Skip to content

Commit

Permalink
Merge pull request #20676 from aleks1001/f/aws_chime_voice_connector_…
Browse files Browse the repository at this point in the history
…origination

F/aws chime voice connector origination
  • Loading branch information
anGie44 authored Sep 8, 2021
2 parents c94ebdf + 862264a commit f478dae
Show file tree
Hide file tree
Showing 5 changed files with 499 additions and 0 deletions.
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

0 comments on commit f478dae

Please sign in to comment.