forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analytics_workspace_table_resource.go
More file actions
293 lines (244 loc) · 9.5 KB
/
log_analytics_workspace_table_resource.go
File metadata and controls
293 lines (244 loc) · 9.5 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package loganalytics
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/tables"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
type LogAnalyticsWorkspaceTableResource struct{}
var (
_ sdk.ResourceWithUpdate = LogAnalyticsWorkspaceTableResource{}
_ sdk.ResourceWithCustomizeDiff = LogAnalyticsWorkspaceTableResource{}
)
type LogAnalyticsWorkspaceTableResourceModel struct {
Name string `tfschema:"name"`
WorkspaceId string `tfschema:"workspace_id"`
Plan string `tfschema:"plan"`
RetentionInDays int64 `tfschema:"retention_in_days"`
TotalRetentionInDays int64 `tfschema:"total_retention_in_days"`
}
func (r LogAnalyticsWorkspaceTableResource) CustomizeDiff() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
rd := metadata.ResourceDiff
if string(tables.TablePlanEnumBasic) == rd.Get("plan").(string) {
if v, ok := rd.GetOk("retention_in_days"); ok && v.(int) != 30 {
return fmt.Errorf("cannot set retention_in_days because the retention is fixed at 30 days on Basic plan")
}
}
return nil
},
}
}
func (r LogAnalyticsWorkspaceTableResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: workspaces.ValidateWorkspaceID,
},
"name": {
Type: pluginsdk.TypeString,
Required: true,
},
"plan": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(tables.TablePlanEnumAnalytics),
ValidateFunc: validation.StringInSlice([]string{
string(tables.TablePlanEnumAnalytics),
string(tables.TablePlanEnumBasic),
}, false),
},
"retention_in_days": {
Type: pluginsdk.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(4, 730),
},
"total_retention_in_days": {
Type: pluginsdk.TypeInt,
Optional: true,
ValidateFunc: validation.Any(validation.IntBetween(4, 730), validation.IntInSlice([]int{1095, 1460, 1826, 2191, 2556, 2922, 3288, 3653, 4018, 4383})),
},
}
}
func (r LogAnalyticsWorkspaceTableResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}
func (r LogAnalyticsWorkspaceTableResource) ModelObject() interface{} {
return &LogAnalyticsWorkspaceTableResourceModel{}
}
func (r LogAnalyticsWorkspaceTableResource) ResourceType() string {
return "azurerm_log_analytics_workspace_table"
}
func (r LogAnalyticsWorkspaceTableResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return tables.ValidateTableID
}
func (r LogAnalyticsWorkspaceTableResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model LogAnalyticsWorkspaceTableResourceModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding %+v", err)
}
client := metadata.Client.LogAnalytics.TablesClient
tableName := model.Name
log.Printf("[INFO] preparing arguments for AzureRM Log Analytics Workspace Table %s update.", tableName)
workspaceId, err := workspaces.ParseWorkspaceID(model.WorkspaceId)
if err != nil {
return fmt.Errorf("invalid workspace object ID for table %s: %s", tableName, err)
}
id := tables.NewTableID(workspaceId.SubscriptionId, workspaceId.ResourceGroupName, workspaceId.WorkspaceName, tableName)
updateInput := tables.Table{
Properties: &tables.TableProperties{
Plan: pointer.To(tables.TablePlanEnum(model.Plan)),
},
}
if model.Plan == string(tables.TablePlanEnumAnalytics) {
// The service will return HTTP 400 if it's specified `0` in payload, to keep it as default, we need to pass `-1`
updateInput.Properties.RetentionInDays = pointer.FromInt64(-1)
// `0` is not a valid value for `retention_in_days`, so we can use it to validate if it's specified.
if model.RetentionInDays != 0 {
updateInput.Properties.RetentionInDays = pointer.To(model.RetentionInDays)
}
}
if model.TotalRetentionInDays != 0 {
updateInput.Properties.TotalRetentionInDays = pointer.To(model.TotalRetentionInDays)
}
if err := client.CreateOrUpdateThenPoll(ctx, id, updateInput); err != nil {
return fmt.Errorf("failed to update table %s in workspace %s in resource group %s: %s", tableName, workspaceId.WorkspaceName, workspaceId.ResourceGroupName, err)
}
metadata.SetID(id)
return nil
},
}
}
func (r LogAnalyticsWorkspaceTableResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.LogAnalytics.TablesClient
id, err := tables.ParseTableID(metadata.ResourceData.Id())
if err != nil {
return err
}
var state LogAnalyticsWorkspaceTableResourceModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}
existing, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("reading Log Analytics Workspace Table %s: %v", id, err)
}
if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
updateInput := tables.Table{
Properties: &tables.TableProperties{
Plan: props.Plan,
},
}
if metadata.ResourceData.HasChange("plan") {
updateInput.Properties.Plan = pointer.To(tables.TablePlanEnum(state.Plan))
}
if state.Plan == string(tables.TablePlanEnumAnalytics) {
updateInput.Properties.RetentionInDays = existing.Model.Properties.RetentionInDays
if metadata.ResourceData.HasChange("retention_in_days") {
updateInput.Properties.RetentionInDays = pointer.To(state.RetentionInDays)
// `0` is not a valid value for `retention_in_days`, and the service will return HTTP 400
// to reset it to its default value, we need to pass `-1`
if state.RetentionInDays == 0 {
updateInput.Properties.RetentionInDays = pointer.FromInt64(-1)
}
}
}
if metadata.ResourceData.HasChange("total_retention_in_days") {
updateInput.Properties.TotalRetentionInDays = pointer.To(state.TotalRetentionInDays)
}
if err := client.UpdateThenPoll(ctx, *id, updateInput); err != nil {
return fmt.Errorf("failed to update table: %s: %+v", id.TableName, err)
}
}
}
return nil
},
}
}
func (r LogAnalyticsWorkspaceTableResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := tables.ParseTableID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("while parsing resource ID: %+v", err)
}
workspaceId, err := workspaces.ParseWorkspaceID(metadata.ResourceData.Get("workspace_id").(string))
if err != nil {
return fmt.Errorf("while parsing resource ID: %+v", err)
}
client := metadata.Client.LogAnalytics.TablesClient
resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving Log Analytics Workspace Table %s: %+v", *id, err)
}
state := LogAnalyticsWorkspaceTableResourceModel{
Name: id.TableName,
WorkspaceId: workspaceId.ID(),
}
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
if pointer.From(props.Plan) == tables.TablePlanEnumAnalytics {
state.RetentionInDays = pointer.From(props.RetentionInDays)
}
state.TotalRetentionInDays = pointer.From(props.TotalRetentionInDays)
state.Plan = string(pointer.From(props.Plan))
}
}
return metadata.Encode(&state)
},
}
}
func (r LogAnalyticsWorkspaceTableResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model LogAnalyticsWorkspaceTableResourceModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding %+v", err)
}
client := metadata.Client.LogAnalytics.TablesClient
id, err := tables.ParseTableID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("while parsing resource ID: %+v", err)
}
// We do not delete the resource here, just set the retention to workspace default value, which is
// achieved by setting the value to `-1`
retentionInDays := utils.Int64(-1)
totalRetentionInDays := utils.Int64(-1)
updateInput := tables.Table{
Properties: &tables.TableProperties{
RetentionInDays: retentionInDays,
TotalRetentionInDays: totalRetentionInDays,
},
}
if err := client.CreateOrUpdateThenPoll(ctx, *id, updateInput); err != nil {
return fmt.Errorf("failed to update table %s in workspace %s in resource group %s: %s", id.TableName, id.WorkspaceName, id.ResourceGroupName, err)
}
return nil
},
}
}