forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_log_analytics_workspace.go
214 lines (174 loc) · 5.58 KB
/
resource_arm_log_analytics_workspace.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
package azurerm
import (
"fmt"
"log"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmLogAnalyticsWorkspace() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogAnalyticsWorkspaceCreateUpdate,
Read: resourceArmLogAnalyticsWorkspaceRead,
Update: resourceArmLogAnalyticsWorkspaceCreateUpdate,
Delete: resourceArmLogAnalyticsWorkspaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRmLogAnalyticsWorkspaceName,
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameDiffSuppressSchema(),
"sku": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(operationalinsights.Free),
string(operationalinsights.PerNode),
string(operationalinsights.Premium),
string(operationalinsights.Standalone),
string(operationalinsights.Standard),
string(operationalinsights.Unlimited),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},
"retention_in_days": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(30, 730),
},
"workspace_id": {
Type: schema.TypeString,
Computed: true,
},
"portal_url": {
Type: schema.TypeString,
Computed: true,
},
"primary_shared_key": {
Type: schema.TypeString,
Computed: true,
},
"secondary_shared_key": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmLogAnalyticsWorkspaceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).workspacesClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for AzureRM Log Analytics workspace creation.")
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
skuName := d.Get("sku").(string)
sku := &operationalinsights.Sku{
Name: operationalinsights.SkuNameEnum(skuName),
}
retentionInDays := int32(d.Get("retention_in_days").(int))
tags := d.Get("tags").(map[string]interface{})
parameters := operationalinsights.Workspace{
Name: &name,
Location: &location,
Tags: expandTags(tags),
WorkspaceProperties: &operationalinsights.WorkspaceProperties{
Sku: sku,
RetentionInDays: &retentionInDays,
},
}
future, err := client.CreateOrUpdate(ctx, resGroup, name, parameters)
if err != nil {
return err
}
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return err
}
read, err := client.Get(ctx, resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Log Analytics Workspace '%s' (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmLogAnalyticsWorkspaceRead(d, meta)
}
func resourceArmLogAnalyticsWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).workspacesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["workspaces"]
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM Log Analytics workspaces '%s': %+v", name, err)
}
d.Set("name", resp.Name)
d.Set("location", resp.Location)
d.Set("resource_group_name", resGroup)
d.Set("workspace_id", resp.CustomerID)
d.Set("portal_url", resp.PortalURL)
if sku := resp.Sku; sku != nil {
d.Set("sku", sku.Name)
}
d.Set("retention_in_days", resp.RetentionInDays)
sharedKeys, err := client.GetSharedKeys(ctx, resGroup, name)
if err != nil {
log.Printf("[ERROR] Unable to List Shared keys for Log Analytics workspaces %s: %+v", name, err)
} else {
d.Set("primary_shared_key", sharedKeys.PrimarySharedKey)
d.Set("secondary_shared_key", sharedKeys.SecondarySharedKey)
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func resourceArmLogAnalyticsWorkspaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).workspacesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["workspaces"]
resp, err := client.Delete(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
}
return fmt.Errorf("Error issuing AzureRM delete request for Log Analytics Workspaces '%s': %+v", name, err)
}
return nil
}
func validateAzureRmLogAnalyticsWorkspaceName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
r, _ := regexp.Compile("^[A-Za-z0-9][A-Za-z0-9-]+[A-Za-z0-9]$")
if !r.MatchString(value) {
errors = append(errors, fmt.Errorf("Workspace Name can only contain alphabet, number, and '-' character. You can not use '-' as the start and end of the name"))
}
length := len(value)
if length > 63 || 4 > length {
errors = append(errors, fmt.Errorf("Workspace Name can only be between 4 and 63 letters"))
}
return
}