Merge pull request #127 from ahmetalpbalkan/apiversion

management: Custom API versions and User Agent Header
This commit is contained in:
Paul Meyer 2015-05-15 10:11:09 -07:00
Родитель f70269e81f 95b5fb116f
Коммит fb196506a9
4 изменённых файлов: 37 добавлений и 19 удалений

Просмотреть файл

@ -6,8 +6,11 @@ import (
)
const (
defaultAzureManagementURL = "https://management.core.windows.net"
defaultOperationPollInterval = time.Second * 30
DefaultAzureManagementURL = "https://management.core.windows.net"
DefaultOperationPollInterval = time.Second * 30
DefaultAPIVersion = "2014-10-01"
DefaultUserAgent = "azure-sdk-for-go"
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."
@ -15,15 +18,16 @@ const (
// Client provides a client to the Azure API.
type Client struct {
managementURL string
publishSettings publishSettings
pollInterval time.Duration
config ClientConfig
}
// 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.
@ -31,17 +35,22 @@ func NewAnonymousClient() Client {
return Client{}
}
func defaultConfig() ClientConfig {
// 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,
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())
return NewClientFromConfig(subscriptionID, managementCert, DefaultConfig())
}
// NewClientFromConfig creates a new Client using a given ClientConfig
@ -51,14 +60,11 @@ func NewClientFromConfig(subscriptionID string, managementCert []byte, config Cl
func makeClient(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) {
var client Client
if subscriptionID == "" {
return client, errors.New("azure: subscription ID required")
} else if len(managementCert) == 0 {
return client, errors.New("azure: management certificate required")
} else if config.ManagementURL == "" {
return client, errors.New("azure: base URL required")
} else if config.OperationPollInterval <= 0 {
return client, errors.New("azure: operation polling interval must be a positive duration")
}
publishSettings := publishSettings{
@ -67,9 +73,19 @@ func makeClient(subscriptionID string, managementCert []byte, config ClientConfi
SubscriptionKey: managementCert,
}
// Validate client configuration
if config.ManagementURL == "" {
return client, errors.New("azure: base URL required")
} else if config.OperationPollInterval <= 0 {
return client, errors.New("azure: operation polling interval must be a positive duration")
} else if config.APIVersion == "" {
return client, errors.New("azure: client configuration must specify an API version")
} else if config.UserAgent == "" {
config.UserAgent = DefaultUserAgent
}
return Client{
managementURL: config.ManagementURL,
publishSettings: publishSettings,
pollInterval: config.OperationPollInterval,
config: config,
}, nil
}

Просмотреть файл

@ -11,7 +11,7 @@ import (
const (
msVersionHeader = "x-ms-version"
msVersionHeaderValue = "2014-10-01"
uaHeader = "User-Agent"
contentHeader = "Content-Type"
defaultContentHeaderValue = "application/xml"
requestIDHeader = "X-Ms-Request-Id"
@ -143,7 +143,7 @@ func (client *Client) createAzureRequest(url string, requestType string, content
var request *http.Request
var err error
url = fmt.Sprintf("%s/%s/%s", client.managementURL, client.publishSettings.SubscriptionID, url)
url = fmt.Sprintf("%s/%s/%s", client.config.ManagementURL, client.publishSettings.SubscriptionID, url)
if data != nil {
body := bytes.NewBuffer(data)
request, err = http.NewRequest(requestType, url, body)
@ -155,7 +155,9 @@ func (client *Client) createAzureRequest(url string, requestType string, content
return nil, err
}
request.Header.Add(msVersionHeader, msVersionHeaderValue)
request.Header.Add(msVersionHeader, client.config.APIVersion)
request.Header.Add(uaHeader, client.config.UserAgent)
if len(contentType) > 0 {
request.Header.Add(contentHeader, contentType)
} else {

Просмотреть файл

@ -69,7 +69,7 @@ func (client *Client) WaitAsyncOperation(operationID OperationID) error {
}
return fmt.Errorf("Azure Operation ID=%s has failed", operationID)
case OperationStatusInProgress:
time.Sleep(client.pollInterval)
time.Sleep(client.config.OperationPollInterval)
default:
return fmt.Errorf("Unknown operation status:%s (ID=%s)", operation.Status, operationID)
}

Просмотреть файл

@ -13,7 +13,7 @@ import (
// ClientFromPublishSettingsFile reads a publish settings file downloaded from https://manage.windowsazure.com/publishsettings.
// If subscriptionID is left empty, the first subscription in the file is used.
func ClientFromPublishSettingsFile(filePath, subscriptionID string) (client Client, err error) {
return ClientFromPublishSettingsFileWithConfig(filePath, subscriptionID, defaultConfig())
return ClientFromPublishSettingsFileWithConfig(filePath, subscriptionID, DefaultConfig())
}
// ClientFromPublishSettingsFileWithConfig reads a publish settings file downloaded from https://manage.windowsazure.com/publishsettings.