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_saved_search - update validation for azurerm_log_analytics_saved_search #26701

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func resourceLogAnalyticsSavedSearch() *pluginsdk.Resource {
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
// https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9!-_]*:[a-zA-Z0-9!_-]+=[a-zA-Z0-9!_-]+|^[a-zA-Z0-9!-_]*:[a-zA-Z0-9!_-]+`),
regexp.MustCompile(`^[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]+))*\))))*$`),
"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'",
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ func TestAccLogAnalyticsSavedSearch_withTag(t *testing.T) {
})
}

func TestAccLogAnalyticsSavedSearch_functionParameter(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_log_analytics_saved_search", "test")
r := LogAnalyticsSavedSearchResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.function_parameter(data, "foo:(*)"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Copy link

Choose a reason for hiding this comment

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

ideally a test case should be added that verifies multiple parameters as well, perhaps in 2-3 different forms, just to test the repetitive part of the regex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

multiple parameters are tested in the TestAccLogAnalyticsSavedSearch_complete test case

Config: r.function_parameter(data, "foo:int=1"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.function_parameter(data, "foo:(bar:long)"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccLogAnalyticsSavedSearch_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_log_analytics_saved_search", "test")
r := LogAnalyticsSavedSearchResource{}
Expand Down Expand Up @@ -172,6 +201,38 @@ resource "azurerm_log_analytics_saved_search" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (LogAnalyticsSavedSearchResource) function_parameter(data acceptance.TestData, para string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
}

resource "azurerm_log_analytics_saved_search" "test" {
name = "acctestLASS-%d"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

category = "Saved Search Test Category"
Copy link

Choose a reason for hiding this comment

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

Not sure if spaces are allowed here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

per the testing result it should be fine

display_name = "Create or Update Saved Search Test"
Copy link

Choose a reason for hiding this comment

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

Suggested change
display_name = "Create or Update Saved Search Test"
display_name = "SavedSearchTest"

query = "Heartbeat | summarize Count() by Computer | take a"
Copy link

Choose a reason for hiding this comment

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

Suggested change
query = "Heartbeat | summarize Count() by Computer | take a"
query = "Heartbeat | summarize Count() by Computer | take 1"


function_alias = "heartbeat_func"
function_parameters = ["%s"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, para)
}

func (LogAnalyticsSavedSearchResource) withTag(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/log_analytics_saved_search.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The following arguments are supported:

* `function_alias` - (Optional) The function alias if the query serves as a function. Changing this forces a new resource to be created.

* `function_parameters` - (Optional) The function parameters if the query serves as a function. Changing this forces a new resource to be created.
* `function_parameters` - (Optional) The function parameters if the query serves as a function. Changing this forces a new resource to be created. For more examples and proper syntax please refer to [this document](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions)
Copy link

Choose a reason for hiding this comment

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

Suggested change
* `function_parameters` - (Optional) The function parameters if the query serves as a function. Changing this forces a new resource to be created. For more examples and proper syntax please refer to [this document](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions)
* `function_parameters` - (Optional) The function parameters if the query serves as a function. Changing this forces a new resource to be created. For more examples and proper syntax please refer to [this document](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions).


* `tags` - (Optional) A mapping of tags which should be assigned to the Logs Analytics Saved Search. Changing this forces a new resource to be created.

Expand Down
Loading