storage.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "context"
  16. "errors"
  17. "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/storage/mgmt/storage"
  18. "github.com/Azure/go-autorest/autorest"
  19. "github.com/Azure/go-autorest/autorest/to"
  20. "github.com/docker/api/azure/login"
  21. "github.com/docker/api/context/store"
  22. )
  23. // CreateStorageAccount creates a new storage account.
  24. func CreateStorageAccount(ctx context.Context, aciContext store.AciContext, accountName string) (storage.Account, error) {
  25. storageAccountsClient := getStorageAccountsClient(aciContext)
  26. result, err := storageAccountsClient.CheckNameAvailability(
  27. ctx,
  28. storage.AccountCheckNameAvailabilityParameters{
  29. Name: to.StringPtr(accountName),
  30. Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
  31. })
  32. if err != nil {
  33. return storage.Account{}, err
  34. }
  35. if !*result.NameAvailable {
  36. return storage.Account{}, errors.New("storage account name already exists" + accountName)
  37. }
  38. future, err := storageAccountsClient.Create(
  39. ctx,
  40. aciContext.ResourceGroup,
  41. accountName,
  42. storage.AccountCreateParameters{
  43. Sku: &storage.Sku{
  44. Name: storage.StandardLRS,
  45. },
  46. Location: to.StringPtr(aciContext.Location),
  47. AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{}})
  48. if err != nil {
  49. return storage.Account{}, err
  50. }
  51. err = future.WaitForCompletionRef(ctx, storageAccountsClient.Client)
  52. if err != nil {
  53. return storage.Account{}, err
  54. }
  55. return future.Result(storageAccountsClient)
  56. }
  57. // DeleteStorageAccount deletes a given storage account
  58. func DeleteStorageAccount(ctx context.Context, aciContext store.AciContext, accountName string) (autorest.Response, error) {
  59. storageAccountsClient := getStorageAccountsClient(aciContext)
  60. response, err := storageAccountsClient.Delete(ctx, aciContext.ResourceGroup, accountName)
  61. if err != nil {
  62. return autorest.Response{}, err
  63. }
  64. return response, err
  65. }
  66. // ListKeys lists the storage account keys
  67. func ListKeys(ctx context.Context, aciContext store.AciContext, accountName string) (storage.AccountListKeysResult, error) {
  68. storageAccountsClient := getStorageAccountsClient(aciContext)
  69. keys, err := storageAccountsClient.ListKeys(ctx, aciContext.ResourceGroup, accountName)
  70. if err != nil {
  71. return storage.AccountListKeysResult{}, err
  72. }
  73. return keys, nil
  74. }
  75. func getStorageAccountsClient(aciContext store.AciContext) storage.AccountsClient {
  76. storageAccountsClient := storage.NewAccountsClient(aciContext.SubscriptionID)
  77. autho, _ := login.NewAuthorizerFromLogin()
  78. storageAccountsClient.Authorizer = autho
  79. return storageAccountsClient
  80. }