Skip to content

Commit

Permalink
Fix issue #584 add option to set AI daily cap and hit daily cap… (#5480)
Browse files Browse the repository at this point in the history
This PR should fix #584, so we can set now a daily cap for Applications Insight in GB. Beside setting a daily cap for Applications Insight it's also possible now to disable notifications if daily cap is hit.

Compared to other resources this configuration is separated to it's own billing feature api. Which requires to update billing feature api once Application Insight was created.
  • Loading branch information
Nick Metz authored Jan 31, 2020
1 parent adb682d commit 0446b69
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Client struct {
APIKeysClient *insights.APIKeysClient
ComponentsClient *insights.ComponentsClient
WebTestsClient *insights.WebTestsClient
BillingClient *insights.ComponentCurrentBillingFeaturesClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -25,10 +26,14 @@ func NewClient(o *common.ClientOptions) *Client {
webTestsClient := insights.NewWebTestsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&webTestsClient.Client, o.ResourceManagerAuthorizer)

billingClient := insights.NewComponentCurrentBillingFeaturesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&billingClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AnalyticsItemsClient: &analyticsItemsClient,
APIKeysClient: &apiKeysClient,
ComponentsClient: &componentsClient,
WebTestsClient: &webTestsClient,
BillingClient: &billingClient,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ func resourceArmApplicationInsights() *schema.Resource {

"tags": tags.Schema(),

"daily_data_cap_in_gb": {
Type: schema.TypeFloat,
Optional: true,
Computed: true,
ValidateFunc: validation.FloatBetween(0, 1000),
},

"daily_data_cap_notifications_disabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},

"app_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -105,7 +118,9 @@ func resourceArmApplicationInsights() *schema.Resource {

func resourceArmApplicationInsightsCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppInsights.ComponentsClient
billingClient := meta.(*clients.Client).AppInsights.BillingClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)

defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Application Insights creation.")
Expand Down Expand Up @@ -167,13 +182,36 @@ func resourceArmApplicationInsightsCreateUpdate(d *schema.ResourceData, meta int
return fmt.Errorf("Cannot read AzureRM Application Insights '%s' (Resource Group %s) ID", name, resGroup)
}

billingRead, err := billingClient.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error read Application Insights Billing Features %q (Resource Group %q): %+v", name, resGroup, err)
}

applicationInsightsComponentBillingFeatures := insights.ApplicationInsightsComponentBillingFeatures{
CurrentBillingFeatures: billingRead.CurrentBillingFeatures,
DataVolumeCap: billingRead.DataVolumeCap,
}

if v, ok := d.GetOk("daily_data_cap_in_gb"); ok {
applicationInsightsComponentBillingFeatures.DataVolumeCap.Cap = utils.Float(v.(float64))
}

if v, ok := d.GetOk("daily_data_cap_notifications_disabled"); ok {
applicationInsightsComponentBillingFeatures.DataVolumeCap.StopSendNotificationWhenHitCap = utils.Bool(v.(bool))
}

if _, err = billingClient.Update(ctx, resGroup, name, applicationInsightsComponentBillingFeatures); err != nil {
return fmt.Errorf("Error update Application Insights Billing Feature %q (Resource Group %q): %+v", name, resGroup, err)
}

d.SetId(*read.ID)

return resourceArmApplicationInsightsRead(d, meta)
}

func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppInsights.ComponentsClient
billingClient := meta.(*clients.Client).AppInsights.BillingClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -196,6 +234,11 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error making Read request on AzureRM Application Insights '%s': %+v", name, err)
}

billingResp, err := billingClient.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on AzureRM Application Insights Billing Feature '%s': %+v", name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
Expand All @@ -212,6 +255,11 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}
}
}

if billingProps := billingResp.DataVolumeCap; billingProps != nil {
d.Set("daily_data_cap_in_gb", billingProps.Cap)
d.Set("daily_data_cap_notifications_disabled", billingProps.StopSendNotificationWhenHitCap)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func TestAccAzureRMApplicationInsights_complete(t *testing.T) {
resource.TestCheckResourceAttr(data.ResourceName, "application_type", "web"),
resource.TestCheckResourceAttr(data.ResourceName, "retention_in_days", "120"),
resource.TestCheckResourceAttr(data.ResourceName, "sampling_percentage", "50"),
resource.TestCheckResourceAttr(data.ResourceName, "daily_data_cap_in_gb", "50"),
resource.TestCheckResourceAttr(data.ResourceName, "daily_data_cap_notifications_disabled", "true"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.Hello", "World"),
),
Expand Down Expand Up @@ -297,12 +299,14 @@ resource "azurerm_resource_group" "test" {
}
resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
application_type = "%s"
retention_in_days = 120
sampling_percentage = 50
name = "acctestappinsights-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
application_type = "%s"
retention_in_days = 120
sampling_percentage = 50
daily_data_cap_in_gb = 50
daily_data_cap_notifications_disabled = true
tags = {
Hello = "World"
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/application_insights.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ The following arguments are supported:

* `application_type` - (Required) Specifies the type of Application Insights to create. Valid values are `ios` for _iOS_, `java` for _Java web_, `MobileCenter` for _App Center_, `Node.JS` for _Node.js_, `other` for _General_, `phone` for _Windows Phone_, `store` for _Windows Store_ and `web` for _ASP.NET_. Please note these values are case sensitive; unmatched values are treated as _ASP.NET_ by Azure. Changing this forces a new resource to be created.

* `daily_data_cap_in_gb` - (Optional) Specifies the Application Insights component daily data volume cap in GB.

* `daily_data_cap_notifications_disabled` - (Optional) Specifies if a notification email will be send when the daily data volume cap is met.

* `retention_in_days` - (Optional) Specifies the retention period in days. Possible values are `30`, `60`, `90`, `120`, `180`, `270`, `365`, `550` or `730`.

* `sampling_percentage` - (Optional) Specifies the percentage of the data produced by the monitored application that is sampled for Application Insights telemetry.
Expand Down

0 comments on commit 0446b69

Please sign in to comment.