From 4d7b65b2dc05e80d01c915252f5ca36dcf23d91d Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Mon, 26 Aug 2019 09:34:29 -0700 Subject: [PATCH 1/3] Fix crash around workspace linked service properties --- ..._log_analytics_workspace_linked_service.go | 24 +++++-- ...analytics_workspace_linked_service_test.go | 68 +++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/azurerm/resource_arm_log_analytics_workspace_linked_service.go b/azurerm/resource_arm_log_analytics_workspace_linked_service.go index 8e51b7f21c3d..5475f4cd332c 100644 --- a/azurerm/resource_arm_log_analytics_workspace_linked_service.go +++ b/azurerm/resource_arm_log_analytics_workspace_linked_service.go @@ -63,10 +63,13 @@ As such the existing 'azurerm_log_analytics_workspace_linked_service' resource i }, "linked_service_properties": { - Type: schema.TypeList, - Optional: true, - Computed: true, - ForceNew: true, + Type: schema.TypeList, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"resource_id"}, + MaxItems: 1, + Deprecated: "This property has been deprecated in favour of the 'resource_id' property and will be removed in version 2.0 of the provider", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "resource_id": { @@ -115,8 +118,8 @@ func resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate(d *schema.Resourc resourceId := d.Get("resource_id").(string) if resourceId == "" { - props := d.Get("linked_service_properties").(map[string]interface{}) - resourceId = props["resource_id"].(string) + props := d.Get("linked_service_properties").([]interface{}) + resourceId = expandLogAnalyticsWorkspaceLinkedServiceProperties(props) if resourceId == "" { return fmt.Errorf("A `resource_id` must be specified either using the `resource_id` field at the top level or within the `linked_service_properties` block") } @@ -211,6 +214,15 @@ func resourceArmLogAnalyticsWorkspaceLinkedServiceDelete(d *schema.ResourceData, return nil } +func expandLogAnalyticsWorkspaceLinkedServiceProperties(input []interface{}) string { + if len(input) == 0 { + return "" + } + + props := input[0].(map[string]interface{}) + return props["resource_id"].(string) +} + func flattenLogAnalyticsWorkspaceLinkedServiceProperties(input *operationalinsights.LinkedServiceProperties) []interface{} { if input == nil { return []interface{}{} diff --git a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go index 7fdcf9345fa2..8039891dcb7d 100644 --- a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go +++ b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "net/http" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -94,6 +95,48 @@ func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_complete(t *testing.T) { }) } +// Deprecated - remove in 2.0 +func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(t *testing.T) { + ri := tf.AccRandTimeInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(ri, testLocation()), + ExpectError: regexp.MustCompile("A `resource_id` must be specified either using the `resource_id` field at the top level or within the `linked_service_properties` block"), + }, + }, + }) +} + +// Deprecated - remove in 2.0 +func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(t *testing.T) { + resourceName := "azurerm_log_analytics_linked_service.test" + ri := tf.AccRandTimeInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMLogAnalyticsLinkedServiceExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*ArmClient).logAnalytics.LinkedServicesClient ctx := testAccProvider.Meta().(*ArmClient).StopContext @@ -196,6 +239,31 @@ resource "azurerm_log_analytics_workspace_linked_service" "test" { `, template) } +func testAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(rInt int, location string) string { + template := testAccAzureRMLogAnalyticsLinkedService_template(rInt, location) + return fmt.Sprintf(` +%s +resource "azurerm_log_analytics_linked_service" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + workspace_name = "${azurerm_log_analytics_workspace.test.name}" +} +`, template) +} + +func testAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(rInt int, location string) string { + template := testAccAzureRMLogAnalyticsLinkedService_template(rInt, location) + return fmt.Sprintf(` +%s +resource "azurerm_log_analytics_linked_service" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + workspace_name = "${azurerm_log_analytics_workspace.test.name}" + linked_service_properties { + resource_id = "${azurerm_automation_account.test.id}" + } +} +`, template) +} + func testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From 9f121d8cde29d43c4b1bc90299952f11064ee9cf Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Mon, 26 Aug 2019 09:40:55 -0700 Subject: [PATCH 2/3] Fix tests --- ...rm_log_analytics_workspace_linked_service_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go index 8039891dcb7d..681f7833262e 100644 --- a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go +++ b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go @@ -102,7 +102,7 @@ func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(t *testing.T) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy, + CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy, Steps: []resource.TestStep{ { Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(ri, testLocation()), @@ -125,7 +125,7 @@ func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(t { Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(ri, testLocation()), Check: resource.ComposeTestCheckFunc( - testCheckAzureRMLogAnalyticsLinkedServiceExists(resourceName), + testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceExists(resourceName), ), }, { @@ -240,10 +240,10 @@ resource "azurerm_log_analytics_workspace_linked_service" "test" { } func testAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(rInt int, location string) string { - template := testAccAzureRMLogAnalyticsLinkedService_template(rInt, location) + template := testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt, location) return fmt.Sprintf(` %s -resource "azurerm_log_analytics_linked_service" "test" { +resource "azurerm_log_analytics_workspace_linked_service" "test" { resource_group_name = "${azurerm_resource_group.test.name}" workspace_name = "${azurerm_log_analytics_workspace.test.name}" } @@ -251,10 +251,10 @@ resource "azurerm_log_analytics_linked_service" "test" { } func testAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(rInt int, location string) string { - template := testAccAzureRMLogAnalyticsLinkedService_template(rInt, location) + template := testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt, location) return fmt.Sprintf(` %s -resource "azurerm_log_analytics_linked_service" "test" { +resource "azurerm_log_analytics_workspace_linked_service" "test" { resource_group_name = "${azurerm_resource_group.test.name}" workspace_name = "${azurerm_log_analytics_workspace.test.name}" linked_service_properties { From 2c74660b503649d5fc20c6576871f32b583926e1 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Mon, 26 Aug 2019 10:02:19 -0700 Subject: [PATCH 3/3] Fix test --- .../resource_arm_log_analytics_workspace_linked_service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go index 681f7833262e..cb742524887c 100644 --- a/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go +++ b/azurerm/resource_arm_log_analytics_workspace_linked_service_test.go @@ -114,7 +114,7 @@ func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(t *testing.T) // Deprecated - remove in 2.0 func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_linkedServiceProperties(t *testing.T) { - resourceName := "azurerm_log_analytics_linked_service.test" + resourceName := "azurerm_log_analytics_workspace_linked_service.test" ri := tf.AccRandTimeInt() resource.ParallelTest(t, resource.TestCase{