storagelogin.go 1.8 KB

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