Microsoft Azure Java SDK: snapshot copy - azure

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

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.

Related

How do I query Azure resources for a list of ADF pipelines?

I'm documenting an Azure Data Factory with hundreds of pipelines. I would like to automate the process, perhaps with KQL, so I do not have to write down each item by hand. How do I query Azure resources for a list of ADF pipelines?
You can use the API given below to get Lists of Pipelines.
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/pipelines?api-version=2018-06-01
Below is the list of URI Parameters:
factoryName
resourceGroupName
subscriptionId
api-version
Sample Java Code
import com.azure.core.util.Context;
public final class Main {
public static void pipelinesListByFactory(com.azure.resourcemanager.datafactory.DataFactoryManager manager) {
manager.pipelines().listByFactory("exampleResourceGroup", "exampleFactoryName", Context.NONE);
}
}
Reference - Official Documentation

requests Table Azure Application Insights which operation belongs to which resource?

I am using Workbook in Azure Monitor and trying to create a dashboard.
My All resources data goes into 1 Application Insights resource.
I have resources like storage, Azure Functions, VPN, Web App etc created in this azure account.
One subscription i am using.
I am trying to run query on requests table
I want to create dashboard for a particular WebApp say namely "ABC"
OR
I want to create 1 dashboard for resources WebApp, Azure Function, Storage say of name containing "XYZ"
requests table does not contain resourceid column. Which other Table i should use to get resource type and resource id, name
I had like the same question while logging into a common app insights resource.
From what I saw, there was no common property across all the different logging applications, I could have been using to determine the telemetry-source.
What I ended up with was to add a custom property to all telemetry using a telemetry initializer (which can be added during the startup e.g. within Azure Functions, as well as AppService). For the Storage, I don't know whether this can be also done.
// C# sample for the initializer
public class ComponentNameTelemetryInitializer : ITelemetryInitializer
{
private readonly string _ComponentName;
public ComponentNameTelemetryInitializer(string assemblyName)
{
_ComponentName = assemblyName;
}
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties propTelemetry)
{
propTelemetry.Properties["ComponentName"] = _ComponentName;
}
}
}
That way I can just filter the log for the custom dimension ComponentName and I get all entries from a specific application.

What's the difference between ListByResourceGroup() and GetByResourceGroup() in azure sdk?

When working with azure sdk I see ListByResourceGroup(), GetByResourceGroup() return the same value. What is the difference between them? If I want to get all information of vm in my subcriptions, what function should I use?
T GetByResourceGroup(string resourceGroupName, string name)
IEnumerable<T> ListByResourceGroup(string resourceGroupName)
So, if you use GetByResourceGroup, you need to provide 2 parameters. One is the resource group name, the other is the name for the VM. And you will only get the target VM.
However, ListByResourceGroup only reqiure for the resource group name. And you will be able to get all the VMs in the resource group.
If you want to get all the VMs, you can use the following as a sample:
static void Main(string[] args)
{
IAzure azure = Azure.Authenticate("path_to_auth_file").WithDefaultSubscription();
var vms = azure.VirtualMachines.List();
foreach( var vm in vms)
{
Console.WriteLine(vm.Name);
}
}
I just simply output the VM's name, you can do what you want in the foreach.
Just to add to existing answer, in Azure SDK LIST always gets all resources from subscription or resource group (usually all resources of the same type), whereas GET is used to retrieve a specific resource (if it exists), hence you need to specify the resourceGroup AND the resource name.
I'd be inclined to think this is universal. If you think about it for a second those verbs in english are meant exactly for that.

how to create azure batch pool based on custom VM image using java sdk

