Prasad Nagaraj
Prasad Nagaraj

Reputation: 11

Microsoft Azure Java SDK: snapshot copy

I am looking for a way to copy azure managed disk snapshots between regions using Java SDK. Any suggestions or pointers will be helpful

Thanks in advance Prasad

Upvotes: 1

Views: 440

Answers (1)

Charles Xu
Charles Xu

Reputation: 31462

You can use Java SDK to create Azure managed disk snapshots with different resource groups and regions:

Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withLinuxFromDisk(osDisk)
  .create();

See Java: Manage Azure Managed Disks to get more details.

Update-1

If you want to copy the snapshot from other regions, just change the withLinuxFromDisk() into withLinuxFromSnapshot().

You can get more interfaces about Azure snapshot in Java SDK from Java SDK for Azure.

Update-2

For your issue that you want to create a snapshot from a snapshot. With the code example below and it works well.

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.management.compute.Snapshot;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;

import java.io.IOException;


public class test {

    public static void main(String[] args) throws IOException {

        ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
                "xxxxxxxxx",
                "xxxxxxxxx",
                "xxxxxxxxx",
                AzureEnvironment.AZURE);

        Azure.Authenticated azureAuth = Azure.authenticate(credentials);
        Azure azure = azureAuth.withDefaultSubscription();

        Snapshot osSnapshot = azure.snapshots().define("managedOSSnapshotName")
                .withRegion(Region.US_EAST)
                .withExistingResourceGroup("charlesJava")
                .withDataFromSnapshot("/subscriptions/xxxxxxx/resourceGroups/groupName/providers/Microsoft.Compute/snapshots/snapshottest")
                .create();
    }
}

The parameter that the .withDataFromSnapshot() is the resource Id, in other words, it's the snapshot resource Id. But first of all, you should get the authentication of the resource group which you want to use with at least Contributor permission. For this step, you can create a service principal and add the role for your resource group which the snapshot in.

Upvotes: 1

Related Questions