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

Bug Fix: azurerm_log_analytics_linked_service crash fix #4142

Merged
merged 2 commits into from
Aug 28, 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
24 changes: 18 additions & 6 deletions azurerm/resource_arm_log_analytics_linked_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ func resourceArmLogAnalyticsLinkedService() *schema.Resource {
},

"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",
mbfrahry marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_id": {
Expand Down Expand Up @@ -108,8 +111,8 @@ func resourceArmLogAnalyticsLinkedServiceCreateUpdate(d *schema.ResourceData, me

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 = expandLogAnalyticsLinkedServiceProperties(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 @@ -204,6 +207,15 @@ func resourceArmLogAnalyticsLinkedServiceDelete(d *schema.ResourceData, meta int
return nil
}

func expandLogAnalyticsLinkedServiceProperties(input []interface{}) string {
if len(input) == 0 {
return ""
}

props := input[0].(map[string]interface{})
return props["resource_id"].(string)
}

func flattenLogAnalyticsLinkedServiceProperties(input *operationalinsights.LinkedServiceProperties) []interface{} {
if input == nil {
return []interface{}{}
Expand Down
70 changes: 70 additions & 0 deletions azurerm/resource_arm_log_analytics_linked_service_test.go
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 @@ -94,6 +95,48 @@ func TestAccAzureRMLogAnalyticsLinkedService_complete(t *testing.T) {
})
}

// Deprecated - remove in 2.0
func TestAccAzureRMLogAnalyticsLinkedService_noResourceID(t *testing.T) {
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLogAnalyticsLinkedService_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 TestAccAzureRMLogAnalyticsLinkedService_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: testAccAzureRMLogAnalyticsLinkedService_linkedServiceProperties(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsLinkedServiceExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMLogAnalyticsLinkedServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).logAnalytics.LinkedServicesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -196,6 +239,33 @@ resource "azurerm_log_analytics_linked_service" "test" {
`, template)
}

func testAccAzureRMLogAnalyticsLinkedService_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 testAccAzureRMLogAnalyticsLinkedService_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 testAccAzureRMLogAnalyticsLinkedService_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down