Skip to content

Commit

Permalink
Merge pull request #2625 from citrusbyte/feature/appinsights-data-source
Browse files Browse the repository at this point in the history
Add application insights data source
  • Loading branch information
katbyte authored Jan 10, 2019
2 parents f5e4719 + 601fdb9 commit 97973d3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
75 changes: 75 additions & 0 deletions azurerm/data_source_application_insights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"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: validate.NoEmptyStrings,
},

"instrumentation_key": {
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,
},
},
}
}

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)
d.Set("location", resp.Location)
d.Set("app_id", resp.AppID)
d.Set("application_type", resp.ApplicationType)
flattenAndSetTags(d, resp.Tags)

return nil
}
58 changes: 58 additions & 0 deletions azurerm/data_source_application_insights_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.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 {
return fmt.Sprintf(`
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}"
}
`, rInt, location)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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{
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<a href="/docs/providers/azurerm/d/app_service_plan.html">azurerm_app_service_plan</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-application-insights") %>>
<a href="/docs/providers/azurerm/d/application_insights.html">azurerm_application_insights</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-azuread-application") %>>
<a href="/docs/providers/azurerm/d/azuread_application.html">azurerm_azuread_application</a>
</li>
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/application_insights.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
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.
* `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.

0 comments on commit 97973d3

Please sign in to comment.