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.51k stars 4.6k forks source link

Saved Search Validation Incorrectly Flags Tabular Parameters #26685

Closed Folling closed 3 weeks ago

Folling commented 1 month ago

Is there an existing issue for this?

Community Note

https://github.com/hashicorp/terraform-provider-azurerm/blob/1aef7c1bc3e8afa3a15a1091ae2dd4c823e816ed/internal/services/loganalytics/log_analytics_saved_search_resource.go#L95

This regex is wrong and excludes tabular parameters in the form of arg:(*) or arg:(sub_arg:type,sub_arg:type2).

For more info, see here: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions.

I also think the regex currently in use has some issues, such as the first segment being * instead of + or that ! is allowed within argument names. There's more, but let's keep it brief. Here's a regex I think is (more) correct:

^[a-zA-Z_][a-zA-Z0-9-_]*:([a-z]+(=[^,\n]+)?|\(\*\)|(\([a-zA-Z_][a-zA-Z0-9-_]*:[a-z]+(,[a-zA-Z_][a-zA-Z0-9-_]*:([a-z]+))*\)))(,\s*[a-zA-Z_][a-zA-Z0-9-_]*:([a-z]+(=[^,\n]+)?|\(\*\)|(\([a-zA-Z_][a-zA-Z0-9-_]*:[a-z]+(,\s*[a-zA-Z_][a-zA-Z0-9-_]*:([a-z]+))*\))))*$

I'm aware it's a bit of a mess, but it matches things pretty convincingly (note that tabular arguments do not support default values): https://regex101.com/r/n99EPo/1

Terraform Version

v1.9.0

AzureRM Provider Version

3.112.0

Affected Resource(s)/Data Source(s)

azurerm_log_analytics_saved_search

Terraform Configuration Files

resource "azurerm_log_analytics_saved_search" "hello_world" {
  name                       = "hello_world"
  log_analytics_workspace_id = data.azurerm_log_analytics_workspace.example.id

  category     = "FooBar"
  display_name = "HelloWorld"
  query        = <<QUERY
    print("Hello World!")
  QUERY

  function_alias = "HelloWorld"
  function_parameters = [
    "foo:(*)"
  ]
}

Debug Output/Panic Output

│ Error: invalid value for function_parameters.0 (Log Analytics Saved Search Function Parameters must be in the following format: param-name1:type1=default_value1 OR param-name1:type1 OR param-name1:string='string goes here')
│ 
│   with module.saved_searches.azurerm_log_analytics_saved_search.hello_world,
│   on modules/saved_searches/main.tf line 400, in resource "azurerm_log_analytics_saved_search" "hello_world":
│  400:     "foo:(*)"
│

Expected Behaviour

Terraform should respect all valid function parameter definitions.

Actual Behaviour

It doesn't.

Steps to Reproduce

No response

Important Factoids

No response

References

https://github.com/hashicorp/terraform-provider-azurerm/pull/11341 https://github.com/hashicorp/terraform-provider-azurerm/issues/10989

ziyeqf commented 1 month ago

Hi @Folling, thanks for reporting and providing the regexp.

The code was committed 3 years ago, might be out of date. I opened a PR(https://github.com/hashicorp/terraform-provider-azurerm/pull/26701) to update the validation.

For any further discussion please leave comments