Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi environment support #47

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions azure/client.go

This file was deleted.

77 changes: 77 additions & 0 deletions management/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package azure

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.
type AzureError struct {
XMLName xml.Name `xml:"Error"`
Code string
Message string
}

// Error implements the error interface for the AzureError type.
func (e *AzureError) Error() string {
return fmt.Sprintf("Error response from Azure. Code: %s, Message: %s", e.Code, e.Message)
}

// Client provides a client to the Azure API.
type Client struct {
publishSettings publishSettings
baseURL string
}

// ClientConfig provides a configuration for use by a Client.
type ClientConfig struct {
ManagementCert []byte
BaseURL string
}

// NewAnonymousClient creates a new azure.Client with no properties set.
func NewAnonymousClient() Client {
return Client{}
}

// NewClient creates a new azure.Client using the given subscription ID and
// management certificate.
func NewClient(subscriptionID string, managementCert []byte) (Client, error) {
return makeClient(subscriptionID, managementCert, defaultAzureManagementURL)
}

// NewClientFromConfig creates a new azure.Client using a given ClientConfig
func NewClientFromConfig(subscriptionID string, config ClientConfig) (Client, error) {
return makeClient(subscriptionID, config.ManagementCert, config.BaseURL)
}

func makeClient(subscriptionID string, managementCert []byte, baseURL 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 baseURL == "" {
return client, errors.New("azure: base URL required")
}

publishSettings := publishSettings{
SubscriptionID: subscriptionID,
SubscriptionCert: managementCert,
SubscriptionKey: managementCert,
}

return Client{
baseURL: baseURL,
publishSettings: publishSettings,
}, nil
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions azure/http.go → 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,8 @@ 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.baseURL, client.publishSettings.SubscriptionID, url)

if data != nil {
body := bytes.NewBuffer(data)
request, err = http.NewRequest(requestType, url, body)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 0 additions & 20 deletions azure/publishSettings.go → management/publishSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ import (
"io/ioutil"
)

func (client *Client) importPublishSettings(id string, certPath string) error {
if len(id) == 0 {
return fmt.Errorf(errParamNotSpecified, "id")
}
if len(certPath) == 0 {
return fmt.Errorf(errParamNotSpecified, "certPath")
}

cert, err := ioutil.ReadFile(certPath)
if err != nil {
return err
}

client.publishSettings.SubscriptionID = id
client.publishSettings.SubscriptionCert = cert
client.publishSettings.SubscriptionKey = cert

return nil
}

func (client *Client) importPublishSettingsFile(filePath string) error {
if len(filePath) == 0 {
return fmt.Errorf(errParamNotSpecified, "filePath")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.