diff --git a/aws/resource_aws_cognito_user_pool_client.go b/aws/resource_aws_cognito_user_pool_client.go index 692f8bcf9fc..e306e70c216 100644 --- a/aws/resource_aws_cognito_user_pool_client.go +++ b/aws/resource_aws_cognito_user_pool_client.go @@ -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" @@ -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 @@ -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 +} diff --git a/aws/resource_aws_cognito_user_pool_client_test.go b/aws/resource_aws_cognito_user_pool_client_test.go index 903ccb40ec8..53b0328fa9f 100644 --- a/aws/resource_aws_cognito_user_pool_client_test.go +++ b/aws/resource_aws_cognito_user_pool_client_test.go @@ -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) @@ -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) }