Skip to content

Commit

Permalink
r/cognitive_account: fixing a crash & supporting AnomalyDetector / Qn…
Browse files Browse the repository at this point in the history
…AMaker (#8357)

Co-authored-by: Tom Bamford <[email protected]>
  • Loading branch information
tombuildsstuff and manicminer authored Sep 10, 2020
1 parent ee37f9b commit 668643e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
33 changes: 17 additions & 16 deletions azurerm/internal/services/cognitive/cognitive_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func resourceArmCognitiveAccount() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Academic",
"AnomalyDetector",
"Bing.Autosuggest",
"Bing.Autosuggest.v7",
"Bing.CustomSearch",
Expand Down Expand Up @@ -138,7 +139,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 +150,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 +163,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 Expand Up @@ -207,12 +208,12 @@ func resourceArmCognitiveAccountUpdate(d *schema.ResourceData, meta interface{})
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if d.Get("kind").(string) == "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 := d.Get("kind"); 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.Update(ctx, id.ResourceGroup, id.Name, props); err != nil {
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
2 changes: 1 addition & 1 deletion website/docs/r/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following arguments are supported:

* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.

* `kind` - (Required) Specifies the type of Cognitive Service Account that should be created. Possible values are `Academic`, `Bing.Autosuggest`, `Bing.Autosuggest.v7`, `Bing.CustomSearch`, `Bing.Search`, `Bing.Search.v7`, `Bing.Speech`, `Bing.SpellCheck`, `Bing.SpellCheck.v7`, `CognitiveServices`, `ComputerVision`, `ContentModerator`, `CustomSpeech`, `CustomVision.Prediction`, `CustomVision.Training`, `Emotion`, `Face`,`FormRecognizer`, `ImmersiveReader`, `LUIS`, `LUIS.Authoring`, `QnAMaker`, `Recommendations`, `SpeakerRecognition`, `Speech`, `SpeechServices`, `SpeechTranslation`, `TextAnalytics`, `TextTranslation` and `WebLM`. Changing this forces a new resource to be created.
* `kind` - (Required) Specifies the type of Cognitive Service Account that should be created. Possible values are `Academic`, `AnomalyDetector`, `Bing.Autosuggest`, `Bing.Autosuggest.v7`, `Bing.CustomSearch`, `Bing.Search`, `Bing.Search.v7`, `Bing.Speech`, `Bing.SpellCheck`, `Bing.SpellCheck.v7`, `CognitiveServices`, `ComputerVision`, `ContentModerator`, `CustomSpeech`, `CustomVision.Prediction`, `CustomVision.Training`, `Emotion`, `Face`,`FormRecognizer`, `ImmersiveReader`, `LUIS`, `LUIS.Authoring`, `QnAMaker`, `Recommendations`, `SpeakerRecognition`, `Speech`, `SpeechServices`, `SpeechTranslation`, `TextAnalytics`, `TextTranslation` and `WebLM`. Changing this forces a new resource to be created.

* `sku_name` - (Required) Specifies the SKU Name for this Cognitive Service Account. Possible values are `F0`, `F1`, `S0`, `S1`, `S2`, `S3`, `S4`, `S5`, `S6`, `P0`, `P1`, and `P2`.

Expand Down

0 comments on commit 668643e

Please sign in to comment.