-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
loganalytics.go
72 lines (61 loc) · 2.56 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
package azure
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2020-03-01-preview/operationalinsights"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
// LogAnalyticsWorkspaceExists indicates whether the operatonal insights workspaces exists.
// This function would fail the test if there is an error.
func LogAnalyticsWorkspaceExists(t testing.TestingT, workspaceName string, resourceGroupName string, subscriptionID string) bool {
exists, err := LogAnalyticsWorkspaceExistsE(workspaceName, resourceGroupName, subscriptionID)
require.NoError(t, err)
return exists
}
// GetLogAnalyticsWorkspace gets an operational insights workspace if it exists in a subscription.
// This function would fail the test if there is an error.
func GetLogAnalyticsWorkspace(t testing.TestingT, workspaceName string, resourceGroupName string, subscriptionID string) *operationalinsights.Workspace {
ws, err := GetLogAnalyticsWorkspaceE(workspaceName, resourceGroupName, subscriptionID)
require.NoError(t, err)
return ws
}
// GetLogAnalyticsWorkspaceE gets an operational insights workspace if it exists in a subscription.
func GetLogAnalyticsWorkspaceE(workspaceName, resoureGroupName, subscriptionID string) (*operationalinsights.Workspace, error) {
client, err := GetLogAnalyticsWorkspacesClientE(subscriptionID)
if err != nil {
return nil, err
}
ws, err := client.Get(context.Background(), resoureGroupName, workspaceName)
if err != nil {
return nil, err
}
return &ws, nil
}
// LogAnalyticsWorkspaceExistsE indicates whether the operatonal insights workspaces exists and may return an error.
func LogAnalyticsWorkspaceExistsE(workspaceName string, resourceGroupName string, subscriptionID string) (bool, error) {
_, err := GetLogAnalyticsWorkspaceE(workspaceName, resourceGroupName, subscriptionID)
if err != nil {
if ResourceNotFoundErrorExists(err) {
return false, nil
}
return false, err
}
return true, nil
}
// GetLogAnalyticsWorkspacesClientE return workspaces client; otherwise error.
func GetLogAnalyticsWorkspacesClientE(subscriptionID string) (*operationalinsights.WorkspacesClient, error) {
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
fmt.Println("Workspace client error getting subscription")
return nil, err
}
client := operationalinsights.NewWorkspacesClient(subscriptionID)
authorizer, err := NewAuthorizer()
if err != nil {
fmt.Println("authorizer error")
return nil, err
}
client.Authorizer = *authorizer
return &client, nil
}