From eb3837c31f51c0f494c1c4e3890e6a5ffe57f7bd Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Jul 2020 16:35:12 +0800 Subject: [PATCH 1/2] Fix TC for TestAccAzureRMApplicationInsightsAPIKey_requiresImport --- .../application_insights_api_key_resource.go | 18 +++++++++++++++++- ...plication_insights_api_key_resource_test.go | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go b/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go index 38f3bb7514c2..d8daf52084de 100644 --- a/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go +++ b/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go @@ -97,8 +97,24 @@ func resourceArmApplicationInsightsAPIKeyCreate(d *schema.ResourceData, meta int appInsightsName := id.Path["components"] if features.ShouldResourcesBeImported() { + var existingAPIKeyList insights.ApplicationInsightsComponentAPIKeyListResult + var keyId string + existingAPIKeyList, err = client.List(ctx, resGroup, appInsightsName) + for _, existingAPIKey := range *existingAPIKeyList.Value { + existingAPIKeyId, err := azure.ParseAzureResourceID(*existingAPIKey.ID) + if err != nil { + return err + } + + existingAppInsightsName := existingAPIKeyId.Path["components"] + if appInsightsName == existingAppInsightsName { + keyId = existingAPIKeyId.Path["apikeys"] + break + } + } + var existing insights.ApplicationInsightsComponentAPIKey - existing, err = client.Get(ctx, resGroup, appInsightsName, name) + existing, err = client.Get(ctx, resGroup, appInsightsName, keyId) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { return fmt.Errorf("Error checking for presence of existing Application Insights API key %q (Resource Group %q): %s", name, resGroup, err) diff --git a/azurerm/internal/services/applicationinsights/tests/application_insights_api_key_resource_test.go b/azurerm/internal/services/applicationinsights/tests/application_insights_api_key_resource_test.go index 408ae8942bb9..c5405f9c5f28 100644 --- a/azurerm/internal/services/applicationinsights/tests/application_insights_api_key_resource_test.go +++ b/azurerm/internal/services/applicationinsights/tests/application_insights_api_key_resource_test.go @@ -46,8 +46,8 @@ func TestAccAzureRMApplicationInsightsAPIKey_requiresImport(t *testing.T) { ), }, { - Config: testAccAzureRMApplicationInsightsAPIKey_requiresImport(data, "[]", "[]"), - ExpectError: acceptance.RequiresImportError("azurerm_application_insights"), + Config: testAccAzureRMApplicationInsightsAPIKey_requiresImport(data, "[]", `["annotations"]`), + ExpectError: acceptance.RequiresImportError("azurerm_application_insights_api_key"), }, }, }) From c7c38b5d068c1f0e2b25a6fd51449835fd6fd41a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Jul 2020 21:36:17 +0800 Subject: [PATCH 2/2] Update code --- .../application_insights_api_key_resource.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go b/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go index d8daf52084de..633c48607493 100644 --- a/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go +++ b/azurerm/internal/services/applicationinsights/application_insights_api_key_resource.go @@ -100,6 +100,12 @@ func resourceArmApplicationInsightsAPIKeyCreate(d *schema.ResourceData, meta int var existingAPIKeyList insights.ApplicationInsightsComponentAPIKeyListResult var keyId string existingAPIKeyList, err = client.List(ctx, resGroup, appInsightsName) + if err != nil { + if !utils.ResponseWasNotFound(existingAPIKeyList.Response) { + return fmt.Errorf("checking for presence of existing Application Insights API key list (Application Insights %q / Resource Group %q): %s", appInsightsName, resGroup, err) + } + } + for _, existingAPIKey := range *existingAPIKeyList.Value { existingAPIKeyId, err := azure.ParseAzureResourceID(*existingAPIKey.ID) if err != nil { @@ -117,7 +123,7 @@ func resourceArmApplicationInsightsAPIKeyCreate(d *schema.ResourceData, meta int existing, err = client.Get(ctx, resGroup, appInsightsName, keyId) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for presence of existing Application Insights API key %q (Resource Group %q): %s", name, resGroup, err) + return fmt.Errorf("checking for presence of existing Application Insights API key %q (Resource Group %q): %s", name, resGroup, err) } }