Skip to content

Commit

Permalink
Supporting workspace mode for application_insights_resource (#12818)
Browse files Browse the repository at this point in the history
As per issue #7667, updating the application_insights_resource to enable support of workspace mode, available with updated package 2020-02-02 in the Go SDK at v56.0.

Updated the insights API from 2015-05-01 to 2020-02-02
Fixed upgrade issues (enum changes )
Supporting workspace ID as an optional parameter when creating AI resources
Added workspace_id to data source resource
Documentation updates
  • Loading branch information
dylanmorley authored Aug 5, 2021
1 parent 2648905 commit 0937cf0
Show file tree
Hide file tree
Showing 37 changed files with 371 additions and 249 deletions.
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/appinsights/mgmt/2020-02-02/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/appinsights/mgmt/2020-02-02/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 = *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,7 @@ 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/appinsights/mgmt/2020-02-02/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,6 +63,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,6 +173,10 @@ func resourceApplicationInsightsCreateUpdate(d *pluginsdk.ResourceData, meta int
DisableIPMasking: utils.Bool(disableIpMasking),
}

if workspaceRaw, hasWorkspaceId := d.GetOk("workspace_id"); hasWorkspaceId {
applicationInsightsComponentProperties.WorkspaceResourceID = utils.String(workspaceRaw.(string))
}

if v, ok := d.GetOk("retention_in_days"); ok {
applicationInsightsComponentProperties.RetentionInDays = utils.Int32(int32(v.(int)))
}
Expand Down Expand Up @@ -259,6 +269,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/appinsights/mgmt/2020-02-02/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/appinsights/mgmt/2020-02-02/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/appinsights/mgmt/2020-02-02/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.

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

Loading

0 comments on commit 0937cf0

Please sign in to comment.