2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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-06-19 18:52:36 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management/affinitygroup"
|
2015-06-16 19:54:52 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/networksecuritygroup"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/osimage"
|
2015-06-17 07:04:10 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management/sql"
|
2015-06-16 19:54:52 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management/storageservice"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachinedisk"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachineimage"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualnetwork"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
2015-04-24 18:18:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure used to instantiate a
|
|
|
|
// new Azure management client.
|
|
|
|
type Config struct {
|
2015-08-03 22:12:30 +02:00
|
|
|
Settings []byte
|
2015-04-24 18:18:24 +02:00
|
|
|
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 {
|
2015-06-16 19:54:52 +02:00
|
|
|
mgmtClient management.Client
|
|
|
|
|
2015-06-19 18:52:36 +02:00
|
|
|
affinityGroupClient affinitygroup.AffinityGroupClient
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
hostedServiceClient hostedservice.HostedServiceClient
|
|
|
|
|
|
|
|
secGroupClient networksecuritygroup.SecurityGroupClient
|
|
|
|
|
|
|
|
osImageClient osimage.OSImageClient
|
|
|
|
|
2015-06-19 18:39:50 +02:00
|
|
|
sqlClient sql.SQLDatabaseClient
|
2015-06-17 07:04:10 +02:00
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
storageServiceClient storageservice.StorageServiceClient
|
|
|
|
|
|
|
|
vmClient virtualmachine.VirtualMachineClient
|
|
|
|
|
|
|
|
vmDiskClient virtualmachinedisk.DiskClient
|
|
|
|
|
|
|
|
vmImageClient virtualmachineimage.Client
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
// unfortunately; because of how Azure's network API works; doing networking operations
|
2015-06-16 19:54:52 +02:00
|
|
|
// concurrently is very hazardous, and we need a mutex to guard the VirtualNetworkClient.
|
|
|
|
vnetClient virtualnetwork.VirtualNetworkClient
|
2015-05-29 00:10:21 +02:00
|
|
|
mutex *sync.Mutex
|
2015-06-16 19:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getStorageClientForStorageService is helper method which returns the
|
|
|
|
// storage.Client associated to the given storage service name.
|
|
|
|
func (c Client) getStorageClientForStorageService(serviceName string) (storage.Client, error) {
|
|
|
|
var storageClient storage.Client
|
|
|
|
|
|
|
|
keys, err := c.storageServiceClient.GetStorageServiceKeys(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return storageClient, fmt.Errorf("Failed getting Storage Service keys for %s: %s", serviceName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
storageClient, err = storage.NewBasicClient(serviceName, keys.PrimaryKey)
|
|
|
|
if err != nil {
|
|
|
|
return storageClient, fmt.Errorf("Failed creating Storage Service client for %s: %s", serviceName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageClient, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStorageServiceBlobClient is a helper method which returns the
|
|
|
|
// storage.BlobStorageClient associated to the given storage service name.
|
|
|
|
func (c Client) getStorageServiceBlobClient(serviceName string) (storage.BlobStorageClient, error) {
|
|
|
|
storageClient, err := c.getStorageClientForStorageService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return storage.BlobStorageClient{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageClient.GetBlobService(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStorageServiceQueueClient is a helper method which returns the
|
|
|
|
// storage.QueueServiceClient associated to the given storage service name.
|
|
|
|
func (c Client) getStorageServiceQueueClient(serviceName string) (storage.QueueServiceClient, error) {
|
|
|
|
storageClient, err := c.getStorageClientForStorageService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return storage.QueueServiceClient{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageClient.GetQueueService(), err
|
2015-05-29 00:10:21 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-08-03 22:12:30 +02:00
|
|
|
func (c *Config) NewClientFromSettingsData() (*Client, error) {
|
|
|
|
mc, err := management.ClientFromPublishSettingsData(c.Settings, c.SubscriptionID)
|
2015-05-29 00:10:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
return &Client{
|
2015-06-16 19:54:52 +02:00
|
|
|
mgmtClient: mc,
|
2015-06-19 18:52:36 +02:00
|
|
|
affinityGroupClient: affinitygroup.NewClient(mc),
|
2015-06-16 19:54:52 +02:00
|
|
|
hostedServiceClient: hostedservice.NewClient(mc),
|
|
|
|
secGroupClient: networksecuritygroup.NewClient(mc),
|
|
|
|
osImageClient: osimage.NewClient(mc),
|
2015-06-17 07:04:10 +02:00
|
|
|
sqlClient: sql.NewClient(mc),
|
2015-06-16 19:54:52 +02:00
|
|
|
storageServiceClient: storageservice.NewClient(mc),
|
|
|
|
vmClient: virtualmachine.NewClient(mc),
|
|
|
|
vmDiskClient: virtualmachinedisk.NewClient(mc),
|
|
|
|
vmImageClient: virtualmachineimage.NewClient(mc),
|
|
|
|
vnetClient: virtualnetwork.NewClient(mc),
|
|
|
|
mutex: &sync.Mutex{},
|
2015-05-29 00:10:21 +02:00
|
|
|
}, 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{
|
2015-06-16 19:54:52 +02:00
|
|
|
mgmtClient: mc,
|
2015-06-19 18:52:36 +02:00
|
|
|
affinityGroupClient: affinitygroup.NewClient(mc),
|
2015-06-16 19:54:52 +02:00
|
|
|
hostedServiceClient: hostedservice.NewClient(mc),
|
|
|
|
secGroupClient: networksecuritygroup.NewClient(mc),
|
|
|
|
osImageClient: osimage.NewClient(mc),
|
2015-06-17 07:04:10 +02:00
|
|
|
sqlClient: sql.NewClient(mc),
|
2015-06-16 19:54:52 +02:00
|
|
|
storageServiceClient: storageservice.NewClient(mc),
|
|
|
|
vmClient: virtualmachine.NewClient(mc),
|
|
|
|
vmDiskClient: virtualmachinedisk.NewClient(mc),
|
|
|
|
vmImageClient: virtualmachineimage.NewClient(mc),
|
|
|
|
vnetClient: virtualnetwork.NewClient(mc),
|
|
|
|
mutex: &sync.Mutex{},
|
2015-05-29 00:10:21 +02:00
|
|
|
}, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|