Skip to content

Commit

Permalink
Bug Fix: azurerm_log_analytics_workspace_linked_service crash fix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Aug 28, 2019
1 parent 73f1756 commit a489e56
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
24 changes: 18 additions & 6 deletions azurerm/resource_arm_log_analytics_workspace_linked_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,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": {
Expand Down Expand Up @@ -116,8 +119,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")
}
Expand Down Expand Up @@ -212,6 +215,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{}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"net/http"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -95,6 +96,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: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy,
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_workspace_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(
testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).logAnalytics.LinkedServicesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -197,6 +240,31 @@ resource "azurerm_log_analytics_workspace_linked_service" "test" {
`, template)
}

func testAccAzureRMLogAnalyticsWorkspaceLinkedService_noResourceID(rInt int, location string) string {
template := testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt, location)
return fmt.Sprintf(`
%s
resource "azurerm_log_analytics_workspace_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 := testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt, location)
return fmt.Sprintf(`
%s
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 {
resource_id = "${azurerm_automation_account.test.id}"
}
}
`, template)
}

func testAccAzureRMLogAnalyticsWorkspaceLinkedService_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit a489e56

Please sign in to comment.