storagelogin.go 1.6 KB

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