Skip to content

Commit

Permalink
Merge pull request #48 from anweiss/multi-env
Browse files Browse the repository at this point in the history
Support custom configuration and `[]byte` cert constructor
  • Loading branch information
ahmetb committed Feb 27, 2015
2 parents 499c1df + 7d098a2 commit ae28d1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
60 changes: 40 additions & 20 deletions management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions management/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

const (
azureManagementDnsName = "https://management.core.windows.net"
msVersionHeader = "x-ms-version"
msVersionHeaderValue = "2014-05-01"
contentHeader = "Content-Type"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ae28d1e

Please sign in to comment.