Skip to content

Commit

Permalink
Merge pull request #4256 from loivis/data-source-cognito-user-pools
Browse files Browse the repository at this point in the history
data-source/cognito_user_pools: add attribute of arns
  • Loading branch information
bflad authored Apr 20, 2018
2 parents d40aeef + 8b8fa36 commit b4bb3f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
21 changes: 20 additions & 1 deletion aws/data_source_aws_cognito_user_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -21,6 +22,11 @@ func dataSourceAwsCognitoUserPools() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -29,14 +35,25 @@ func dataSourceAwsCognitoUserPoolsRead(d *schema.ResourceData, meta interface{})
conn := meta.(*AWSClient).cognitoidpconn
name := d.Get("name").(string)
var ids []string
var arns []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))
id := aws.StringValue(pool.Id)
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "cognito-idp",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("userpool/%s", id),
}.String()

ids = append(ids, id)
arns = append(arns, arn)
}
}

Expand All @@ -46,6 +63,8 @@ func dataSourceAwsCognitoUserPoolsRead(d *schema.ResourceData, meta interface{})

d.SetId(name)
d.Set("ids", ids)
d.Set("arns", arns)

return nil
}

Expand Down
5 changes: 3 additions & 2 deletions aws/data_source_aws_cognito_user_pools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func TestAccDataSourceAwsCognitoUserPools_basic(t *testing.T) {
{
Config: testAccDataSourceAwsCognitoUserPoolsConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_cognito_user_pools.selected", "ids.#", "3"),
resource.TestCheckResourceAttr("data.aws_cognito_user_pools.selected", "ids.#", "2"),
resource.TestCheckResourceAttr("data.aws_cognito_user_pools.selected", "arns.#", "2"),
),
},
{
Expand All @@ -32,7 +33,7 @@ func TestAccDataSourceAwsCognitoUserPools_basic(t *testing.T) {
func testAccDataSourceAwsCognitoUserPoolsConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "main" {
count = 3
count = 2
name = "%s"
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/cognito_user_pools.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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}"]
provider_arns = ["${data.aws_cognito_user_pools.selected.arns}"]
}
```

Expand Down

0 comments on commit b4bb3f8

Please sign in to comment.