Vadim Visnevski
Vadim Visnevski

Reputation: 35

Create Azure DataDisk with C#

I'm trying to create a DataDisk in Azure with C#. But nothing seems to work. However was able to do it with PowerShell.

I use Azure SDK. Below is a piece of code that is suppose to create a disk and attach it.

var vm = azure.VirtualMachines.List().Where(x => x.Name.ToLower() == vmName.ToLower()).FirstOrDefault();

var disk = new DataDisk(
                vm.StorageProfile.DataDisks.Count + 1,
                DiskCreateOptionTypes.Empty,
                vmName + "_disk");

disk.DiskSizeGB = 128;
disk.Validate();
vm.StorageProfile.DataDisks.Add(disk);
vm.StorageProfile.Validate();
vm.Update();

I don't have any errors. But nothing is created.

Could someone tell me what I'm doing wrong?

Upvotes: 3

Views: 206

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

You're missing the Apply() method after vm.Update(). Please use vm.Update().Apply() instead of vm.Update() in your code.

Here is my test code, and works fine:

#your other code.
DataDisk disk = new DataDisk(vm.StorageProfile.DataDisks.Count + 1,
                DiskCreateOptionTypes.Empty,
                "ivandisk222");

disk.DiskSizeGB = 15;
disk.Validate();
vm.StorageProfile.DataDisks.Add(disk);
vm.StorageProfile.Validate();

vm.Update().Apply();

After the code completes, I can see the data disk is added to the vm:

enter image description here

Upvotes: 2

Related Questions