-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for ChimeSDKVoice Sip Rule (#32070)
* Adding support for ChimeSDKVoice Sip Rule * Lint fixes for test files and website * import, docs fix * Adding change log * Fix changelog * r/aws_chimesdkvoice_sip_rule(test): re-order test check funcs * r/aws_chimesdkvoice_sip_rule(docs): tidy argument casing --------- Co-authored-by: Jared Baker <jared.baker@hashicorp.com>
- Loading branch information
1 parent
56c2361
commit a19337d
Showing
6 changed files
with
539 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_chimesdkvoice_sip_rule | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package chimesdkvoice | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/chimesdkvoice" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" | ||
"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" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" | ||
) | ||
|
||
// @SDKResource("aws_chimesdkvoice_sip_rule", name="Sip Rule") | ||
func ResourceSipRule() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateWithoutTimeout: resourceSipRuleCreate, | ||
ReadWithoutTimeout: resourceSipRuleRead, | ||
UpdateWithoutTimeout: resourceSipRuleUpdate, | ||
DeleteWithoutTimeout: resourceSipRuleDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"disabled": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 256), | ||
}, | ||
"target_applications": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
MinItems: 1, | ||
MaxItems: 25, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"aws_region": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"priority": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntAtLeast(1), | ||
}, | ||
"sip_media_application_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 256), | ||
}, | ||
}, | ||
}, | ||
}, | ||
"trigger_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(chimesdkvoice.SipRuleTriggerType_Values(), false), | ||
}, | ||
"trigger_value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSipRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx) | ||
|
||
input := &chimesdkvoice.CreateSipRuleInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
TriggerType: aws.String(d.Get("trigger_type").(string)), | ||
TriggerValue: aws.String(d.Get("trigger_value").(string)), | ||
TargetApplications: expandSipRuleTargetApplications(d.Get("target_applications").(*schema.Set).List()), | ||
} | ||
|
||
if v, ok := d.GetOk("disabled"); ok { | ||
input.Disabled = aws.Bool(v.(bool)) | ||
} | ||
|
||
resp, err := conn.CreateSipRuleWithContext(ctx, input) | ||
|
||
if err != nil || resp.SipRule == nil { | ||
return sdkdiag.AppendErrorf(diags, "creating ChimeSKVoice Sip Rule: %s", err) | ||
} | ||
|
||
d.SetId(aws.StringValue(resp.SipRule.SipRuleId)) | ||
|
||
return append(diags, resourceSipRuleRead(ctx, d, meta)...) | ||
} | ||
|
||
func resourceSipRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx) | ||
|
||
getInput := &chimesdkvoice.GetSipRuleInput{ | ||
SipRuleId: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetSipRuleWithContext(ctx, getInput) | ||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) { | ||
log.Printf("[WARN] ChimeSDKVoice Sip Rule %s not found", d.Id()) | ||
d.SetId("") | ||
return diags | ||
} | ||
|
||
if err != nil || resp.SipRule == nil { | ||
return sdkdiag.AppendErrorf(diags, "getting Sip Rule (%s): %s", d.Id(), err) | ||
} | ||
|
||
d.Set("name", resp.SipRule.Name) | ||
d.Set("disabled", resp.SipRule.Disabled) | ||
d.Set("trigger_type", resp.SipRule.TriggerType) | ||
d.Set("trigger_value", resp.SipRule.TriggerValue) | ||
d.Set("target_applications", flattenSipRuleTargetApplications(resp.SipRule.TargetApplications)) | ||
return diags | ||
} | ||
|
||
func resourceSipRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx) | ||
|
||
updateInput := &chimesdkvoice.UpdateSipRuleInput{ | ||
SipRuleId: aws.String(d.Id()), | ||
Name: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if d.HasChanges("target_applications") { | ||
updateInput.TargetApplications = expandSipRuleTargetApplications(d.Get("target_applications").(*schema.Set).List()) | ||
} | ||
|
||
if d.HasChanges("disabled") { | ||
updateInput.Disabled = aws.Bool(d.Get("disabled").(bool)) | ||
} | ||
|
||
if _, err := conn.UpdateSipRuleWithContext(ctx, updateInput); err != nil { | ||
return sdkdiag.AppendErrorf(diags, "updating Sip Rule (%s): %s", d.Id(), err) | ||
} | ||
|
||
return append(diags, resourceSipRuleRead(ctx, d, meta)...) | ||
} | ||
|
||
func resourceSipRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx) | ||
|
||
input := &chimesdkvoice.DeleteSipRuleInput{ | ||
SipRuleId: aws.String(d.Id()), | ||
} | ||
|
||
if _, err := conn.DeleteSipRuleWithContext(ctx, input); err != nil { | ||
if tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) { | ||
log.Printf("[WARN] ChimeSDKVoice Sip Rule %s not found", d.Id()) | ||
return diags | ||
} | ||
return sdkdiag.AppendErrorf(diags, "deleting Sip Rule (%s)", d.Id()) | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func expandSipRuleTargetApplications(data []interface{}) []*chimesdkvoice.SipRuleTargetApplication { | ||
var targetApplications []*chimesdkvoice.SipRuleTargetApplication | ||
|
||
for _, rItem := range data { | ||
item := rItem.(map[string]interface{}) | ||
application := &chimesdkvoice.SipRuleTargetApplication{ | ||
SipMediaApplicationId: aws.String(item["sip_media_application_id"].(string)), | ||
Priority: aws.Int64(int64(item["priority"].(int))), | ||
AwsRegion: aws.String(item["aws_region"].(string)), | ||
} | ||
|
||
targetApplications = append(targetApplications, application) | ||
} | ||
|
||
return targetApplications | ||
} | ||
|
||
func flattenSipRuleTargetApplications(apiObject []*chimesdkvoice.SipRuleTargetApplication) []interface{} { | ||
var rawSipRuleTargetApplications []interface{} | ||
|
||
for _, e := range apiObject { | ||
rawTargetApplication := map[string]interface{}{ | ||
"sip_media_application_id": aws.StringValue(e.SipMediaApplicationId), | ||
"priority": aws.Int64Value(e.Priority), | ||
"aws_region": aws.StringValue(e.AwsRegion), | ||
} | ||
|
||
rawSipRuleTargetApplications = append(rawSipRuleTargetApplications, rawTargetApplication) | ||
} | ||
return rawSipRuleTargetApplications | ||
} |
Oops, something went wrong.