From 1ce7372a806a970acc9078ee14139a7d9adebe6d Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 20 Nov 2019 00:01:21 -0600 Subject: [PATCH 1/6] add sampling_percentage arg for azurerm_application_insights --- azurerm/resource_arm_application_insights.go | 13 +++++ .../resource_arm_application_insights_test.go | 50 +++++++++++++++++++ .../docs/r/application_insights.html.markdown | 2 + 3 files changed, 65 insertions(+) diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index 04e1a01a1b95..4cd00d37a6b7 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -63,6 +63,11 @@ func resourceArmApplicationInsights() *schema.Resource { }, true), }, + "sampling_percentage": { + Type: schema.TypeFloat, + Optional: true, + }, + "tags": tags.Schema(), "app_id": { @@ -111,6 +116,10 @@ func resourceArmApplicationInsightsCreateUpdate(d *schema.ResourceData, meta int ApplicationType: insights.ApplicationType(applicationType), } + if v, ok := d.GetOk("sampling_percentage"); ok { + applicationInsightsComponentProperties.SamplingPercentage = utils.Float(v.(float64)) + } + insightProperties := insights.ApplicationInsightsComponent{ Name: &name, Location: &location, @@ -176,6 +185,10 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{} d.Set("application_type", string(props.ApplicationType)) d.Set("app_id", props.AppID) d.Set("instrumentation_key", props.InstrumentationKey) + + if v := props.SamplingPercentage; v != nil { + d.Set("sampling_percentage", v) + } } return tags.FlattenAndSet(d, resp.Tags) diff --git a/azurerm/resource_arm_application_insights_test.go b/azurerm/resource_arm_application_insights_test.go index 7b0e0e3a7006..c92886f815dd 100644 --- a/azurerm/resource_arm_application_insights_test.go +++ b/azurerm/resource_arm_application_insights_test.go @@ -279,6 +279,35 @@ func testCheckAzureRMApplicationInsightsExists(resourceName string) resource.Tes } } +func TestAccAzureRMApplicationInsights_complete(t *testing.T) { + resourceName := "azurerm_application_insights.test" + ri := tf.AccRandTimeInt() + config := testAccAzureRMApplicationInsights_complete(ri, testLocation(), "web") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMApplicationInsightsDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightsExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "application_type", "web"), + resource.TestCheckResourceAttr(resourceName, "sampling_percentage", "50"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.Hello", "World"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccAzureRMApplicationInsights_basic(rInt int, location string, applicationType string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { @@ -308,3 +337,24 @@ resource "azurerm_application_insights" "import" { } `, template) } + +func testAccAzureRMApplicationInsights_complete(rInt int, location string, applicationType string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +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" + sampling_percentage = 50 + + tags = { + Hello = "World" + } +} +`, rInt, location, rInt, applicationType) +} diff --git a/website/docs/r/application_insights.html.markdown b/website/docs/r/application_insights.html.markdown index 80922b18dc3c..4d587f7fb281 100644 --- a/website/docs/r/application_insights.html.markdown +++ b/website/docs/r/application_insights.html.markdown @@ -49,6 +49,8 @@ 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. +* `sampling_percentage` - (Optional) Specifies the percentage of the data produced by the application being monitored that is being sampled for Application Insights telemetry. + * `tags` - (Optional) A mapping of tags to assign to the resource. ## Attributes Reference From ad16f225c51b2191766a7fc6819b0a181b57446e Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 20 Nov 2019 00:06:58 -0600 Subject: [PATCH 2/6] reword sampling_percentage description --- website/docs/r/application_insights.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/application_insights.html.markdown b/website/docs/r/application_insights.html.markdown index 4d587f7fb281..062259870be0 100644 --- a/website/docs/r/application_insights.html.markdown +++ b/website/docs/r/application_insights.html.markdown @@ -49,7 +49,7 @@ 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. -* `sampling_percentage` - (Optional) Specifies the percentage of the data produced by the application being monitored that is being sampled for Application Insights telemetry. +* `sampling_percentage` - (Optional) Specifies the percentage of the data produced by the monitored application that is sampled for Application Insights telemetry. * `tags` - (Optional) A mapping of tags to assign to the resource. From 4d50c8321f26b8df2020d8c9129e3443ac83b732 Mon Sep 17 00:00:00 2001 From: aqche <39076898+aqche@users.noreply.github.com> Date: Wed, 20 Nov 2019 19:04:58 -0600 Subject: [PATCH 3/6] no nil check before setting sampling_percentage needed Co-Authored-By: Tom Harvey --- azurerm/resource_arm_application_insights.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index 4cd00d37a6b7..9855bdb78428 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -188,7 +188,7 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{} if v := props.SamplingPercentage; v != nil { d.Set("sampling_percentage", v) - } + d.Set("sampling_percentage", props.SamplingPercentage) } return tags.FlattenAndSet(d, resp.Tags) From 8c1fbaefd593472cc73815e5c11d7fc322decb49 Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 20 Nov 2019 19:06:29 -0600 Subject: [PATCH 4/6] make sampling_percentage default to 100 --- azurerm/resource_arm_application_insights.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index 4cd00d37a6b7..e82a0cf2d14b 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -66,6 +66,7 @@ func resourceArmApplicationInsights() *schema.Resource { "sampling_percentage": { Type: schema.TypeFloat, Optional: true, + Default: 100, }, "tags": tags.Schema(), @@ -108,16 +109,14 @@ func resourceArmApplicationInsightsCreateUpdate(d *schema.ResourceData, meta int } applicationType := d.Get("application_type").(string) + samplingPercentage := utils.Float(d.Get("sampling_percentage").(float64)) location := azure.NormalizeLocation(d.Get("location").(string)) t := d.Get("tags").(map[string]interface{}) applicationInsightsComponentProperties := insights.ApplicationInsightsComponentProperties{ - ApplicationID: &name, - ApplicationType: insights.ApplicationType(applicationType), - } - - if v, ok := d.GetOk("sampling_percentage"); ok { - applicationInsightsComponentProperties.SamplingPercentage = utils.Float(v.(float64)) + ApplicationID: &name, + ApplicationType: insights.ApplicationType(applicationType), + SamplingPercentage: samplingPercentage, } insightProperties := insights.ApplicationInsightsComponent{ From 46dcd924838475737deb41956fa2f44d4bdaf72d Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 20 Nov 2019 19:08:48 -0600 Subject: [PATCH 5/6] fix syntax errors around setting sampling_percentage --- azurerm/resource_arm_application_insights.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index b98bbb8facb8..4b97e7ccce77 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -184,9 +184,6 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{} d.Set("application_type", string(props.ApplicationType)) d.Set("app_id", props.AppID) d.Set("instrumentation_key", props.InstrumentationKey) - - if v := props.SamplingPercentage; v != nil { - d.Set("sampling_percentage", v) d.Set("sampling_percentage", props.SamplingPercentage) } From ebd76a47a63cba64292d0fc2afb20396a5ac2f4a Mon Sep 17 00:00:00 2001 From: Austin Date: Thu, 21 Nov 2019 00:36:45 -0600 Subject: [PATCH 6/6] validate sampling_percentage between 0 and 100 --- azurerm/resource_arm_application_insights.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index 4b97e7ccce77..dba18cf56124 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -64,9 +64,10 @@ func resourceArmApplicationInsights() *schema.Resource { }, "sampling_percentage": { - Type: schema.TypeFloat, - Optional: true, - Default: 100, + Type: schema.TypeFloat, + Optional: true, + Default: 100, + ValidateFunc: validation.FloatBetween(0, 100), }, "tags": tags.Schema(),