Azure在创建VM时分配角色

inkz8wg9  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在用Java编写一个程序,该程序在Azure中创建虚拟机示例,将脚本上载到容器,然后在虚拟机中下载并执行脚本。但是,我目前在授予计算机对容器的访问权限方面遇到了困难。

.withSystemAssignedManagedServiceIdentity()

但是,这还不够,显然我还必须向VM添加角色(在我的情况下是Storage Reader)。当我在门户中手动添加角色时,在设置计算机后,我会通过SSH看到我具有访问权限。但是,是否有方法在Java程序的VM创建过程中添加角色?

xytpbqjk

xytpbqjk1#

这是可能的,你可以使用withSystemAssignedIdentityBasedAccessTo(String resourceId, BuiltInRole role)方法。
以下是示例:

VirtualMachine virtualMachine = azureResourceManager.virtualMachines()
                    .define(linuxVMName)
                        .withRegion(region)
                        .withNewResourceGroup(rgName)
                        .withNewPrimaryNetwork("10.0.0.0/28")
                        .withPrimaryPrivateIPAddressDynamic()
                        .withNewPrimaryPublicIPAddress(pipName)
                        .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                        .withRootUsername(userName)
                        .withRootPassword(password)
                        .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
                        .withOSDiskCaching(CachingTypes.READ_WRITE)
                        .withSystemAssignedManagedServiceIdentity()
                        .withSystemAssignedIdentityBasedAccessTo("<storage-account-resource-id>", BuiltInRole.READER)
                        .create();

相关问题