Skip to content

Commit

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

Add new resource_aws_chime_voice_connector_termination_credentials
  • Loading branch information
YakDriver authored Oct 13, 2021
2 parents b95a988 + 03eafd8 commit 8913fd8
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/21162.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_chime_voice_connector_termination_credentials
```
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ func Provider() *schema.Provider {
"aws_chime_voice_connector_streaming": resourceAwsChimeVoiceConnectorStreaming(),
"aws_chime_voice_connector_origination": resourceAwsChimeVoiceConnectorOrigination(),
"aws_chime_voice_connector_termination": resourceAwsChimeVoiceConnectorTermination(),
"aws_chime_voice_connector_termination_credentials": resourceAwsChimeVoiceConnectorTerminationCredentials(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudcontrolapi_resource": resourceAwsCloudControlApiResource(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
Expand Down
162 changes: 162 additions & 0 deletions aws/resource_aws_chime_voice_connector_termination_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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 resourceAwsChimeVoiceConnectorTerminationCredentials() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationCredentialsCreate,
ReadWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationCredentialsRead,
UpdateWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationCredentialsUpdate,
DeleteWithoutTimeout: resourceAwsChimeVoiceConnectorTerminationCredentialsDelete,

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

Schema: map[string]*schema.Schema{
"credentials": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
"voice_connector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

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

input := &chime.PutVoiceConnectorTerminationCredentialsInput{
VoiceConnectorId: aws.String(vcId),
Credentials: expandCredentials(d.Get("credentials").(*schema.Set).List()),
}

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

}

d.SetId(vcId)

return resourceAwsChimeVoiceConnectorTerminationCredentialsRead(ctx, d, meta)
}

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

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

_, err := conn.ListVoiceConnectorTerminationCredentialsWithContext(ctx, input)
if !d.IsNewResource() && isAWSErr(err, chime.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Chime Voice Connector (%s) termination credentials not found, removing from state", d.Id())
d.SetId("")
return nil
}

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

d.Set("voice_connector_id", d.Id())

return nil
}

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

if d.HasChanges("credentials") {
input := &chime.PutVoiceConnectorTerminationCredentialsInput{
VoiceConnectorId: aws.String(d.Id()),
Credentials: expandCredentials(d.Get("credentials").(*schema.Set).List()),
}

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

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

return resourceAwsChimeVoiceConnectorTerminationCredentialsRead(ctx, d, meta)
}

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

input := &chime.DeleteVoiceConnectorTerminationCredentialsInput{
VoiceConnectorId: aws.String(d.Id()),
Usernames: expandCredentialsUsernames(d.Get("credentials").(*schema.Set).List()),
}

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

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

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

return nil
}

func expandCredentialsUsernames(data []interface{}) []*string {
var rawNames []*string

for _, rData := range data {
item := rData.(map[string]interface{})
rawNames = append(rawNames, aws.String(item["username"].(string)))
}

return rawNames
}

func expandCredentials(data []interface{}) []*chime.Credential {
var credentials []*chime.Credential

for _, rItem := range data {
item := rItem.(map[string]interface{})
credentials = append(credentials, &chime.Credential{
Username: aws.String(item["username"].(string)),
Password: aws.String(item["password"].(string)),
})
}

return credentials
}
196 changes: 196 additions & 0 deletions aws/resource_aws_chime_voice_connector_termination_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
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 TestAccAWSChimeVoiceConnectorTerminationCredentials_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_termination_credentials.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorTerminationCredentialsDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorTerminationCredentialsConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorTerminationCredentialsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "credentials.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"credentials"},
},
},
})
}

func TestAccAWSChimeVoiceConnectorTerminationCredentials_disappears(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_termination_credentials.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorTerminationCredentialsDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorTerminationCredentialsConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorTerminationCredentialsExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsChimeVoiceConnectorTerminationCredentials(), resourceName),
),
ExpectNonEmptyPlan: false,
},
},
})
}

func TestAccAWSChimeVoiceConnectorTerminationCredentials_update(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_chime_voice_connector_termination_credentials.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, chime.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSChimeVoiceConnectorTerminationCredentialsDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSChimeVoiceConnectorTerminationCredentialsConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorTerminationCredentialsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "credentials.#", "1"),
),
},
{
Config: testAccAWSChimeVoiceConnectorTerminationCredentialsConfigUpdated(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSChimeVoiceConnectorTerminationCredentialsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "credentials.#", "2"),
),
},
},
})
}

func testAccCheckAWSChimeVoiceConnectorTerminationCredentialsExists(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 termination credentials ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).chimeconn
input := &chime.ListVoiceConnectorTerminationCredentialsInput{
VoiceConnectorId: aws.String(rs.Primary.ID),
}

resp, err := conn.ListVoiceConnectorTerminationCredentials(input)
if err != nil {
return err
}

if resp == nil || resp.Usernames == nil {
return fmt.Errorf("no Chime Voice Connector Termintation credentials (%s) found", rs.Primary.ID)
}

return nil
}
}

func testAccCheckAWSChimeVoiceConnectorTerminationCredentialsDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_chime_voice_connector_termination_credentials" {
continue
}
conn := testAccProvider.Meta().(*AWSClient).chimeconn
input := &chime.ListVoiceConnectorTerminationCredentialsInput{
VoiceConnectorId: aws.String(rs.Primary.ID),
}
resp, err := conn.ListVoiceConnectorTerminationCredentials(input)

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

if err != nil {
return err
}

if resp != nil && resp.Usernames != nil {
return fmt.Errorf("error Chime Voice Connector Termination credentials still exists")
}
}

return nil
}

func testAccAWSChimeVoiceConnectorTerminationCredentialsConfigBase(rName 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"]
cidr_allow_list = ["50.35.78.0/27"]
}
`, rName)
}

func testAccAWSChimeVoiceConnectorTerminationCredentialsConfig(rName string) string {
return composeConfig(testAccAWSChimeVoiceConnectorTerminationCredentialsConfigBase(rName), `
resource "aws_chime_voice_connector_termination_credentials" "test" {
voice_connector_id = aws_chime_voice_connector.chime.id
credentials {
username = "test1"
password = "test1!"
}
depends_on = [aws_chime_voice_connector_termination.test]
}
`)
}

func testAccAWSChimeVoiceConnectorTerminationCredentialsConfigUpdated(rName string) string {
return composeConfig(testAccAWSChimeVoiceConnectorTerminationCredentialsConfigBase(rName), `
resource "aws_chime_voice_connector_termination_credentials" "test" {
voice_connector_id = aws_chime_voice_connector.chime.id
credentials {
username = "test1"
password = "test1!"
}
credentials {
username = "test2"
password = "test2!"
}
depends_on = [aws_chime_voice_connector_termination.test]
}
`)
}
Loading

0 comments on commit 8913fd8

Please sign in to comment.