StorageAccountHelper.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package login
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/docker/api/context/store"
  7. )
  8. // StorageAccountHelper helper for Azure Storage Account
  9. type StorageAccountHelper struct {
  10. LoginService AzureLoginService
  11. AciContext store.AciContext
  12. }
  13. // GetAzureStorageAccountKey retrieves the storage account ket from the current azure login
  14. func (helper StorageAccountHelper) GetAzureStorageAccountKey(ctx context.Context, accountName string) (string, error) {
  15. client, err := GetStorageAccountsClient(helper.AciContext.SubscriptionID)
  16. if err != nil {
  17. return "", err
  18. }
  19. result, err := client.ListKeys(ctx, helper.AciContext.ResourceGroup, accountName, "")
  20. if err != nil {
  21. return "", errors.Wrap(err, fmt.Sprintf("could not access storage account acountKeys for %s, using the azure login", accountName))
  22. }
  23. if result.Keys != nil && len((*result.Keys)) < 1 {
  24. return "", fmt.Errorf("no key could be obtained for storage account %s from your azure login", accountName)
  25. }
  26. key := (*result.Keys)[0]
  27. return *key.Value, nil
  28. }