Skip to content

Commit

Permalink
[WIP] - Sandbox SMS Phone Number
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Jun 19, 2023
1 parent ba85a49 commit 512ba90
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions internal/service/sns/sms_sandbox_phone_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package sns

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/service/comprehend/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"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-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
)

// @SDKResource("aws_sns_sms_sandbox_phone_number")
func ResourceSMSSandboxPhoneNumber() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceSMSSandboxPhoneNumberCreate,
ReadWithoutTimeout: resourceSMSSandboxPhoneNumberRead,
DeleteWithoutTimeout: resourceSMSSandboxPhoneNumberDelete,

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

Schema: map[string]*schema.Schema{
"phone_number": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"language_code": {
Type: schema.TypeString,
Required: true,
Computed: true,
ValidateDiagFunc: enum.Validate[types.SyntaxLanguageCode](),
},
},
}
}

func resourceSMSSandboxPhoneNumberCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SNSConn(ctx)

phoneNumber := d.Get("phone_number").(string)
input := &sns.CreateSMSSandboxPhoneNumberInput{
PhoneNumber: aws.String(phoneNumber),
LanguageCode: aws.String(d.Get("language_code").(string)),
}

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

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating SNS SMS Sandbox Phone Number (%s): %s", d.Id(), err)
}

if d.IsNewResource() {
d.SetId(phoneNumber)
}

return resourceSMSSandboxPhoneNumberRead(ctx, d, meta)
}

func resourceSMSSandboxPhoneNumberRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SNSConn(ctx)

phoneNumber := ""
status := ""

err := conn.ListSMSSandboxPhoneNumbersPagesWithContext(ctx, &sns.ListSMSSandboxPhoneNumbersInput{}, func(page *sns.ListSMSSandboxPhoneNumbersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, i := range page.PhoneNumbers {
phoneNumber = aws.StringValue(i.PhoneNumber)
status = aws.StringValue(i.Status)

if phoneNumber == d.Id() {
break
}
}

return !lastPage
})

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, sns.ErrCodeResourceNotFoundException) {
log.Printf("[WARN] SNS SMS Sandbox Phone Number (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading SNS SMS Sandbox Phone Number: %s", err)
}

d.Set("phone_number", phoneNumber)
d.Set("status", status)

return diags
}

func resourceSMSSandboxPhoneNumberDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SNSConn(ctx)

_, err := conn.PutDataProtectionPolicyWithContext(ctx, &sns.PutDataProtectionPolicyInput{
DataProtectionPolicy: aws.String(""),
ResourceArn: aws.String(d.Get("arn").(string)),
})

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting SNS Data Protection Policy (%s): %s", d.Id(), err)
}

return diags
}

0 comments on commit 512ba90

Please sign in to comment.