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

Feature/aws cognito resource server #4530

Merged
merged 3 commits into from
May 31, 2018
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ func Provider() terraform.ResourceProvider {
"aws_cognito_user_pool": resourceAwsCognitoUserPool(),
"aws_cognito_user_pool_client": resourceAwsCognitoUserPoolClient(),
"aws_cognito_user_pool_domain": resourceAwsCognitoUserPoolDomain(),
"aws_cognito_resource_server": resourceAwsCognitoResourceServer(),
"aws_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(),
"aws_cloudwatch_dashboard": resourceAwsCloudWatchDashboard(),
"aws_codedeploy_app": resourceAwsCodeDeployApp(),
Expand Down
178 changes: 178 additions & 0 deletions aws/resource_aws_cognito_resource_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsCognitoResourceServer() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCognitoResourceServerCreate,
Read: resourceAwsCognitoResourceServerRead,
Update: resourceAwsCognitoResourceServerUpdate,
Delete: resourceAwsCognitoResourceServerDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Copy link
Contributor

Choose a reason for hiding this comment

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

The current resource ID (only Identifier) does not have enough information to work with the passthrough importer. We also need the user pool ID so the read function can work directly from the ID.

In this case, we should probably prepend the user pool ID to the resource ID, e.g.

# Choosing pipe delimiter here as the identifier might be a URL
d.SetId(fmt.Sprintf("%s|%s", userPoolID, identifier))

},

// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateResourceServer.html
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

It appears that name should allow updates. I'll confirm with acceptance testing and remove ForceNew: true if that is the case.

},
"scope": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 25,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scope_description": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateCognitoResourceServerScopeDescription,
},
"scope_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateCognitoResourceServerScopeName,
},
"scope_identifier": {
Copy link
Contributor

Choose a reason for hiding this comment

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

This attribute is a little awkward as its not defined by the API and is seemingly only used to build the computed scope_identifiers attribute below. Instead of creating this intermediate attribute, let's just directly create scope_identifiers from the scopes returned by the API to remove any confusion. 👍

Type: schema.TypeString,
Computed: true,
},
},
},
},
"user_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"scope_identifiers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceAwsCognitoResourceServerCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn

params := &cognitoidentityprovider.CreateResourceServerInput{
Identifier: aws.String(d.Get("identifier").(string)),
Name: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}

if v, ok := d.GetOk("scope"); ok {
configs := v.(*schema.Set).List()
params.Scopes = expandCognitoResourceServerScope(configs)
}

log.Printf("[DEBUG] Creating Cognito Resource Server: %s", params)

resp, err := conn.CreateResourceServer(params)

if err != nil {
return errwrap.Wrapf("Error creating Cognito Resource Server: {{err}}", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick: We do not need to use errwrap.Wrapf() when simply returning a single error back to the operator -- fmt.Errorf() is preferred

}

d.SetId(*resp.ResourceServer.Identifier)

return resourceAwsCognitoResourceServerRead(d, meta)
}

func resourceAwsCognitoResourceServerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn

params := &cognitoidentityprovider.DescribeResourceServerInput{
Identifier: aws.String(d.Id()),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}

log.Printf("[DEBUG] Reading Cognito Resource Server: %s", params)

resp, err := conn.DescribeResourceServer(params)

if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
log.Printf("[WARN] Cognito Resource Server %s is already gone", d.Id())
d.SetId("")
return nil
}
return err
}

d.SetId(*resp.ResourceServer.Identifier)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not call d.SetId() in read functions 👍

d.Set("name", *resp.ResourceServer.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

To prevent panics, we should ensure that resp and resp.ResourceServer are not nil before trying to dereference them.

Also d.Set() is able to handle nil values directly, so we should prefer to not dereference values to prevent potential panics.

d.Set("user_pool_id", *resp.ResourceServer.UserPoolId)

scopes := flattenCognitoResourceServerScope(*resp.ResourceServer.Identifier, resp.ResourceServer.Scopes)
if err := d.Set("scope", scopes); err != nil {
return fmt.Errorf("Failed setting schema: %s", err)
}

var scopeIdentifiers []string
for _, elem := range scopes {

scopeIdentifier := elem["scope_identifier"].(string)
scopeIdentifiers = append(scopeIdentifiers, scopeIdentifier)
}
d.Set("scope_identifiers", scopeIdentifiers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this attribute is a non-scalar type, we should perform d.Set() error checking similar to scopes above.

return nil
}

func resourceAwsCognitoResourceServerUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn

params := &cognitoidentityprovider.UpdateResourceServerInput{
Identifier: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}

log.Printf("[DEBUG] Updating Cognito Resource Server: %s", params)

_, err := conn.UpdateResourceServer(params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Scopes can be updated but its not being added to the update parameters, so currently they would generate a perpetual difference.

if err != nil {
return errwrap.Wrapf("Error updating Cognito Resource Server: {{err}}", err)
}

return resourceAwsCognitoResourceServerRead(d, meta)
}

func resourceAwsCognitoResourceServerDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn

params := &cognitoidentityprovider.DeleteResourceServerInput{
Identifier: aws.String(d.Id()),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}

log.Printf("[DEBUG] Deleting Resource Server: %s", params)

_, err := conn.DeleteResourceServer(params)

if err != nil {
return errwrap.Wrapf("Error deleting Resource Server: {{err}}", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should check for isAWSErr(err, cognitoidentityprovider.ErrCodeResourceNotFoundError, "") here and return nil so we can skip errors if the resource is deleted outside of Terraform.

}

return nil
}
149 changes: 149 additions & 0 deletions aws/resource_aws_cognito_resource_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package aws

import (
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSCognitoResourceServer_basic(t *testing.T) {
identifier := fmt.Sprintf("tf-acc-test-resource-server-id-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
name := fmt.Sprintf("tf-acc-test-resource-server-name-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
poolName := fmt.Sprintf("tf-acc-test-pool-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoResourceServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoResourceServerConfig_basic(identifier, name, poolName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoResourceServerExists("aws_cognito_resource_server.main"),
resource.TestCheckResourceAttr("aws_cognito_resource_server.main", "identifier", identifier),
resource.TestCheckResourceAttr("aws_cognito_resource_server.main", "name", name),
resource.TestCheckResourceAttr("aws_cognito_user_pool.main", "name", poolName),
),
},
},
})
}

