forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_insights_smart_detection_rule_resource.go
232 lines (197 loc) · 8.75 KB
/
application_insights_smart_detection_rule_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package applicationinsights
import (
"fmt"
"log"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2020-02-02/insights"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/applicationinsights/migration"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/applicationinsights/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceApplicationInsightsSmartDetectionRule() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceApplicationInsightsSmartDetectionRuleUpdate,
Read: resourceApplicationInsightsSmartDetectionRuleRead,
Update: resourceApplicationInsightsSmartDetectionRuleUpdate,
Delete: resourceApplicationInsightsSmartDetectionRuleDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.SmartDetectionRuleID(id)
return err
}),
SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.SmartDetectionRuleUpgradeV0ToV1{},
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Slow page load time",
"Slow server response time",
"Long dependency duration",
"Degradation in server response time",
"Degradation in dependency duration",
// The below rules are currently preview and may change in future
"Degradation in trace severity ratio",
"Abnormal rise in exception volume",
"Potential memory leak detected",
"Potential security issue detected",
"Abnormal rise in daily data volume",
}, false),
DiffSuppressFunc: smartDetectionRuleNameDiff,
},
"application_insights_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"send_emails_to_subscription_owners": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"additional_email_recipients": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
},
},
}
}
func resourceApplicationInsightsSmartDetectionRuleUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppInsights.SmartDetectionRuleClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Application Insights Smart Detection Rule update.")
// The Smart Detection Rule name from the UI doesn't match what the API accepts.
// We'll have the user submit what the name looks like in the UI and convert it behind the scenes to match what the API accepts
name := convertUiNameToApiName(d.Get("name"))
appInsightsId, err := parse.ComponentID(d.Get("application_insights_id").(string))
if err != nil {
return err
}
id := parse.NewSmartDetectionRuleID(appInsightsId.SubscriptionId, appInsightsId.ResourceGroup, appInsightsId.Name, name)
smartDetectionRuleProperties := insights.ApplicationInsightsComponentProactiveDetectionConfiguration{
Name: &name,
Enabled: utils.Bool(d.Get("enabled").(bool)),
SendEmailsToSubscriptionOwners: utils.Bool(d.Get("send_emails_to_subscription_owners").(bool)),
CustomEmails: utils.ExpandStringSlice(d.Get("additional_email_recipients").(*pluginsdk.Set).List()),
}
_, err = client.Update(ctx, id.ResourceGroup, id.ComponentName, name, smartDetectionRuleProperties)
if err != nil {
return fmt.Errorf("updating Application Insights Smart Detection Rule %s: %+v", id, err)
}
d.SetId(id.ID())
return resourceApplicationInsightsSmartDetectionRuleRead(d, meta)
}
func resourceApplicationInsightsSmartDetectionRuleRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppInsights.SmartDetectionRuleClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.SmartDetectionRuleID(d.Id())
if err != nil {
return err
}
log.Printf("[DEBUG] Reading AzureRM Application Insights Smart Detection Rule %s", id)
result, err := client.Get(ctx, id.ResourceGroup, id.ComponentName, id.SmartDetectionRuleName)
if err != nil {
if utils.ResponseWasNotFound(result.Response) {
log.Printf("[WARN] AzureRM Application Insights Smart Detection Rule %s not found, removing from state", id)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on AzureRM Application Insights Smart Detection Rule %s: %+v", id, err)
}
d.Set("name", result.Name)
d.Set("application_insights_id", parse.NewComponentID(id.SubscriptionId, id.ResourceGroup, id.ComponentName).ID())
d.Set("enabled", result.Enabled)
d.Set("send_emails_to_subscription_owners", result.SendEmailsToSubscriptionOwners)
d.Set("additional_email_recipients", utils.FlattenStringSlice(result.CustomEmails))
return nil
}
func resourceApplicationInsightsSmartDetectionRuleDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).AppInsights.SmartDetectionRuleClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.SmartDetectionRuleID(d.Id())
if err != nil {
return err
}
log.Printf("[DEBUG] reseting AzureRM Application Insights Smart Detection Rule %s", id)
result, err := client.Get(ctx, id.ResourceGroup, id.ComponentName, id.SmartDetectionRuleName)
if err != nil {
if utils.ResponseWasNotFound(result.Response) {
log.Printf("[WARN] AzureRM Application Insights Smart Detection Rule %s not found, removing from state", id)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on AzureRM Application Insights Smart Detection Rule %s: %+v", id, err)
}
smartDetectionRuleProperties := insights.ApplicationInsightsComponentProactiveDetectionConfiguration{
Name: utils.String(id.SmartDetectionRuleName),
Enabled: result.RuleDefinitions.IsEnabledByDefault,
SendEmailsToSubscriptionOwners: result.RuleDefinitions.SupportsEmailNotifications,
CustomEmails: utils.ExpandStringSlice([]interface{}{}),
}
// Application Insights defaults all the Smart Detection Rules so if a user wants to delete a rule, we'll update it back to it's default values.
_, err = client.Update(ctx, id.ResourceGroup, id.ComponentName, id.SmartDetectionRuleName, smartDetectionRuleProperties)
if err != nil {
if utils.ResponseWasNotFound(result.Response) {
return nil
}
return fmt.Errorf("issuing AzureRM reset update request for Application Insights Smart Detection Rule %q: %+v", id.String(), err)
}
return nil
}
// The Smart Detection Rule name from the UI doesn't match what the API accepts.
// This Diff checks if the old and new name match when converted to the API version of the name
func smartDetectionRuleNameDiff(_, old string, new string, _ *pluginsdk.ResourceData) bool {
apiNew := convertUiNameToApiName(new)
return strings.EqualFold(old, apiNew)
}
func convertUiNameToApiName(uiName interface{}) string {
apiName := uiName.(string)
switch uiName.(string) {
case "Slow page load time":
apiName = "slowpageloadtime"
case "Slow server response time":
apiName = "slowserverresponsetime"
case "Long dependency duration":
apiName = "longdependencyduration"
case "Degradation in server response time":
apiName = "degradationinserverresponsetime"
case "Degradation in dependency duration":
apiName = "degradationindependencyduration"
case "Degradation in trace severity ratio":
apiName = "extension_traceseveritydetector"
case "Abnormal rise in exception volume":
apiName = "extension_exceptionchangeextension"
case "Potential memory leak detected":
apiName = "extension_memoryleakextension"
case "Potential security issue detected":
apiName = "extension_securityextensionspackage"
case "Abnormal rise in daily data volume":
apiName = "extension_billingdatavolumedailyspikeextension"
}
return apiName
}