From 9d509a93055249eb95087793aed06395b4fa8385 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Tue, 17 Sep 2019 16:14:56 +0100 Subject: [PATCH 01/36] Add App Insights AnalyticsItems Client --- .../services/applicationinsights/client.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/azurerm/internal/services/applicationinsights/client.go b/azurerm/internal/services/applicationinsights/client.go index d375a35da727..bb031a26f320 100644 --- a/azurerm/internal/services/applicationinsights/client.go +++ b/azurerm/internal/services/applicationinsights/client.go @@ -6,12 +6,17 @@ import ( ) type Client struct { - APIKeyClient *insights.APIKeysClient - ComponentsClient *insights.ComponentsClient - WebTestsClient *insights.WebTestsClient + AnalyticsItemsClient *insights.AnalyticsItemsClient + APIKeyClient *insights.APIKeysClient + ComponentsClient *insights.ComponentsClient + WebTestsClient *insights.WebTestsClient } func BuildClient(o *common.ClientOptions) *Client { + + AnalyticsItemsClient := insights.NewAnalyticsItemsClient(o.SubscriptionId) + o.ConfigureClient(&AnalyticsItemsClient.Client, o.ResourceManagerAuthorizer) + APIKeyClient := insights.NewAPIKeysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&APIKeyClient.Client, o.ResourceManagerAuthorizer) @@ -22,8 +27,9 @@ func BuildClient(o *common.ClientOptions) *Client { o.ConfigureClient(&WebTestsClient.Client, o.ResourceManagerAuthorizer) return &Client{ - APIKeyClient: &APIKeyClient, - ComponentsClient: &ComponentsClient, - WebTestsClient: &WebTestsClient, + AnalyticsItemsClient: &AnalyticsItemsClient, + APIKeyClient: &APIKeyClient, + ComponentsClient: &ComponentsClient, + WebTestsClient: &WebTestsClient, } } From 83229ee721dcd1685900eaf5176f246dc4531063 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Tue, 17 Sep 2019 16:29:33 +0100 Subject: [PATCH 02/36] Add spec for App Insights AnalyticsItem resource --- azurerm/provider.go | 1 + ...arm_application_insights_analytics_item.go | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 azurerm/resource_arm_application_insights_analytics_item.go diff --git a/azurerm/provider.go b/azurerm/provider.go index c3447f6b81b9..6242d71d70fb 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -175,6 +175,7 @@ func Provider() terraform.ResourceProvider { "azurerm_application_gateway": resourceArmApplicationGateway(), "azurerm_application_insights_api_key": resourceArmApplicationInsightsAPIKey(), "azurerm_application_insights": resourceArmApplicationInsights(), + "azurerm_application_insights_analytics_item": resourceArmApplicationInsightsAnalyticsItem(), "azurerm_application_insights_web_test": resourceArmApplicationInsightsWebTests(), "azurerm_application_security_group": resourceArmApplicationSecurityGroup(), "azurerm_automation_account": resourceArmAutomationAccount(), diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go new file mode 100644 index 000000000000..878977f36b36 --- /dev/null +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -0,0 +1,105 @@ +package azurerm + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + + "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" +) + +func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { + return &schema.Resource{ + Create: resourceArmApplicationInsightsAnalyticsItemCreate, + Read: resourceArmApplicationInsightsAnalyticsItemRead, + Update: resourceArmApplicationInsightsAnalyticsItemUpdate, + Delete: resourceArmApplicationInsightsAnalyticsItemDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "application_insights_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: azure.ValidateResourceID, + }, + + "version": { + Type: schema.TypeString, + Computed: true, + }, + + "item_id": { + Type: schema.TypeString, + Computed: true, + }, + + "content": { + Type: schema.TypeString, + Required: true, + }, + + "scope": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(insights.ItemScopeShared), + string(insights.ItemScopeUser), + }, false), + }, + + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(insights.Query), + string(insights.Function), + string(insights.Folder), + string(insights.Recent), + }, false), + }, + + "time_created": { + Type: schema.TypeString, + Computed: true, + }, + + "time_modified": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceArmApplicationInsightsAnalyticsItemCreate(d *schema.ResourceData, meta interface{}) error { + return resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d, meta, false) +} +func resourceArmApplicationInsightsAnalyticsItemUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d, meta, true) +} +func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceData, meta interface{}, overwrite bool) error { + return fmt.Errorf("Not implemented") +} + +func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, meta interface{}) error { + return fmt.Errorf("Not implemented") +} + +func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, meta interface{}) error { + return fmt.Errorf("Not implemented") +} From 9be3733acfd74bbb7d99d7e1756bceddbfd966c2 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Wed, 18 Sep 2019 09:37:18 +0100 Subject: [PATCH 03/36] Add basic test --- ...pplication_insights_analytics_item_test.go | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 azurerm/resource_arm_application_insights_analytics_item_test.go diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go new file mode 100644 index 000000000000..b920bbadec3f --- /dev/null +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -0,0 +1,112 @@ +package azurerm + +import ( + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + + "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" +) + +func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { + resourceName := "azurerm_application_insights_analytics_item.test" + ri := tf.AccRandTimeInt() + config := testAccAzureRMApplicationInsightsAnalyticsItem_basic(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "scope", "shared"), + resource.TestCheckResourceAttr(resourceName, "type", "query"), + resource.TestCheckResourceAttr(resourceName, "content", "requests #test"), + ), + }, + }, + }) +} + +func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_application_insights_analytics_item" { + continue + } + + name := rs.Primary.Attributes["name"] + resGroup := rs.Primary.Attributes["resource_group_name"] + + resp, err := conn.Get(ctx, resGroup, name, insights.AnalyticsItems, "", "testquery") + + if err != nil { + return nil + } + + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("Application Insights AnalyticsItem still exists:\n%#v", resp) + } + } + + return nil +} + +func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + name := rs.Primary.Attributes["name"] + resGroup := rs.Primary.Attributes["resource_group_name"] + conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := conn.Get(ctx, resGroup, name, insights.AnalyticsItems, "", "testquery") + if err != nil { + return fmt.Errorf("Bad: Get on appInsightsAnalyticsItemsClient: %+v", err) + } + + if resp.StatusCode == http.StatusNotFound { + return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' (resource group: '%q') does not exist", name, resGroup) + } + + return nil + } +} + +func testAccAzureRMApplicationInsightsAnalyticsItem_basic(rInt int, location 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 = "web" +} + +resource "azurerm_application_insights_analytics_item" "test" { + name = "testquery" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #test" + scope = "shared" + type = "query" +} +`, rInt, location, rInt) +} From ede1e7a5b36c1bed6583f77902845f449dd22ef9 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Wed, 18 Sep 2019 15:42:42 +0100 Subject: [PATCH 04/36] Implement resource --- ...arm_application_insights_analytics_item.go | 104 ++++++++++++++++-- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 878977f36b36..9cc6e5e2afcf 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -41,11 +41,6 @@ func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { Computed: true, }, - "item_id": { - Type: schema.TypeString, - Computed: true, - }, - "content": { Type: schema.TypeString, Required: true, @@ -93,13 +88,106 @@ func resourceArmApplicationInsightsAnalyticsItemUpdate(d *schema.ResourceData, m return resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d, meta, true) } func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceData, meta interface{}, overwrite bool) error { - return fmt.Errorf("Not implemented") + client := meta.(*ArmClient).appInsights.AnalyticsItemsClient + ctx := meta.(*ArmClient).StopContext + + resourceGroupName := d.Get("resource_group_name").(string) + appInsightsID := d.Get("application_insights_id").(string) + + id, err := azure.ParseAzureResourceID(appInsightsID) + if err != nil { + return fmt.Errorf("Error parsing resource ID: %s", err) + } + + appInsightsName := id.Path["components"] + + name := d.Get("name").(string) + content := d.Get("content").(string) + scopeName := d.Get("scope").(string) + typeName := d.Get("type").(string) + + properties := insights.ApplicationInsightsComponentAnalyticsItem{ + Name: &name, + Type: insights.ItemType(typeName), + Scope: insights.ItemScope(scopeName), + Content: &content, + } + result, err := client.Put(ctx, resourceGroupName, appInsightsName, insights.AnalyticsItems, properties, &overwrite) + if err != nil { + return fmt.Errorf("Error Putting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) + } + + d.SetId(*result.ID) + + return resourceArmApplicationInsightsAnalyticsItemRead(d, meta) } func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, meta interface{}) error { - return fmt.Errorf("Not implemented") + client := meta.(*ArmClient).appInsights.AnalyticsItemsClient + ctx := meta.(*ArmClient).StopContext + + resourceGroupName := d.Get("resource_group_name").(string) + appInsightsID := d.Get("application_insights_id").(string) + scopeName := d.Get("scope").(string) + itemID := d.Id() + + id, err := azure.ParseAzureResourceID(appInsightsID) + if err != nil { + return fmt.Errorf("Error parsing resource ID: %s", err) + } + + appInsightsName := id.Path["components"] + name := d.Get("name").(string) + + var scopePath insights.ItemScopePath + if scopeName == "user" { + scopePath = insights.MyanalyticsItems + } else { + scopePath = insights.AnalyticsItems + } + result, err := client.Get(ctx, resourceGroupName, appInsightsName, scopePath, itemID, name) + if err != nil { + return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) + } + + d.Set("version", result.Version) + d.Set("content", result.Content) + d.Set("scope", string(result.Scope)) + d.Set("type", string(result.Type)) + d.Set("time_created", result.TimeCreated) + d.Set("time_modified", result.TimeModified) + + return nil } func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, meta interface{}) error { - return fmt.Errorf("Not implemented") + + client := meta.(*ArmClient).appInsights.AnalyticsItemsClient + ctx := meta.(*ArmClient).StopContext + + resourceGroupName := d.Get("resource_group_name").(string) + appInsightsID := d.Get("application_insights_id").(string) + scopeName := d.Get("scope").(string) + itemID := d.Id() + + id, err := azure.ParseAzureResourceID(appInsightsID) + if err != nil { + return fmt.Errorf("Error parsing resource ID: %s", err) + } + + appInsightsName := id.Path["components"] + name := d.Get("name").(string) + + var scopePath insights.ItemScopePath + if scopeName == "user" { + scopePath = insights.MyanalyticsItems + } else { + scopePath = insights.AnalyticsItems + } + _, err = client.Delete(ctx, resourceGroupName, appInsightsName, scopePath, itemID, name) + if err != nil { + return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) + } + + return nil } From f669c62b3ef0edbe6e12af732d5ce33736411e6f Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Wed, 18 Sep 2019 16:38:09 +0100 Subject: [PATCH 05/36] Get test working --- ...pplication_insights_analytics_item_test.go | 87 ++++++++++++------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index b920bbadec3f..d6f01ae106bb 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" @@ -20,12 +21,13 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy, + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery"), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery"), + resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), resource.TestCheckResourceAttr(resourceName, "content", "requests #test"), @@ -35,50 +37,42 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { }) } -func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient - ctx := testAccProvider.Meta().(*ArmClient).StopContext - - for _, rs := range s.RootModule().Resources { - if rs.Type != "azurerm_application_insights_analytics_item" { - continue - } - - name := rs.Primary.Attributes["name"] - resGroup := rs.Primary.Attributes["resource_group_name"] - - resp, err := conn.Get(ctx, resGroup, name, insights.AnalyticsItems, "", "testquery") - - if err != nil { - return nil +func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_application_insights_analytics_item" { + continue + } + name := rs.Primary.Attributes["name"] + resGroup := rs.Primary.Attributes["resource_group_name"] + + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName) + if err != nil { + return fmt.Errorf("Error checking if item has been destroyed: %s", err) + } + if exists { + return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' (resource group: '%q') still exists", name, resGroup) + } } - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("Application Insights AnalyticsItem still exists:\n%#v", resp) - } + return nil } - - return nil } -func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { return fmt.Errorf("Not found: %s", resourceName) } - name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient - ctx := testAccProvider.Meta().(*ArmClient).StopContext - resp, err := conn.Get(ctx, resGroup, name, insights.AnalyticsItems, "", "testquery") + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName) if err != nil { - return fmt.Errorf("Bad: Get on appInsightsAnalyticsItemsClient: %+v", err) + return fmt.Errorf("Error checking if item exists: %s", err) } - - if resp.StatusCode == http.StatusNotFound { + if !exists { return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' (resource group: '%q') does not exist", name, resGroup) } @@ -86,6 +80,37 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) } } +func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string) (bool, error) { + resGroup := rs.Primary.Attributes["resource_group_name"] + + appInsightsID := rs.Primary.Attributes["application_insights_id"] + id, err := azure.ParseAzureResourceID(appInsightsID) + if err != nil { + return false, fmt.Errorf("Error parsing resource ID: %s", err) + } + + appInsightsName := id.Path["components"] + + conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + includeContent := false + resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, insights.ItemScopeShared, insights.ItemTypeParameterQuery, &includeContent) + if resp.StatusCode == http.StatusNotFound { + return false, nil + } + if err != nil { + return false, fmt.Errorf("Bad: List on appInsightsAnalyticsItemsClient: %+v", err) + } + for _, item := range *resp.Value { + if *item.Name == queryName && item.Scope == insights.ItemScopeShared { + return true, nil + } + } + + return false, nil +} + func testAccAzureRMApplicationInsightsAnalyticsItem_basic(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From c20b84c91432eb652df2985388fc0bc69b1cd14f Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 10:31:31 +0100 Subject: [PATCH 06/36] Add docs --- website/azurerm.erb | 5 ++ ...tion_insights_analytics_item.html.markdown | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 website/docs/r/application_insights_analytics_item.html.markdown diff --git a/website/azurerm.erb b/website/azurerm.erb index fdb9605fc58b..deb8cabebf17 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -539,6 +539,11 @@ azurerm_application_insights_api_key + +
  • + azurerm_application_insights_analytics_item +
  • +
  • azurerm_application_insights_web_test
  • diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown new file mode 100644 index 000000000000..daa3f52a0efb --- /dev/null +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -0,0 +1,66 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_application_insights_analytics_item" +sidebar_current: "docs-azurerm-resource-application-insights-x" +description: |- + Manages an Application Insights Analytics Item component. +--- + +# azurerm_application_insights_analytics_item + +Manages an Application Insights Analytics Item component. + +## Example Usage + +```terraform +resource "azurerm_resource_group" "test" { + name = "tf-test" + location = "West Europe" +} + +resource "azurerm_application_insights" "test" { + name = "tf-test-appinsights" + location = "West Europe" + resource_group_name = "${azurerm_resource_group.test.name}" + application_type = "web" +} + +resource "azurerm_application_insights_analytics_item" "test" { + name = "testquery" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #simple example query" + scope = "shared" + type = "query" +} +``` + +*********************************************TODO!! + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the Application Insights Analytics Item. Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the resource group in which to create the Application Insights component. + +* `application_insights_id` - (Required) The ID of the Application Insights component on which the Analytics Item exists. Changing this forces a new resource to be created. + +* `type` - (Required) The type of Analytics Item to create. Can be one of `query`, `function`, `folder`, `recent`. Changing this forces a new resource to be created. + +* `scope` - (Required) The scope for the Analytics Item. Can be `shared` or `user`. Changing this forces a new resource to be created. + +* `content` - (Required) The content for the Analytics Item, for example the query text if `type` is `query`. + +TODO!! + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the Application Insights Analytics Item. + +* `time_created` - A string containing the time the Analytics Item was created. + +* `time_modified` - A string containing the time the Analytics Item was last modified. From f3c35f719c9c81eae2d573adbef6fc35c7002eec Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 10:31:49 +0100 Subject: [PATCH 07/36] Extend tests --- ...pplication_insights_analytics_item_test.go | 89 ++++++++++++++++--- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index d6f01ae106bb..67e463aee98b 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -21,12 +21,12 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery"), + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery"), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared), resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), @@ -37,7 +37,40 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { }) } -func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string) resource.TestCheckFunc { +func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { + resourceName1 := "azurerm_application_insights_analytics_item.test1" + resourceName2 := "azurerm_application_insights_analytics_item.test2" + ri := tf.AccRandTimeInt() + config := testAccAzureRMApplicationInsightsAnalyticsItem_multiple(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery1", insights.ItemScopeShared), + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery2", insights.ItemScopeUser), + ), + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1, "testquery1", insights.ItemScopeShared), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2, "testquery2", insights.ItemScopeUser), + resource.TestCheckResourceAttr(resourceName1, "name", "testquery1"), + resource.TestCheckResourceAttr(resourceName1, "scope", "shared"), + resource.TestCheckResourceAttr(resourceName1, "type", "query"), + resource.TestCheckResourceAttr(resourceName1, "content", "requests #test1"), + resource.TestCheckResourceAttr(resourceName2, "name", "testquery2"), + resource.TestCheckResourceAttr(resourceName2, "scope", "user"), + resource.TestCheckResourceAttr(resourceName2, "type", "query"), + resource.TestCheckResourceAttr(resourceName2, "content", "requests #test2"), + ), + }, + }, + }) +} + +func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, itemScope insights.ItemScope) resource.TestCheckFunc { return func(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_application_insights_analytics_item" { @@ -46,7 +79,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string) re name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope) if err != nil { return fmt.Errorf("Error checking if item has been destroyed: %s", err) } @@ -59,7 +92,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string) re } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string, itemScope insights.ItemScope) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -68,7 +101,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope) if err != nil { return fmt.Errorf("Error checking if item exists: %s", err) } @@ -80,7 +113,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string) (bool, error) { +func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string, itemScope insights.ItemScope) (bool, error) { resGroup := rs.Primary.Attributes["resource_group_name"] appInsightsID := rs.Primary.Attributes["application_insights_id"] @@ -95,7 +128,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor ctx := testAccProvider.Meta().(*ArmClient).StopContext includeContent := false - resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, insights.ItemScopeShared, insights.ItemTypeParameterQuery, &includeContent) + resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, itemScope, insights.ItemTypeParameterQuery, &includeContent) if resp.StatusCode == http.StatusNotFound { return false, nil } @@ -103,7 +136,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor return false, fmt.Errorf("Bad: List on appInsightsAnalyticsItemsClient: %+v", err) } for _, item := range *resp.Value { - if *item.Name == queryName && item.Scope == insights.ItemScopeShared { + if *item.Name == queryName && item.Scope == itemScope { return true, nil } } @@ -130,8 +163,42 @@ resource "azurerm_application_insights_analytics_item" "test" { resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #test" - scope = "shared" - type = "query" + scope = "shared" + type = "query" +} +`, rInt, location, rInt) +} + +func testAccAzureRMApplicationInsightsAnalyticsItem_multiple(rInt int, location 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 = "web" +} + +resource "azurerm_application_insights_analytics_item" "test1" { + name = "testquery1" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #test1" + scope = "shared" + type = "query" +} + +resource "azurerm_application_insights_analytics_item" "test2" { + name = "testquery2" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #test2" + scope = "user" + type = "query" } `, rInt, location, rInt) } From 1ed0bdb7299231a8851cb1a58f20a183ad7d9f0c Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 10:32:07 +0100 Subject: [PATCH 08/36] Fix handling of item scopes --- ...urce_arm_application_insights_analytics_item.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 9cc6e5e2afcf..bce499211e31 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -106,13 +106,21 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD scopeName := d.Get("scope").(string) typeName := d.Get("type").(string) + itemType := insights.ItemType(typeName) + itemScope := insights.ItemScope(scopeName) properties := insights.ApplicationInsightsComponentAnalyticsItem{ Name: &name, - Type: insights.ItemType(typeName), - Scope: insights.ItemScope(scopeName), + Type: itemType, + Scope: itemScope, Content: &content, } - result, err := client.Put(ctx, resourceGroupName, appInsightsName, insights.AnalyticsItems, properties, &overwrite) + var itemScopePath insights.ItemScopePath + if itemScope == insights.ItemScopeUser { + itemScopePath = insights.MyanalyticsItems + } else { + itemScopePath = insights.AnalyticsItems + } + result, err := client.Put(ctx, resourceGroupName, appInsightsName, itemScopePath, properties, &overwrite) if err != nil { return fmt.Errorf("Error Putting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) } From 645fa95e7623bef65393d1ab5267ee3f9c472ba0 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:14:44 +0100 Subject: [PATCH 09/36] Add Function test --- ..._arm_application_insights_analytics_item.go | 5 +++++ ...application_insights_analytics_item_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index bce499211e31..785346345a4e 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -68,6 +68,11 @@ func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { }, false), }, + "function_alias": { + Type: schema.TypeString, + Optional: true, + }, + "time_created": { Type: schema.TypeString, Computed: true, diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 67e463aee98b..54c8b33c51bd 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -40,6 +40,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { resourceName1 := "azurerm_application_insights_analytics_item.test1" resourceName2 := "azurerm_application_insights_analytics_item.test2" + resourceName3 := "azurerm_application_insights_analytics_item.test3" ri := tf.AccRandTimeInt() config := testAccAzureRMApplicationInsightsAnalyticsItem_multiple(ri, testLocation()) @@ -49,6 +50,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { CheckDestroy: resource.ComposeTestCheckFunc( testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery1", insights.ItemScopeShared), testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery2", insights.ItemScopeUser), + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testfunction1", insights.ItemScopeShared), ), Steps: []resource.TestStep{ { @@ -56,6 +58,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { Check: resource.ComposeTestCheckFunc( testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1, "testquery1", insights.ItemScopeShared), testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2, "testquery2", insights.ItemScopeUser), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName3, "testfunction1", insights.ItemScopeShared), resource.TestCheckResourceAttr(resourceName1, "name", "testquery1"), resource.TestCheckResourceAttr(resourceName1, "scope", "shared"), resource.TestCheckResourceAttr(resourceName1, "type", "query"), @@ -64,6 +67,11 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { resource.TestCheckResourceAttr(resourceName2, "scope", "user"), resource.TestCheckResourceAttr(resourceName2, "type", "query"), resource.TestCheckResourceAttr(resourceName2, "content", "requests #test2"), + resource.TestCheckResourceAttr(resourceName3, "name", "testfunction1"), + resource.TestCheckResourceAttr(resourceName3, "scope", "shared"), + resource.TestCheckResourceAttr(resourceName3, "type", "function"), + resource.TestCheckResourceAttr(resourceName3, "content", "requests #test3"), + resource.TestCheckResourceAttr(resourceName3, "function_alias", "myfunction"), ), }, }, @@ -200,5 +208,15 @@ resource "azurerm_application_insights_analytics_item" "test2" { scope = "user" type = "query" } + +resource "azurerm_application_insights_analytics_item" "test3" { + name = "testfunction1" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #test3" + scope = "shared" + type = "function" + function_alias = "myfunction" +} `, rInt, location, rInt) } From f44b0f6aca2f92dc69f2c8c4fc53dab0289617ad Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:14:56 +0100 Subject: [PATCH 10/36] Add Function to docs --- .../docs/r/application_insights_analytics_item.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index daa3f52a0efb..3c729eba595e 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -53,6 +53,8 @@ The following arguments are supported: * `content` - (Required) The content for the Analytics Item, for example the query text if `type` is `query`. +* `function_alias` - (Optional) The alias to use for the function. Required when `type` is `function`. + TODO!! ## Attributes Reference From fc8e2d8f92dce344663e4412e482f88aaf598368 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:23:54 +0100 Subject: [PATCH 11/36] Fix test handling of functions --- ...pplication_insights_analytics_item_test.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 54c8b33c51bd..70e110796bd6 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -21,12 +21,12 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared), + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), @@ -48,17 +48,17 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery1", insights.ItemScopeShared), - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery2", insights.ItemScopeUser), - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testfunction1", insights.ItemScopeShared), + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery1", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery2", insights.ItemScopeUser, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testfunction1", insights.ItemScopeShared, insights.ItemTypeParameterFunction), ), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1, "testquery1", insights.ItemScopeShared), - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2, "testquery2", insights.ItemScopeUser), - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName3, "testfunction1", insights.ItemScopeShared), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1, "testquery1", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2, "testquery2", insights.ItemScopeUser, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName3, "testfunction1", insights.ItemScopeShared, insights.ItemTypeParameterFunction), resource.TestCheckResourceAttr(resourceName1, "name", "testquery1"), resource.TestCheckResourceAttr(resourceName1, "scope", "shared"), resource.TestCheckResourceAttr(resourceName1, "type", "query"), @@ -78,7 +78,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { }) } -func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, itemScope insights.ItemScope) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) resource.TestCheckFunc { return func(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_application_insights_analytics_item" { @@ -87,7 +87,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, it name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope, itemType) if err != nil { return fmt.Errorf("Error checking if item has been destroyed: %s", err) } @@ -100,7 +100,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, it } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string, itemScope insights.ItemScope) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -109,7 +109,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope, itemType) if err != nil { return fmt.Errorf("Error checking if item exists: %s", err) } @@ -121,7 +121,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string, itemScope insights.ItemScope) (bool, error) { +func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) (bool, error) { resGroup := rs.Primary.Attributes["resource_group_name"] appInsightsID := rs.Primary.Attributes["application_insights_id"] @@ -136,7 +136,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor ctx := testAccProvider.Meta().(*ArmClient).StopContext includeContent := false - resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, itemScope, insights.ItemTypeParameterQuery, &includeContent) + resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, itemScope, itemType, &includeContent) if resp.StatusCode == http.StatusNotFound { return false, nil } From 07074d3cbafce57825bcdf13af441e47920606be Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:24:11 +0100 Subject: [PATCH 12/36] Fix implementation for functions --- ...esource_arm_application_insights_analytics_item.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 785346345a4e..e905bd864c89 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -110,6 +110,7 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD content := d.Get("content").(string) scopeName := d.Get("scope").(string) typeName := d.Get("type").(string) + functionAlias := d.Get("function_alias").(string) itemType := insights.ItemType(typeName) itemScope := insights.ItemScope(scopeName) @@ -119,6 +120,12 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD Scope: itemScope, Content: &content, } + if functionAlias != "" { + properties.Properties = &insights.ApplicationInsightsComponentAnalyticsItemProperties{ + FunctionAlias: &functionAlias, + } + } + var itemScopePath insights.ItemScopePath if itemScope == insights.ItemScopeUser { itemScopePath = insights.MyanalyticsItems @@ -170,6 +177,10 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met d.Set("time_created", result.TimeCreated) d.Set("time_modified", result.TimeModified) + if result.Properties != nil { + d.Set("function_alias", result.Properties.FunctionAlias) + } + return nil } From 65b5144bc059559f23794e638b8104f7145f1d9d Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:28:34 +0100 Subject: [PATCH 13/36] Add docs annotation --- .../docs/r/application_insights_analytics_item.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 3c729eba595e..2acaca54684a 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -49,7 +49,7 @@ The following arguments are supported: * `type` - (Required) The type of Analytics Item to create. Can be one of `query`, `function`, `folder`, `recent`. Changing this forces a new resource to be created. -* `scope` - (Required) The scope for the Analytics Item. Can be `shared` or `user`. Changing this forces a new resource to be created. +* `scope` - (Required) The scope for the Analytics Item. Can be `shared` or `user`. Changing this forces a new resource to be created. Must be `shared` for for functions. * `content` - (Required) The content for the Analytics Item, for example the query text if `type` is `query`. From af343fa54c93d20c740fdceb7c4690c0b0a08cce Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:30:29 +0100 Subject: [PATCH 14/36] Fixup comment in example query --- .../docs/r/application_insights_analytics_item.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 2acaca54684a..9ab0498f46c9 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -29,7 +29,7 @@ resource "azurerm_application_insights_analytics_item" "test" { name = "testquery" resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" - content = "requests #simple example query" + content = "requests //simple example query" scope = "shared" type = "query" } From 86af1e8675384314d3008f689a06e0f19d283e70 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:49:51 +0100 Subject: [PATCH 15/36] Add test for updating a query --- ...pplication_insights_analytics_item_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 70e110796bd6..7363b3225cfa 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -37,6 +37,41 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { }) } +func TestAccAzureRMApplicationInsightsAnalyticsItem_basicWithUpdate(t *testing.T) { + resourceName := "azurerm_application_insights_analytics_item.test" + ri := tf.AccRandTimeInt() + config1 := testAccAzureRMApplicationInsightsAnalyticsItem_basic(ri, testLocation()) + config2 := testAccAzureRMApplicationInsightsAnalyticsItem_basic2(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + Steps: []resource.TestStep{ + { + Config: config1, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + resource.TestCheckResourceAttr(resourceName, "name", "testquery"), + resource.TestCheckResourceAttr(resourceName, "scope", "shared"), + resource.TestCheckResourceAttr(resourceName, "type", "query"), + resource.TestCheckResourceAttr(resourceName, "content", "requests #test"), + ), + }, + { + Config: config2, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + resource.TestCheckResourceAttr(resourceName, "name", "testquery"), + resource.TestCheckResourceAttr(resourceName, "scope", "shared"), + resource.TestCheckResourceAttr(resourceName, "type", "query"), + resource.TestCheckResourceAttr(resourceName, "content", "requests #updated"), + ), + }, + }, + }) +} + func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { resourceName1 := "azurerm_application_insights_analytics_item.test1" resourceName2 := "azurerm_application_insights_analytics_item.test2" @@ -177,6 +212,31 @@ resource "azurerm_application_insights_analytics_item" "test" { `, rInt, location, rInt) } +func testAccAzureRMApplicationInsightsAnalyticsItem_basic2(rInt int, location 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 = "web" +} + +resource "azurerm_application_insights_analytics_item" "test" { + name = "testquery" + resource_group_name = "${azurerm_resource_group.test.name}" + application_insights_id = "${azurerm_application_insights.test.id}" + content = "requests #updated" + scope = "shared" + type = "query" +} +`, rInt, location, rInt) +} + func testAccAzureRMApplicationInsightsAnalyticsItem_multiple(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From 4abd62d94bcdf858dc218a9d1913a678b761eb95 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 11:49:59 +0100 Subject: [PATCH 16/36] Fix updating query --- azurerm/resource_arm_application_insights_analytics_item.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index e905bd864c89..48b6de419c6a 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -106,6 +106,7 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD appInsightsName := id.Path["components"] + itemID := d.Id() name := d.Get("name").(string) content := d.Get("content").(string) scopeName := d.Get("scope").(string) @@ -115,6 +116,7 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD itemType := insights.ItemType(typeName) itemScope := insights.ItemScope(scopeName) properties := insights.ApplicationInsightsComponentAnalyticsItem{ + ID: &itemID, Name: &name, Type: itemType, Scope: itemScope, From f10b177c5a77eb10ca7d5eb7916e2a444676c81f Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 13:53:04 +0100 Subject: [PATCH 17/36] Remove TODO comments --- .../docs/r/application_insights_analytics_item.html.markdown | 4 ---- 1 file changed, 4 deletions(-) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 9ab0498f46c9..4b31d7614f0d 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -35,8 +35,6 @@ resource "azurerm_application_insights_analytics_item" "test" { } ``` -*********************************************TODO!! - ## Argument Reference The following arguments are supported: @@ -55,8 +53,6 @@ The following arguments are supported: * `function_alias` - (Optional) The alias to use for the function. Required when `type` is `function`. -TODO!! - ## Attributes Reference The following attributes are exported: From 18fc5cea41c1bdacd48e1ac5bdb1fa0d1c79c8fa Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 13:55:20 +0100 Subject: [PATCH 18/36] Add comment for version --- .../docs/r/application_insights_analytics_item.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 4b31d7614f0d..8f2560fe863b 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -62,3 +62,5 @@ The following attributes are exported: * `time_created` - A string containing the time the Analytics Item was created. * `time_modified` - A string containing the time the Analytics Item was last modified. + +* `version` - A string indicating the version of the query format \ No newline at end of file From 759132f813262d1794d863b36eff3430783d6864 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 14:02:41 +0100 Subject: [PATCH 19/36] remove hard-coded string --- azurerm/resource_arm_application_insights_analytics_item.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 48b6de419c6a..00f925507e81 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -205,7 +205,7 @@ func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, m name := d.Get("name").(string) var scopePath insights.ItemScopePath - if scopeName == "user" { + if scopeName == string(insights.ItemScopeUser) { scopePath = insights.MyanalyticsItems } else { scopePath = insights.AnalyticsItems From 49b15e67f10c52ff231e47c1eda7a5a869c30110 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 14:09:57 +0100 Subject: [PATCH 20/36] Update azurerm/resource_arm_application_insights_analytics_item.go Co-Authored-By: Tom Harvey --- azurerm/resource_arm_application_insights_analytics_item.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 00f925507e81..4a4741a7afef 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -25,6 +25,7 @@ func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, + ValidateFunc: validation.NoEmptyStrings, }, "resource_group_name": azure.SchemaResourceGroupName(), From 3d2c95a397e2c1cba8b2a9b8123061dc82482c5e Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 14:09:20 +0100 Subject: [PATCH 21/36] Verify state import --- .../resource_arm_application_insights_analytics_item_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 7363b3225cfa..29bc659a0de9 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -33,6 +33,11 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "content", "requests #test"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } From 303a898f768418346c3c940fdb0784818d5e6a62 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 15:03:46 +0100 Subject: [PATCH 22/36] Fix up validation reference --- .../resource_arm_application_insights_analytics_item.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 4a4741a7afef..5187d6f93d38 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" "github.com/hashicorp/terraform/helper/schema" @@ -22,10 +23,10 @@ func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.NoEmptyStrings, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.NoEmptyStrings, }, "resource_group_name": azure.SchemaResourceGroupName(), From ecd436b7e74c20b3814d4fdf6df0b04a8635d9f2 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 15:04:25 +0100 Subject: [PATCH 23/36] Remove Read dependency on anything other than ID --- ...arm_application_insights_analytics_item.go | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 5187d6f93d38..2fe9aec373e0 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -141,7 +141,14 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD return fmt.Errorf("Error Putting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) } - d.SetId(*result.ID) + // See comments in Read method about ID format + var generatedID string + if itemScope == insights.ItemScopeShared { + generatedID = appInsightsID + "/analyticsItems/" + *result.ID + } else { + generatedID = appInsightsID + "/myanalyticsItems/" + *result.ID + } + d.SetId(generatedID) return resourceArmApplicationInsightsAnalyticsItemRead(d, meta) } @@ -150,28 +157,30 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met client := meta.(*ArmClient).appInsights.AnalyticsItemsClient ctx := meta.(*ArmClient).StopContext - resourceGroupName := d.Get("resource_group_name").(string) - appInsightsID := d.Get("application_insights_id").(string) - scopeName := d.Get("scope").(string) - itemID := d.Id() + id := d.Id() - id, err := azure.ParseAzureResourceID(appInsightsID) + resourceID, err := azure.ParseAzureResourceID(id) if err != nil { return fmt.Errorf("Error parsing resource ID: %s", err) } - - appInsightsName := id.Path["components"] - name := d.Get("name").(string) - - var scopePath insights.ItemScopePath - if scopeName == "user" { - scopePath = insights.MyanalyticsItems - } else { - scopePath = insights.AnalyticsItems + resourceGroupName := resourceID.Path["resourceGroups"] + appInsightsName := resourceID.Path["components"] + + // Use the following generated ID format: + // /analyticsItems/ [for shared scope items] + // /myanalyticsItems/ [for user scope items] + // Pull out the itemID and note the scope used + itemID := resourceID.Path["analyticsItems"] + itemScopePath := insights.AnalyticsItems + if itemID == "" { + // no "analyticsItems" component - try "myanalyticsItems" and set scope path + itemID = resourceID.Path["myanalyticsItems"] + itemScopePath = insights.MyanalyticsItems } - result, err := client.Get(ctx, resourceGroupName, appInsightsName, scopePath, itemID, name) + + result, err := client.Get(ctx, resourceGroupName, appInsightsName, itemScopePath, itemID, "") if err != nil { - return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) + return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) } d.Set("version", result.Version) From 19a4863ba1092cca60a4476a7415f07ce4ae51e8 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 15:12:54 +0100 Subject: [PATCH 24/36] Update generated ID handling for Delete --- ...arm_application_insights_analytics_item.go | 65 +++++++++---------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 2fe9aec373e0..533cb58cc578 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -141,7 +141,7 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD return fmt.Errorf("Error Putting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) } - // See comments in Read method about ID format + // See comments in resourcesArmApplicationInsightsAnalyticsItemParseID method about ID format var generatedID string if itemScope == insights.ItemScopeShared { generatedID = appInsightsID + "/analyticsItems/" + *result.ID @@ -158,24 +158,9 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met ctx := meta.(*ArmClient).StopContext id := d.Id() - - resourceID, err := azure.ParseAzureResourceID(id) + resourceGroupName, appInsightsName, itemScopePath, itemID, err := resourcesArmApplicationInsightsAnalyticsItemParseID(id) if err != nil { - return fmt.Errorf("Error parsing resource ID: %s", err) - } - resourceGroupName := resourceID.Path["resourceGroups"] - appInsightsName := resourceID.Path["components"] - - // Use the following generated ID format: - // /analyticsItems/ [for shared scope items] - // /myanalyticsItems/ [for user scope items] - // Pull out the itemID and note the scope used - itemID := resourceID.Path["analyticsItems"] - itemScopePath := insights.AnalyticsItems - if itemID == "" { - // no "analyticsItems" component - try "myanalyticsItems" and set scope path - itemID = resourceID.Path["myanalyticsItems"] - itemScopePath = insights.MyanalyticsItems + return fmt.Errorf("Error parsing Application Insights Analytics Item ID %s: %s", id, err) } result, err := client.Get(ctx, resourceGroupName, appInsightsName, itemScopePath, itemID, "") @@ -202,29 +187,39 @@ func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, m client := meta.(*ArmClient).appInsights.AnalyticsItemsClient ctx := meta.(*ArmClient).StopContext - resourceGroupName := d.Get("resource_group_name").(string) - appInsightsID := d.Get("application_insights_id").(string) - scopeName := d.Get("scope").(string) - itemID := d.Id() + id := d.Id() + resourceGroupName, appInsightsName, itemScopePath, itemID, err := resourcesArmApplicationInsightsAnalyticsItemParseID(id) + if err != nil { + return fmt.Errorf("Error parsing Application Insights Analytics Item ID %s: %s", id, err) + } - id, err := azure.ParseAzureResourceID(appInsightsID) + _, err = client.Delete(ctx, resourceGroupName, appInsightsName, itemScopePath, itemID, "") if err != nil { - return fmt.Errorf("Error parsing resource ID: %s", err) + return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) } - appInsightsName := id.Path["components"] - name := d.Get("name").(string) + return nil +} - var scopePath insights.ItemScopePath - if scopeName == string(insights.ItemScopeUser) { - scopePath = insights.MyanalyticsItems - } else { - scopePath = insights.AnalyticsItems - } - _, err = client.Delete(ctx, resourceGroupName, appInsightsName, scopePath, itemID, name) +func resourcesArmApplicationInsightsAnalyticsItemParseID(id string) (string, string, insights.ItemScopePath, string, error) { + resourceID, err := azure.ParseAzureResourceID(id) if err != nil { - return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", name, resourceGroupName, appInsightsName, err) + return "", "", "", "", fmt.Errorf("Error parsing resource ID: %s", err) } + resourceGroupName := resourceID.Path["resourceGroups"] + appInsightsName := resourceID.Path["components"] - return nil + // Use the following generated ID format: + // /analyticsItems/ [for shared scope items] + // /myanalyticsItems/ [for user scope items] + // Pull out the itemID and note the scope used + itemID := resourceID.Path["analyticsItems"] + itemScopePath := insights.AnalyticsItems + if itemID == "" { + // no "analyticsItems" component - try "myanalyticsItems" and set scope path + itemID = resourceID.Path["myanalyticsItems"] + itemScopePath = insights.MyanalyticsItems + } + + return resourceGroupName, appInsightsName, itemScopePath, itemID, nil } From 897b8f1d45c81ae4aba73e41066c21f71cac2cce Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 15:20:03 +0100 Subject: [PATCH 25/36] Add docs on import for ID format --- ...ation_insights_analytics_item.html.markdown | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 8f2560fe863b..1594cb7c66d1 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -63,4 +63,20 @@ The following attributes are exported: * `time_modified` - A string containing the time the Analytics Item was last modified. -* `version` - A string indicating the version of the query format \ No newline at end of file +* `version` - A string indicating the version of the query format + +## Import + +Application Insights Analytics Items can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_application_insights_analytics_item.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.insights/components/analyticsItems/11111111-1111-1111-1111-111111111111 +``` + +-> **Please Note:** This is a Terraform Unique ID matching the format: `{appInsightsID}/analyticsItems/{itemId}` for items with `scope` set to `shared`, or `{appInsightsID}/myanalyticsItems/{itemId}` for items with `scope` set to `user` + +To find the Analytics Item ID you can query the REST API using the [`az rest` CLI command](https://docs.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest#az-rest), e.g. + +```shell +az rest --method GET --uri "https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.insights/components/appinsightstest/analyticsItems?api-version=2015-05-01" +``` From 5029f28a8fd1553814d25cf939c4598a4de17abf Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 15:31:01 +0100 Subject: [PATCH 26/36] Simplify test helpers based on review comments --- ...pplication_insights_analytics_item_test.go | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 29bc659a0de9..c676d85b34fc 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -21,12 +21,12 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy(), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName), resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), @@ -51,12 +51,12 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basicWithUpdate(t *testing.T resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy(), Steps: []resource.TestStep{ { Config: config1, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName), resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), @@ -66,7 +66,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basicWithUpdate(t *testing.T { Config: config2, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName, "testquery", insights.ItemScopeShared, insights.ItemTypeParameterQuery), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName), resource.TestCheckResourceAttr(resourceName, "name", "testquery"), resource.TestCheckResourceAttr(resourceName, "scope", "shared"), resource.TestCheckResourceAttr(resourceName, "type", "query"), @@ -85,20 +85,16 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { config := testAccAzureRMApplicationInsightsAnalyticsItem_multiple(ri, testLocation()) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery1", insights.ItemScopeShared, insights.ItemTypeParameterQuery), - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testquery2", insights.ItemScopeUser, insights.ItemTypeParameterQuery), - testCheckAzureRMApplicationInsightAnalyticsItemDestroy("testfunction1", insights.ItemScopeShared, insights.ItemTypeParameterFunction), - ), + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMApplicationInsightAnalyticsItemDestroy(), Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1, "testquery1", insights.ItemScopeShared, insights.ItemTypeParameterQuery), - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2, "testquery2", insights.ItemScopeUser, insights.ItemTypeParameterQuery), - testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName3, "testfunction1", insights.ItemScopeShared, insights.ItemTypeParameterFunction), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName1), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName2), + testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName3), resource.TestCheckResourceAttr(resourceName1, "name", "testquery1"), resource.TestCheckResourceAttr(resourceName1, "scope", "shared"), resource.TestCheckResourceAttr(resourceName1, "type", "query"), @@ -118,7 +114,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { }) } -func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightAnalyticsItemDestroy() resource.TestCheckFunc { return func(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_application_insights_analytics_item" { @@ -127,7 +123,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, it name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope, itemType) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs) if err != nil { return fmt.Errorf("Error checking if item has been destroyed: %s", err) } @@ -140,7 +136,7 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy(queryName string, it } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) resource.TestCheckFunc { +func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -149,7 +145,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, name := rs.Primary.Attributes["name"] resGroup := rs.Primary.Attributes["resource_group_name"] - exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs, queryName, itemScope, itemType) + exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs) if err != nil { return fmt.Errorf("Error checking if item exists: %s", err) } @@ -161,8 +157,14 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string, } } -func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState, queryName string, itemScope insights.ItemScope, itemType insights.ItemTypeParameter) (bool, error) { +func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState) (bool, error) { resGroup := rs.Primary.Attributes["resource_group_name"] + scopeName := rs.Primary.Attributes["scope"] + typeName := rs.Primary.Attributes["type"] + name := rs.Primary.Attributes["name"] + + itemScope := insights.ItemScope(scopeName) + itemType := insights.ItemTypeParameter(typeName) appInsightsID := rs.Primary.Attributes["application_insights_id"] id, err := azure.ParseAzureResourceID(appInsightsID) @@ -184,7 +186,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor return false, fmt.Errorf("Bad: List on appInsightsAnalyticsItemsClient: %+v", err) } for _, item := range *resp.Value { - if *item.Name == queryName && item.Scope == itemScope { + if *item.Name == name && item.Scope == itemScope { return true, nil } } From fd010d598dd99a851be44ad548311f37875dbed0 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 16:02:28 +0100 Subject: [PATCH 27/36] Fix error text --- azurerm/resource_arm_application_insights_analytics_item.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 533cb58cc578..8af9e0c68d85 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -195,7 +195,7 @@ func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, m _, err = client.Delete(ctx, resourceGroupName, appInsightsName, itemScopePath, itemID, "") if err != nil { - return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) + return fmt.Errorf("Error Deleting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) } return nil From 2fa32c0d75d1517767de732cbb607acf9a561087 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 16:41:14 +0100 Subject: [PATCH 28/36] Rename test for easier selection --- .../resource_arm_application_insights_analytics_item_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index c676d85b34fc..32c32820033a 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -42,7 +42,7 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { }) } -func TestAccAzureRMApplicationInsightsAnalyticsItem_basicWithUpdate(t *testing.T) { +func TestAccAzureRMApplicationInsightsAnalyticsItem_update(t *testing.T) { resourceName := "azurerm_application_insights_analytics_item.test" ri := tf.AccRandTimeInt() config1 := testAccAzureRMApplicationInsightsAnalyticsItem_basic(ri, testLocation()) From 8fb853162faa1dfd7891bf882b28e18a05489718 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 16:41:30 +0100 Subject: [PATCH 29/36] Set all required properties on import --- ...arm_application_insights_analytics_item.go | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 8af9e0c68d85..eaad85083335 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -142,12 +142,7 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD } // See comments in resourcesArmApplicationInsightsAnalyticsItemParseID method about ID format - var generatedID string - if itemScope == insights.ItemScopeShared { - generatedID = appInsightsID + "/analyticsItems/" + *result.ID - } else { - generatedID = appInsightsID + "/myanalyticsItems/" + *result.ID - } + generatedID := appInsightsID + resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(itemScope, *result.ID) d.SetId(generatedID) return resourceArmApplicationInsightsAnalyticsItemRead(d, meta) @@ -168,6 +163,11 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met return fmt.Errorf("Error Getting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) } + idSuffix := resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(result.Scope, itemID) + appInsightsID := id[0 : len(id)-len(idSuffix)] + d.Set("application_insights_id", appInsightsID) + d.Set("resource_group_name", resourceGroupName) + d.Set("name", result.Name) d.Set("version", result.Version) d.Set("content", result.Content) d.Set("scope", string(result.Scope)) @@ -195,7 +195,7 @@ func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, m _, err = client.Delete(ctx, resourceGroupName, appInsightsName, itemScopePath, itemID, "") if err != nil { - return fmt.Errorf("Error Deleting Application Insights Analytics Item %s (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) + return fmt.Errorf("Error Deleting Application Insights Analytics Item '%s' (Resource Group %s, App Insights Name: %s): %s", itemID, resourceGroupName, appInsightsName, err) } return nil @@ -206,7 +206,7 @@ func resourcesArmApplicationInsightsAnalyticsItemParseID(id string) (string, str if err != nil { return "", "", "", "", fmt.Errorf("Error parsing resource ID: %s", err) } - resourceGroupName := resourceID.Path["resourceGroups"] + resourceGroupName := resourceID.ResourceGroup appInsightsName := resourceID.Path["components"] // Use the following generated ID format: @@ -223,3 +223,12 @@ func resourcesArmApplicationInsightsAnalyticsItemParseID(id string) (string, str return resourceGroupName, appInsightsName, itemScopePath, itemID, nil } + +func resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(itemScope insights.ItemScope, itemID string) string { + // See comments in resourcesArmApplicationInsightsAnalyticsItemParseID method about ID format + if itemScope == insights.ItemScopeShared { + return "/analyticsItems/" + itemID + } else { + return "/myanalyticsItems/" + itemID + } +} From 7723541051c81d20137181219c9cd91329498259 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 17:01:52 +0100 Subject: [PATCH 30/36] Correctly interpret ID on updates --- ...urce_arm_application_insights_analytics_item.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index eaad85083335..50653e1bb5c4 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -101,14 +101,22 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD resourceGroupName := d.Get("resource_group_name").(string) appInsightsID := d.Get("application_insights_id").(string) - id, err := azure.ParseAzureResourceID(appInsightsID) + resourceID, err := azure.ParseAzureResourceID(appInsightsID) if err != nil { return fmt.Errorf("Error parsing resource ID: %s", err) } - appInsightsName := id.Path["components"] + appInsightsName := resourceID.Path["components"] + + id := d.Id() + itemID := "" + if id != "" { + _, _, _, itemID, err = resourcesArmApplicationInsightsAnalyticsItemParseID(id) + if err != nil { + return fmt.Errorf("Error parsing Application Insights Analytics Item ID %s: %s", id, err) + } + } - itemID := d.Id() name := d.Get("name").(string) content := d.Get("content").(string) scopeName := d.Get("scope").(string) From 135f4fbba982be40bdc2a2003c7038a1a11ad4c9 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 19 Sep 2019 20:28:26 +0100 Subject: [PATCH 31/36] Remove redundant resource_group_name Resource group name can be derived from the app insights id --- .../resource_arm_application_insights_analytics_item.go | 6 +----- ...esource_arm_application_insights_analytics_item_test.go | 7 +------ .../r/application_insights_analytics_item.html.markdown | 3 --- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 50653e1bb5c4..8304022be2e0 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -29,8 +29,6 @@ func resourceArmApplicationInsightsAnalyticsItem() *schema.Resource { ValidateFunc: validate.NoEmptyStrings, }, - "resource_group_name": azure.SchemaResourceGroupName(), - "application_insights_id": { Type: schema.TypeString, Required: true, @@ -98,14 +96,13 @@ func resourceArmApplicationInsightsAnalyticsItemCreateUpdate(d *schema.ResourceD client := meta.(*ArmClient).appInsights.AnalyticsItemsClient ctx := meta.(*ArmClient).StopContext - resourceGroupName := d.Get("resource_group_name").(string) appInsightsID := d.Get("application_insights_id").(string) resourceID, err := azure.ParseAzureResourceID(appInsightsID) if err != nil { return fmt.Errorf("Error parsing resource ID: %s", err) } - + resourceGroupName := resourceID.ResourceGroup appInsightsName := resourceID.Path["components"] id := d.Id() @@ -174,7 +171,6 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met idSuffix := resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(result.Scope, itemID) appInsightsID := id[0 : len(id)-len(idSuffix)] d.Set("application_insights_id", appInsightsID) - d.Set("resource_group_name", resourceGroupName) d.Set("name", result.Name) d.Set("version", result.Version) d.Set("content", result.Content) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 32c32820033a..d6bd164aae11 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -158,7 +158,6 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) } func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState) (bool, error) { - resGroup := rs.Primary.Attributes["resource_group_name"] scopeName := rs.Primary.Attributes["scope"] typeName := rs.Primary.Attributes["type"] name := rs.Primary.Attributes["name"] @@ -171,6 +170,7 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor if err != nil { return false, fmt.Errorf("Error parsing resource ID: %s", err) } + resGroup := id.ResourceGroup appInsightsName := id.Path["components"] @@ -210,7 +210,6 @@ resource "azurerm_application_insights" "test" { resource "azurerm_application_insights_analytics_item" "test" { name = "testquery" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #test" scope = "shared" @@ -235,7 +234,6 @@ resource "azurerm_application_insights" "test" { resource "azurerm_application_insights_analytics_item" "test" { name = "testquery" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #updated" scope = "shared" @@ -260,7 +258,6 @@ resource "azurerm_application_insights" "test" { resource "azurerm_application_insights_analytics_item" "test1" { name = "testquery1" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #test1" scope = "shared" @@ -269,7 +266,6 @@ resource "azurerm_application_insights_analytics_item" "test1" { resource "azurerm_application_insights_analytics_item" "test2" { name = "testquery2" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #test2" scope = "user" @@ -278,7 +274,6 @@ resource "azurerm_application_insights_analytics_item" "test2" { resource "azurerm_application_insights_analytics_item" "test3" { name = "testfunction1" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests #test3" scope = "shared" diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 1594cb7c66d1..66c858c33bfb 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -27,7 +27,6 @@ resource "azurerm_application_insights" "test" { resource "azurerm_application_insights_analytics_item" "test" { name = "testquery" - resource_group_name = "${azurerm_resource_group.test.name}" application_insights_id = "${azurerm_application_insights.test.id}" content = "requests //simple example query" scope = "shared" @@ -41,8 +40,6 @@ The following arguments are supported: * `name` - (Required) Specifies the name of the Application Insights Analytics Item. Changing this forces a new resource to be created. -* `resource_group_name` - (Required) The name of the resource group in which to create the Application Insights component. - * `application_insights_id` - (Required) The ID of the Application Insights component on which the Analytics Item exists. Changing this forces a new resource to be created. * `type` - (Required) The type of Analytics Item to create. Can be one of `query`, `function`, `folder`, `recent`. Changing this forces a new resource to be created. From 4b32adc2b3544af7f6e0b622b320b0c88cbe6d55 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Mon, 23 Sep 2019 15:37:02 +0100 Subject: [PATCH 32/36] Convert List to Get and fix test --- ...pplication_insights_analytics_item_test.go | 42 +++++-------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index d6bd164aae11..40f0953ef61e 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -2,15 +2,11 @@ package azurerm import ( "fmt" - "net/http" "testing" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" - - "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" ) func TestAccAzureRMApplicationInsightsAnalyticsItem_basic(t *testing.T) { @@ -121,14 +117,13 @@ func testCheckAzureRMApplicationInsightAnalyticsItemDestroy() resource.TestCheck continue } name := rs.Primary.Attributes["name"] - resGroup := rs.Primary.Attributes["resource_group_name"] exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs) if err != nil { return fmt.Errorf("Error checking if item has been destroyed: %s", err) } if exists { - return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' (resource group: '%q') still exists", name, resGroup) + return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' still exists", name) } } @@ -143,14 +138,13 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) return fmt.Errorf("Not found: %s", resourceName) } name := rs.Primary.Attributes["name"] - resGroup := rs.Primary.Attributes["resource_group_name"] exists, err := testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs) if err != nil { return fmt.Errorf("Error checking if item exists: %s", err) } if !exists { - return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' (resource group: '%q') does not exist", name, resGroup) + return fmt.Errorf("Bad: Application Insights AnalyticsItem '%q' does not exist", name) } return nil @@ -158,40 +152,24 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) } func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState) (bool, error) { - scopeName := rs.Primary.Attributes["scope"] - typeName := rs.Primary.Attributes["type"] - name := rs.Primary.Attributes["name"] - - itemScope := insights.ItemScope(scopeName) - itemType := insights.ItemTypeParameter(typeName) - appInsightsID := rs.Primary.Attributes["application_insights_id"] - id, err := azure.ParseAzureResourceID(appInsightsID) - if err != nil { - return false, fmt.Errorf("Error parsing resource ID: %s", err) - } - resGroup := id.ResourceGroup + id := rs.Primary.Attributes["id"] - appInsightsName := id.Path["components"] + resGroup, appInsightsName, itemScopePath, itemID, err := resourcesArmApplicationInsightsAnalyticsItemParseID(id) conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient ctx := testAccProvider.Meta().(*ArmClient).StopContext - includeContent := false - resp, err := conn.List(ctx, resGroup, appInsightsName, insights.AnalyticsItems, itemScope, itemType, &includeContent) - if resp.StatusCode == http.StatusNotFound { - return false, nil - } + response, err := conn.Get(ctx, resGroup, appInsightsName, itemScopePath, itemID, "") if err != nil { - return false, fmt.Errorf("Bad: List on appInsightsAnalyticsItemsClient: %+v", err) - } - for _, item := range *resp.Value { - if *item.Name == name && item.Scope == itemScope { - return true, nil + if response.Response.IsHTTPStatus(404) { + return false, nil } + return false, fmt.Errorf("Bad: Get on appInsightsAnalyticsItemsClient (id: %s): %+v", id, err) } + _ = response - return false, nil + return true, nil } func testAccAzureRMApplicationInsightsAnalyticsItem_basic(rInt int, location string) string { From ce06b8cbad0a036d70fce11492f49c47c64ea006 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Mon, 23 Sep 2019 15:54:17 +0100 Subject: [PATCH 33/36] Check error parsing ID --- .../resource_arm_application_insights_analytics_item_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index 40f0953ef61e..e353ab9b0c7e 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -156,6 +156,9 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terrafor id := rs.Primary.Attributes["id"] resGroup, appInsightsName, itemScopePath, itemID, err := resourcesArmApplicationInsightsAnalyticsItemParseID(id) + if err != nil { + return false, fmt.Errorf("Failed to parse ID (id: %s): %+v", id, err) + } conn := testAccProvider.Meta().(*ArmClient).appInsights.AnalyticsItemsClient ctx := testAccProvider.Meta().(*ArmClient).StopContext From aa52a2a7ea51a4c5868769388aa3921554594fa8 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 26 Sep 2019 18:41:55 +0100 Subject: [PATCH 34/36] Update website/docs/r/application_insights_analytics_item.html.markdown Co-Authored-By: Matthew Frahry --- .../docs/r/application_insights_analytics_item.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/application_insights_analytics_item.html.markdown b/website/docs/r/application_insights_analytics_item.html.markdown index 66c858c33bfb..b194a4551c5e 100644 --- a/website/docs/r/application_insights_analytics_item.html.markdown +++ b/website/docs/r/application_insights_analytics_item.html.markdown @@ -44,7 +44,7 @@ The following arguments are supported: * `type` - (Required) The type of Analytics Item to create. Can be one of `query`, `function`, `folder`, `recent`. Changing this forces a new resource to be created. -* `scope` - (Required) The scope for the Analytics Item. Can be `shared` or `user`. Changing this forces a new resource to be created. Must be `shared` for for functions. +* `scope` - (Required) The scope for the Analytics Item. Can be `shared` or `user`. Changing this forces a new resource to be created. Must be `shared` for functions. * `content` - (Required) The content for the Analytics Item, for example the query text if `type` is `query`. From 8c0869eeee9d19062b3161288d56646f289d85aa Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Thu, 26 Sep 2019 18:54:14 +0100 Subject: [PATCH 35/36] Add extra import checks --- ...pplication_insights_analytics_item_test.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index e353ab9b0c7e..feb2c247657b 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -59,6 +59,11 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "content", "requests #test"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: config2, Check: resource.ComposeTestCheckFunc( @@ -69,6 +74,11 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "content", "requests #updated"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -106,6 +116,21 @@ func TestAccAzureRMApplicationInsightsAnalyticsItem_multiple(t *testing.T) { resource.TestCheckResourceAttr(resourceName3, "function_alias", "myfunction"), ), }, + { + ResourceName: resourceName1, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceName2, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceName3, + ImportState: true, + ImportStateVerify: true, + }, }, }) } From d07ed3d175ed9af70e31a19545e1b864a2a4ece3 Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Fri, 27 Sep 2019 00:15:29 +0100 Subject: [PATCH 36/36] fix whitespace --- azurerm/internal/services/applicationinsights/client.go | 1 - azurerm/resource_arm_application_insights_analytics_item.go | 1 - azurerm/resource_arm_application_insights_analytics_item_test.go | 1 - 3 files changed, 3 deletions(-) diff --git a/azurerm/internal/services/applicationinsights/client.go b/azurerm/internal/services/applicationinsights/client.go index bb031a26f320..07ba4310bbe0 100644 --- a/azurerm/internal/services/applicationinsights/client.go +++ b/azurerm/internal/services/applicationinsights/client.go @@ -13,7 +13,6 @@ type Client struct { } func BuildClient(o *common.ClientOptions) *Client { - AnalyticsItemsClient := insights.NewAnalyticsItemsClient(o.SubscriptionId) o.ConfigureClient(&AnalyticsItemsClient.Client, o.ResourceManagerAuthorizer) diff --git a/azurerm/resource_arm_application_insights_analytics_item.go b/azurerm/resource_arm_application_insights_analytics_item.go index 8304022be2e0..8e71cadcc015 100644 --- a/azurerm/resource_arm_application_insights_analytics_item.go +++ b/azurerm/resource_arm_application_insights_analytics_item.go @@ -187,7 +187,6 @@ func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, met } func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient).appInsights.AnalyticsItemsClient ctx := meta.(*ArmClient).StopContext diff --git a/azurerm/resource_arm_application_insights_analytics_item_test.go b/azurerm/resource_arm_application_insights_analytics_item_test.go index feb2c247657b..7c04e25b0d15 100644 --- a/azurerm/resource_arm_application_insights_analytics_item_test.go +++ b/azurerm/resource_arm_application_insights_analytics_item_test.go @@ -177,7 +177,6 @@ func testCheckAzureRMApplicationInsightsAnalyticsItemExists(resourceName string) } func testCheckAzureRMApplicationInsightsAnalyticsItemExistsInternal(rs *terraform.ResourceState) (bool, error) { - id := rs.Primary.Attributes["id"] resGroup, appInsightsName, itemScopePath, itemID, err := resourcesArmApplicationInsightsAnalyticsItemParseID(id)