This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
loganalytics.go
261 lines (228 loc) · 8.71 KB
/
loganalytics.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package armhelpers
import (
"context"
"fmt"
"strings"
oi "github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
om "github.com/Azure/azure-sdk-for-go/services/preview/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement"
"github.com/Azure/go-autorest/autorest/to"
)
// EnsureDefaultLogAnalyticsWorkspace ensures the default log analytics exists corresponding to specified location in selected subscription
func (az *AzureClient) EnsureDefaultLogAnalyticsWorkspace(ctx context.Context, resourceGroup, location string) (workspaceResourceID string, err error) {
AzureCloudLocationToOmsRegionCodeMap := map[string]string{
"australiasoutheast": "ASE",
"australiaeast": "EAU",
"australiacentral": "CAU",
"canadacentral": "CCA",
"centralindia": "CIN",
"centralus": "CUS",
"eastasia": "EA",
"eastus": "EUS",
"eastus2": "EUS2",
"eastus2euap": "EAP",
"francecentral": "PAR",
"japaneast": "EJP",
"koreacentral": "SE",
"northeurope": "NEU",
"southcentralus": "SCUS",
"southeastasia": "SEA",
"uksouth": "SUK",
"usgovvirginia": "USGV",
"westcentralus": "EUS",
"westeurope": "WEU",
"westus": "WUS",
"westus2": "WUS2",
}
AzureCloudRegionToOmsRegionMap := map[string]string{
"australiacentral": "australiacentral",
"australiacentral2": "australiacentral",
"australiaeast": "australiaeast",
"australiasoutheast": "australiasoutheast",
"brazilsouth": "southcentralus",
"canadacentral": "canadacentral",
"canadaeast": "canadacentral",
"centralus": "centralus",
"centralindia": "centralindia",
"eastasia": "eastasia",
"eastus": "eastus",
"eastus2": "eastus2",
"francecentral": "francecentral",
"francesouth": "francecentral",
"japaneast": "japaneast",
"japanwest": "japaneast",
"koreacentral": "koreacentral",
"koreasouth": "koreacentral",
"northcentralus": "eastus",
"northeurope": "northeurope",
"southafricanorth": "westeurope",
"southafricawest": "westeurope",
"southcentralus": "southcentralus",
"southeastasia": "southeastasia",
"southindia": "centralindia",
"uksouth": "uksouth",
"ukwest": "uksouth",
"westcentralus": "eastus",
"westeurope": "westeurope",
"westindia": "centralindia",
"westus": "westus",
"westus2": "westus2",
}
AzureChinaLocationToOmsRegionCodeMap := map[string]string{
"chinaeast": "EAST2",
"chinaeast2": "EAST2",
"chinanorth": "EAST2",
"chinanorth2": "EAST2",
}
AzureChinaRegionToOmsRegionMap := map[string]string{
"chinaeast": "chinaeast2",
"chinaeast2": "chinaeast2",
"chinanorth": "chinaeast2",
"chinanorth2": "chinaeast2",
}
AzureFairfaxLocationToOmsRegionCodeMap := map[string]string{
"usgovvirginia": "USGV",
}
AzureFairfaxRegionToOmsRegionMap := map[string]string{
"usgovvirginia": "usgovvirginia",
}
defaultWorkspaceRegion := "eastus"
defaultWorkspaceRegionCode := "EUS"
switch az.environment.Name {
case "AzurePublicCloud":
if region, found := AzureCloudRegionToOmsRegionMap[location]; found {
defaultWorkspaceRegion = region
}
if regionCode, found := AzureCloudLocationToOmsRegionCodeMap[defaultWorkspaceRegion]; found {
defaultWorkspaceRegionCode = regionCode
}
case "AzureChinaCloud":
if region, found := AzureChinaRegionToOmsRegionMap[location]; found {
defaultWorkspaceRegion = region
} else {
defaultWorkspaceRegion = "chinaeast2"
}
if regionCode, found := AzureChinaLocationToOmsRegionCodeMap[defaultWorkspaceRegion]; found {
defaultWorkspaceRegionCode = regionCode
} else {
defaultWorkspaceRegionCode = "EAST2"
}
case "AzureUSGovernmentCloud":
if region, found := AzureFairfaxLocationToOmsRegionCodeMap[location]; found {
defaultWorkspaceRegion = region
} else {
defaultWorkspaceRegion = "usgovvirginia"
}
if regionCode, found := AzureFairfaxRegionToOmsRegionMap[defaultWorkspaceRegion]; found {
defaultWorkspaceRegionCode = regionCode
} else {
defaultWorkspaceRegionCode = "USGV"
}
default:
return "", fmt.Errorf("container monitoring addon not supported in this cloud: %s", az.environment.Name)
}
if region, found := AzureCloudRegionToOmsRegionMap[location]; found {
defaultWorkspaceRegion = region
}
if regionCode, found := AzureCloudLocationToOmsRegionCodeMap[defaultWorkspaceRegion]; found {
defaultWorkspaceRegionCode = regionCode
}
defaultWorkspaceResourceGroup := fmt.Sprintf("DefaultResourceGroup-%s", defaultWorkspaceRegionCode)
defaultWorkspaceName := fmt.Sprintf("DefaultWorkspace-%s-%s", az.subscriptionID, defaultWorkspaceRegionCode)
resp, err := az.CheckResourceGroupExistence(ctx, defaultWorkspaceResourceGroup)
if err != nil {
return "", err
}
if resp.StatusCode == 404 {
_, err = az.EnsureResourceGroup(ctx, defaultWorkspaceResourceGroup, defaultWorkspaceRegion, nil)
if err != nil {
return "", err
}
}
wsList, err := az.workspacesClient.ListByResourceGroup(ctx, defaultWorkspaceResourceGroup)
if err != nil {
if wsList.Response.StatusCode != 404 {
return "", err
}
}
if wsList.Value != nil {
for _, ws := range *wsList.Value {
if strings.EqualFold(defaultWorkspaceName, *ws.Name) {
return *ws.ID, nil
}
}
}
WorkspaceParameters := oi.Workspace{
Location: &defaultWorkspaceRegion,
WorkspaceProperties: &oi.WorkspaceProperties{
Sku: &oi.Sku{
Name: oi.Standalone,
},
},
}
future, err := az.workspacesClient.CreateOrUpdate(ctx, defaultWorkspaceResourceGroup, defaultWorkspaceName, WorkspaceParameters)
if err != nil {
return "", err
}
err = future.WaitForCompletionRef(ctx, az.workspacesClient.Client)
if err != nil {
return "", err
}
ws, err := future.Result(az.workspacesClient)
if err != nil {
return "", err
}
return *ws.ID, nil
}
// GetLogAnalyticsWorkspaceInfo gets the details about the workspace
func (az *AzureClient) GetLogAnalyticsWorkspaceInfo(ctx context.Context, workspaceSubscriptionID, workspaceResourceGroup, workspaceName string) (workspaceID string, workspaceKey string, workspaceLocation string, err error) {
if !strings.EqualFold(workspaceSubscriptionID, az.subscriptionID) {
az.workspacesClient = oi.NewWorkspacesClientWithBaseURI(az.environment.ResourceManagerEndpoint, workspaceSubscriptionID)
az.workspacesClient.Authorizer = az.authorizationClient.Authorizer
}
resp, err := az.workspacesClient.Get(ctx, workspaceResourceGroup, workspaceName)
if err != nil {
return "", "", "", err
}
workspaceID = *resp.WorkspaceProperties.CustomerID
workspaceLocation = *resp.Location
result, err := az.workspacesClient.GetSharedKeys(ctx, workspaceResourceGroup, workspaceName)
if err != nil {
return "", "", "", err
}
return workspaceID, *result.PrimarySharedKey, workspaceLocation, nil
}
// AddContainerInsightsSolution adds container insights solution for the specified log analytics workspace
func (az *AzureClient) AddContainerInsightsSolution(ctx context.Context, workspaceSubscriptionID, workspaceResourceGroup, workspaceName, workspaceLocation string) (result bool, err error) {
solutionClient := om.NewSolutionsClientWithBaseURI(az.environment.ResourceManagerEndpoint, workspaceSubscriptionID, "Microsoft.OperationalInsights", "workspaces", workspaceName)
solutionClient.Authorizer = az.workspacesClient.Authorizer
solutionName := "ContainerInsights(" + workspaceName + ")"
workspaceResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.OperationalInsights/workspaces/%s", workspaceSubscriptionID, workspaceResourceGroup, workspaceName)
status, err := solutionClient.CreateOrUpdate(ctx, workspaceResourceGroup, solutionName, om.Solution{
Name: to.StringPtr(solutionName),
Type: to.StringPtr("Microsoft.OperationsManagement/solutions"),
Location: to.StringPtr(workspaceLocation),
Plan: &om.SolutionPlan{
Name: to.StringPtr(solutionName),
Publisher: to.StringPtr("Microsoft"),
PromotionCode: to.StringPtr(""),
Product: to.StringPtr("OMSGallery/ContainerInsights"),
},
Properties: &om.SolutionProperties{
WorkspaceResourceID: to.StringPtr(workspaceResourceID),
},
})
if err != nil {
return false, err
}
err = status.Future.WaitForCompletionRef(ctx, solutionClient.Client)
if err != nil {
return false, err
}
solution, err := status.Result(solutionClient)
if err != nil {
return false, err
}
return strings.EqualFold(*solution.Name, solutionName), nil
}