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

log_analytics_workspace - exclude Free from daily_quota_gb settings #9228

Merged
merged 4 commits into from
Nov 10, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"regexp"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2020-03-01-preview/operationalinsights"
Expand Down Expand Up @@ -81,10 +82,11 @@ func resourceArmLogAnalyticsWorkspace() *schema.Resource {
},

"daily_quota_gb": {
Type: schema.TypeFloat,
Optional: true,
Default: -1.0,
ValidateFunc: validation.Any(validation.FloatBetween(-1, -1), validation.FloatAtLeast(0)),
Type: schema.TypeFloat,
Optional: true,
Default: -1.0,
DiffSuppressFunc: dailyQuotaGbDiffSuppressFunc,
ValidateFunc: validation.FloatAtLeast(0),
},

"workspace_id": {
Expand Down Expand Up @@ -146,7 +148,6 @@ func resourceArmLogAnalyticsWorkspaceCreateUpdate(d *schema.ResourceData, meta i
}

retentionInDays := int32(d.Get("retention_in_days").(int))
dailyQuotaGb := d.Get("daily_quota_gb").(float64)

t := d.Get("tags").(map[string]interface{})

Expand All @@ -157,12 +158,18 @@ func resourceArmLogAnalyticsWorkspaceCreateUpdate(d *schema.ResourceData, meta i
WorkspaceProperties: &operationalinsights.WorkspaceProperties{
Sku: sku,
RetentionInDays: &retentionInDays,
WorkspaceCapping: &operationalinsights.WorkspaceCapping{
DailyQuotaGb: &dailyQuotaGb,
},
},
}

dailyQuotaGb, ok := d.GetOk("daily_quota_gb")
if ok && strings.EqualFold(skuName, string(operationalinsights.WorkspaceSkuNameEnumFree)) {
return fmt.Errorf("`Free` tier SKU quota is not configurable and is hard set to 0.5GB")
} else if !strings.EqualFold(skuName, string(operationalinsights.WorkspaceSkuNameEnumFree)) {
parameters.WorkspaceProperties.WorkspaceCapping = &operationalinsights.WorkspaceCapping{
DailyQuotaGb: utils.Float(dailyQuotaGb.(float64)),
}
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
if err != nil {
return err
Expand Down Expand Up @@ -208,7 +215,10 @@ func resourceArmLogAnalyticsWorkspaceRead(d *schema.ResourceData, meta interface
d.Set("sku", sku.Name)
}
d.Set("retention_in_days", resp.RetentionInDays)
if workspaceCapping := resp.WorkspaceCapping; workspaceCapping != nil {
if resp.WorkspaceProperties != nil && resp.WorkspaceProperties.Sku != nil && strings.EqualFold(string(resp.WorkspaceProperties.Sku.Name), string(operationalinsights.WorkspaceSkuNameEnumFree)) {
// Special case for "Free" tier
d.Set("daily_quota_gb", utils.Float(0.5))
} else if workspaceCapping := resp.WorkspaceCapping; workspaceCapping != nil {
d.Set("daily_quota_gb", resp.WorkspaceCapping.DailyQuotaGb)
} else {
d.Set("daily_quota_gb", utils.Float(-1))
Expand Down Expand Up @@ -263,3 +273,12 @@ func ValidateAzureRmLogAnalyticsWorkspaceName(v interface{}, _ string) (warnings

return warnings, errors
}

func dailyQuotaGbDiffSuppressFunc(_, _, _ string, d *schema.ResourceData) bool {
// (@jackofallops) - 'free' is a legacy special case that is always set to 0.5GB
if skuName := d.Get("sku").(string); strings.EqualFold(skuName, string(operationalinsights.WorkspaceSkuNameEnumFree)) {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,33 @@ func TestAccAzureRMLogAnalyticsWorkspace_withVolumeCap(t *testing.T) {
})
}

func TestAccAzureRMLogAnalyticsWorkspace_removeVolumeCap(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_log_analytics_workspace", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLogAnalyticsWorkspace_withVolumeCap(data, 5.5),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsWorkspaceExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMLogAnalyticsWorkspace_removeVolumeCap(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsWorkspaceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "daily_quota_gb", "-1"),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMLogAnalyticsWorkspaceDestroy(s *terraform.State) error {
conn := acceptance.AzureProvider.Meta().(*clients.Client).LogAnalytics.WorkspacesClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -352,3 +379,28 @@ resource "azurerm_log_analytics_workspace" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, volumeCapGb)
}

func testAccAzureRMLogAnalyticsWorkspace_removeVolumeCap(data acceptance.TestData) 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"
retention_in_days = 30

tags = {
Environment = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
6 changes: 5 additions & 1 deletion website/docs/r/log_analytics_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ The following arguments are supported:

~> **NOTE:** A new pricing model took effect on `2018-04-03`, which requires the SKU `PerGB2018`. If you're provisioned resources before this date you have the option of remaining with the previous Pricing SKU and using the other SKU's defined above. More information about [the Pricing SKU's is available at the following URI](http://aka.ms/PricingTierWarning).

~> **NOTE:** The `Free` SKU has a default `daily_quota_gb` value of `0.5` (GB).

* `retention_in_days` - (Optional) The workspace data retention in days. Possible values are either 7 (Free Tier only) or range between 30 and 730.

* `daily_quota_gb` - (Optional) The workspace daily quota for ingestion in GB. Defaults to -1 (unlimited).
* `daily_quota_gb` - (Optional) The workspace daily quota for ingestion in GB. Defaults to -1 (unlimited) if omitted.

~> **NOTE:** When `sku_name` is set to `Free` this field can be set to a maximum of `0.5` (GB), and has a default value of `0.5`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

Expand Down