Skip to content

Commit

Permalink
r/cognitive_account: fixing a crash when provisioning a QnAMaker app
Browse files Browse the repository at this point in the history
Fixes #8355
  • Loading branch information
tombuildsstuff committed Sep 4, 2020
1 parent 9fb571e commit 816cd64
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
20 changes: 10 additions & 10 deletions azurerm/internal/services/cognitive/cognitive_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func resourceArmCognitiveAccountCreate(d *schema.ResourceData, meta interface{})
existing, err := client.GetProperties(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Cognitive Account %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing Cognitive Account %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

Expand All @@ -149,7 +149,7 @@ func resourceArmCognitiveAccountCreate(d *schema.ResourceData, meta interface{})

sku, err := expandAccountSkuName(d.Get("sku_name").(string))
if err != nil {
return fmt.Errorf("error expanding sku_name for Cognitive Account %s (Resource Group %q): %v", name, resourceGroup, err)
return fmt.Errorf("expanding sku_name for Cognitive Account %s (Resource Group %q): %v", name, resourceGroup, err)
}

props := cognitiveservices.Account{
Expand All @@ -162,21 +162,21 @@ func resourceArmCognitiveAccountCreate(d *schema.ResourceData, meta interface{})
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if kind == "QnAMaker" && *props.Properties.APIProperties.QnaRuntimeEndpoint == "" {
return fmt.Errorf("the QnAMaker runtime endpoint `qna_runtime_endpoint` is required when kind is set to `QnAMaker`")
}

if v, ok := d.GetOk("qna_runtime_endpoint"); ok {
props.Properties.APIProperties.QnaRuntimeEndpoint = utils.String(v.(string))
if kind == "QnAMaker" {
if v, ok := d.GetOk("qna_runtime_endpoint"); ok && v != "" {
props.Properties.APIProperties.QnaRuntimeEndpoint = utils.String(v.(string))
} else {
return fmt.Errorf("the QnAMaker runtime endpoint `qna_runtime_endpoint` is required when kind is set to `QnAMaker`")
}
}

if _, err := client.Create(ctx, resourceGroup, name, props); err != nil {
return fmt.Errorf("Error creating Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.GetProperties(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("retrieving Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*read.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"net/http"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -174,6 +175,22 @@ func TestAccAzureRMCognitiveAccount_qnaRuntimeEndpoint(t *testing.T) {
})
}

func TestAccAzureRMCognitiveAccount_qnaRuntimeEndpointUnspecified(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppCognitiveAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCognitiveAccount_qnaRuntimeEndpointUnspecified(data),
ExpectError: regexp.MustCompile("the QnAMaker runtime endpoint `qna_runtime_endpoint` is required when kind is set to `QnAMaker`"),
},
},
})
}

func TestAccAzureRMCognitiveAccount_cognitiveServices(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test")

Expand Down Expand Up @@ -265,8 +282,7 @@ resource "azurerm_cognitive_account" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "Face"
sku_name = "S0"
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Expand All @@ -287,8 +303,7 @@ resource "azurerm_cognitive_account" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "SpeechServices"
sku_name = "S0"
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Expand All @@ -303,8 +318,7 @@ resource "azurerm_cognitive_account" "import" {
location = azurerm_cognitive_account.test.location
resource_group_name = azurerm_cognitive_account.test.resource_group_name
kind = azurerm_cognitive_account.test.kind
sku_name = "S0"
sku_name = "S0"
}
`, template)
}
Expand All @@ -325,8 +339,7 @@ resource "azurerm_cognitive_account" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "Face"
sku_name = "S0"
sku_name = "S0"
tags = {
Acceptance = "Test"
Expand All @@ -347,16 +360,35 @@ resource "azurerm_resource_group" "test" {
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
name = "acctestcogacc-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "QnAMaker"
qna_runtime_endpoint = "%s"
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, url)
}

sku_name = "S0"
func testAccAzureRMCognitiveAccount_qnaRuntimeEndpointUnspecified(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
`, data.RandomInteger, data.Locations.Ternary, data.RandomInteger, url)
resource "azurerm_resource_group" "test" {
name = "acctestRG-cognitive-%d"
location = "%s"
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "QnAMaker"
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMCognitiveAccount_cognitiveServices(data acceptance.TestData) string {
Expand Down

0 comments on commit 816cd64

Please sign in to comment.