I want to use custom ubuntu VM image that I had created for by batch job. I can create a new pool by selecting the custom image from the azure portal itself but I wanted to write build script to do the same using the azure batch java sdk. This is what I was able to come up with:
List<NodeAgentSku> skus = client.accountOperations().listNodeAgentSkus().findAll({ it.osType() == OSType.LINUX })
String skuId = null
ImageReference imageRef = new ImageReference().withVirtualMachineImageId('/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Compute/images/$CUSTOM_VM_IMAGE_NAME')
for (NodeAgentSku sku : skus) {
for (ImageReference imgRef : sku.verifiedImageReferences()) {
if (imgRef.publisher().equalsIgnoreCase(osPublisher) && imgRef.offer().equalsIgnoreCase(osOffer) && imgRef.sku() == '18.04-LTS') {
skuId = sku.id()
break
}
}
}
VirtualMachineConfiguration configuration = new VirtualMachineConfiguration()
configuration.withNodeAgentSKUId(skuId).withImageReference(imageRef)
client.poolOperations().createPool(poolId, poolVMSize, configuration, poolVMCount)
But I am getting exception:
Caused by: com.microsoft.azure.batch.protocol.models.BatchErrorException: Status code 403, {
"odata.metadata":"https://analyticsbatch.eastus.batch.azure.com/$metadata#Microsoft.Azure.Batch.Protocol.Entities.Container.errors/#Element","code":"AuthenticationFailed","message":{
"lang":"en-US","value":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:bf9bf7fd-2ef5-497b-867c-858d081137e6\nTime:2019-04-17T23:08:17.7144177Z"
},"values":[
{
"key":"AuthenticationErrorDetail","value":"The specified type of authentication SharedKey is not allowed when external resources of type Compute are linked."
}
]
}
I definitely think the way I am getting the skuId is wrong. Since client.accountOperations().listNodeAgentSkus() does not list the custom image, I just thought of giving skuId based of the ubuntu version that I had used to create the custom image.
So what is the correct way to create pool using custom VM image for azure batch account using java sdk?
You must use Azure Active Directory credentials in order to create a pool with a custom image. It is in the prerequisites section of the Batch Custom Image doc.
This is a frequently asked question:
Custom Image under AzureBatch ImageReference class not working
Azure Batch Pool: How do I use a custom VM Image via Python?
Just shows as the error, you need to authenticate to Azure first and then you could create the pool with a custom image as you want.
First, you need an Azure Batch Account, you can create it in the Azure portal or using Azure CLI. Or you also can create the batch account through Java. See Manage the Azure Batch Account through Java.
Then I think you also need to authenticate to your batch account. There are two ways below:
Use the account name, key, and URL to create a BatchSharedKeyCredentials instance for authentication with the Azure Batch service. The BatchClient class is the simplest entry point for creating and interacting with Azure Batch objects.
BatchSharedKeyCredentials cred = new BatchSharedKeyCredentials(batchUri, batchAccount, batchKey);
BatchClient client = BatchClient.open(cred);
The other way is using AAD (Azure Active Directory) authentication to create the client. See this document for detail.
BatchApplicationTokenCredentials cred = new BatchApplicationTokenCredentials(batchEndpoint, clientId, applicationSecret, applicationDomain, null, null);
BatchClient client = BatchClient.open(cred);
Then you can create the pool with the custom as you want. Just like this:
System.out.println("Created a pool using an Azure Marketplace image.");
VirtualMachineConfiguration configuration = new VirtualMachineConfiguration();
configuration.withNodeAgentSKUId(skuId).withImageReference(imageRef);
client.poolOperations().createPool(poolId, poolVMSize, configuration, poolVMCount);
System.out.println("Created a Pool: " + poolId);
For more details, see Azure Batch Libraries for Java.

JClouds for Azure Blob

I can't find an example on how to create a new container/bucket with specific Location (Singapore) using JClouds. All the examples that I found on google are using null as default location.
azureBlobStore.createContainerInLocation(null, containerName);
Could any of you, JClouds veterans, help me out here?
I haven't used JClouds, but just went and looked at the docs for Azure storage. First thing they show is creation of a blob context:
BlobStoreContext context = new BlobStoreContextFactory().createContext("azureblob", accesskeyid, secretkey);
According to the Javadocs, the params are provider, identity, and credential. That being the case, you probably need to pass the storage account and key from the Windows Azure portal into the 2nd and 3rd parameters. Once you do this, your location is set for you, to the data center where you set up the storage account (In Windows Azure, a storage account is associated with a specific data center upon creation - all containers and objects are then created in that data center as well). I don't think the Location parameter is meaningful when setting up your Azure blob container. That Location parameter is nullable, since it only applies to a subset of cloud providers based on that provider's API (see Javadocs for more details).
I was looking for the same answer the other day and just wanted to echo what David said. Here is the code for AzureBlobStore.java in jclouds 1.5
#Override
public boolean createContainerInLocation(Location location, String container) {
return sync.createContainer(container);
}
As you can see, the location is ignored because your Azure account is already tied to a specific location.

Resources