Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/cognito_user_pool_client - add support for application_arn in the analytics_configuration block #16734

Merged
merged 6 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/16734.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cognito_user_pool_client: Add support for `application_arn` in the `analytics_configuration` block.
```
85 changes: 54 additions & 31 deletions aws/resource_aws_cognito_user_pool_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,8 @@ func resourceAwsCognitoUserPoolClient() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.ExplicitAuthFlowsTypeAdminNoSrpAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeCustomAuthFlowOnly,
cognitoidentityprovider.ExplicitAuthFlowsTypeUserPasswordAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeAllowAdminUserPasswordAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeAllowCustomAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeAllowUserPasswordAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeAllowUserSrpAuth,
cognitoidentityprovider.ExplicitAuthFlowsTypeAllowRefreshTokenAuth,
}, false),
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(cognitoidentityprovider.ExplicitAuthFlowsType_Values(), false),
},
},

Expand Down Expand Up @@ -93,12 +84,8 @@ func resourceAwsCognitoUserPoolClient() *schema.Resource {
Optional: true,
MaxItems: 3,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.OAuthFlowTypeCode,
cognitoidentityprovider.OAuthFlowTypeImplicit,
cognitoidentityprovider.OAuthFlowTypeClientCredentials,
}, false),
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(cognitoidentityprovider.OAuthFlowType_Values(), false),
},
},

Expand Down Expand Up @@ -166,17 +153,28 @@ func resourceAwsCognitoUserPoolClient() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"application_id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"analytics_configuration.0.application_id", "analytics_configuration.0.application_arn"},
},
"application_arn": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"analytics_configuration.0.application_id", "analytics_configuration.0.application_arn"},
ConflictsWith: []string{"analytics_configuration.0.external_id", "analytics_configuration.0.role_arn"},
ValidateFunc: validateArn,
},
"external_id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
ConflictsWith: []string{"analytics_configuration.0.application_arn"},
Optional: true,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"analytics_configuration.0.application_arn"},
ValidateFunc: validateArn,
},
"user_data_shared": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -424,10 +422,22 @@ func expandAwsCognitoUserPoolClientAnalyticsConfig(l []interface{}) *cognitoiden

m := l[0].(map[string]interface{})

analyticsConfig := &cognitoidentityprovider.AnalyticsConfigurationType{
ApplicationId: aws.String(m["application_id"].(string)),
ExternalId: aws.String(m["external_id"].(string)),
RoleArn: aws.String(m["role_arn"].(string)),
analyticsConfig := &cognitoidentityprovider.AnalyticsConfigurationType{}

if v, ok := m["role_arn"]; ok && v != "" {
analyticsConfig.RoleArn = aws.String(v.(string))
}

if v, ok := m["external_id"]; ok && v != "" {
analyticsConfig.ExternalId = aws.String(v.(string))
}

if v, ok := m["application_id"]; ok && v != "" {
analyticsConfig.ApplicationId = aws.String(v.(string))
}

if v, ok := m["application_arn"]; ok && v != "" {
analyticsConfig.ApplicationArn = aws.String(v.(string))
}

if v, ok := m["user_data_shared"]; ok {
Expand All @@ -443,11 +453,24 @@ func flattenAwsCognitoUserPoolClientAnalyticsConfig(analyticsConfig *cognitoiden
}

m := map[string]interface{}{
"application_id": aws.StringValue(analyticsConfig.ApplicationId),
"external_id": aws.StringValue(analyticsConfig.ExternalId),
"role_arn": aws.StringValue(analyticsConfig.RoleArn),
"user_data_shared": aws.BoolValue(analyticsConfig.UserDataShared),
}

if analyticsConfig.ExternalId != nil {
m["external_id"] = aws.StringValue(analyticsConfig.ExternalId)
}

if analyticsConfig.RoleArn != nil {
m["role_arn"] = aws.StringValue(analyticsConfig.RoleArn)
}

if analyticsConfig.ApplicationId != nil {
m["application_id"] = aws.StringValue(analyticsConfig.ApplicationId)
}

if analyticsConfig.ApplicationArn != nil {
m["application_arn"] = aws.StringValue(analyticsConfig.ApplicationArn)
}

return []interface{}{m}
}
48 changes: 48 additions & 0 deletions aws/resource_aws_cognito_user_pool_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ func TestAccAWSCognitoUserPoolClient_analyticsConfig(t *testing.T) {
})
}

func TestAccAWSCognitoUserPoolClient_analyticsConfigWithArn(t *testing.T) {
var client cognitoidentityprovider.UserPoolClientType
userPoolName := acctest.RandString(10)
clientName := acctest.RandString(10)
resourceName := "aws_cognito_user_pool_client.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSCognitoIdentityProvider(t)
testAccPreCheckAWSPinpointApp(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoUserPoolClientDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolClientConfigAnalyticsWithArnConfig(userPoolName, clientName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolClientExists(resourceName, &client),
resource.TestCheckResourceAttr(resourceName, "analytics_configuration.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "analytics_configuration.0.application_arn", "aws_pinpoint_app.test", "arn"),
testAccCheckResourceAttrGlobalARN(resourceName, "analytics_configuration.0.role_arn", "iam", "role/aws-service-role/cognito-idp.amazonaws.com/AWSServiceRoleForAmazonCognitoIdp"),
resource.TestCheckResourceAttr(resourceName, "analytics_configuration.0.user_data_shared", "false"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccAWSCognitoUserPoolClientImportStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCognitoUserPoolClient_disappears(t *testing.T) {
var client cognitoidentityprovider.UserPoolClientType
userPoolName := fmt.Sprintf("tf-acc-cognito-user-pool-%s", acctest.RandString(7))
Expand Down Expand Up @@ -557,3 +592,16 @@ resource "aws_cognito_user_pool_client" "test" {
}
`, clientName)
}

func testAccAWSCognitoUserPoolClientConfigAnalyticsWithArnConfig(userPoolName, clientName string) string {
return testAccAWSCognitoUserPoolClientConfigAnalyticsConfigBase(userPoolName, clientName) + fmt.Sprintf(`
resource "aws_cognito_user_pool_client" "test" {
name = "%[1]s"
user_pool_id = aws_cognito_user_pool.test.id

analytics_configuration {
application_arn = aws_pinpoint_app.test.arn
}
}
`, clientName)
}
9 changes: 6 additions & 3 deletions website/docs/r/cognito_user_pool_client.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ The following arguments are supported:

### Analytics Configuration

* `application_id` - (Required) The application ID for an Amazon Pinpoint application.
* `external_id` - (Required) An ID for the Analytics Configuration.
* `role_arn` - (Required) The ARN of an IAM role that authorizes Amazon Cognito to publish events to Amazon Pinpoint analytics.
Either `application_arn` or `application_id` is required.

* `application_arn` - (Optional) The application ARN for an Amazon Pinpoint application. Conflicts with `external_id` and `role_arn`.
* `application_id` - (Optional) The application ID for an Amazon Pinpoint application.
* `external_id` - (Optional) An ID for the Analytics Configuration. Conflicts with `application_arn`.
* `role_arn` - (Optional) The ARN of an IAM role that authorizes Amazon Cognito to publish events to Amazon Pinpoint analytics. Conflicts with `application_arn`.
* `user_data_shared` (Optional) If set to `true`, Amazon Cognito will include user data in the events it publishes to Amazon Pinpoint analytics.

## Attributes Reference
Expand Down