hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.64k forks source link

Support import of existing Log Analytics Solutions with name format Solution[WorkspaceName] #10230

Open lukecalderon opened 3 years ago

lukecalderon commented 3 years ago

Description

Importing an existing log analytics solution works fine for most of the ones from Microsoft, however attempting to import the LA solution Stop-Start-VM throws the following error:

Error: Expected "Start-Stop-VM[WorkspaceName]" to match 'Solution(WorkspaceName)'

Tracking down this error in the code shows it's filtering the resource ID to get the solution name, but doing a split on normal brackets. As this solution implements itself with square brackets, it causes the above error to be thrown.

Is it possible for this to be modified to accept existing LA solutions with square brackets?

Affected Resources

Code link

https://github.com/terraform-providers/terraform-provider-azurerm/blob/46371739b555d3af2702f8a739388d85d1aa0faa/azurerm/internal/services/loganalytics/log_analytics_solution_resource.go#L194

ruandersMSFT commented 3 years ago

@lukecalderon I just imported an existing Log Analytics Solution successfully where this format was used. The trick was that I had to put quotation marks around the ResourceID String.

Example:

Error: A resource with the ID "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/MyResourceGroupName/providers/Microsoft.OperationsManagement/solutions/NetworkMonitoring(az-log-p-usgovva-azmon)" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_log_analytics_solution" for more information.

Import command that worked (keep quotes around the Resource ID):

terraform import azurerm_log_analytics_solutions.NetworkMonitoring "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/MyResourceGroupName/providers/Microsoft.OperationsManagement/solutions/NetworkMonitoring(az-log-p-usgovva-azmon)"

If I didn't have the quotes around the Resource ID, I would get an error showing:

az-log-p-usgovva-azmon : The term 'az-log-p-usgovva-azmon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:236

lukecalderon commented 3 years ago

Import command that worked (keep quotes around the Resource ID):

terraform import azurerm_log_analytics_solutions.NetworkMonitoring "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/MyResourceGroupName/providers/Microsoft.OperationsManagement/solutions/NetworkMonitoring(az-log-p-usgovva-azmon)"

@ruandersMSFT Unfortunately I tried this and it still doesn't work. The actual LAW solution I'm having issues with is the Stop-Start-VM[law-name].

Note the log analytics workspace name is enclosed in square brackets, which is what throws the error. If I change these to normal brackets, I get a resource not found error which is expected. This solution does not support the name being changed to use normal brackets, so it's not possible to redeploy.

I have imported other log analytics solutions which are formatted with normal brackets without an issue, so I'm confident the issue here is as I mentioned originally.

front5stoneybaloney commented 1 year ago

Is there a workaround for this? I am facing the exact same issue and can't find a solution. The name of a resource Microsoft.OperationsManagement/solutions contains square brackets [] in its name like "SQLlogs[log-workspace]" and terraform plan won't go through.

lukecalderon commented 1 year ago

Is there a workaround for this? I am facing the exact same issue and can't find a solution. The name of a resource Microsoft.OperationsManagement/solutions contains square brackets [] in its name like "SQLlogs[log-workspace]" and terraform plan won't go through.

I don't think I ever worked around this. It might be potentially possible with the AzAPI provider instead, but I've not tested it personally.

front5stoneybaloney commented 1 year ago

Thanks Luke, I will look into having this resource re-created with a proper name. Cheers for the swift reply!

MBH999 commented 2 months ago

I have raised a PR to resolve this importing issue.

I think the issue occurs when creating the solution in the portal, as it generates the name with [] instead of ().

I have tested with the code in the PR, and it allows me to import the resource into state when it has the "incorrectly" formatted name, then used that code to destroy and recreate the solution, and it creates it with the "correctly" formatted name.