diff --git a/azurerm/internal/services/cognitive/resource_arm_cognitive_account.go b/azurerm/internal/services/cognitive/resource_arm_cognitive_account.go index 44eb1950f9d8..23c17e54b987 100644 --- a/azurerm/internal/services/cognitive/resource_arm_cognitive_account.go +++ b/azurerm/internal/services/cognitive/resource_arm_cognitive_account.go @@ -84,10 +84,23 @@ func resourceArmCognitiveAccount() *schema.Resource { }, false), }, + "sku_name": { + Type: schema.TypeString, + Optional: true, // required in 2.0 + Computed: true, // remove in 2.0 + ConflictsWith: []string{"sku"}, + ValidateFunc: validation.StringInSlice([]string{ + "F0", "F1", "S0", "S1", "S2", "S3", "S4", "S5", "S6", "P0", "P1", "P2", + }, false), + }, + "sku": { - Type: schema.TypeList, - Required: true, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + Computed: true, + ConflictsWith: []string{"sku_name"}, + Deprecated: "This property has been deprecated in favour of the 'sku_name' property and will be removed in version 2.0 of the provider", + MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "name": { @@ -154,17 +167,25 @@ func resourceArmCognitiveAccountCreate(d *schema.ResourceData, meta interface{}) } } - location := azure.NormalizeLocation(d.Get("location").(string)) - kind := d.Get("kind").(string) - t := d.Get("tags").(map[string]interface{}) - sku := expandCognitiveAccountSku(d) + var sku *cognitiveservices.Sku + if b, ok := d.GetOk("sku_name"); ok { + var err error + sku, err = expandAccountSkuName(b.(string)) + if err != nil { + return fmt.Errorf("error expanding sku_name for Cognitive Account %s (Resource Group %q): %v", name, resourceGroup, err) + } + } else if _, ok := d.GetOk("sku"); ok { + sku = expandCognitiveAccountSku(d) + } else { + return fmt.Errorf("One of `sku` or `sku_name` must be set for Cognitive Account %q (Resource Group %q)", name, resourceGroup) + } properties := cognitiveservices.Account{ - Kind: utils.String(kind), - Location: utils.String(location), + Kind: utils.String(d.Get("kind").(string)), + Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))), Sku: sku, Properties: &cognitiveservices.AccountProperties{}, - Tags: tags.Expand(t), + Tags: tags.Expand(d.Get("tags").(map[string]interface{})), } if _, err := client.Create(ctx, resourceGroup, name, properties); err != nil { @@ -194,16 +215,25 @@ func resourceArmCognitiveAccountUpdate(d *schema.ResourceData, meta interface{}) resourceGroup := id.ResourceGroup name := id.Path["accounts"] - t := d.Get("tags").(map[string]interface{}) - sku := expandCognitiveAccountSku(d) + var sku *cognitiveservices.Sku + if b, ok := d.GetOk("sku_name"); ok { + var err error + sku, err = expandAccountSkuName(b.(string)) + if err != nil { + return fmt.Errorf("error expanding sku_name for Cognitive Account %s (Resource Group %q): %v", name, resourceGroup, err) + } + } else if _, ok := d.GetOk("sku"); ok { + sku = expandCognitiveAccountSku(d) + } else { + return fmt.Errorf("One of `sku` or `sku_name` must be set for Cognitive Account %q (Resource Group %q)", name, resourceGroup) + } properties := cognitiveservices.Account{ Sku: sku, - Tags: tags.Expand(t), + Tags: tags.Expand(d.Get("tags").(map[string]interface{})), } - _, err = client.Update(ctx, resourceGroup, name, properties) - if err != nil { + if _, err = client.Update(ctx, resourceGroup, name, properties); err != nil { return fmt.Errorf("Error updating Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err) } @@ -242,6 +272,10 @@ func resourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) e d.Set("location", azure.NormalizeLocation(*location)) } + if sku := resp.Sku; sku != nil { + d.Set("sku_name", sku.Name) + } + if err = d.Set("sku", flattenCognitiveAccountSku(resp.Sku)); err != nil { return fmt.Errorf("Error setting `sku`: %+v", err) } @@ -262,7 +296,6 @@ func resourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) e } d.Set("primary_access_key", keys.Key1) - d.Set("secondary_access_key", keys.Key2) return tags.FlattenAndSet(d, resp.Tags) @@ -291,6 +324,25 @@ func resourceArmCognitiveAccountDelete(d *schema.ResourceData, meta interface{}) return nil } +func expandAccountSkuName(skuName string) (*cognitiveservices.Sku, error) { + var tier cognitiveservices.SkuTier + switch skuName[0:1] { + case "F": + tier = cognitiveservices.Free + case "S": + tier = cognitiveservices.Standard + case "P": + tier = cognitiveservices.Premium + default: + return nil, fmt.Errorf("sku_name %s has unknown sku tier %s", skuName, skuName[0:1]) + } + + return &cognitiveservices.Sku{ + Name: utils.String(skuName), + Tier: tier, + }, nil +} + func expandCognitiveAccountSku(d *schema.ResourceData) *cognitiveservices.Sku { skus := d.Get("sku").([]interface{}) sku := skus[0].(map[string]interface{}) diff --git a/azurerm/internal/services/cognitive/tests/resource_arm_cognitive_account_test.go b/azurerm/internal/services/cognitive/tests/resource_arm_cognitive_account_test.go index a22e57786150..225262bca466 100644 --- a/azurerm/internal/services/cognitive/tests/resource_arm_cognitive_account_test.go +++ b/azurerm/internal/services/cognitive/tests/resource_arm_cognitive_account_test.go @@ -36,6 +36,29 @@ func TestAccAzureRMCognitiveAccount_basic(t *testing.T) { }) } +func TestAccAzureRMCognitiveAccount_basicOldSku(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_basicOldSku(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMCognitiveAccountExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "kind", "Face"), + resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"), + resource.TestCheckResourceAttrSet(data.ResourceName, "primary_access_key"), + resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_access_key"), + ), + }, + data.ImportStep(), + }, + }) +} + func TestAccAzureRMCognitiveAccount_speechServices(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cognitive_account", "test") @@ -202,6 +225,24 @@ resource "azurerm_resource_group" "test" { 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 = "Face" + + sku_name = "S0" +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} + +func testAccAzureRMCognitiveAccount_basicOldSku(data acceptance.TestData) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + resource "azurerm_cognitive_account" "test" { name = "acctestcogacc-%d" location = "${azurerm_resource_group.test.location}" @@ -229,10 +270,7 @@ resource "azurerm_cognitive_account" "test" { resource_group_name = "${azurerm_resource_group.test.name}" kind = "SpeechServices" - sku { - name = "S0" - tier = "Standard" - } + sku_name = "S0" } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger) } @@ -248,10 +286,7 @@ resource "azurerm_cognitive_account" "import" { resource_group_name = "${azurerm_cognitive_account.test.resource_group_name}" kind = "${azurerm_cognitive_account.test.kind}" - sku { - name = "S0" - tier = "Standard" - } + sku_name = "S0" } `, template) } @@ -269,10 +304,7 @@ resource "azurerm_cognitive_account" "test" { resource_group_name = "${azurerm_resource_group.test.name}" kind = "Face" - sku { - name = "S0" - tier = "Standard" - } + sku_name = "S0" tags = { Acceptance = "Test" diff --git a/website/docs/guides/2.0-upgrade-guide.html.markdown b/website/docs/guides/2.0-upgrade-guide.html.markdown index 1df8dc28bd0d..7e0fe818bcb5 100644 --- a/website/docs/guides/2.0-upgrade-guide.html.markdown +++ b/website/docs/guides/2.0-upgrade-guide.html.markdown @@ -231,6 +231,10 @@ The AzureAD Data Sources and Resources have been moved to [the new AzureAD Provi A guide on how to migrate to using the new Provider [can be found here](https://www.terraform.io/docs/providers/azurerm/guides/migrating-to-azuread.html). +### Resource: `azurerm_cognitive_account` + +The deprecated `sku` block has been replaced by the `sku_name` field and will be removed. + ### Resource: `azurerm_connection_monitor` The `azurerm_connection_monitor` resource will be deprecated in favour of a new resources `azurerm_network_connection_monitor`. diff --git a/website/docs/r/cognitive_account.html.markdown b/website/docs/r/cognitive_account.html.markdown index 10056cecb437..803e239a0bf7 100644 --- a/website/docs/r/cognitive_account.html.markdown +++ b/website/docs/r/cognitive_account.html.markdown @@ -24,10 +24,7 @@ resource "azurerm_cognitive_account" "example" { resource_group_name = "${azurerm_resource_group.example.name}" kind = "Face" - sku { - name = "S0" - tier = "Standard" - } + sku_name = "S0" tags = { Acceptance = "Test" @@ -47,17 +44,10 @@ The following arguments are supported: * `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`, `LUIS`, `LUIS.Authoring`, `QnAMaker`, `Recommendations`, `SpeakerRecognition`, `Speech`, `SpeechServices`, `SpeechTranslation`, `TextAnalytics`, `TextTranslation` and `WebLM`. Changing this forces a new resource to be created. -* `sku` - (Required) A `sku` block as defined below. +* `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`. * `tags` - (Optional) A mapping of tags to assign to the resource. ---- - -A `sku` block supports the following: - -* `name` - (Required) Specifies the Name of the Sku. Possible values are `F0`, `F1`, `S0`, `S1`, `S2`, `S3`, `S4`, `S5`, `S6`, `P0`, `P1` and `P2`. - -* `tier` - (Required) Specifies the Tier of the Sku. Possible values include `Free`, `Standard` and `Premium`. ## Attributes Reference