Skip to content

Commit

Permalink
support
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Jul 6, 2021
1 parent 5b86cc6 commit 889b7b4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 19 deletions.
104 changes: 86 additions & 18 deletions azurerm/internal/services/cognitive/cognitive_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func resourceCognitiveAccount() *pluginsdk.Resource {
"ImmersiveReader",
"LUIS",
"LUIS.Authoring",
"MetricsAdvisor",
"Personalizer",
"QnAMaker",
"Recommendations",
Expand All @@ -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,
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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{})),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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{})),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down
10 changes: 9 additions & 1 deletion website/docs/r/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.

---
Expand Down

0 comments on commit 889b7b4

Please sign in to comment.