-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New data source: azurerm_cognitive_account (#8773)
Resolves #7765 Note that I do not use this resource and as such have not tested this functionality manually. Instead I am relying on the test coverage, and using the existing resource code, modified for the data source. If anyone uses this resource and sees anything missing in the data source that they expect to retrieve, let me know.
- Loading branch information
Showing
4 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
azurerm/internal/services/cognitive/cognitive_account_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cognitive | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmCognitiveAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmCognitiveAccountRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"location": azure.SchemaLocationForDataSource(), | ||
|
||
"kind": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"sku_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"qna_runtime_endpoint": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"endpoint": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"primary_access_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"secondary_access_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Cognitive.AccountsClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.GetProperties(ctx, resourceGroup, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: Cognitive Services Account %q (Resource Group %q) was not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error reading the state of AzureRM Cognitive Services Account %q: %+v", name, err) | ||
} | ||
|
||
keys, err := client.ListKeys(ctx, resourceGroup, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: Keys for Cognitive Services Account %q (Resource Group %q) were not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error obtaining keys for Cognitive Services Account %q in Resource Group %q: %v", name, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
d.Set("kind", resp.Kind) | ||
|
||
if sku := resp.Sku; sku != nil { | ||
d.Set("sku_name", sku.Name) | ||
} | ||
|
||
if props := resp.Properties; props != nil { | ||
if apiProps := props.APIProperties; apiProps != nil { | ||
d.Set("qna_runtime_endpoint", apiProps.QnaRuntimeEndpoint) | ||
} | ||
d.Set("endpoint", props.Endpoint) | ||
} | ||
|
||
d.Set("primary_access_key", keys.Key1) | ||
d.Set("secondary_access_key", keys.Key2) | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
azurerm/internal/services/cognitive/tests/cognitive_account_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
) | ||
|
||
func TestAccDataSourceAzureRMCognitiveAccount_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_cognitive_account", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMAppCognitiveAccountDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceCognitiveAccount_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMCognitiveAccountExists(data.ResourceName), | ||
resource.TestCheckResourceAttr(data.ResourceName, "kind", "Face"), | ||
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"), | ||
resource.TestCheckResourceAttrSet(data.ResourceName, "primary_access_key"), | ||
resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_access_key"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceCognitiveAccount_basic(data acceptance.TestData) string { | ||
template := testAccDataCognitiveAccount_template(data) | ||
return fmt.Sprintf(` | ||
%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" | ||
tags = { | ||
Acceptance = "Test" | ||
} | ||
} | ||
data "azurerm_cognitive_account" "test" { | ||
name = azurerm_cognitive_account.test.name | ||
resource_group_name = azurerm_cognitive_account.test.resource_group_name | ||
} | ||
`, template, data.RandomInteger) | ||
} | ||
|
||
func testAccDataCognitiveAccount_template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
subcategory: "Cognitive Services" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_cognitive_account" | ||
description: |- | ||
Gets information about an existing Cognitive Services Account. | ||
--- | ||
|
||
# Data Source: azurerm_cognitive_account | ||
|
||
Use this data source to access information about an existing Cognitive Services Account. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_cognitive_account" "test" { | ||
name = "example-account" | ||
resource_group_name = "cognitive_account_rg" | ||
} | ||
output "primary_access_key" { | ||
value = "${data.azurerm_cognitive_account.test.primary_access_key}" | ||
} | ||
``` | ||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) Specifies the name of the Cognitive Services Account. | ||
|
||
* `resource_group_name` - (Required) Specifies the name of the resource group where the Cognitive Services Account resides. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `location` - The Azure location where the Cognitive Services Account exists | ||
|
||
* `kind` - The kind of the Cognitive Services Account | ||
|
||
* `sku_name` - The sku name of the Cognitive Services Account | ||
|
||
* `endpoint` - The endpoint of the Cognitive Services Account | ||
|
||
* `qna_runtime_endpoint` - If `kind` is `QnAMaker` the link to the QNA runtime. | ||
* `primary_access_key` - The primary access key of the Cognitive Services Account | ||
|
||
* `secondary_access_key` - The secondary access key of the Cognitive Services Account | ||
|
||
* `tags` - A mapping of tags to assigned to the resource. | ||
|
||
## Timeouts | ||
|
||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: | ||
|
||
* `read` - (Defaults to 5 minutes) Used when retrieving the Cognitive Services Account. |