Skip to content

Commit

Permalink
r/cognito_user_pool_client: update ImportStateIdFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei.wang committed Apr 24, 2018
1 parent b5247ec commit de5d2cc
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions aws/resource_aws_cognito_user_pool_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,30 @@ func TestAccAWSCognitoUserPoolClient_importBasic(t *testing.T) {

resourceName := "aws_cognito_user_pool_client.client"

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

conn := testAccProvider.Meta().(*AWSClient).cognitoidpconn
// do we need pagination-like logic here in case too many existing user pools?
pools, err := conn.ListUserPools(&cognitoidentityprovider.ListUserPoolsInput{MaxResults: aws.Int64(60)})
if err != nil {
return "", fmt.Errorf("failed to list cognito user pools: %s", err)
}
for _, pool := range pools.UserPools {
if aws.StringValue(pool.Name) == userPoolName {
userPoolId = aws.StringValue(pool.Id)
}
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}
if len(userPoolId) == 0 {
return "", fmt.Errorf("cognito user pool %s not found", userPoolName)

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

clients, err := conn.ListUserPoolClients(&cognitoidentityprovider.ListUserPoolClientsInput{
conn := testAccProvider.Meta().(*AWSClient).cognitoidpconn
userPoolId := rs.Primary.Attributes["user_pool_id"]
clientId := rs.Primary.ID

params := &cognitoidentityprovider.DescribeUserPoolClientInput{
UserPoolId: aws.String(userPoolId),
MaxResults: aws.Int64(10),
})
if err != nil {
return "", fmt.Errorf("failed to list clients in user pool %s: %s", userPoolName, err)
}
for _, client := range clients.UserPoolClients {
if aws.StringValue(client.ClientName) == clientName {
clientId = aws.StringValue(client.ClientId)
}
ClientId: aws.String(clientId),
}
if len(clientId) == 0 {
return "", fmt.Errorf("client %s not found in cognito user pool %s", clientName, userPoolName)

_, err := conn.DescribeUserPoolClient(params)

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

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

0 comments on commit de5d2cc

Please sign in to comment.