From 44485162597f1c0043f6526775754e629d15520b Mon Sep 17 00:00:00 2001 From: "xiaowei.wang" Date: Sun, 15 Apr 2018 17:01:32 +0200 Subject: [PATCH] add new data source cognito_user_pools --- aws/data_source_aws_cognito_user_pools.go | 77 +++++++++++++++++++ ...data_source_aws_cognito_user_pools_test.go | 51 ++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + website/docs/d/cognito_user_pools.markdown | 39 ++++++++++ 5 files changed, 171 insertions(+) create mode 100644 aws/data_source_aws_cognito_user_pools.go create mode 100644 aws/data_source_aws_cognito_user_pools_test.go create mode 100644 website/docs/d/cognito_user_pools.markdown diff --git a/aws/data_source_aws_cognito_user_pools.go b/aws/data_source_aws_cognito_user_pools.go new file mode 100644 index 00000000000..f365e0987c8 --- /dev/null +++ b/aws/data_source_aws_cognito_user_pools.go @@ -0,0 +1,77 @@ +package aws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsCognitoUserPools() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsCognitoUserPoolsRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceAwsCognitoUserPoolsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cognitoidpconn + name := d.Get("name").(string) + var ids []string + + pools, err := getAllCognitoUserPools(conn) + if err != nil { + return fmt.Errorf("Error listing cognito user pools: %s", err) + } + for _, pool := range pools { + if name == aws.StringValue(pool.Name) { + ids = append(ids, aws.StringValue(pool.Id)) + } + } + + if len(ids) == 0 { + return fmt.Errorf("No cognito user pool found with name: %s", name) + } + + d.SetId(name) + d.Set("ids", ids) + return nil +} + +func getAllCognitoUserPools(conn *cognitoidentityprovider.CognitoIdentityProvider) ([]*cognitoidentityprovider.UserPoolDescriptionType, error) { + var pools []*cognitoidentityprovider.UserPoolDescriptionType + var nextToken string + + for { + input := &cognitoidentityprovider.ListUserPoolsInput{ + // MaxResults Valid Range: Minimum value of 1. Maximum value of 60 + MaxResults: aws.Int64(int64(60)), + } + if nextToken != "" { + input.NextToken = aws.String(nextToken) + } + out, err := conn.ListUserPools(input) + if err != nil { + return pools, err + } + pools = append(pools, out.UserPools...) + + if out.NextToken == nil { + break + } + nextToken = aws.StringValue(out.NextToken) + } + + return pools, nil +} diff --git a/aws/data_source_aws_cognito_user_pools_test.go b/aws/data_source_aws_cognito_user_pools_test.go new file mode 100644 index 00000000000..2e49cbbb1d8 --- /dev/null +++ b/aws/data_source_aws_cognito_user_pools_test.go @@ -0,0 +1,51 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAwsCognitoUserPools_basic(t *testing.T) { + rName := fmt.Sprintf("tf_acc_ds_cognito_user_pools_%s", acctest.RandString(7)) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsCognitoUserPoolsConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_cognito_user_pools.selected", "ids.#", "3"), + ), + }, + { + Config: testAccDataSourceAwsCognitoUserPoolsConfig_notFound(rName), + ExpectError: regexp.MustCompile(`No cognito user pool found with name:`), + }, + }, + }) +} + +func testAccDataSourceAwsCognitoUserPoolsConfig_basic(rName string) string { + return fmt.Sprintf(` +resource "aws_cognito_user_pool" "main" { + count = 3 + name = "%s" +} + +data "aws_cognito_user_pools" "selected" { + name = "${aws_cognito_user_pool.main.*.name[0]}" +} +`, rName) +} + +func testAccDataSourceAwsCognitoUserPoolsConfig_notFound(rName string) string { + return fmt.Sprintf(` +data "aws_cognito_user_pools" "selected" { + name = "%s-not-found" +} +`, rName) +} diff --git a/aws/provider.go b/aws/provider.go index 270a23f0a5b..f704fc2f241 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider { "aws_cloudformation_stack": dataSourceAwsCloudFormationStack(), "aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(), "aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(), + "aws_cognito_user_pools": dataSourceAwsCognitoUserPools(), "aws_db_instance": dataSourceAwsDbInstance(), "aws_db_snapshot": dataSourceAwsDbSnapshot(), "aws_dynamodb_table": dataSourceAwsDynamoDbTable(), diff --git a/website/aws.erb b/website/aws.erb index 05f3638a36f..6a33791d177 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -73,6 +73,9 @@ > aws_cloudwatch_log_group + > + aws_cognito_user_pools + > aws_db_instance diff --git a/website/docs/d/cognito_user_pools.markdown b/website/docs/d/cognito_user_pools.markdown new file mode 100644 index 00000000000..ebbf82fc99a --- /dev/null +++ b/website/docs/d/cognito_user_pools.markdown @@ -0,0 +1,39 @@ +--- +layout: "aws" +page_title: "AWS: aws_cognito_user_pools" +sidebar_current: "docs-aws-cognito-user-pools" +description: |- + Get list of cognito user pools. +--- + +# Data Source: aws_cognito_user_pools + +Use this data source to get a list of cognito user pools. + +## Example Usage + +```hcl +data "aws_api_gateway_rest_api" "selected" { + name = "${var.api_gateway_name}" +} + +data "aws_cognito_user_pools" "selected" { + name = "${var.cognito_user_pool_name}" +} + +resource "aws_api_gateway_authorizer" "cognito" { + name = "cognito" + type = "COGNITO_USER_POOLS" + rest_api_id = "${data.aws_api_gateway_rest_api.selected.id}" + provider_arns = ["${data.aws_cognito_user_pools.selected.ids}"] +} +``` + +## Argument Reference + +* `name` - (required) Name of the cognito user pools. Name is not a unique attribute for cognito user pool, so multiple pools might be returned with given name. + + +## Attributes Reference + +* `ids` - The list of cognito user pool ids.