Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_cognitive_account: deprecate sku in favour of sku_name #5380

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
katbyte marked this conversation as resolved.
Show resolved Hide resolved
}, 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": {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -202,6 +225,24 @@ resource "azurerm_resource_group" "test" {
location = "%s"
}
resource "azurerm_cognitive_account" "test" {
name = "acctestcogacc-%d"
katbyte marked this conversation as resolved.
Show resolved Hide resolved
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}"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions website/docs/guides/2.0-upgrade-guide.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 simplified and replaced by the `sku_name` property and will be removed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The deprecated `sku` block has been simplified and replaced by the `sku_name` property and will be removed.
The deprecated `sku` block has been replaced by the `sku_name` field and will be removed.
The `sku_name` field will become Required.


### Resource: `azurerm_connection_monitor`

The `azurerm_connection_monitor` resource will be deprecated in favour of a new resources `azurerm_network_connection_monitor`.
Expand Down
14 changes: 2 additions & 12 deletions website/docs/r/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down