-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4212 from loivis/data-source-cognito-user-pool-ids
New Data Source: cognito_user_pools
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |