From 816cd640571a699d3df3fbb7ab6c7e63bf803a57 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 4 Sep 2020 15:53:16 +0200 Subject: [PATCH 1/3] r/cognitive_account: fixing a crash when provisioning a QnAMaker app Fixes #8355 --- .../cognitive/cognitive_account_resource.go | 20 +++---- .../tests/cognitive_account_resource_test.go | 60 ++++++++++++++----- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource.go b/azurerm/internal/services/cognitive/cognitive_account_resource.go index e5c790bb35c2..65143febafb7 100644 --- a/azurerm/internal/services/cognitive/cognitive_account_resource.go +++ b/azurerm/internal/services/cognitive/cognitive_account_resource.go @@ -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) } } @@ -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{ @@ -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) diff --git a/azurerm/internal/services/cognitive/tests/cognitive_account_resource_test.go b/azurerm/internal/services/cognitive/tests/cognitive_account_resource_test.go index fb69760bd094..0c6829055222 100644 --- a/azurerm/internal/services/cognitive/tests/cognitive_account_resource_test.go +++ b/azurerm/internal/services/cognitive/tests/cognitive_account_resource_test.go @@ -3,6 +3,7 @@ package tests import ( "fmt" "net/http" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -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") @@ -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) } @@ -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) } @@ -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) } @@ -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" @@ -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 { From fb56fa47412c0b0e409b7d9a66289833ae6791e2 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 4 Sep 2020 15:58:45 +0200 Subject: [PATCH 2/3] r/cognitive_account: supporting a kind of `AnomalyDetector` Fixes #7662 --- .../internal/services/cognitive/cognitive_account_resource.go | 1 + website/docs/r/cognitive_account.html.markdown | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource.go b/azurerm/internal/services/cognitive/cognitive_account_resource.go index 65143febafb7..cff2dfe8198a 100644 --- a/azurerm/internal/services/cognitive/cognitive_account_resource.go +++ b/azurerm/internal/services/cognitive/cognitive_account_resource.go @@ -57,6 +57,7 @@ func resourceArmCognitiveAccount() *schema.Resource { ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ "Academic", + "AnomalyDetector", "Bing.Autosuggest", "Bing.Autosuggest.v7", "Bing.CustomSearch", diff --git a/website/docs/r/cognitive_account.html.markdown b/website/docs/r/cognitive_account.html.markdown index 05bc79ae49e9..b7567a8fbfdd 100644 --- a/website/docs/r/cognitive_account.html.markdown +++ b/website/docs/r/cognitive_account.html.markdown @@ -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`. From 12b845a4f5e146ed1b26c4ce6e235ae28c97fc2a Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 7 Sep 2020 12:10:51 +0100 Subject: [PATCH 3/3] r/cognitive_account: fix a crash when updating a QnAMaker app --- .../services/cognitive/cognitive_account_resource.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource.go b/azurerm/internal/services/cognitive/cognitive_account_resource.go index cff2dfe8198a..e859001a089c 100644 --- a/azurerm/internal/services/cognitive/cognitive_account_resource.go +++ b/azurerm/internal/services/cognitive/cognitive_account_resource.go @@ -208,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 {