From 889b7b4079888ee759d066868da2a29dd17ca4fd Mon Sep 17 00:00:00 2001 From: ms-henglu Date: Tue, 6 Jul 2021 16:24:42 +0800 Subject: [PATCH] support --- .../cognitive/cognitive_account_resource.go | 104 +++++++++++++++--- .../cognitive_account_resource_test.go | 39 +++++++ .../docs/r/cognitive_account.html.markdown | 10 +- 3 files changed, 134 insertions(+), 19 deletions(-) diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource.go b/azurerm/internal/services/cognitive/cognitive_account_resource.go index 954f52ab11cd..2cdf4530a3a4 100644 --- a/azurerm/internal/services/cognitive/cognitive_account_resource.go +++ b/azurerm/internal/services/cognitive/cognitive_account_resource.go @@ -86,6 +86,7 @@ func resourceCognitiveAccount() *pluginsdk.Resource { "ImmersiveReader", "LUIS", "LUIS.Authoring", + "MetricsAdvisor", "Personalizer", "QnAMaker", "Recommendations", @@ -107,6 +108,20 @@ func resourceCognitiveAccount() *pluginsdk.Resource { }, false), }, + "aad_client_id": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "aad_tenant_id": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "custom_subdomain_name": { Type: pluginsdk.TypeString, Optional: true, @@ -244,6 +259,20 @@ func resourceCognitiveAccount() *pluginsdk.Resource { }, }, + "super_user": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "website_name": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "tags": tags.Schema(), "endpoint": { @@ -314,12 +343,17 @@ func resourceCognitiveAccountCreate(d *pluginsdk.ResourceData, meta interface{}) publicNetworkAccess = cognitiveservices.PublicNetworkAccessDisabled } + apiProps, err := expandCognitiveAccountAPIProperties(d) + if err != nil { + return err + } + props := cognitiveservices.Account{ Kind: utils.String(kind), Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))), Sku: sku, Properties: &cognitiveservices.AccountProperties{ - APIProperties: &cognitiveservices.APIProperties{}, + APIProperties: apiProps, NetworkAcls: networkAcls, CustomSubDomainName: utils.String(d.Get("custom_subdomain_name").(string)), AllowedFqdnList: utils.ExpandStringSlice(d.Get("fqdns").([]interface{})), @@ -338,14 +372,6 @@ func resourceCognitiveAccountCreate(d *pluginsdk.ResourceData, meta interface{}) } props.Identity = identity - 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, id.ResourceGroup, id.Name, props); err != nil { return fmt.Errorf("creating %s: %+v", id, err) } @@ -403,10 +429,15 @@ func resourceCognitiveAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) publicNetworkAccess = cognitiveservices.PublicNetworkAccessDisabled } + apiProps, err := expandCognitiveAccountAPIProperties(d) + if err != nil { + return err + } + props := cognitiveservices.Account{ Sku: sku, Properties: &cognitiveservices.AccountProperties{ - APIProperties: &cognitiveservices.APIProperties{}, + APIProperties: apiProps, NetworkAcls: networkAcls, CustomSubDomainName: utils.String(d.Get("custom_subdomain_name").(string)), AllowedFqdnList: utils.ExpandStringSlice(d.Get("fqdns").([]interface{})), @@ -424,14 +455,6 @@ func resourceCognitiveAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) } props.Identity = identity - 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 { return fmt.Errorf("updating %s: %+v", *id, err) } @@ -495,6 +518,10 @@ func resourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{}) e if props := resp.Properties; props != nil { if apiProps := props.APIProperties; apiProps != nil { d.Set("qna_runtime_endpoint", apiProps.QnaRuntimeEndpoint) + d.Set("aad_client_id", apiProps.AadClientID) + d.Set("aad_tenant_id", apiProps.AadTenantID) + d.Set("super_user", apiProps.SuperUser) + d.Set("website_name", apiProps.WebsiteName) } d.Set("endpoint", props.Endpoint) d.Set("custom_subdomain_name", props.CustomSubDomainName) @@ -684,6 +711,47 @@ func expandCognitiveAccountIdentity(vs []interface{}) (*cognitiveservices.Identi return &managedServiceIdentity, nil } +func expandCognitiveAccountAPIProperties(d *pluginsdk.ResourceData) (*cognitiveservices.APIProperties, error) { + props := cognitiveservices.APIProperties{} + kind := d.Get("kind") + if kind == "QnAMaker" { + if v, ok := d.GetOk("qna_runtime_endpoint"); ok && v != "" { + props.QnaRuntimeEndpoint = utils.String(v.(string)) + } else { + return nil, fmt.Errorf("the QnAMaker runtime endpoint `qna_runtime_endpoint` is required when kind is set to `QnAMaker`") + } + } + if v, ok := d.GetOk("aad_client_id"); ok { + if kind == "MetricsAdvisor" { + props.AadClientID = utils.String(v.(string)) + } else { + return nil, fmt.Errorf("aad_client_id can only used set when kind is set to `MetricsAdvisor`") + } + } + if v, ok := d.GetOk("aad_tenant_id"); ok { + if kind == "MetricsAdvisor" { + props.AadTenantID = utils.String(v.(string)) + } else { + return nil, fmt.Errorf("aad_tenant_id can only used set when kind is set to `MetricsAdvisor`") + } + } + if v, ok := d.GetOk("super_user"); ok { + if kind == "MetricsAdvisor" { + props.SuperUser = utils.String(v.(string)) + } else { + return nil, fmt.Errorf("super_user can only used set when kind is set to `MetricsAdvisor`") + } + } + if v, ok := d.GetOk("website_name"); ok { + if kind == "MetricsAdvisor" { + props.WebsiteName = utils.String(v.(string)) + } else { + return nil, fmt.Errorf("website_name can only used set when kind is set to `MetricsAdvisor`") + } + } + return &props, nil +} + func flattenCognitiveAccountNetworkAcls(input *cognitiveservices.NetworkRuleSet) []interface{} { if input == nil { return []interface{}{} diff --git a/azurerm/internal/services/cognitive/cognitive_account_resource_test.go b/azurerm/internal/services/cognitive/cognitive_account_resource_test.go index c89e55a350d4..05b5c309f1a3 100644 --- a/azurerm/internal/services/cognitive/cognitive_account_resource_test.go +++ b/azurerm/internal/services/cognitive/cognitive_account_resource_test.go @@ -276,6 +276,21 @@ func TestAccCognitiveAccount_identity(t *testing.T) { }) } +func TestAccCognitiveAccount_metricsAdvisor(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test") + r := CognitiveAccountResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.metricsAdvisor(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (t CognitiveAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := parse.AccountID(state.ID) if err != nil { @@ -576,6 +591,30 @@ resource "azurerm_cognitive_account" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger) } +func (CognitiveAccountResource) metricsAdvisor(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} +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 = "MetricsAdvisor" + sku_name = "S0" + custom_subdomain_name = "acctestcogacc-%d" + aad_client_id = "310d7b2e-d1d1-4b87-9807-5b885b290c00" + aad_tenant_id = "72f988bf-86f1-41af-91ab-2d7cd011db47" + super_user = "mock_user1" + website_name = "mock_name2" +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + func (CognitiveAccountResource) withMultipleCognitiveAccounts(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { diff --git a/website/docs/r/cognitive_account.html.markdown b/website/docs/r/cognitive_account.html.markdown index 1704f2e55e17..fcfbf2eae4e5 100644 --- a/website/docs/r/cognitive_account.html.markdown +++ b/website/docs/r/cognitive_account.html.markdown @@ -44,10 +44,14 @@ 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`, `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`, `Personalizer`, `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`, `MetricsAdvisor`, `Personalizer`, `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`, `S`, `S0`, `S1`, `S2`, `S3`, `S4`, `S5`, `S6`, `P0`, `P1`, and `P2`. +* `aad_client_id` - (Optional) The Azure AD Client ID (Application ID). This attribute is only set when kind is `MetricsAdvisor`. Changing this forces a new resource to be created. + +* `aad_tenant_id` - (Optional) The Azure AD Tenant ID. This attribute is only set when kind is `MetricsAdvisor`. Changing this forces a new resource to be created. + * `qna_runtime_endpoint` - (Optional) A URL to link a QnAMaker cognitive account to a QnA runtime. -> **NOTE:** This URL is mandatory if the `kind` is set to `QnAMaker`. @@ -68,6 +72,10 @@ The following arguments are supported: * `storage` - (Optional) An `identity` block is documented below. +* `super_user` - (Optional) The super user of Metrics Advisor. This attribute is only set when kind is `MetricsAdvisor`. Changing this forces a new resource to be created. + +* `website_name` - (Optional) The website name of Metrics Advisor. This attribute is only set when kind is `MetricsAdvisor`. Changing this forces a new resource to be created. + * `tags` - (Optional) A mapping of tags to assign to the resource. ---