Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for App Insights Analytics Items #4374

Merged
merged 36 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9d509a9
Add App Insights AnalyticsItems Client
stuartleeks Sep 17, 2019
83229ee
Add spec for App Insights AnalyticsItem resource
stuartleeks Sep 17, 2019
9be3733
Add basic test
stuartleeks Sep 18, 2019
ede1e7a
Implement resource
stuartleeks Sep 18, 2019
f669c62
Get test working
stuartleeks Sep 18, 2019
c20b84c
Add docs
stuartleeks Sep 19, 2019
f3c35f7
Extend tests
stuartleeks Sep 19, 2019
1ed0bdb
Fix handling of item scopes
stuartleeks Sep 19, 2019
645fa95
Add Function test
stuartleeks Sep 19, 2019
f44b0f6
Add Function to docs
stuartleeks Sep 19, 2019
fc8e2d8
Fix test handling of functions
stuartleeks Sep 19, 2019
07074d3
Fix implementation for functions
stuartleeks Sep 19, 2019
65b5144
Add docs annotation
stuartleeks Sep 19, 2019
af343fa
Fixup comment in example query
stuartleeks Sep 19, 2019
86af1e8
Add test for updating a query
stuartleeks Sep 19, 2019
4abd62d
Fix updating query
stuartleeks Sep 19, 2019
f10b177
Remove TODO comments
stuartleeks Sep 19, 2019
18fc5ce
Add comment for version
stuartleeks Sep 19, 2019
759132f
remove hard-coded string
stuartleeks Sep 19, 2019
49b15e6
Update azurerm/resource_arm_application_insights_analytics_item.go
stuartleeks Sep 19, 2019
3d2c95a
Verify state import
stuartleeks Sep 19, 2019
303a898
Fix up validation reference
stuartleeks Sep 19, 2019
ecd436b
Remove Read dependency on anything other than ID
stuartleeks Sep 19, 2019
19a4863
Update generated ID handling for Delete
stuartleeks Sep 19, 2019
897b8f1
Add docs on import for ID format
stuartleeks Sep 19, 2019
5029f28
Simplify test helpers based on review comments
stuartleeks Sep 19, 2019
fd010d5
Fix error text
stuartleeks Sep 19, 2019
2fa32c0
Rename test for easier selection
stuartleeks Sep 19, 2019
8fb8531
Set all required properties on import
stuartleeks Sep 19, 2019
7723541
Correctly interpret ID on updates
stuartleeks Sep 19, 2019
135f4fb
Remove redundant resource_group_name
stuartleeks Sep 19, 2019
4b32adc
Convert List to Get and fix test
stuartleeks Sep 23, 2019
ce06b8c
Check error parsing ID
stuartleeks Sep 23, 2019
aa52a2a
Update website/docs/r/application_insights_analytics_item.html.markdown
stuartleeks Sep 26, 2019
8c0869e
Add extra import checks
stuartleeks Sep 26, 2019
d07ed3d
fix whitespace
stuartleeks Sep 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions azurerm/internal/services/applicationinsights/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ 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)

Expand All @@ -22,8 +26,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,
}
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
237 changes: 237 additions & 0 deletions azurerm/resource_arm_application_insights_analytics_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package azurerm

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"
"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,
ValidateFunc: validate.NoEmptyStrings,
},

"application_insights_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"version": {
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),
},

"function_alias": {
Type: schema.TypeString,
Optional: true,
},

"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 {
client := meta.(*ArmClient).appInsights.AnalyticsItemsClient
ctx := meta.(*ArmClient).StopContext

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()
itemID := ""
if id != "" {
_, _, _, itemID, err = resourcesArmApplicationInsightsAnalyticsItemParseID(id)
if err != nil {
return fmt.Errorf("Error parsing Application Insights Analytics Item ID %s: %s", id, err)
}
}

name := d.Get("name").(string)
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)
properties := insights.ApplicationInsightsComponentAnalyticsItem{
ID: &itemID,
Name: &name,
Type: itemType,
Scope: itemScope,
Content: &content,
}
if functionAlias != "" {
properties.Properties = &insights.ApplicationInsightsComponentAnalyticsItemProperties{
FunctionAlias: &functionAlias,
}
}

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)
}

// See comments in resourcesArmApplicationInsightsAnalyticsItemParseID method about ID format
generatedID := appInsightsID + resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(itemScope, *result.ID)
d.SetId(generatedID)

return resourceArmApplicationInsightsAnalyticsItemRead(d, meta)
}

func resourceArmApplicationInsightsAnalyticsItemRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appInsights.AnalyticsItemsClient
ctx := meta.(*ArmClient).StopContext

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)
}

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", itemID, resourceGroupName, appInsightsName, err)
}

idSuffix := resourcesArmApplicationInsightsAnalyticsItemGenerateIDSuffix(result.Scope, itemID)
appInsightsID := id[0 : len(id)-len(idSuffix)]
d.Set("application_insights_id", appInsightsID)
d.Set("name", result.Name)
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)

if result.Properties != nil {
d.Set("function_alias", result.Properties.FunctionAlias)
}

return nil
}

func resourceArmApplicationInsightsAnalyticsItemDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appInsights.AnalyticsItemsClient
ctx := meta.(*ArmClient).StopContext

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)
}

_, 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 nil
}

func resourcesArmApplicationInsightsAnalyticsItemParseID(id string) (string, string, insights.ItemScopePath, string, error) {
resourceID, err := azure.ParseAzureResourceID(id)
if err != nil {
return "", "", "", "", fmt.Errorf("Error parsing resource ID: %s", err)
}
resourceGroupName := resourceID.ResourceGroup
appInsightsName := resourceID.Path["components"]

// Use the following generated ID format:
// <appinsightsID>/analyticsItems/<itemID> [for shared scope items]
// <appinsightsID>/myanalyticsItems/<itemID> [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
}

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
}
}
Loading