storage_helper.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 login
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "github.com/docker/api/context/store"
  19. )
  20. // StorageAccountHelper helper for Azure Storage Account
  21. type StorageAccountHelper struct {
  22. LoginService AzureLoginService
  23. AciContext store.AciContext
  24. }
  25. // GetAzureStorageAccountKey retrieves the storage account ket from the current azure login
  26. func (helper StorageAccountHelper) GetAzureStorageAccountKey(ctx context.Context, accountName string) (string, error) {
  27. client, err := NewStorageAccountsClient(helper.AciContext.SubscriptionID)
  28. if err != nil {
  29. return "", err
  30. }
  31. result, err := client.ListKeys(ctx, helper.AciContext.ResourceGroup, accountName, "")
  32. if err != nil {
  33. return "", errors.Wrap(err, fmt.Sprintf("could not access storage account acountKeys for %s, using the azure login", accountName))
  34. }
  35. if result.Keys != nil && len((*result.Keys)) < 1 {
  36. return "", fmt.Errorf("no key could be obtained for storage account %s from your azure login", accountName)
  37. }
  38. key := (*result.Keys)[0]
  39. return *key.Value, nil
  40. }