Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application insights data source #2625

Merged
merged 7 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor could we sort this alphabetically to match the rest of this list? it helps when skimming it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. I did an alphabetical sort of this list and there actually many elements out of order:

diff --git a/azurerm/provider.go b/azurerm/provider.go
index 13b180ea..842c4630 100644
--- a/azurerm/provider.go
+++ b/azurerm/provider.go
@@ -89,27 +89,28 @@ func Provider() terraform.ResourceProvider {
 		},
 
 		DataSourcesMap: map[string]*schema.Resource{
-			"azurerm_azuread_application":                   dataSourceArmAzureADApplication(),
-			"azurerm_azuread_service_principal":             dataSourceArmActiveDirectoryServicePrincipal(),
 			"azurerm_api_management":                        dataSourceApiManagementService(),
-			"azurerm_application_security_group":            dataSourceArmApplicationSecurityGroup(),
-			"azurerm_app_service":                           dataSourceArmAppService(),
 			"azurerm_app_service_plan":                      dataSourceAppServicePlan(),
+			"azurerm_app_service":                           dataSourceArmAppService(),
+			"azurerm_application_insights":                  dataSourceArmApplicationInsights(),
+			"azurerm_application_security_group":            dataSourceArmApplicationSecurityGroup(),
+			"azurerm_azuread_application":                   dataSourceArmAzureADApplication(),
+			"azurerm_azuread_service_principal":             dataSourceArmActiveDirectoryServicePrincipal(),
 			"azurerm_batch_account":                         dataSourceArmBatchAccount(),
 			"azurerm_builtin_role_definition":               dataSourceArmBuiltInRoleDefinition(),
 			"azurerm_cdn_profile":                           dataSourceArmCdnProfile(),
 			"azurerm_client_config":                         dataSourceArmClientConfig(),
-			"azurerm_cosmosdb_account":                      dataSourceArmCosmosDBAccount(),
 			"azurerm_container_registry":                    dataSourceArmContainerRegistry(),
+			"azurerm_cosmosdb_account":                      dataSourceArmCosmosDBAccount(),
 			"azurerm_data_lake_store":                       dataSourceArmDataLakeStoreAccount(),
 			"azurerm_dev_test_lab":                          dataSourceArmDevTestLab(),
 			"azurerm_dns_zone":                              dataSourceArmDnsZone(),
 			"azurerm_eventhub_namespace":                    dataSourceEventHubNamespace(),
 			"azurerm_image":                                 dataSourceArmImage(),
-			"azurerm_key_vault":                             dataSourceArmKeyVault(),
-			"azurerm_key_vault_key":                         dataSourceArmKeyVaultKey(),
 			"azurerm_key_vault_access_policy":               dataSourceArmKeyVaultAccessPolicy(),
+			"azurerm_key_vault_key":                         dataSourceArmKeyVaultKey(),
 			"azurerm_key_vault_secret":                      dataSourceArmKeyVaultSecret(),
+			"azurerm_key_vault":                             dataSourceArmKeyVault(),
 			"azurerm_kubernetes_cluster":                    dataSourceArmKubernetesCluster(),
 			"azurerm_log_analytics_workspace":               dataSourceLogAnalyticsWorkspace(),
 			"azurerm_logic_app_workflow":                    dataSourceArmLogicAppWorkflow(),
@@ -120,8 +121,8 @@ func Provider() terraform.ResourceProvider {
 			"azurerm_monitor_log_profile":                   dataSourceArmMonitorLogProfile(),
 			"azurerm_network_interface":                     dataSourceArmNetworkInterface(),
 			"azurerm_network_security_group":                dataSourceArmNetworkSecurityGroup(),
-			"azurerm_notification_hub":                      dataSourceNotificationHub(),
 			"azurerm_notification_hub_namespace":            dataSourceNotificationHubNamespace(),
+			"azurerm_notification_hub":                      dataSourceNotificationHub(),
 			"azurerm_platform_image":                        dataSourceArmPlatformImage(),
 			"azurerm_public_ip":                             dataSourceArmPublicIP(),
 			"azurerm_public_ips":                            dataSourceArmPublicIPs(),
@@ -143,7 +144,6 @@ 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{

Would you prefer leaving sorting all the lines for another PR and just place azurerm_application_insights where it should be, or should I send the change for all the lines? Sorting all the lines is a bit out of scope of this PR, IMO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another PR for this would be great 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll mark this one as resolved and then send a subsequent PR once this gets merged with the sorting. Sounds good?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #2638

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. I did an alphabetical sort of this list and there actually many elements out of order:

@inkel sorry I didn't realise they weren't in order! Thanks for #2638 in any case, I'll take a look now :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! <3

},

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
inkel marked this conversation as resolved.
Show resolved Hide resolved

* `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.