diff --git a/azurerm/internal/services/applicationinsights/client/client.go b/azurerm/internal/services/applicationinsights/client/client.go index 6e4466f4dff4..17d8bf3fd68a 100644 --- a/azurerm/internal/services/applicationinsights/client/client.go +++ b/azurerm/internal/services/applicationinsights/client/client.go @@ -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 { @@ -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, } } diff --git a/azurerm/internal/services/applicationinsights/resource_arm_application_insights.go b/azurerm/internal/services/applicationinsights/resource_arm_application_insights.go index 272e29f1b5d5..d596b74ee05d 100644 --- a/azurerm/internal/services/applicationinsights/resource_arm_application_insights.go +++ b/azurerm/internal/services/applicationinsights/resource_arm_application_insights.go @@ -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, @@ -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.") @@ -167,6 +182,28 @@ 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) @@ -174,6 +211,7 @@ func resourceArmApplicationInsightsCreateUpdate(d *schema.ResourceData, meta int 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() @@ -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 { @@ -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) } diff --git a/azurerm/internal/services/applicationinsights/tests/resource_arm_application_insights_test.go b/azurerm/internal/services/applicationinsights/tests/resource_arm_application_insights_test.go index dbec9f93435b..6d742ed2b804 100644 --- a/azurerm/internal/services/applicationinsights/tests/resource_arm_application_insights_test.go +++ b/azurerm/internal/services/applicationinsights/tests/resource_arm_application_insights_test.go @@ -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"), ), @@ -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" diff --git a/website/docs/r/application_insights.html.markdown b/website/docs/r/application_insights.html.markdown index 328d71885eee..b8e3b0b402f3 100644 --- a/website/docs/r/application_insights.html.markdown +++ b/website/docs/r/application_insights.html.markdown @@ -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.