Search code examples
azure-java-sdk

How to create Azure Managed Disk Snapshot with Encryption and Network Access Policy?


I am trying to use the SDK to create a snapshot for the managed disk using the below

azureSdkClients
        .getComputeManager()
        .snapshots()
        .define(snapshotName)
        .withRegion(disk.regionId)
        .withExistingResourceGroup(context.resourceGroupName);
        .withWindowsFromDisk(context.azureDisk)
        .withIncremental(incr)
        .create()

But this doesn't have the options for setting encryption and network acess policy? Is it supported by the SDK API ? or should I use a different API ? I see SnapshotInner as one implementation of Snapshot. I am not sure if I can use the inner class as it doesn't allow me to set the name of the snapshot


Solution

  • Regarding the issue, please refer to the following steps

    SDK

    
        <dependency>
          <groupId>com.azure.resourcemanager</groupId>
          <artifactId>azure-resourcemanager-compute</artifactId>
          <version>2.2.0</version>
        </dependency>
    
        <dependency>
          <groupId>com.azure.resourcemanager</groupId>
          <artifactId>azure-resourcemanager-keyvault</artifactId>
          <version>2.2.0</version>
        </dependency>
    
        <dependency>
          <groupId>com.azure</groupId>
          <artifactId>azure-identity</artifactId>
          <version>1.2.3</version>
        </dependency>
    

    Code

    
            String clientId="";
            String clientSecret="";
            String tenant="";
            String subId="";
            AzureProfile profile = new AzureProfile(tenant,subId, AzureEnvironment.AZURE);
            TokenCredential credential = new ClientSecretCredentialBuilder()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                    .tenantId(tenant)
                    .build();
    
    
    
            ComputeManagementClientImpl computeClient = new ComputeManagementClientBuilder()
                    .pipeline(HttpPipelineProvider.buildHttpPipeline(credential,profile))
                    .endpoint(profile.getEnvironment().getResourceManagerEndpoint())
                    .subscriptionId(profile.getSubscriptionId())
                    .buildClient();
    
           SnapshotInner sp = new SnapshotInner()
                    .withCreationData(new CreationData().withSourceResourceId("")                                             .withCreateOption(DiskCreateOption.COPY))
                    .withSku(new SnapshotSku().withName(SnapshotStorageAccountTypes.PREMIUM_LRS))
                    .withEncryption(new Encryption().withType(EncryptionType.ENCRYPTION_AT_REST_WITH_PLATFORM_KEY))
                    .withNetworkAccessPolicy(NetworkAccessPolicy.ALLOW_ALL)
                    .withLocation("eastasia");
    
            computeClient.getSnapshots().createOrUpdate("testdata","testdfg",sp);
    

    For more details, please refer to here.