Skip to content

Commit

Permalink
Merge pull request #4212 from loivis/data-source-cognito-user-pool-ids
Browse files Browse the repository at this point in the history
New Data Source: cognito_user_pools
  • Loading branch information
bflad authored Apr 17, 2018
2 parents fd0d5e3 + 4448516 commit ef43a69
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
77 changes: 77 additions & 0 deletions aws/data_source_aws_cognito_user_pools.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions aws/data_source_aws_cognito_user_pools_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<li<%= sidebar_current("docs-aws-datasource-cloudwatch-log-group") %>>
<a href="/docs/providers/aws/d/cloudwatch_log_group.html">aws_cloudwatch_log_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-cognito-user-pools") %>>
<a href="/docs/providers/aws/d/cognito_user_pools.html">aws_cognito_user_pools</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-db-instance") %>>
<a href="/docs/providers/aws/d/db_instance.html">aws_db_instance</a>
</li>
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/cognito_user_pools.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ef43a69

Please sign in to comment.