From cc14b887bcb5aa4bd1e878af86f1f2f97a0be157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Tue, 8 Jan 2019 16:40:16 -0300 Subject: [PATCH 1/7] Add Application Insights data source This simple data source allows to return the ID and Instrumentation Key for a given App Insights bucket name. --- azurerm/data_source_application_insights.go | 51 +++++++++++++++++++ .../data_source_application_insights_test.go | 39 ++++++++++++++ azurerm/provider.go | 1 + 3 files changed, 91 insertions(+) create mode 100644 azurerm/data_source_application_insights.go create mode 100644 azurerm/data_source_application_insights_test.go diff --git a/azurerm/data_source_application_insights.go b/azurerm/data_source_application_insights.go new file mode 100644 index 000000000000..c4a664197551 --- /dev/null +++ b/azurerm/data_source_application_insights.go @@ -0,0 +1,51 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmApplicationInsights() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmApplicationInsightsRead, + Schema: map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + + "instrumentation_key": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).appInsightsClient + ctx := meta.(*ArmClient).StopContext + + resGroup := d.Get("resource_group_name").(string) + name := d.Get("name").(string) + + resp, err := client.Get(ctx, resGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Application Insights bucket %q (Resource Group %q) was not found", name, resGroup) + } + + return fmt.Errorf("Error making Read request on Application Insights bucket %q (Resource Group %q): %+v", name, resGroup, err) + } + + d.SetId(*resp.ID) + d.Set("instrumentation_key", *resp.InstrumentationKey) + + return nil +} diff --git a/azurerm/data_source_application_insights_test.go b/azurerm/data_source_application_insights_test.go new file mode 100644 index 000000000000..cadd6cb74bb4 --- /dev/null +++ b/azurerm/data_source_application_insights_test.go @@ -0,0 +1,39 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceApplicationInsights_basic(t *testing.T) { + dataSourceName := "data.azurerm_application_insights.test" + ri := acctest.RandInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccResourceApplicationInsights_complete(ri, location), + Check: resource.TestCheckResourceAttrSet(dataSourceName, "instrumentation_key"), + }, + }, + }) +} + +func testAccResourceApplicationInsights_complete(rInt int, location string) string { + resource := testAccAzureRMApplicationInsights_basic(rInt, location, "other") + + return fmt.Sprintf(` + %s + +data "azurerm_application_insights" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_application_insights.test.name}" +} +`, resource) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index ba72b793742f..13b180ea77ef 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -143,6 +143,7 @@ func Provider() terraform.ResourceProvider { "azurerm_virtual_machine": dataSourceArmVirtualMachine(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), + "azurerm_application_insights": dataSourceArmApplicationInsights(), }, ResourcesMap: map[string]*schema.Resource{ From 4ae55646d8059ed6c83e17df5bba253c92d898de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Tue, 8 Jan 2019 16:56:34 -0300 Subject: [PATCH 2/7] Add documentation for app insights data source --- website/azurerm.erb | 4 +++ .../docs/d/application_insights.html.markdown | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 website/docs/d/application_insights.html.markdown diff --git a/website/azurerm.erb b/website/azurerm.erb index 42ef3e286552..31667784ca49 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -60,6 +60,10 @@ azurerm_app_service_plan + > + azurerm_application_insights + + > azurerm_azuread_application diff --git a/website/docs/d/application_insights.html.markdown b/website/docs/d/application_insights.html.markdown new file mode 100644 index 000000000000..ca0566ac4f5d --- /dev/null +++ b/website/docs/d/application_insights.html.markdown @@ -0,0 +1,34 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_application_insights" +sidebar_current: "docs-azurerm-datasource-application-insights" +description: |- + Gets information about an existing Application Insights component. +--- + +# Data Source: azurerm_application_insights + +Use this data source to access information about an existing Application Insights component. + +## Example Usage + +```hcl +data "azurerm_application_insights" "test" { + name = "production" + resource_group_name = "networking" +} + +output "application_insights_instrumentation_key" { + value = "${data.azurerm_application_insights.test.instrumentation_key}" +} +``` + +## Argument Reference + +* `name` - (Required) Specifies the name of the Application Insights component. +* `resource_group_name` - (Required) Specifies the name of the resource group the Application Insights component is located in. + +## Attributes Reference + +* `id` - The ID of the Virtual Machine. +* `instrumentation_key` - The instrumentation key of the Application Insights component. From 9898fc3ea9d742dea8b7fd2bea46c41b91406445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 9 Jan 2019 14:41:14 -0300 Subject: [PATCH 3/7] Add additional attributes on app insights data source --- azurerm/data_source_application_insights.go | 24 +++++++++++++++ .../data_source_application_insights_test.go | 29 +++++++++++++++---- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/azurerm/data_source_application_insights.go b/azurerm/data_source_application_insights.go index c4a664197551..366db75fae7a 100644 --- a/azurerm/data_source_application_insights.go +++ b/azurerm/data_source_application_insights.go @@ -24,6 +24,26 @@ func dataSourceArmApplicationInsights() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "location": { + Type: schema.TypeString, + Computed: true, + }, + + "application_type": { + Type: schema.TypeString, + Computed: true, + }, + + "app_id": { + Type: schema.TypeString, + Computed: true, + }, + + "tags": { + Type: schema.TypeMap, + Computed: true, + }, }, } } @@ -46,6 +66,10 @@ func dataSourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface d.SetId(*resp.ID) d.Set("instrumentation_key", *resp.InstrumentationKey) + d.Set("location", *resp.Location) + d.Set("app_id", *resp.AppID) + d.Set("application_type", resp.ApplicationType) + d.Set("tags", resp.Tags) return nil } diff --git a/azurerm/data_source_application_insights_test.go b/azurerm/data_source_application_insights_test.go index cadd6cb74bb4..374729de2d93 100644 --- a/azurerm/data_source_application_insights_test.go +++ b/azurerm/data_source_application_insights_test.go @@ -19,21 +19,40 @@ func TestAccDataSourceApplicationInsights_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccResourceApplicationInsights_complete(ri, location), - Check: resource.TestCheckResourceAttrSet(dataSourceName, "instrumentation_key"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "instrumentation_key"), + resource.TestCheckResourceAttrSet(dataSourceName, "app_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "location"), + resource.TestCheckResourceAttr(dataSourceName, "application_type", "other"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(dataSourceName, "tags.foo", "bar"), + ), }, }, }) } func testAccResourceApplicationInsights_complete(rInt int, location string) string { - resource := testAccAzureRMApplicationInsights_basic(rInt, location, "other") - return fmt.Sprintf(` - %s +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_application_insights" "test" { + name = "acctestappinsights-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + application_type = "other" + + tags { + "foo" = "bar" + } +} data "azurerm_application_insights" "test" { resource_group_name = "${azurerm_resource_group.test.name}" name = "${azurerm_application_insights.test.name}" } -`, resource) +`, rInt, location) } From 17741a20fe28eae624c02be99670b88def48ef11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 9 Jan 2019 22:33:50 -0300 Subject: [PATCH 4/7] Validate against empty strings instead of zero value --- azurerm/data_source_application_insights.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/data_source_application_insights.go b/azurerm/data_source_application_insights.go index 366db75fae7a..e0c7dfc6dad9 100644 --- a/azurerm/data_source_application_insights.go +++ b/azurerm/data_source_application_insights.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -17,7 +17,7 @@ func dataSourceArmApplicationInsights() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.NoZeroValues, + ValidateFunc: validate.NoEmptyStrings, }, "instrumentation_key": { From 1dd1d93fcd33242fb04b91d72a8601e04ed13827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 9 Jan 2019 22:37:07 -0300 Subject: [PATCH 5/7] Let ResourceData.Set handle dereferencing pointers This also makes the syntax more consistent when using either strings or maps. Thanks @kt for the tip! --- azurerm/data_source_application_insights.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azurerm/data_source_application_insights.go b/azurerm/data_source_application_insights.go index e0c7dfc6dad9..58df488b7468 100644 --- a/azurerm/data_source_application_insights.go +++ b/azurerm/data_source_application_insights.go @@ -65,9 +65,9 @@ func dataSourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface } d.SetId(*resp.ID) - d.Set("instrumentation_key", *resp.InstrumentationKey) - d.Set("location", *resp.Location) - d.Set("app_id", *resp.AppID) + d.Set("instrumentation_key", resp.InstrumentationKey) + d.Set("location", resp.Location) + d.Set("app_id", resp.AppID) d.Set("application_type", resp.ApplicationType) d.Set("tags", resp.Tags) From 9a082d4281b38fafcec5a14e73946dbbc5229df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 9 Jan 2019 22:39:49 -0300 Subject: [PATCH 6/7] Use helper to set tags --- azurerm/data_source_application_insights.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/data_source_application_insights.go b/azurerm/data_source_application_insights.go index 58df488b7468..b6cc24266773 100644 --- a/azurerm/data_source_application_insights.go +++ b/azurerm/data_source_application_insights.go @@ -69,7 +69,7 @@ func dataSourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface d.Set("location", resp.Location) d.Set("app_id", resp.AppID) d.Set("application_type", resp.ApplicationType) - d.Set("tags", resp.Tags) + flattenAndSetTags(d, resp.Tags) return nil } From 601fdb99591f5314de1efabc246bf63d4c1256dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 9 Jan 2019 22:46:54 -0300 Subject: [PATCH 7/7] Update App Insights data source documentation --- website/docs/d/application_insights.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/d/application_insights.html.markdown b/website/docs/d/application_insights.html.markdown index ca0566ac4f5d..e499b257802f 100644 --- a/website/docs/d/application_insights.html.markdown +++ b/website/docs/d/application_insights.html.markdown @@ -31,4 +31,8 @@ output "application_insights_instrumentation_key" { ## Attributes Reference * `id` - The ID of the Virtual Machine. +* `app_id` - The App ID associated with this Application Insights component. +* `application_type` - The type of the component. * `instrumentation_key` - The instrumentation key of the Application Insights component. +* `location` - The Azure location where the component exists. +* `tags` - Tags applied to the component.