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

Cognito: Added Identity Pool Roles Attachment #863

Merged
merged 2 commits into from
Oct 23, 2017

Conversation

Ninir
Copy link
Contributor

@Ninir Ninir commented Jun 14, 2017

Description

This adds the AWS Cognito Identity Roles association resource, based on the following API: http://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_SetIdentityPoolRoles.html

(Migrated from hashicorp/terraform#12846)

Related issues

Tests

$ make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSCognitoIdentityPoolRolesAttachment_'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/04/21 20:27:48 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSCognitoIdentityPoolRolesAttachment_ -timeout 120m
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_basic
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_basic (59.64s)
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings (103.70s)
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithError
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithError (17.80s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    181.171s

TODOs

  • Add the Cognito Identity Roles Resource
  • Add Cognito Identity Roles Resource validation
  • Add the Cognito Identity Roles Resource documentation
  • Add Cognito Identity Roles Resource test cases
  • Compile & play with it, in order to ensure every cases are handled & guards are present

@Ninir Ninir changed the title Cognito: Added Identity Pool Roles Attachment [WIP] Cognito: Added Identity Pool Roles Attachment Jun 14, 2017
"ambiguous_role_resolution": {
Type: schema.TypeString,
ValidateFunc: validateCognitoRoleMappingsAmbiguousRoleResolution,
Optional: true, // Required if Type equals Token or Rules.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, as token is required and has only 2 possibles values (Token or Rules), this seems required too.
The API states that this is Optional though.

@radeksimko radeksimko added the new-resource Introduces a new resource. label Jun 15, 2017
@Ninir Ninir requested a review from radeksimko August 30, 2017 12:07
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Ninir
Sorry it took me so long to get to this!

I looked at the API structs and the schema to understand the reasoning behind such deeply nested schema, which I think is/was the primary blocker for this PR.

How would you feel about flattening the schema like this?

map[string]*schema.Schema{
	"identity_pool_id": {
		Type:     schema.TypeString,
		Required: true,
		ForceNew: true,
	},

	"role_mapping": {
		Type:     schema.TypeSet,
		Optional: true,
		MaxItems: 1,
		Set:      nil,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"identity_provider": {
					Type:     schema.TypeString,
					Required: true,
				},
				"ambiguous_role_resolution": {
					Type:         schema.TypeString,
					ValidateFunc: validateCognitoRoleMappingsAmbiguousRoleResolution,
					Optional:     true, // Required if Type equals Token or Rules.
				},
				"type": {
					Type:         schema.TypeString,
					Required:     true,
					ValidateFunc: validateCognitoRoleMappingsType,
				},
				"mapping_rule": {
					Type:     schema.TypeList,
					Required: true,
					MaxItems: 25,
					Elem: &schema.Resource{
						Schema: map[string]*schema.Schema{
							"claim": {
								Type:         schema.TypeString,
								Required:     true,
								ValidateFunc: validateCognitoRoleMappingsRulesClaim,
							},
							"match_type": {
								Type:         schema.TypeString,
								Required:     true,
								ValidateFunc: validateCognitoRoleMappingsRulesMatchType,
							},
							"role_arn": {
								Type:         schema.TypeString,
								Required:     true,
								ValidateFunc: validateArn,
							},
							"value": {
								Type:         schema.TypeString,
								Required:     true,
								ValidateFunc: validateCognitoRoleMappingsRulesValue,
							},
						},
					},
				},
			},
		},
	},

	"roles": {
		Type:     schema.TypeMap,
		Required: true,
		ForceNew: true,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"authenticated": {
					Type:         schema.TypeString,
					ValidateFunc: validateArn,
					Optional:     true, // Required if unauthenticated isn't defined.
				},
				"unauthenticated": {
					Type:         schema.TypeString,
					ValidateFunc: validateArn,
					Optional:     true, // Required if authenticated isn't defined.
				},
			},
		},
	},
}

then the HCL config could look something like this

resource "aws_cognito_identity_pool_roles_attachment" "main" {
  identity_pool_id = "${aws_cognito_identity_pool.main.id}"

  role_mapping {
    identity_provider         = "graph.facebook.com"
    ambiguous_role_resolution = "AuthenticatedRole"
    type                      = "Rules"

    mapping_rule {
      claim      = "isAdmin"
      match_type = "Equals"
      role_arn   = "${aws_iam_role.authenticated.arn}"
      value      = "paid"
    }
  }

  roles {
    "authenticated" = "${aws_iam_role.authenticated.arn}"
  }
}

eventually I'm thinking identity_provider could be shortened to idp, but that's really a nitpick in this context - the main goal is to reduce the nesting here.

Let me know what you think - then we can proceed with the rest of the PR as the schema change is the probably the most significant thing here.

@radeksimko radeksimko added the waiting-response Maintainers are waiting on response from community or contributor. label Oct 16, 2017
@Ninir Ninir force-pushed the f-cognito-ip-roles-attachment branch 2 times, most recently from 83891a3 to c595651 Compare October 20, 2017 21:49
@Ninir
Copy link
Contributor Author

Ninir commented Oct 20, 2017

@radeksimko This is a much better approach, thank you for the suggestion :)

While tests are passing, I would like to check another time that everything is ok on my side before merging the work:

make testacc TEST=./aws TESTARGS='-run=TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings -timeout 120m
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappings (47.47s)
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithAmbiguousRoleResolutionError
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithAmbiguousRoleResolutionError (11.74s)
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithRulesTypeError
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithRulesTypeError (11.12s)
=== RUN   TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithTokenTypeError
--- PASS: TestAccAWSCognitoIdentityPoolRolesAttachment_roleMappingsWithTokenTypeError (11.02s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	81.410s

@Ninir Ninir removed the waiting-response Maintainers are waiting on response from community or contributor. label Oct 20, 2017
@Ninir Ninir force-pushed the f-cognito-ip-roles-attachment branch from c595651 to 4716be9 Compare October 20, 2017 21:54
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left you one comment, but this LGTM once you fix the Printf vet errors as reported in Travis.

Thanks for bringing it to the finish line! 👍

return nil
}

const baseAWSCognitoIdentityPoolRolesAttachmentConfig = `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but how do you feel about exposing this as a wrapper around fmt.Sprinf so that we get the validation-at-compile-time benefit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link isn't valid anymore, but yeah - I thought you could do something like this:

func testAccAWSCognitoIdentityPoolRolesAttachmentConfig_basic(name string) string {
    return fmt.Sprintf(baseAWSCognitoIdentityPoolRolesAttachmentConfig(name)+`
resource "aws_cognito_identity_pool_roles_attachment" "main" {

the benefit you get there is you may change baseAWSCognitoIdentityPoolRolesAttachmentConfig template and add/remove modifiers as necessary without having to refactor all references.

It's not a big deal though - so feel free to ignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :) thanks for the advice!

@Ninir Ninir force-pushed the f-cognito-ip-roles-attachment branch from 4716be9 to e7ed12f Compare October 23, 2017 09:09
@Ninir Ninir changed the title [WIP] Cognito: Added Identity Pool Roles Attachment Cognito: Added Identity Pool Roles Attachment Oct 23, 2017
@Ninir
Copy link
Contributor Author

Ninir commented Oct 23, 2017

@radeksimko Ready to go on my side :)

@Ninir Ninir merged commit 88876bd into hashicorp:master Oct 23, 2017
@Ninir Ninir deleted the f-cognito-ip-roles-attachment branch October 23, 2017 19:07
@ghost
Copy link

ghost commented Apr 10, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-resource Introduces a new resource.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants