Skip to content

Commit

Permalink
Merge pull request #3762 from loivis/3753-cognito-user-pool-client-im…
Browse files Browse the repository at this point in the history
…port

resource/cognito_user_pool_client: fix import with user pool id
  • Loading branch information
bflad authored Apr 24, 2018
2 parents b693ab0 + de5d2cc commit b9b86c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
17 changes: 16 additions & 1 deletion aws/resource_aws_cognito_user_pool_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
Expand All @@ -18,7 +20,7 @@ func resourceAwsCognitoUserPoolClient() *schema.Resource {
Delete: resourceAwsCognitoUserPoolClientDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsCognitoUserPoolClientImport,
},

// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html
Expand Down Expand Up @@ -340,3 +342,16 @@ func resourceAwsCognitoUserPoolClientDelete(d *schema.ResourceData, meta interfa

return nil
}

func resourceAwsCognitoUserPoolClientImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if len(strings.Split(d.Id(), "/")) != 2 || len(d.Id()) < 3 {
return []*schema.ResourceData{}, fmt.Errorf("[ERR] Wrong format of resource: %s. Please follow 'user-pool-id/client-id'", d.Id())
}
userPoolId := strings.Split(d.Id(), "/")[0]
clientId := strings.Split(d.Id(), "/")[1]
d.SetId(clientId)
d.Set("user_pool_id", userPoolId)
log.Printf("[DEBUG] Importing client %s for user pool %s", clientId, userPoolId)

return []*schema.ResourceData{d}, nil
}
61 changes: 56 additions & 5 deletions aws/resource_aws_cognito_user_pool_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,59 @@ func TestAccAWSCognitoUserPoolClient_basic(t *testing.T) {
})
}

func TestAccAWSCognitoUserPoolClient_importBasic(t *testing.T) {
userPoolName := fmt.Sprintf("tf-acc-cognito-user-pool-%s", acctest.RandString(7))
clientName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resourceName := "aws_cognito_user_pool_client.client"

getStateId := func(s *terraform.State) (string, error) {

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return "", errors.New("No Cognito User Pool Client ID set")
}

conn := testAccProvider.Meta().(*AWSClient).cognitoidpconn
userPoolId := rs.Primary.Attributes["user_pool_id"]
clientId := rs.Primary.ID

params := &cognitoidentityprovider.DescribeUserPoolClientInput{
UserPoolId: aws.String(userPoolId),
ClientId: aws.String(clientId),
}

_, err := conn.DescribeUserPoolClient(params)

if err != nil {
return "", err
}

return fmt.Sprintf("%s/%s", userPoolId, clientId), nil
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolClientConfig_basic(userPoolName, clientName),
},
{
ResourceName: resourceName,
ImportStateIdFunc: getStateId,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCognitoUserPoolClient_allFields(t *testing.T) {
userPoolName := fmt.Sprintf("tf-acc-cognito-user-pool-%s", acctest.RandString(7))
clientName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
Expand Down Expand Up @@ -142,12 +195,10 @@ resource "aws_cognito_user_pool" "pool" {
}
resource "aws_cognito_user_pool_client" "client" {
name = "%s"
user_pool_id = "${aws_cognito_user_pool.pool.id}"
explicit_auth_flows = [ "ADMIN_NO_SRP_AUTH" ]
name = "%s"
user_pool_id = "${aws_cognito_user_pool.pool.id}"
explicit_auth_flows = ["ADMIN_NO_SRP_AUTH"]
}
`, userPoolName, clientName)
}

Expand Down

0 comments on commit b9b86c3

Please sign in to comment.