From 54d48df7b3706aa81d9aa988fe323086cff21e9d Mon Sep 17 00:00:00 2001 From: magodo Date: Sat, 9 May 2020 16:29:50 +0800 Subject: [PATCH 1/2] `azurerm_automation_account` DS set its own ID Previously, it uses the automation account agent registration info's ID as its ID, which cause other resource which refers to that ID will fail to parse the resource ID (as it doesn't apply to resource ID format). Nevertheless, we shall change to use the ID of resource `azurerm_automation_account` as the DS's ID. --- .../automation_account_data_source.go | 19 ++++++++++++++----- .../automation_account_data_source_test.go | 3 +++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/azurerm/internal/services/automation/automation_account_data_source.go b/azurerm/internal/services/automation/automation_account_data_source.go index c63bbb24198a..2ced13127ebb 100644 --- a/azurerm/internal/services/automation/automation_account_data_source.go +++ b/azurerm/internal/services/automation/automation_account_data_source.go @@ -44,7 +44,8 @@ func dataSourceArmAutomationAccount() *schema.Resource { } func dataSourceAutomationAccountRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Automation.AgentRegistrationInfoClient + iclient := meta.(*clients.Client).Automation.AgentRegistrationInfoClient + client := meta.(*clients.Client).Automation.AccountClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -56,11 +57,19 @@ func dataSourceAutomationAccountRead(d *schema.ResourceData, meta interface{}) e if utils.ResponseWasNotFound(resp.Response) { return fmt.Errorf("Error: Automation Account %q (Resource Group %q) was not found", name, resourceGroupName) } - return fmt.Errorf("Error making Read request on Automation Account Registration Information %q (Resource Group %q): %+v", name, resourceGroupName, err) + return fmt.Errorf("Error making Read request on Automation %q (Resource Group %q): %+v", name, resourceGroupName, err) } d.SetId(*resp.ID) - d.Set("primary_key", resp.Keys.Primary) - d.Set("secondary_key", resp.Keys.Secondary) - d.Set("endpoint", resp.Endpoint) + + iresp, err := iclient.Get(ctx, resourceGroupName, name) + if err != nil { + if utils.ResponseWasNotFound(iresp.Response) { + return fmt.Errorf("Error: Automation Account Registration Information %q (Resource Group %q) was not found", name, resourceGroupName) + } + return fmt.Errorf("Error making Read request on Automation Account Registration Information %q (Resource Group %q): %+v", name, resourceGroupName, err) + } + d.Set("primary_key", iresp.Keys.Primary) + d.Set("secondary_key", iresp.Keys.Secondary) + d.Set("endpoint", iresp.Endpoint) return nil } diff --git a/azurerm/internal/services/automation/tests/automation_account_data_source_test.go b/azurerm/internal/services/automation/tests/automation_account_data_source_test.go index 902c56fa0af6..831b25f2a814 100644 --- a/azurerm/internal/services/automation/tests/automation_account_data_source_test.go +++ b/azurerm/internal/services/automation/tests/automation_account_data_source_test.go @@ -2,6 +2,7 @@ package tests import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -20,6 +21,8 @@ func TestAccDataSourceAutomationAccount(t *testing.T) { Config: testAccDataSourceAutomationAccount_complete(resourceGroupName, data), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(data.ResourceName, "resource_group_name", resourceGroupName), + resource.TestMatchResourceAttr(data.ResourceName, "id", + regexp.MustCompile(`^/subscriptions/[^/]+/resourceGroups/[^/]+/providers/Microsoft.Automation/automationAccounts/[^/]+$`)), ), }, }, From 877890ab9ae3fe55482a58c89d831c7bb76ddf50 Mon Sep 17 00:00:00 2001 From: magodo Date: Mon, 11 May 2020 10:03:24 +0800 Subject: [PATCH 2/2] modify regexp: escape dot --- .../automation/tests/automation_account_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/automation/tests/automation_account_data_source_test.go b/azurerm/internal/services/automation/tests/automation_account_data_source_test.go index 831b25f2a814..fe75fb1a2803 100644 --- a/azurerm/internal/services/automation/tests/automation_account_data_source_test.go +++ b/azurerm/internal/services/automation/tests/automation_account_data_source_test.go @@ -22,7 +22,7 @@ func TestAccDataSourceAutomationAccount(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(data.ResourceName, "resource_group_name", resourceGroupName), resource.TestMatchResourceAttr(data.ResourceName, "id", - regexp.MustCompile(`^/subscriptions/[^/]+/resourceGroups/[^/]+/providers/Microsoft.Automation/automationAccounts/[^/]+$`)), + regexp.MustCompile(`^/subscriptions/[^/]+/resourceGroups/[^/]+/providers/Microsoft\.Automation/automationAccounts/[^/]+$`)), ), }, },