func TestAccAWSCognitoResourceServer_full(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should descriptively name this _scopes to signify what its actually testing

identifier := fmt.Sprintf("tf-acc-test-resource-server-id-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
name := fmt.Sprintf("tf-acc-test-resource-server-name-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
poolName := fmt.Sprintf("tf-acc-test-pool-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoResourceServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoResourceServerConfig_full(identifier, name, poolName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoResourceServerExists("aws_cognito_resource_server.main"),
resource.TestCheckResourceAttr("aws_cognito_resource_server.main", "identifier", identifier),
resource.TestCheckResourceAttr("aws_cognito_resource_server.main", "name", name),
resource.TestCheckResourceAttrSet("aws_cognito_resource_server.main", "scope_identifiers"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is currently failing:

=== RUN   TestAccAWSCognitoResourceServer_full
--- FAIL: TestAccAWSCognitoResourceServer_full (5.86s)
    testing.go:518: Step 0 error: Check failed: 1 error(s) occurred:
        
        * Check 4/5 error: aws_cognito_resource_server.main: Attribute 'scope_identifiers' expected to be set

Did you mean to do this?

resource.TestCheckResourceAttr("aws_cognito_resource_server.main", "scope.#", "2"),

resource.TestCheckResourceAttr("aws_cognito_user_pool.main", "name", poolName),
),
},
},
})
}

func testAccCheckAWSCognitoResourceServerExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return errors.New("No Cognito Resource Server ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).cognitoidpconn

_, err := conn.DescribeResourceServer(&cognitoidentityprovider.DescribeResourceServerInput{
Identifier: aws.String(rs.Primary.ID),
UserPoolId: aws.String(rs.Primary.Attributes["user_pool_id"]),
})

if err != nil {
return err
}

return nil
}
}

func testAccCheckAWSCognitoResourceServerDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cognitoidpconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_cognito_resource_server" {
continue
}

_, err := conn.DescribeResourceServer(&cognitoidentityprovider.DescribeResourceServerInput{
Identifier: aws.String(rs.Primary.ID),
UserPoolId: aws.String(rs.Primary.Attributes["user_pool_id"]),
})

if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
return nil
}
return err
}
}

return nil
}

func testAccAWSCognitoResourceServerConfig_basic(identifier string, name string, poolName string) string {
return fmt.Sprintf(`
resource "aws_cognito_resource_server" "main" {
identifier = "%s"
name = "%s"
user_pool_id = "${aws_cognito_user_pool.main.id}"
}

resource "aws_cognito_user_pool" "main" {
name = "%s"
}
`, identifier, name, poolName)
}

func testAccAWSCognitoResourceServerConfig_full(identifier string, name string, poolName string) string {
return fmt.Sprintf(`
resource "aws_cognito_resource_server" "main" {
identifier = "%s"
name = "%s"

scope = {
scope_name = "scope_1_name"
scope_description = "scope_1_description"
}

scope = {
scope_name = "scope_2_name"
scope_description = "scope_2_description"
}

user_pool_id = "${aws_cognito_user_pool.main.id}"
}

resource "aws_cognito_user_pool" "main" {
name = "%s"
}
`, identifier, name, poolName)
}
37 changes: 37 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,43 @@ func flattenCognitoUserPoolPasswordPolicy(s *cognitoidentityprovider.PasswordPol
return []map[string]interface{}{}
}

func expandCognitoResourceServerScope(inputs []interface{}) []*cognitoidentityprovider.ResourceServerScopeType {
configs := make([]*cognitoidentityprovider.ResourceServerScopeType, len(inputs), len(inputs))
for i, input := range inputs {
param := input.(map[string]interface{})
config := &cognitoidentityprovider.ResourceServerScopeType{}

if v, ok := param["scope_description"]; ok {
config.ScopeDescription = aws.String(v.(string))
}

if v, ok := param["scope_name"]; ok {
config.ScopeName = aws.String(v.(string))
}

configs[i] = config
}

return configs
}

func flattenCognitoResourceServerScope(identifier string, inputs []*cognitoidentityprovider.ResourceServerScopeType) []map[string]interface{} {
values := make([]map[string]interface{}, 0)

for _, input := range inputs {
if input == nil {
continue
}
var value = map[string]interface{}{
"scope_name": aws.StringValue(input.ScopeName),
"scope_description": aws.StringValue(input.ScopeDescription),
"scope_identifier": strings.Join([]string{identifier, "/", aws.StringValue(input.ScopeName)}, ""),
}
values = append(values, value)
}
return values
}

func expandCognitoUserPoolSchema(inputs []interface{}) []*cognitoidentityprovider.SchemaAttributeType {
configs := make([]*cognitoidentityprovider.SchemaAttributeType, len(inputs), len(inputs))

Expand Down
Loading