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

Added keys to output for cog service account #2825

Merged
merged 16 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 29 additions & 1 deletion azurerm/resource_arm_cognitive_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ func resourceArmCognitiveAccount() *schema.Resource {
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,
},
},
}
}
Expand Down Expand Up @@ -192,6 +204,7 @@ func resourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) e
name := id.Path["accounts"]

resp, err := client.GetProperties(ctx, resourceGroup, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Cognitive Services Account %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
Expand All @@ -209,14 +222,29 @@ func resourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) e
d.Set("location", azureRMNormalizeLocation(*location))
}

if err := d.Set("sku", flattenCognitiveAccountSku(resp.Sku)); err != nil {
if err = d.Set("sku", flattenCognitiveAccountSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}

if props := resp.AccountProperties; props != nil {
d.Set("endpoint", props.Endpoint)
}

keys, err := client.ListKeys(ctx, resourceGroup, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Not able to obtain keys for Cognitive Services Account %q in Resource Group %q - removing from state!", name, resourceGroup)
JimPaine marked this conversation as resolved.
Show resolved Hide resolved
d.SetId("")
return nil
}
return fmt.Errorf("Error obtaining keys for Cognitive Services Account %q in Resource Group %q: %v", name, resourceGroup, err)
}

d.Set("primary_access_key", keys.Key1)

d.Set("secondary_access_key", keys.Key2)

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down
10 changes: 10 additions & 0 deletions azurerm/resource_arm_cognitive_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestAccAzureRMCognitiveAccount_basic(t *testing.T) {
testCheckAzureRMCognitiveAccountExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "kind", "Face"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
),
},
{
Expand Down Expand Up @@ -54,6 +56,8 @@ func TestAccAzureRMCognitiveAccount_speechServices(t *testing.T) {
testCheckAzureRMCognitiveAccountExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "kind", "SpeechServices"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
),
},
{
Expand Down Expand Up @@ -111,6 +115,8 @@ func TestAccAzureRMCognitiveAccount_complete(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "kind", "Face"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Acceptance", "Test"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
),
},
{
Expand Down Expand Up @@ -138,6 +144,8 @@ func TestAccAzureRMCognitiveAccount_update(t *testing.T) {
testCheckAzureRMCognitiveAccountExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "kind", "Face"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
),
},
{
Expand All @@ -147,6 +155,8 @@ func TestAccAzureRMCognitiveAccount_update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "kind", "Face"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Acceptance", "Test"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ The following attributes are exported:

* `endpoint` - The endpoint used to connect to the Cognitive Service Account.

* `primary_access_key` - A primary access key which can be used to connect to the Cognitive Service Account.

* `secondary_access_key` - The secondary access key which can be used to connect to the Cognitive Service Account.

## Import

Cognitive Service Accounts can be imported using the `resource id`, e.g.
Expand Down