github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azure/config.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/Azure/azure-sdk-for-go/management" 8 "github.com/Azure/azure-sdk-for-go/management/affinitygroup" 9 "github.com/Azure/azure-sdk-for-go/management/hostedservice" 10 "github.com/Azure/azure-sdk-for-go/management/networksecuritygroup" 11 "github.com/Azure/azure-sdk-for-go/management/osimage" 12 "github.com/Azure/azure-sdk-for-go/management/sql" 13 "github.com/Azure/azure-sdk-for-go/management/storageservice" 14 "github.com/Azure/azure-sdk-for-go/management/virtualmachine" 15 "github.com/Azure/azure-sdk-for-go/management/virtualmachinedisk" 16 "github.com/Azure/azure-sdk-for-go/management/virtualmachineimage" 17 "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" 18 "github.com/Azure/azure-sdk-for-go/storage" 19 ) 20 21 // Config is the configuration structure used to instantiate a 22 // new Azure management client. 23 type Config struct { 24 Settings []byte 25 SubscriptionID string 26 Certificate []byte 27 ManagementURL string 28 } 29 30 // Client contains all the handles required for managing Azure services. 31 type Client struct { 32 mgmtClient management.Client 33 34 affinityGroupClient affinitygroup.AffinityGroupClient 35 36 hostedServiceClient hostedservice.HostedServiceClient 37 38 osImageClient osimage.OSImageClient 39 40 sqlClient sql.SQLDatabaseClient 41 42 storageServiceClient storageservice.StorageServiceClient 43 44 vmClient virtualmachine.VirtualMachineClient 45 46 vmDiskClient virtualmachinedisk.DiskClient 47 48 vmImageClient virtualmachineimage.Client 49 50 // unfortunately; because of how Azure's network API works; doing networking operations 51 // concurrently is very hazardous, and we need a mutex to guard the VirtualNetworkClient. 52 vnetClient virtualnetwork.VirtualNetworkClient 53 vnetMutex *sync.Mutex 54 55 // same as the above for security group rule operations: 56 secGroupClient networksecuritygroup.SecurityGroupClient 57 secGroupMutex *sync.Mutex 58 } 59 60 // getStorageClientForStorageService is helper method which returns the 61 // storage.Client associated to the given storage service name. 62 func (c Client) getStorageClientForStorageService(serviceName string) (storage.Client, error) { 63 var storageClient storage.Client 64 65 keys, err := c.storageServiceClient.GetStorageServiceKeys(serviceName) 66 if err != nil { 67 return storageClient, fmt.Errorf("Failed getting Storage Service keys for %s: %s", serviceName, err) 68 } 69 70 storageClient, err = storage.NewBasicClient(serviceName, keys.PrimaryKey) 71 if err != nil { 72 return storageClient, fmt.Errorf("Failed creating Storage Service client for %s: %s", serviceName, err) 73 } 74 75 return storageClient, err 76 } 77 78 // getStorageServiceBlobClient is a helper method which returns the 79 // storage.BlobStorageClient associated to the given storage service name. 80 func (c Client) getStorageServiceBlobClient(serviceName string) (storage.BlobStorageClient, error) { 81 storageClient, err := c.getStorageClientForStorageService(serviceName) 82 if err != nil { 83 return storage.BlobStorageClient{}, err 84 } 85 86 return storageClient.GetBlobService(), nil 87 } 88 89 // getStorageServiceQueueClient is a helper method which returns the 90 // storage.QueueServiceClient associated to the given storage service name. 91 func (c Client) getStorageServiceQueueClient(serviceName string) (storage.QueueServiceClient, error) { 92 storageClient, err := c.getStorageClientForStorageService(serviceName) 93 if err != nil { 94 return storage.QueueServiceClient{}, err 95 } 96 97 return storageClient.GetQueueService(), err 98 } 99 100 func (c *Config) NewClientFromSettingsData() (*Client, error) { 101 mc, err := management.ClientFromPublishSettingsData(c.Settings, c.SubscriptionID) 102 if err != nil { 103 return nil, err 104 } 105 106 return &Client{ 107 mgmtClient: mc, 108 affinityGroupClient: affinitygroup.NewClient(mc), 109 hostedServiceClient: hostedservice.NewClient(mc), 110 secGroupClient: networksecuritygroup.NewClient(mc), 111 secGroupMutex: &sync.Mutex{}, 112 osImageClient: osimage.NewClient(mc), 113 sqlClient: sql.NewClient(mc), 114 storageServiceClient: storageservice.NewClient(mc), 115 vmClient: virtualmachine.NewClient(mc), 116 vmDiskClient: virtualmachinedisk.NewClient(mc), 117 vmImageClient: virtualmachineimage.NewClient(mc), 118 vnetClient: virtualnetwork.NewClient(mc), 119 vnetMutex: &sync.Mutex{}, 120 }, nil 121 } 122 123 // NewClient returns a new Azure management client created 124 // using a subscription ID and certificate. 125 func (c *Config) NewClient() (*Client, error) { 126 mc, err := management.NewClient(c.SubscriptionID, c.Certificate) 127 if err != nil { 128 return nil, nil 129 } 130 131 return &Client{ 132 mgmtClient: mc, 133 affinityGroupClient: affinitygroup.NewClient(mc), 134 hostedServiceClient: hostedservice.NewClient(mc), 135 secGroupClient: networksecuritygroup.NewClient(mc), 136 secGroupMutex: &sync.Mutex{}, 137 osImageClient: osimage.NewClient(mc), 138 sqlClient: sql.NewClient(mc), 139 storageServiceClient: storageservice.NewClient(mc), 140 vmClient: virtualmachine.NewClient(mc), 141 vmDiskClient: virtualmachinedisk.NewClient(mc), 142 vmImageClient: virtualmachineimage.NewClient(mc), 143 vnetClient: virtualnetwork.NewClient(mc), 144 vnetMutex: &sync.Mutex{}, 145 }, nil 146 }