Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support for workspace mode in application_insights_resource #12583

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -68,10 +68,10 @@ func resourceApplicationInsightsAnalyticsItem() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(insights.Query),
string(insights.Function),
string(insights.Folder),
string(insights.Recent),
string(insights.ItemTypeQuery),
string(insights.ItemTypeFunction),
string(insights.ItemTypeFolder),
string(insights.ItemTypeRecent),
}, false),
},

Expand Down Expand Up @@ -147,9 +147,9 @@ func resourceApplicationInsightsAnalyticsItemCreateUpdate(d *pluginsdk.ResourceD

var itemScopePath insights.ItemScopePath
if itemScope == insights.ItemScopeUser {
itemScopePath = insights.MyanalyticsItems
itemScopePath = insights.ItemScopePathMyanalyticsItems
} else {
itemScopePath = insights.AnalyticsItems
itemScopePath = insights.ItemScopePathAnalyticsItems
}
result, err := client.Put(ctx, resourceGroupName, appInsightsName, itemScopePath, properties, &overwrite)
if err != nil {
Expand Down Expand Up @@ -228,11 +228,11 @@ func ResourcesArmApplicationInsightsAnalyticsItemParseID(id string) (string, str
// <appinsightsID>/myanalyticsItems/<itemID> [for user scope items]
// Pull out the itemID and note the scope used
itemID := resourceID.Path["analyticsItems"]
itemScopePath := insights.AnalyticsItems
itemScopePath := insights.ItemScopePathAnalyticsItems
if itemID == "" {
// no "analyticsItems" component - try "myanalyticsItems" and set scope path
itemID = resourceID.Path["myanalyticsItems"]
itemScopePath = insights.MyanalyticsItems
itemScopePath = insights.ItemScopePathMyanalyticsItems
}

return resourceGroupName, appInsightsName, itemScopePath, itemID, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func dataSourceApplicationInsights() *pluginsdk.Resource {
Computed: true,
},

"workspace_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": {
Type: pluginsdk.TypeMap,
Computed: true,
Expand Down Expand Up @@ -104,6 +109,12 @@ func dataSourceArmApplicationInsightsRead(d *pluginsdk.ResourceData, meta interf
retentionInDays = int(*props.RetentionInDays)
}
d.Set("retention_in_days", retentionInDays)

workspaceId := ""
if props.WorkspaceResourceID != nil {
workspaceId = string(*props.WorkspaceResourceID)
}
d.Set("workspace_id", workspaceId)
}
return tags.FlattenAndSet(d, resp.Tags)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccApplicationInsightsDataSource_basic(t *testing.T) {
check.That(data.ResourceName).Key("instrumentation_key").Exists(),
check.That(data.ResourceName).Key("app_id").Exists(),
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("workspace_id").Exists(),
check.That(data.ResourceName).Key("application_type").HasValue("other"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.foo").HasValue("bar"),
Expand All @@ -40,12 +41,20 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctest-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
retention_in_days = 30
}

resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
application_type = "other"

workspace_id = azurerm_log_analytics_workspace.test.id
tags = {
"foo" = "bar"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"net/http"
"time"

"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down Expand Up @@ -63,6 +64,12 @@ func resourceApplicationInsights() *pluginsdk.Resource {
}, false),
},

"workspace_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceIDOrEmpty,
},

"retention_in_days": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -167,8 +174,8 @@ func resourceApplicationInsightsCreateUpdate(d *pluginsdk.ResourceData, meta int
DisableIPMasking: utils.Bool(disableIpMasking),
}

if v, ok := d.GetOk("retention_in_days"); ok {
applicationInsightsComponentProperties.RetentionInDays = utils.Int32(int32(v.(int)))
if workspaceRaw, hasWorkspaceId := d.GetOk("workspace_id"); hasWorkspaceId {
applicationInsightsComponentProperties.WorkspaceResourceID = utils.String(workspaceRaw.(string))
}

insightProperties := insights.ApplicationInsightsComponent{
Expand All @@ -192,6 +199,22 @@ func resourceApplicationInsightsCreateUpdate(d *pluginsdk.ResourceData, meta int
return fmt.Errorf("Cannot read AzureRM Application Insights '%s' (Resource Group %s) ID", name, resGroup)
}

if v, hasRetention := d.GetOk("retention_in_days"); hasRetention {

resourcesClient := meta.(*clients.Client).Resource.ResourcesClient
genericResource := resources.GenericResource{
Properties: &map[string]interface{}{
"retentionInDays": utils.Int32(int32(v.(int))),
},
}

retentionUpdate, err := resourcesClient.UpdateByID(ctx, *read.ID, "2020-02-02-preview", genericResource)
if err != nil {
return fmt.Errorf("Error updating Application Insights data retention settings %q (Resource Group %q): %+v", name, resGroup, err)
}
retentionUpdate.WaitForCompletionRef(ctx, resourcesClient.Client)
}

billingRead, err := billingClient.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error read Application Insights Billing Features %q (Resource Group %q): %+v", name, resGroup, err)
Expand Down Expand Up @@ -259,6 +282,11 @@ func resourceApplicationInsightsRead(d *pluginsdk.ResourceData, meta interface{}
d.Set("sampling_percentage", props.SamplingPercentage)
d.Set("disable_ip_masking", props.DisableIPMasking)
d.Set("connection_string", props.ConnectionString)

if v := props.WorkspaceResourceID; v != nil {
d.Set("workspace_id", v)
}

if v := props.RetentionInDays; v != nil {
d.Set("retention_in_days", v)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func TestAccApplicationInsights_basiciOS(t *testing.T) {
})
}

func TestAccApplicationInsights_basicWorkspaceMode(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_insights", "test")
r := AppInsightsResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic_workspace_mode(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("workspace_id").Exists(),
),
},
data.ImportStep(),
})
}

func (t AppInsightsResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ComponentID(state.ID)
if err != nil {
Expand Down Expand Up @@ -203,6 +219,35 @@ resource "azurerm_application_insights" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, applicationType)
}

func (AppInsightsResource) basic_workspace_mode(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-appinsights-%d"
location = "%s"
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctest-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
retention_in_days = 30
}

resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
workspace_id = azurerm_log_analytics_workspace.test.id
application_type = "web"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (AppInsightsResource) requiresImport(data acceptance.TestData, applicationType string) string {
template := AppInsightsResource{}.basic(data, applicationType)
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/applicationinsights/parse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down Expand Up @@ -63,8 +63,8 @@ func resourceApplicationInsightsWebTests() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(insights.Multistep),
string(insights.Ping),
string(insights.WebTestKindMultistep),
string(insights.WebTestKindPing),
}, false),
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/appinsights/mgmt/2020-02-02-preview/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading