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

New Resource: aws_lex_resources #2616

Closed
wants to merge 11 commits into from
77 changes: 77 additions & 0 deletions aws/data_source_aws_lex_bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package aws

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsLexBot() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexBotRead,

Schema: map[string]*schema.Schema{
"checksum": {
Type: schema.TypeString,
Computed: true,
},
"child_directed": {
Type: schema.TypeBool,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"failure_reason": {
Type: schema.TypeString,
Computed: true,
},
"idle_session_ttl_in_seconds": {
Type: schema.TypeInt,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
"locale": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateStringMinMaxRegex(lexNameMinLength, lexNameMaxLength, lexNameRegex),
},
"process_behavior": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateStringMinMaxRegex(lexVersionMinLength, lexVersionMaxLength, lexVersionRegex),
},
"voice_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsLexBotRead(d *schema.ResourceData, meta interface{}) error {
// The data source and resource read functions are the same except the resource read expects to have the id set.
d.SetId(d.Get("name").(string))

return resourceAwsLexBotRead(d, meta)
}
51 changes: 51 additions & 0 deletions aws/data_source_aws_lex_bot_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package aws

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsLexBotAlias() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexBotAliasRead,

Schema: map[string]*schema.Schema{
"bot_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateStringMinMaxRegex(lexBotNameMinLength, lexBotNameMaxLength, lexNameRegex),
},
"bot_version": {
Type: schema.TypeString,
Computed: true,
},
"checksum": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateStringMinMaxRegex(lexNameMinLength, lexNameMaxLength, lexNameRegex),
},
},
}
}

func dataSourceAwsLexBotAliasRead(d *schema.ResourceData, meta interface{}) error {
// The data source and resource read functions are the same except the resource read expects to have the id set.
d.SetId(d.Get("name").(string))

return resourceAwsLexBotAliasRead(d, meta)
}
75 changes: 75 additions & 0 deletions aws/data_source_aws_lex_bot_alias_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceLexBotAlias(t *testing.T) {
resourceName := "aws_lex_bot_alias.test"
testId := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testDataSourceLexBotAliasConfig, testId),
Check: resource.ComposeTestCheckFunc(
checkResourceStateComputedAttr(resourceName, dataSourceAwsLexBotAlias()),
),
},
},
})
}

const testDataSourceLexBotAliasConfig = `
resource "aws_lex_intent" "test" {
fulfillment_activity {
type = "ReturnIntent"
}

name = "test_intent_%[1]s"
}

resource "aws_lex_bot" "test" {
abort_statement {
message {
content = "Sorry, I am not able to assist at this time"
content_type = "PlainText"
}
}

child_directed = false

clarification_prompt {
max_attempts = 2

message {
content = "I didn't understand you, what would you like to do?"
content_type = "PlainText"
}
}

name = "test_bot_%[1]s"

intent {
intent_name = "${aws_lex_intent.test.name}"
intent_version = "${aws_lex_intent.test.version}"
}
}

resource "aws_lex_bot_alias" "test" {
bot_name = "${aws_lex_bot.test.name}"
bot_version = "${aws_lex_bot.test.version}"
name = "test_bot_alias_%[1]s"
}

data "aws_lex_bot_alias" "test" {
bot_name = "${aws_lex_bot.test.name}"
name = "${aws_lex_bot_alias.test.name}"
}
`
116 changes: 116 additions & 0 deletions aws/data_source_aws_lex_bot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package aws

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

func testCheckResourceAttrPrefixSet(resourceName, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rm := s.RootModule()
rs, ok := rm.Resources[resourceName]

if !ok {
return fmt.Errorf("resource does not exist in state, %s", resourceName)
}

for attr := range rs.Primary.Attributes {
if strings.HasPrefix(attr, prefix+".") {
return nil
}
}

return fmt.Errorf("resource attribute prefix does not exist in state, %s", prefix)
}
}

func checkResourceStateComputedAttr(resourceName string, expectedResource *schema.Resource) resource.TestCheckFunc {
return func(s *terraform.State) error {
actualResource, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", resourceName)
}

// Ensure the state is populated with all the computed attributes defined by the resource schema.
for k, v := range expectedResource.Schema {
if !v.Computed {
continue
}

if _, ok := actualResource.Primary.Attributes[k]; !ok {
return fmt.Errorf("state missing attribute %s", k)
}
}

return nil
}
}

func TestAccDataSourceLexBot(t *testing.T) {
resourceName := "aws_lex_bot.test"
testId := acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testDataSourceLexBotConfig, testId),
Check: resource.ComposeTestCheckFunc(
checkResourceStateComputedAttr(resourceName, dataSourceAwsLexBot()),
),
},
},
})
}

const testDataSourceLexBotConfig = `
resource "aws_lex_intent" "test" {
fulfillment_activity {
type = "ReturnIntent"
}

name = "test_intent_%[1]s"
}

resource "aws_lex_bot" "test" {
abort_statement {
message {
content = "Sorry, I'm not able to assist at this time"
content_type = "PlainText"
}
}

child_directed = false

clarification_prompt {
max_attempts = 2

message {
content = "I didn't understand you, what would you like to do?"
content_type = "PlainText"
}
}

description = "Bot to order flowers on the behalf of a user"

intent {
intent_name = "${aws_lex_intent.test.name}"
intent_version = "${aws_lex_intent.test.version}"
}

name = "test_bot_%[1]s"
voice_id = "Salli"
}

data "aws_lex_bot" "test" {
name = "${aws_lex_bot.test.name}"
version = "${aws_lex_bot.test.version}"
}
`
53 changes: 53 additions & 0 deletions aws/data_source_aws_lex_intent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package aws

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsLexIntent() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLexIntentRead,

Schema: map[string]*schema.Schema{
"checksum": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateStringMinMaxRegex(lexNameMinLength, lexNameMaxLength, lexNameRegex),
},
"parent_intent_signature": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Default: "$LATEST",
ValidateFunc: validateStringMinMaxRegex(lexVersionMinLength, lexVersionMaxLength, lexVersionRegex),
},
},
}
}

func dataSourceAwsLexIntentRead(d *schema.ResourceData, meta interface{}) error {
// The data source and resource read functions are the same except the resource read expects to have the id set.
d.SetId(d.Get("name").(string))

return resourceAwsLexIntentRead(d, meta)
}
Loading