gpt4 book ai didi

java - Azure 使用 Java SDK 将角色分配给 VM

转载 作者:行者123 更新时间:2023-12-02 08:25:42 26 4
gpt4 key购买 nike

我正在编写一个 Java 程序,该程序创建 VM 并从存储访问文件。但是,我无法为该虚拟机分配“存储贡献者/所有者”角色,因此它可以。

我目前有这段代码,但我不确定这是否是我需要的,而且我也不知道在某些地方要写什么:

rbacManager = GraphRbacManager.authenticate( credentials );
rbacManager.roleAssignments()
.define("roletest")
// which object? and where to find the ID?
.forObjectId("/subscription/" + subscription + "?")
.withBuiltInRole(com.microsoft.azure.management.graphrbac.BuiltInRole.STORAGE_ACCOUNT_CONTRIBUTOR)
// what should go as resource scope?
.withResourceScope(?)
.createAsync();

本质上我想在 Java 代码中执行此步骤: enter image description here

提前谢谢您!

最佳答案

关于该问题,请引用以下步骤

  1. 创建服务主体并将Owner角色分配给sp
az login
az ad sp create-for-rbac -n "MyApp" --role "Owner"\
--scopes /subscriptions/{SubID} \
--sdk-auth
  • 项目
  • a.开发工具包

    <dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager</artifactId>
    <version>2.0.0</version>
    </dependency>
    <dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.2.0</version>
    </dependency>

    b.代码

     AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
    String clientId="<sp appid>";
    String clientSecret="<sp password>";
    String tenant="";
    String subscriptionId=""
    TokenCredential credential = new ClientSecretCredentialBuilder()
    .clientId(clientId)
    .clientSecret(clientSecret)
    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
    .tenantId(tenant)
    .build();
    AzureResourceManager azureResourceManager = AzureResourceManager
    .configure()
    .withLogLevel(HttpLogDetailLevel.BASIC)
    .authenticate(credential, profile)
    .withSubscription(subscriptionId);
    // get storage account
    String accountGroup="";
    String accountName="";
    StorageAccount account = azureResourceManager.storageAccounts().getByResourceGroup(accountGroup,accountName);
    // get vm
    String vmGroup="";
    String vmName="test";
    VirtualMachine virtualMachine = azureResourceManager.virtualMachines().getByResourceGroup(vmGroup,vmName);
    virtualMachine.update()
    .withSystemAssignedManagedServiceIdentity()
    .withSystemAssignedIdentityBasedAccessTo(account.id(), BuiltInRole.fromString("Storage Blob Data Owner"))
    .apply();

    }

    enter image description here

    关于java - Azure 使用 Java SDK 将角色分配给 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64859796/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com