@@ -80,52 +80,55 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
80
80
return & c , nil
81
81
}
82
82
83
- // CreateVolume creates a VHD blob in a given storage account, will create the given storage account if it does not exist in current resource group
83
+ // CreateVolume creates a VHD blob in a storage account that has storageType and location using the given storage account.
84
+ // If no storage account is given, search all the storage accounts associated with the resource group and pick one that
85
+ // fits storage type and location.
84
86
func (c * BlobDiskController ) CreateVolume (name , storageAccount string , storageAccountType storage.SkuName , location string , requestGB int ) (string , string , int , error ) {
85
- key , err := c .common .cloud .getStorageAccesskey (storageAccount )
86
- if err != nil {
87
- glog .V (2 ).Infof ("azureDisk - no key found for storage account %s in resource group %s, begin to create a new storage account" , storageAccount , c .common .resourceGroup )
88
-
89
- cp := storage.AccountCreateParameters {
90
- Sku : & storage.Sku {Name : storageAccountType },
91
- Tags : & map [string ]* string {"created-by" : to .StringPtr ("azure-dd" )},
92
- Location : & location }
93
- cancel := make (chan struct {})
94
-
95
- _ , errchan := c .common .cloud .StorageAccountClient .Create (c .common .resourceGroup , storageAccount , cp , cancel )
96
- err = <- errchan
97
- if err != nil {
98
- return "" , "" , 0 , fmt .Errorf (fmt .Sprintf ("Create Storage Account %s, error: %s" , storageAccount , err ))
99
- }
100
-
101
- key , err = c .common .cloud .getStorageAccesskey (storageAccount )
87
+ var err error
88
+ accounts := []accountWithLocation {}
89
+ if len (storageAccount ) > 0 {
90
+ accounts = append (accounts , accountWithLocation {Name : storageAccount })
91
+ } else {
92
+ // find a storage account
93
+ accounts , err = c .common .cloud .getStorageAccounts ()
102
94
if err != nil {
103
- return "" , "" , 0 , fmt .Errorf ("no key found for storage account %s even after creating a new storage account" , storageAccount )
95
+ // TODO: create a storage account and container
96
+ return "" , "" , 0 , err
104
97
}
105
-
106
- glog .Errorf ("no key found for storage account %s in resource group %s" , storageAccount , c .common .resourceGroup )
107
- return "" , "" , 0 , err
108
98
}
99
+ for _ , account := range accounts {
100
+ glog .V (4 ).Infof ("account %s type %s location %s" , account .Name , account .StorageType , account .Location )
101
+ if (account .StorageType == string (storageAccountType )) && (location == "" || account .Location == location ) {
102
+ // find the access key with this account
103
+ key , err := c .common .cloud .getStorageAccesskey (account .Name )
104
+ if err != nil {
105
+ glog .V (2 ).Infof ("no key found for storage account %s" , account .Name )
106
+ continue
107
+ }
109
108
110
- client , err := azstorage .NewBasicClientOnSovereignCloud (storageAccount , key , c .common .cloud .Environment )
111
- if err != nil {
112
- return "" , "" , 0 , err
113
- }
114
- blobClient := client .GetBlobService ()
109
+ client , err := azstorage .NewBasicClientOnSovereignCloud (account . Name , key , c .common .cloud .Environment )
110
+ if err != nil {
111
+ return "" , "" , 0 , err
112
+ }
113
+ blobClient := client .GetBlobService ()
115
114
116
- container := blobClient .GetContainerReference (vhdContainerName )
117
- _ , err = container .CreateIfNotExists (& azstorage.CreateContainerOptions {Access : azstorage .ContainerAccessTypePrivate })
118
- if err != nil {
119
- return "" , "" , 0 , err
120
- }
115
+ container := blobClient .GetContainerReference (vhdContainerName )
116
+ _ , err = container .CreateIfNotExists (& azstorage.CreateContainerOptions {Access : azstorage .ContainerAccessTypePrivate })
117
+ if err != nil {
118
+ return "" , "" , 0 , err
119
+ }
121
120
122
- diskName , diskURI , err := c .createVHDBlobDisk (blobClient , storageAccount , name , vhdContainerName , int64 (requestGB ))
123
- if err != nil {
124
- return "" , "" , 0 , err
125
- }
121
+ // create a page blob in this account's vhd container
122
+ diskName , diskURI , err := c .createVHDBlobDisk (blobClient , account .Name , name , vhdContainerName , int64 (requestGB ))
123
+ if err != nil {
124
+ return "" , "" , 0 , err
125
+ }
126
126
127
- glog .V (4 ).Infof ("azureDisk - created vhd blob uri: %s" , diskURI )
128
- return diskName , diskURI , requestGB , err
127
+ glog .V (4 ).Infof ("azureDisk - created vhd blob uri: %s" , diskURI )
128
+ return diskName , diskURI , requestGB , err
129
+ }
130
+ }
131
+ return "" , "" , 0 , fmt .Errorf ("failed to find a matching storage account" )
129
132
}
130
133
131
134
// DeleteVolume deletes a VHD blob
@@ -236,24 +239,12 @@ func (c *BlobDiskController) deleteVhdBlob(accountName, accountKey, blobName str
236
239
}
237
240
238
241
//CreateBlobDisk : create a blob disk in a node
239
- func (c * BlobDiskController ) CreateBlobDisk (dataDiskName string , storageAccountType storage.SkuName , sizeGB int , forceStandAlone bool ) (string , error ) {
240
- glog .V (4 ).Infof ("azureDisk - creating blob data disk named:%s on StorageAccountType:%s StandAlone:%v " , dataDiskName , storageAccountType , forceStandAlone )
242
+ func (c * BlobDiskController ) CreateBlobDisk (dataDiskName string , storageAccountType storage.SkuName , sizeGB int ) (string , error ) {
243
+ glog .V (4 ).Infof ("azureDisk - creating blob data disk named:%s on StorageAccountType:%s" , dataDiskName , storageAccountType )
241
244
242
- var storageAccountName = ""
243
- var err error
244
-
245
- if forceStandAlone {
246
- // we have to wait until the storage account is is created
247
- storageAccountName = "p" + MakeCRC32 (c .common .subscriptionID + c .common .resourceGroup + dataDiskName )
248
- err = c .createStorageAccount (storageAccountName , storageAccountType , c .common .location , false )
249
- if err != nil {
250
- return "" , err
251
- }
252
- } else {
253
- storageAccountName , err = c .findSANameForDisk (storageAccountType )
254
- if err != nil {
255
- return "" , err
256
- }
245
+ storageAccountName , err := c .findSANameForDisk (storageAccountType )
246
+ if err != nil {
247
+ return "" , err
257
248
}
258
249
259
250
blobClient , err := c .getBlobSvcClient (storageAccountName )
@@ -266,15 +257,13 @@ func (c *BlobDiskController) CreateBlobDisk(dataDiskName string, storageAccountT
266
257
return "" , err
267
258
}
268
259
269
- if ! forceStandAlone {
270
- atomic .AddInt32 (& c .accounts [storageAccountName ].diskCount , 1 )
271
- }
260
+ atomic .AddInt32 (& c .accounts [storageAccountName ].diskCount , 1 )
272
261
273
262
return diskURI , nil
274
263
}
275
264
276
265
//DeleteBlobDisk : delete a blob disk from a node
277
- func (c * BlobDiskController ) DeleteBlobDisk (diskURI string , wasForced bool ) error {
266
+ func (c * BlobDiskController ) DeleteBlobDisk (diskURI string ) error {
278
267
storageAccountName , vhdName , err := diskNameandSANameFromURI (diskURI )
279
268
if err != nil {
280
269
return err
@@ -286,11 +275,6 @@ func (c *BlobDiskController) DeleteBlobDisk(diskURI string, wasForced bool) erro
286
275
glog .V (4 ).Infof ("azureDisk - deleting volume %s" , diskURI )
287
276
return c .DeleteVolume (diskURI )
288
277
}
289
- // if forced (as in one disk = one storage account)
290
- // delete the account completely
291
- if wasForced {
292
- return c .deleteStorageAccount (storageAccountName )
293
- }
294
278
295
279
blobSvc , err := c .getBlobSvcClient (storageAccountName )
296
280
if err != nil {
0 commit comments