Skip to content

Commit

Permalink
add ACC tests. documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjanne committed May 14, 2018
1 parent ed5fe12 commit 92e4965
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 4 deletions.
8 changes: 4 additions & 4 deletions aws/resource_aws_cognito_resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func resourceAwsCognitoResourceServerRead(d *schema.ResourceData, meta interface
return fmt.Errorf("Failed setting schema: %s", err)
}

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

scope_identifier := elem["scope_identifier"].(string)
scope_identifiers = append(scope_identifiers, scope_identifier)
scopeIdentifier := elem["scope_identifier"].(string)
scopeIdentifiers = append(scopeIdentifiers, scopeIdentifier)
}
d.Set("scope_identifiers", scope_identifiers)
d.Set("scope_identifiers", scopeIdentifiers)
return nil
}

Expand Down
146 changes: 146 additions & 0 deletions aws/resource_aws_cognito_resource_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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) {
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))
scopeName := fmt.Sprintf("tf-acc-test-resource-server-scope-name-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
scopeDescription := fmt.Sprintf("tf-acc-test-resource-server-scope-description-%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, scopeName, scopeDescription, 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),
resource.TestCheckResourceAttrSet("aws_cognito_resource_server.main", "scope_identifiers"),
),
},
},
})
}

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, scopeName string, scopeDescription string, poolName string) string {
return fmt.Sprintf(`
resource "aws_cognito_resource_server" "main" {
identifier = "%s"
name = "%s"
scope = {
scope_name = "%s"
scope_description = "%s"
}
user_pool_id = "${aws_cognito_user_pool.main.id}"
}
resource "aws_cognito_user_pool" "main" {
name = "%s"
}
`, identifier, name, scopeName, scopeDescription, poolName)
}
67 changes: 67 additions & 0 deletions website/docs/r/cognito_resource_server.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
layout: "aws"
page_title: "AWS: aws_cognito_resource_server"
side_bar_current: "docs-aws-resource-cognito-resource-server"
description: |-
Provides a Cognito Resource Server.
---

# aws_cognito_resource_server

Provides a Cognito Resource Server.

## Example Usage

### Create a basic resource server

```hcl
resource "aws_cognito_user_pool" "pool" {
name = "pool"
}
resource "aws_cognito_resource_server" "resource" {
identifier = "res"
name = "resource"
user_pool_id = "${aws_cognito_user_pool.pool.id}"
}
```

### Create a resource server with sample-scope

```hcl
resource "aws_cognito_user_pool" "pool" {
name = "pool"
}
resource "aws_cognito_resource_server" "resource" {
identifier = "res"
name = "resource"
scope = {
scope_name = "sample-scope"
scope_description = "a Sample Scope Description"
}
user_pool_id = "${aws_cognito_user_pool.pool.id}"
}
```

## Argument Reference

The following arguments are supported:

* `identifier` - (Required) An identifier for the resource server.
* `name` - (Required) A name for the resource server.
* `scope` - (Optional) The configuration for an [Authorization Scope](#authorization_scope).

### Authorization Scope

* `scope_name` - (Required) The scope name.
* `scope_description` - (Required) The scope description.

## Attribute Reference

In addition to the arguments, which are exported, the following attributes are exported:

* `scope_identifiers` - A list of all scopes configured for this resource server in the format identifier/scope_name.

0 comments on commit 92e4965

Please sign in to comment.