Skip to content

Commit

Permalink
r\cognitive_account: Add support for qna_service_endpoint_id
Browse files Browse the repository at this point in the history
  • Loading branch information
myc2h6o committed Mar 11, 2022
1 parent 9fdb8c3 commit b4c059f
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/services/cognitive/cognitive_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cognitive/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network"
networkParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
searchValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/search/validate"
storageValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -285,6 +286,7 @@ func resourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{}) e
if props := model.Properties; props != nil {
if apiProps := props.ApiProperties; apiProps != nil {
d.Set("qna_runtime_endpoint", apiProps.QnaRuntimeEndpoint)
d.Set("qna_search_endpoint_id", apiProps.QnaAzureSearchEndpointId)
d.Set("metrics_advisor_aad_client_id", apiProps.AadClientId)
d.Set("metrics_advisor_aad_tenant_id", apiProps.AadTenantId)
d.Set("metrics_advisor_super_user_name", apiProps.SuperUser)
Expand Down Expand Up @@ -502,6 +504,13 @@ func expandCognitiveAccountAPIProperties(d *pluginsdk.ResourceData) (*cognitives
return nil, fmt.Errorf("the QnAMaker runtime endpoint `qna_runtime_endpoint` is required when kind is set to `QnAMaker`")
}
}
if v, ok := d.GetOk("qna_search_endpoint_id"); ok {
if kind == "TextAnalytics" {
props.QnaAzureSearchEndpointId = utils.String(v.(string))
} else {
return nil, fmt.Errorf("qna_search_endpoint_id can only used set when kind is set to `TextAnalytics`")
}
}
if v, ok := d.GetOk("metrics_advisor_aad_client_id"); ok {
if kind == "MetricsAdvisor" {
props.AadClientId = utils.String(v.(string))
Expand Down Expand Up @@ -807,6 +816,12 @@ func resourceCognitiveAccountSchema() map[string]*pluginsdk.Schema {
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},

"qna_search_endpoint_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: searchValidate.SearchServiceID,
},

"storage": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down
129 changes: 129 additions & 0 deletions internal/services/cognitive/cognitive_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ func TestAccCognitiveAccount_qnaRuntimeEndpointUnspecified(t *testing.T) {
})
}

func TestAccCognitiveAccount_qnaSearchEndpointId(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test")
r := CognitiveAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.qnaSearchEndpointId(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.qnaSearchEndpointIdUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.qnaSearchEndpointIdRemoved(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccCognitiveAccount_cognitiveServices(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test")
r := CognitiveAccountResource{}
Expand Down Expand Up @@ -596,6 +625,106 @@ resource "azurerm_cognitive_account" "test" {
`, data.RandomInteger, "West US", data.RandomInteger) // QnAMaker only available in West US
}

func (CognitiveAccountResource) qnaSearchEndpointId(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-cognitive-%[1]d"
location = "%[2]s"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchacc-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "TextAnalytics"
sku_name = "F0"
qna_search_endpoint_id = azurerm_search_service.test.id
}
`, data.RandomInteger, data.Locations.Primary)
}

func (CognitiveAccountResource) qnaSearchEndpointIdUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-cognitive-%[1]d"
location = "%[2]s"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchacc-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"
}
resource "azurerm_search_service" "test2" {
name = "acctestsearchacc2-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "TextAnalytics"
sku_name = "F0"
qna_search_endpoint_id = azurerm_search_service.test2.id
}
`, data.RandomInteger, data.Locations.Primary)
}

func (CognitiveAccountResource) qnaSearchEndpointIdRemoved(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-cognitive-%[1]d"
location = "%[2]s"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchacc-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"
}
resource "azurerm_search_service" "test2" {
name = "acctestsearchacc2-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "TextAnalytics"
sku_name = "F0"
}
`, data.RandomInteger, data.Locations.Primary)
}

func (CognitiveAccountResource) cognitiveServices(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/d/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ The following attributes are exported:
* `endpoint` - The endpoint of the Cognitive Services Account

* `qna_runtime_endpoint` - If `kind` is `QnAMaker` the link to the QNA runtime.

* `qna_search_endpoint_id` - If `kind` is `TextAnalytics` the ID to the Search service.

-> **NOTE:** `qna_search_endpoint_id` is used for [the new version of QnA Maker](https://docs.microsoft.com/azure/cognitive-services/qnamaker/custom-question-answering), while `qna_runtime_endpoint` is used for [the old version of QnA Maker](https://docs.microsoft.com/azure/cognitive-services/qnamaker/overview/overview)

* `primary_access_key` - The primary access key of the Cognitive Services Account

* `secondary_access_key` - The secondary access key of the Cognitive Services Account
Expand Down

0 comments on commit b4c059f

Please sign in to comment.