【问题标题】:Getting StorageAccount-AccessKey in Pulumi Azure Native在Pulumi Azure Native中获取StorageAccount-AccessKey
【发布时间】:2021-11-20 13:52:46
【问题描述】:

我正在尝试在 Pulumi 中从“经典”Azure 切换到 Azure Native。我的要求之一是将 Connectionstring 和 AccessKey 检索到我新创建的 StorageAccount。

在经典 Azure 上,我收到了这些字段

var connectionString=ClassicStorageAccount.PrimaryConnectionString;
var accessKey=ClassicStorageAccount.PrimaryAccessKey;

其中ClassicStorageAccount 的类型为Pulumi.Azure.Storage.Account

现在使用 Azure Native 创建存储帐户后:

var account=new Pulumi.AzureNative.Storage.StorageAccount("myMagicAccount", new StorageAccountArgs{...});

我正在努力检索 AccessKey。

我可以使用

检索连接字符串
var connectionstring=account.StorageAccount.PrimaryEndpoints.Apply(q=>q.Web);

但PrimaryEndpoints 或PrivateEndpointConnections 中的所有属性似乎都不包含我所需的AccessKey。

docs for StorageAccount on Azure Native 在这种方法上没有帮助我

【问题讨论】:

    标签: azure pulumi


    【解决方案1】:

    您可以使用listStorageAccountKeys 方法。

    using System.Threading.Tasks;
    using Pulumi;
    using Pulumi.AzureNative.Resources;
    using Pulumi.AzureNative.Storage;
    using Pulumi.AzureNative.Storage.Inputs;
    
    class MyStack : Stack
    {
        public MyStack()
        {
            // Create an Azure Resource Group
            var resourceGroup = new ResourceGroup("resourceGroup");
    
            // Create an Azure resource (Storage Account)
            var storageAccount = new StorageAccount("sa", new StorageAccountArgs
            {
                ResourceGroupName = resourceGroup.Name,
                Sku = new SkuArgs
                {
                    Name = SkuName.Standard_LRS
                },
                Kind = Kind.StorageV2
            });
    
            // Export the primary key of the Storage Account
            this.PrimaryStorageKey = Output.Tuple(resourceGroup.Name, storageAccount.Name).Apply(names =>
                Output.CreateSecret(GetStorageAccountPrimaryKey(names.Item1, names.Item2)));
        }
    
        [Output]
        public Output<string> PrimaryStorageKey { get; set; }
    
        private static async Task<string> GetStorageAccountPrimaryKey(string resourceGroupName, string accountName)
        {
            var accountKeys = await ListStorageAccountKeys.InvokeAsync(new ListStorageAccountKeysArgs
            {
                ResourceGroupName = resourceGroupName,
                AccountName = accountName
            });
            return accountKeys.Keys[0].Value;
        }
    }
    

    上面的代码来自 Pulumi 在运行 pulumi new azure-csharp 时使用的模板,可以在模板存储库中找到

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2020-09-27
      • 2021-09-04
      • 2020-07-06
      • 2021-12-15
      • 1970-01-01
      • 2022-11-11
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多