【问题标题】:DevTestLabs VirtualNetwork-Subnet: inconsistency between portal and .net sdkDevTestLabs VirtualNetwork-Subnet:portal 和 .net sdk 之间的不一致
【发布时间】:2021-01-05 13:50:22
【问题描述】:

我正在尝试以编程方式将 Use in virtual machine creationAllow public IP creation 设置为 Yes。我找到了正确的方法,这是我的代码:

var fragment = new VirtualNetworkFragment(
    subnetOverrides: virtualNetwork.Subnets.Select(s =>
        new SubnetOverrideFragment(
            labSubnetName: s.Key,
            resourceId: s.Value.Inner.Id,
            useInVmCreationPermission: "Allow",
            usePublicIpAddressPermission: "Allow")).ToList());

var vnet = await BaseClientFactory.CreateDevTestLabClient(azureClient)
    .VirtualNetworks
    .UpdateWithHttpMessagesAsync(dbLab.ResourceGroupName, dbLab.LabId.ToString(), virtualNetworkId, fragment);

不幸的是,代码不起作用,但它也没有抛出任何异常。当我序列化 fragment 变量以将其与在 Azure 上手动执行此操作时发送的请求进行比较时,存在细微差别。

以下是使用 NewtonsoftJson 序列化的主体在代码中的外观(我从 Watch 窗口中获取):

以下是通过 Azure 发送正文时的外观:

唯一的两个变化是我的代码生成了一个请求,其中 allowedSubnetsnull 并且(更重要的是,我认为)我的代码生成 properties.subnetOverrides 而不是正确的 json 的方式略有不同结构体。我不确定这是一个错误还是我做错了什么 - 有什么想法吗?

【问题讨论】:

    标签: c# azure asp.net-core azure-virtual-network azure-devtest-labs


    【解决方案1】:

    根据我的研究,update 操作仅可用于修改虚拟网络的标签,所有其他属性将被忽略。如果要修改虚拟网络的属性,请使用Create Or Update 操作。关于如何用c#sdkMicrosoft.Azure.Management.DevTestLabs实现,请参考以下步骤

    1. Create a service principal and assign role to the sp

    2. 代码

                string clientId = " sp appId";
                string clientSecret = "sp password";
                string tenantId = "";
                string subscriptionId = "";
                var credentials = SdkContext.AzureCredentialsFactory
                    .FromServicePrincipal(clientId,
                        clientSecret,
                        tenantId,
                        AzureEnvironment.AzureGlobalCloud);
    
                DevTestLabsClient client = new DevTestLabsClient(credentials);
                client.SubscriptionId = subscriptionId;
                VirtualNetwork vnet = await client.VirtualNetworks.GetAsync("testdevlab", "test", "Dtltest");
                 // modify the vent's properties 
                 vnet.SubnetOverrides = vnet.SubnetOverrides.Select(s => { s.UseInVmCreationPermission = "Allow"; s.UsePublicIpAddressPermission = "Allow"; return s; }).ToList();
                //update vnet
                var res =await client.VirtualNetworks.CreateOrUpdateAsync("testdevlab", "test", "Dtltest", vnet);
    
    
    

    运行代码之前

    运行代码后

    【讨论】:

    • 我也尝试过这种方法,但问题是SubnetOverrides 在使用 DevTestLabsClient 获取后为空。知道为什么吗?我可以看到天蓝色门户上有子网。 AllowedSubnets 为空。
    • 没关系,我只需使用IAzure 服务下载vnet,然后使用来自Microsoft.Azure.Management.Network.Fluent.NetworkImplSubnets 播种SubnetOverrides。当我用您建议的方法保存它时,它可以工作。谢谢!
    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2018-05-14
    • 2014-04-11
    相关资源
    最近更新 更多