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

azurerm_log_analytics_workspace_linked_service: fix casing issues #2594

Merged
merged 2 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 16 additions & 11 deletions azurerm/resource_arm_log_analytics_workspace_linked_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -17,6 +18,7 @@ func resourceArmLogAnalyticsWorkspaceLinkedService() *schema.Resource {
Read: resourceArmLogAnalyticsWorkspaceLinkedServiceRead,
Update: resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate,
Delete: resourceArmLogAnalyticsWorkspaceLinkedServiceDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand All @@ -25,18 +27,19 @@ func resourceArmLogAnalyticsWorkspaceLinkedService() *schema.Resource {
"resource_group_name": resourceGroupNameDiffSuppressSchema(),

"workspace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRmLogAnalyticsWorkspaceName,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppress.CaseDifference,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to remove this, and instead update the validation to require lower-case here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we should also raise a bug about this API being broken, if there's not one already)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would potentially break existing terraform and not match the portal. Workspace's are allowed to have capitalized characters and that resource works just fine with them (see updated tests)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue opened

ValidateFunc: validateAzureRmLogAnalyticsWorkspaceName,
},

"linked_service_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "automation",
ValidateFunc: validate.NoEmptyStrings,
ValidateFunc: validation.StringInSlice([]string{"automation"}, false),
},

"linked_service_properties": {
Expand All @@ -55,24 +58,24 @@ func resourceArmLogAnalyticsWorkspaceLinkedService() *schema.Resource {
},
},

"tags": tagsSchema(),

// Exported properties
"name": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}

func resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM Log Analytics linked services creation.")

resGroup := d.Get("resource_group_name").(string)

workspaceName := d.Get("workspace_name").(string)
lsName := d.Get("linked_service_name").(string)

Expand All @@ -96,7 +99,6 @@ func resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate(d *schema.Resourc
if err != nil {
return fmt.Errorf("Error retrieving Analytics Workspace Linked Service %q/%q (Resource Group %q): %+v", workspaceName, lsName, resGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Log Analytics Linked Service '%s' (resource group %s) ID", lsName, resGroup)
}
Expand All @@ -110,10 +112,12 @@ func resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate(d *schema.Resourc
func resourceArmLogAnalyticsWorkspaceLinkedServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
workspaceName := id.Path["workspaces"]
lsName := id.Path["linkedservices"]
Expand Down Expand Up @@ -159,16 +163,17 @@ func flattenLogAnalyticsWorkspaceLinkedServiceProperties(input *operationalinsig
func resourceArmLogAnalyticsWorkspaceLinkedServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
workspaceName := id.Path["workspaces"]
lsName := id.Path["linkedservices"]

resp, err := client.Delete(ctx, resGroup, workspaceName, lsName)

if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
Expand Down
121 changes: 30 additions & 91 deletions azurerm/resource_arm_log_analytics_workspace_linked_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,54 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_requiredOnly(t *testing.T) {
func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_basic(t *testing.T) {
resourceName := "azurerm_log_analytics_workspace_linked_service.test"
ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsWorkspaceLinkedServiceRequiredOnly(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctestworkspace-%d/Automation", ri)),
resource.TestCheckResourceAttr(resourceName, "workspace_name", fmt.Sprintf("acctestworkspace-%d", ri)),
resource.TestCheckResourceAttr(resourceName, "linked_service_name", "automation"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_optionalArguments(t *testing.T) {
func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_complete(t *testing.T) {
resourceName := "azurerm_log_analytics_workspace_linked_service.test"
ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsWorkspaceLinkedServiceOptionalArguments(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: testAccAzureRMLogAnalyticsWorkspaceLinkedService_complete(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "linked_service_name", "automation"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -68,11 +76,9 @@ func testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy(s *terraform.Stat
lsName := rs.Primary.Attributes["linked_service_name"]

resp, err := conn.Get(ctx, resourceGroup, workspaceName, lsName)

if err != nil {
return nil
}

if resp.ID == nil {
return nil
}
Expand Down Expand Up @@ -118,63 +124,15 @@ func testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceExists(resourceName strin
}
}

func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_importRequiredOnly(t *testing.T) {
resourceName := "azurerm_log_analytics_workspace_linked_service.test"

ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsWorkspaceLinkedServiceRequiredOnly(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMLogAnalyticsWorkspaceLinkedService_importOptionalArguments(t *testing.T) {
resourceName := "azurerm_log_analytics_workspace_linked_service.test"

ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsWorkspaceLinkedServiceOptionalArguments(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAzureRMLogAnalyticsWorkspaceLinkedServiceRequiredOnly(rInt int, location string) string {
func testAccAzureRMLogAnalyticsWorkspaceLinkedService_prereqs(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestresourcegroup-%d"
location = "%v"
name = "acctestRG-%[1]d"
location = "%[2]v"
}

resource "azurerm_automation_account" "test" {
name = "acctestautomation-%d"
name = "acctestAutomation-%[1]d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -188,12 +146,18 @@ resource "azurerm_automation_account" "test" {
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestworkspace-%d"
name = "acctestWorkSpace-%[1]d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "PerGB2018"
retention_in_days = 30
}
`, rInt, location)
}

func testAccAzureRMLogAnalyticsWorkspaceLinkedService_basic(rInt int, location string) string {
return fmt.Sprintf(`
%s

resource "azurerm_log_analytics_workspace_linked_service" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
Expand All @@ -203,46 +167,21 @@ resource "azurerm_log_analytics_workspace_linked_service" "test" {
resource_id = "${azurerm_automation_account.test.id}"
}
}
`, rInt, location, rInt, rInt)
`, testAccAzureRMLogAnalyticsWorkspaceLinkedService_prereqs(rInt, location))
}

func testAccAzureRMLogAnalyticsWorkspaceLinkedServiceOptionalArguments(rInt int, location string) string {
func testAccAzureRMLogAnalyticsWorkspaceLinkedService_complete(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestresourcegroup-%d"
location = "%v"
}

resource "azurerm_automation_account" "test" {
name = "acctestautomation-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

sku {
name = "Basic"
}

tags {
environment = "development"
}
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestworkspace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "PerGB2018"
retention_in_days = 30
}
%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_name = "automation"
linked_service_name = "Automation"

linked_service_properties {
resource_id = "${azurerm_automation_account.test.id}"
}
}
`, rInt, location, rInt, rInt)
`, testAccAzureRMLogAnalyticsWorkspaceLinkedService_prereqs(rInt, location))
}