diff --git a/management/client.go b/management/client.go index 9c040d7c1705..56cb0df6412d 100644 --- a/management/client.go +++ b/management/client.go @@ -2,17 +2,19 @@ package management import ( "encoding/xml" + "errors" "fmt" ) const ( + defaultAzureManagementURL = "https://management.core.windows.net" 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." ) -//AzureError represents an error returned by the management API. It has an error -//code (for example, ResourceNotFound) and a descriptive message. +// AzureError represents an error returned by the management API. It has an error +// code (for example, ResourceNotFound) and a descriptive message. type AzureError struct { XMLName xml.Name `xml:"Error"` Code string @@ -26,32 +28,50 @@ func (e *AzureError) Error() string { // Client provides a client to the Azure API. type Client struct { + managementURL string publishSettings publishSettings } -//NewAnonymouseClient creates a new azure.Client with no credentials set. +// ClientConfig provides a configuration for use by a Client +type ClientConfig struct { + ManagementURL string +} + +// NewAnonymousClient creates a new azure.Client with no credentials set. func NewAnonymousClient() Client { return Client{} } -//NewClientFromPublishSettingsFile creates a new azure.Client and imports the publish -//settings from the specified file path. -func NewClientFromPublishSettingsFile(publishSettingsFilePath string) (Client, error) { - client := Client{} - err := client.importPublishSettingsFile(publishSettingsFilePath) - if err != nil { - return client, err - } - return client, nil +// NewClient creates a new Client using the given subscription ID and +// management certificate +func NewClient(subscriptionID string, managementCert []byte) (Client, error) { + config := ClientConfig{ManagementURL: defaultAzureManagementURL} + return NewClientFromConfig(subscriptionID, managementCert, config) +} + +// NewClientFromConfig creates a new Client using a given ClientConfig +func NewClientFromConfig(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) { + return makeClient(subscriptionID, managementCert, config.ManagementURL) } -//NewClientFromPublishSettingsFile creates a new azure.Client and imports the publish -//settings from the specified file path. -func NewClientFromPublishSettings(subscriptionId string, managementCertificatePath string) (Client, error) { - client := Client{} - err := client.importPublishSettings(subscriptionId, managementCertificatePath) - if err != nil { - return client, err +func makeClient(subscriptionID string, managementCert []byte, managementURL string) (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 managementURL == "" { + return client, errors.New("azure: base URL required") + } + + publishSettings := publishSettings{ + SubscriptionID: subscriptionID, + SubscriptionCert: managementCert, + SubscriptionKey: managementCert, } - return client, nil + + return Client{ + managementURL: managementURL, + publishSettings: publishSettings, + }, nil } diff --git a/management/http.go b/management/http.go index d9f1753670bb..72521c3d0ef0 100644 --- a/management/http.go +++ b/management/http.go @@ -9,7 +9,6 @@ import ( ) const ( - azureManagementDnsName = "https://management.core.windows.net" msVersionHeader = "x-ms-version" msVersionHeaderValue = "2014-05-01" contentHeader = "Content-Type" @@ -158,7 +157,7 @@ func (client *Client) createAzureRequest(url string, requestType string, content var request *http.Request var err error - url = fmt.Sprintf("%s/%s/%s", azureManagementDnsName, client.publishSettings.SubscriptionID, url) + url = fmt.Sprintf("%s/%s/%s", client.managementURL, client.publishSettings.SubscriptionID, url) if data != nil { body := bytes.NewBuffer(data) request, err = http.NewRequest(requestType, url, body)