-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2625 from citrusbyte/feature/appinsights-data-source
Add application insights data source
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |