2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-05-29 00:10:21 +02:00
|
|
|
"sync"
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
2015-04-24 18:18:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure used to instantiate a
|
|
|
|
// new Azure management client.
|
|
|
|
type Config struct {
|
|
|
|
SettingsFile string
|
|
|
|
SubscriptionID string
|
2015-05-28 00:50:45 +02:00
|
|
|
Certificate []byte
|
|
|
|
ManagementURL string
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
// Client contains all the handles required for managing Azure services.
|
|
|
|
type Client struct {
|
|
|
|
// unfortunately; because of how Azure's network API works; doing networking operations
|
|
|
|
// concurrently is very hazardous, and we need a mutex to guard the management.Client.
|
|
|
|
mutex *sync.Mutex
|
|
|
|
mgmtClient management.Client
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
// NewClientFromSettingsFile returns a new Azure management
|
|
|
|
// client created using a publish settings file.
|
|
|
|
func (c *Config) NewClientFromSettingsFile() (*Client, error) {
|
|
|
|
if _, err := os.Stat(c.SettingsFile); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Publish Settings file %q does not exist!", c.SettingsFile)
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
mc, err := management.ClientFromPublishSettingsFile(c.SettingsFile, c.SubscriptionID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
return &Client{
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
mgmtClient: mc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Azure management client created
|
|
|
|
// using a subscription ID and certificate.
|
|
|
|
func (c *Config) NewClient() (*Client, error) {
|
|
|
|
mc, err := management.NewClient(c.SubscriptionID, c.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
return &Client{
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
mgmtClient: mc,
|
|
|
|
}, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|