diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource.go b/azurerm/internal/services/cognitive/cognitive_account_resource.go index e5c790bb35c2..e859001a089c 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", @@ -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) } } @@ -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{ @@ -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) @@ -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 { 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 { 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`.