forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
170 lines (143 loc) · 6.19 KB
/
client.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
// +build go1.7
// Package management provides the main API client to construct other clients
// and make requests to the Microsoft Azure Service Management REST API.
package management
// Copyright 2017 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"errors"
"fmt"
"runtime"
"time"
"github.com/Azure/azure-sdk-for-go/version"
)
var (
DefaultUserAgent = userAgent()
)
const (
DefaultAzureManagementURL = "https://management.core.windows.net"
DefaultOperationPollInterval = time.Second * 30
DefaultAPIVersion = "2014-10-01"
errPublishSettingsConfiguration = "PublishSettingsFilePath is set. Consequently ManagementCertificatePath and SubscriptionId must not be set."
errManagementCertificateConfiguration = "Both ManagementCertificatePath and SubscriptionId should be set, and PublishSettingsFilePath must not be set."
errParamNotSpecified = "Parameter %s is not specified."
)
type client struct {
publishSettings publishSettings
config ClientConfig
}
// Client is the base Azure Service Management API client instance that
// can be used to construct client instances for various services.
type Client interface {
// SendAzureGetRequest sends a request to the management API using the HTTP GET method
// and returns the response body or an error.
SendAzureGetRequest(url string) ([]byte, error)
// SendAzurePostRequest sends a request to the management API using the HTTP POST method
// and returns the request ID or an error.
SendAzurePostRequest(url string, data []byte) (OperationID, error)
// SendAzurePostRequestWithReturnedResponse sends a request to the management API using
// the HTTP POST method and returns the response body or an error.
SendAzurePostRequestWithReturnedResponse(url string, data []byte) ([]byte, error)
// SendAzurePutRequest sends a request to the management API using the HTTP PUT method
// and returns the request ID or an error. The content type can be specified, however
// if an empty string is passed, the default of "application/xml" will be used.
SendAzurePutRequest(url, contentType string, data []byte) (OperationID, error)
// SendAzureDeleteRequest sends a request to the management API using the HTTP DELETE method
// and returns the request ID or an error.
SendAzureDeleteRequest(url string) (OperationID, error)
// GetOperationStatus gets the status of operation with given Operation ID.
// WaitForOperation utility method can be used for polling for operation status.
GetOperationStatus(operationID OperationID) (GetOperationStatusResponse, error)
// WaitForOperation polls the Azure API for given operation ID indefinitely
// until the operation is completed with either success or failure.
// It is meant to be used for waiting for the result of the methods that
// return an OperationID value (meaning a long running operation has started).
//
// Cancellation of the polling loop (for instance, timing out) is done through
// cancel channel. If the user does not want to cancel, a nil chan can be provided.
// To cancel the method, it is recommended to close the channel provided to this
// method.
//
// If the operation was not successful or cancelling is signaled, an error
// is returned.
WaitForOperation(operationID OperationID, cancel chan struct{}) error
}
// ClientConfig provides a configuration for use by a Client.
type ClientConfig struct {
ManagementURL string
OperationPollInterval time.Duration
UserAgent string
APIVersion string
}
// NewAnonymousClient creates a new azure.Client with no credentials set.
func NewAnonymousClient() Client {
return client{}
}
// DefaultConfig returns the default client configuration used to construct
// a client. This value can be used to make modifications on the default API
// configuration.
func DefaultConfig() ClientConfig {
return ClientConfig{
ManagementURL: DefaultAzureManagementURL,
OperationPollInterval: DefaultOperationPollInterval,
APIVersion: DefaultAPIVersion,
UserAgent: DefaultUserAgent,
}
}
// NewClient creates a new Client using the given subscription ID and
// management certificate.
func NewClient(subscriptionID string, managementCert []byte) (Client, error) {
return NewClientFromConfig(subscriptionID, managementCert, DefaultConfig())
}
// NewClientFromConfig creates a new Client using a given ClientConfig.
func NewClientFromConfig(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) {
return makeClient(subscriptionID, managementCert, config)
}
func makeClient(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) {
var c client
if subscriptionID == "" {
return c, errors.New("azure: subscription ID required")
}
if len(managementCert) == 0 {
return c, errors.New("azure: management certificate required")
}
publishSettings := publishSettings{
SubscriptionID: subscriptionID,
SubscriptionCert: managementCert,
SubscriptionKey: managementCert,
}
// Validate client configuration
switch {
case config.ManagementURL == "":
return c, errors.New("azure: base URL required")
case config.OperationPollInterval <= 0:
return c, errors.New("azure: operation polling interval must be a positive duration")
case config.APIVersion == "":
return c, errors.New("azure: client configuration must specify an API version")
case config.UserAgent == "":
config.UserAgent = DefaultUserAgent
}
return client{
publishSettings: publishSettings,
config: config,
}, nil
}
func userAgent() string {
return fmt.Sprintf("Go/%s (%s-%s) Azure-SDK-For-Go/%s asm/%s",
runtime.Version(),
runtime.GOARCH,
runtime.GOOS,
version.Number,
DefaultAPIVersion)
}