forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managementclient.go
92 lines (77 loc) · 2.49 KB
/
managementclient.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
// +build go1.7
// Package testutils contains some test utilities for the Azure SDK
package testutils
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import (
"encoding/base64"
"os"
"testing"
"github.com/Azure/azure-sdk-for-go/services/classic/management"
)
// GetTestClient returns a management Client for testing. Expects
// AZSUBSCRIPTIONID and AZCERTDATA to be present in the environment. AZCERTDATA
// is the base64encoded binary representation of the PEM certificate data.
func GetTestClient(t *testing.T) management.Client {
subid := os.Getenv("AZSUBSCRIPTIONID")
certdata := os.Getenv("AZCERTDATA")
if subid == "" || certdata == "" {
t.Skip("AZSUBSCRIPTIONID or AZCERTDATA not set, skipping test")
}
cert, err := base64.StdEncoding.DecodeString(certdata)
if err != nil {
t.Fatal(err)
}
client, err := management.NewClient(subid, cert)
if err != nil {
t.Fatal(err)
}
return testClient{client, t}
}
type testClient struct {
management.Client
t *testing.T
}
func chop(d []byte) string {
const maxlen = 5000
s := string(d)
if len(s) > maxlen {
return s[:maxlen] + "..."
}
return s
}
func (l testClient) SendAzureGetRequest(url string) ([]byte, error) {
d, err := l.Client.SendAzureGetRequest(url)
logOperation(l.t, "GET", url, nil, d, "", err)
return d, err
}
func (l testClient) SendAzurePostRequest(url string, data []byte) (management.OperationID, error) {
oid, err := l.Client.SendAzurePostRequest(url, data)
logOperation(l.t, "POST", url, data, nil, oid, err)
return oid, err
}
func (l testClient) SendAzurePutRequest(url string, contentType string, data []byte) (management.OperationID, error) {
oid, err := l.Client.SendAzurePutRequest(url, contentType, data)
logOperation(l.t, "PUT", url, data, nil, oid, err)
return oid, err
}
func (l testClient) SendAzureDeleteRequest(url string) (management.OperationID, error) {
oid, err := l.Client.SendAzureDeleteRequest(url)
logOperation(l.t, "DELETE", url, nil, nil, oid, err)
return oid, err
}
func logOperation(t *testing.T, method, url string, requestData, responseData []byte, oid management.OperationID, err error) {
t.Logf("AZURE> %s %s\n", method, url)
if requestData != nil {
t.Logf(" >>> %s\n", chop(requestData))
}
if err != nil {
t.Logf(" <<< ERROR: %+v\n", err)
} else {
if responseData != nil {
t.Logf(" <<< %s\n", chop(responseData))
} else {
t.Logf(" <<< OperationID: %s\n", oid)
}
}
}