Azure Disk Attachment to VM using C# and StorageManagementClient/ComputeManagementClient











up vote
0
down vote

favorite












I already had a working C# ResourceManager application that could successfully attach disks to virtual machines. However I solved this with the nuget package Microsoft.Azure.Management.Compute. But, since this package causes problems in other developments, it was decided not to use it anymore. For that we are using Microsoft.Azure.Management.Compute.Fluent.



Until now I had the following code (short version):



var computeClient = new ComputeManagementClient(credentials) {
SubscriptionId = credentials.DefaultSubscriptionId };

var ObjVirtualMachines = computeClient.VirtualMachines.Get(resourcegroup, vmname);

ObjVirtualMachines.StorageProfile.DataDisks.Add(
new DataDisk(ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
"Attach", name, null, null, null, null, null,
new ManagedDiskParameters(resourceid, acctype)));

var newUpdatesVM = computeClient.VirtualMachines.CreateOrUpdate(
resourcegroup, ObjVirtualMachines.Name, ObjVirtualMachines);


That works pretty much, but as I said I have to translate to the other package.



First I thought I can solve these problems with the StorageManagementClient.



But I did not get any options to update the virtual machine, because of type mismatch of the parameters.



Any ideas?










share|improve this question
























  • What does i have to translate to the other package mean? Could you please add more information about that?
    – Tom Sun
    Nov 22 at 0:47










  • Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
    – Glowhollow
    Nov 22 at 9:58












  • ok, cool, got it done
    – Glowhollow
    Nov 22 at 13:03










  • If it is resolved, please add it as an answer to close this thread.
    – Tom Sun
    Nov 23 at 1:17















up vote
0
down vote

favorite












I already had a working C# ResourceManager application that could successfully attach disks to virtual machines. However I solved this with the nuget package Microsoft.Azure.Management.Compute. But, since this package causes problems in other developments, it was decided not to use it anymore. For that we are using Microsoft.Azure.Management.Compute.Fluent.



Until now I had the following code (short version):



var computeClient = new ComputeManagementClient(credentials) {
SubscriptionId = credentials.DefaultSubscriptionId };

var ObjVirtualMachines = computeClient.VirtualMachines.Get(resourcegroup, vmname);

ObjVirtualMachines.StorageProfile.DataDisks.Add(
new DataDisk(ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
"Attach", name, null, null, null, null, null,
new ManagedDiskParameters(resourceid, acctype)));

var newUpdatesVM = computeClient.VirtualMachines.CreateOrUpdate(
resourcegroup, ObjVirtualMachines.Name, ObjVirtualMachines);


That works pretty much, but as I said I have to translate to the other package.



First I thought I can solve these problems with the StorageManagementClient.



But I did not get any options to update the virtual machine, because of type mismatch of the parameters.



Any ideas?










share|improve this question
























  • What does i have to translate to the other package mean? Could you please add more information about that?
    – Tom Sun
    Nov 22 at 0:47










  • Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
    – Glowhollow
    Nov 22 at 9:58












  • ok, cool, got it done
    – Glowhollow
    Nov 22 at 13:03










  • If it is resolved, please add it as an answer to close this thread.
    – Tom Sun
    Nov 23 at 1:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I already had a working C# ResourceManager application that could successfully attach disks to virtual machines. However I solved this with the nuget package Microsoft.Azure.Management.Compute. But, since this package causes problems in other developments, it was decided not to use it anymore. For that we are using Microsoft.Azure.Management.Compute.Fluent.



Until now I had the following code (short version):



var computeClient = new ComputeManagementClient(credentials) {
SubscriptionId = credentials.DefaultSubscriptionId };

var ObjVirtualMachines = computeClient.VirtualMachines.Get(resourcegroup, vmname);

ObjVirtualMachines.StorageProfile.DataDisks.Add(
new DataDisk(ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
"Attach", name, null, null, null, null, null,
new ManagedDiskParameters(resourceid, acctype)));

var newUpdatesVM = computeClient.VirtualMachines.CreateOrUpdate(
resourcegroup, ObjVirtualMachines.Name, ObjVirtualMachines);


That works pretty much, but as I said I have to translate to the other package.



First I thought I can solve these problems with the StorageManagementClient.



But I did not get any options to update the virtual machine, because of type mismatch of the parameters.



Any ideas?










share|improve this question















I already had a working C# ResourceManager application that could successfully attach disks to virtual machines. However I solved this with the nuget package Microsoft.Azure.Management.Compute. But, since this package causes problems in other developments, it was decided not to use it anymore. For that we are using Microsoft.Azure.Management.Compute.Fluent.



Until now I had the following code (short version):



var computeClient = new ComputeManagementClient(credentials) {
SubscriptionId = credentials.DefaultSubscriptionId };

var ObjVirtualMachines = computeClient.VirtualMachines.Get(resourcegroup, vmname);

ObjVirtualMachines.StorageProfile.DataDisks.Add(
new DataDisk(ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
"Attach", name, null, null, null, null, null,
new ManagedDiskParameters(resourceid, acctype)));

var newUpdatesVM = computeClient.VirtualMachines.CreateOrUpdate(
resourcegroup, ObjVirtualMachines.Name, ObjVirtualMachines);


That works pretty much, but as I said I have to translate to the other package.



First I thought I can solve these problems with the StorageManagementClient.



But I did not get any options to update the virtual machine, because of type mismatch of the parameters.



Any ideas?







c# azure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 13:11









Pedro Gaspar

425318




425318










asked Nov 21 at 17:11









Glowhollow

599




599












  • What does i have to translate to the other package mean? Could you please add more information about that?
    – Tom Sun
    Nov 22 at 0:47










  • Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
    – Glowhollow
    Nov 22 at 9:58












  • ok, cool, got it done
    – Glowhollow
    Nov 22 at 13:03










  • If it is resolved, please add it as an answer to close this thread.
    – Tom Sun
    Nov 23 at 1:17


















  • What does i have to translate to the other package mean? Could you please add more information about that?
    – Tom Sun
    Nov 22 at 0:47










  • Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
    – Glowhollow
    Nov 22 at 9:58












  • ok, cool, got it done
    – Glowhollow
    Nov 22 at 13:03










  • If it is resolved, please add it as an answer to close this thread.
    – Tom Sun
    Nov 23 at 1:17
















What does i have to translate to the other package mean? Could you please add more information about that?
– Tom Sun
Nov 22 at 0:47




What does i have to translate to the other package mean? Could you please add more information about that?
– Tom Sun
Nov 22 at 0:47












Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
– Glowhollow
Nov 22 at 9:58






Sure, i used the Microsoft.Azure.Management.Compute package, wich has is own namespace. Since this led to incompatibilities, I now have to work with the above package and therefore have a completely new namespace. But since I don't know this namespace, I thought someone would know about it, and have an idea how to implement it.
– Glowhollow
Nov 22 at 9:58














ok, cool, got it done
– Glowhollow
Nov 22 at 13:03




ok, cool, got it done
– Glowhollow
Nov 22 at 13:03












If it is resolved, please add it as an answer to close this thread.
– Tom Sun
Nov 23 at 1:17




If it is resolved, please add it as an answer to close this thread.
– Tom Sun
Nov 23 at 1:17












1 Answer
1






active

oldest

votes

















up vote
0
down vote













ok, here is the answer...



var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(ressgroup, virtualmname, null, new System.Threading.CancellationToken()).Result;
var storagemanagementclient = new StorageManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };

ObjVirtualMachines.StorageProfile.DataDisks.Add(new DataDisk(
ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
DiskCreateOptionTypes.Attach,
name,
null,
null,
null,
null,
new ManagedDiskParametersInner(resourceid, acctype)));

var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(ressgroup, virtualmname, ObjVirtualMachines);


and for the acctype use this.



 var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };
var disk = computeClient.Disks.ListAsync().Result.FirstOrDefault(p => p.Name == name);
var content = azure.Disks.GetById(disk.Id);
var resourceid = disk.Id;
var ressgroup = Resourcegroup.Get(context);
var virtualmname = VirtualMachineName.Get(context);
var acctype = getAccType(content.Sku.AccountType.ToString());


getAccType Function is this...



 public StorageAccountTypes getAccType(string input)
{
switch (input)
{
case "StandardLRS": return StorageAccountTypes.StandardLRS;
case "PremiumLRS": return StorageAccountTypes.PremiumLRS;
// case "StandardSSDLRS":return "StandardSSD_LRS";
// case "UltraSSDLRS":return "UltraSSD_LRS";
default: return StorageAccountTypes.StandardLRS;

}
}


The Problem is that the AccType version in this namespace is limited to 2 (StandardLRS and PremiumLRS) . In the old version you had 4 different versions of it.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417327%2fazure-disk-attachment-to-vm-using-c-sharp-and-storagemanagementclient-computeman%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    ok, here is the answer...



    var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(ressgroup, virtualmname, null, new System.Threading.CancellationToken()).Result;
    var storagemanagementclient = new StorageManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };

    ObjVirtualMachines.StorageProfile.DataDisks.Add(new DataDisk(
    ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
    DiskCreateOptionTypes.Attach,
    name,
    null,
    null,
    null,
    null,
    new ManagedDiskParametersInner(resourceid, acctype)));

    var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(ressgroup, virtualmname, ObjVirtualMachines);


    and for the acctype use this.



     var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };
    var disk = computeClient.Disks.ListAsync().Result.FirstOrDefault(p => p.Name == name);
    var content = azure.Disks.GetById(disk.Id);
    var resourceid = disk.Id;
    var ressgroup = Resourcegroup.Get(context);
    var virtualmname = VirtualMachineName.Get(context);
    var acctype = getAccType(content.Sku.AccountType.ToString());


    getAccType Function is this...



     public StorageAccountTypes getAccType(string input)
    {
    switch (input)
    {
    case "StandardLRS": return StorageAccountTypes.StandardLRS;
    case "PremiumLRS": return StorageAccountTypes.PremiumLRS;
    // case "StandardSSDLRS":return "StandardSSD_LRS";
    // case "UltraSSDLRS":return "UltraSSD_LRS";
    default: return StorageAccountTypes.StandardLRS;

    }
    }


    The Problem is that the AccType version in this namespace is limited to 2 (StandardLRS and PremiumLRS) . In the old version you had 4 different versions of it.






    share|improve this answer

























      up vote
      0
      down vote













      ok, here is the answer...



      var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(ressgroup, virtualmname, null, new System.Threading.CancellationToken()).Result;
      var storagemanagementclient = new StorageManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };

      ObjVirtualMachines.StorageProfile.DataDisks.Add(new DataDisk(
      ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
      DiskCreateOptionTypes.Attach,
      name,
      null,
      null,
      null,
      null,
      new ManagedDiskParametersInner(resourceid, acctype)));

      var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(ressgroup, virtualmname, ObjVirtualMachines);


      and for the acctype use this.



       var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };
      var disk = computeClient.Disks.ListAsync().Result.FirstOrDefault(p => p.Name == name);
      var content = azure.Disks.GetById(disk.Id);
      var resourceid = disk.Id;
      var ressgroup = Resourcegroup.Get(context);
      var virtualmname = VirtualMachineName.Get(context);
      var acctype = getAccType(content.Sku.AccountType.ToString());


      getAccType Function is this...



       public StorageAccountTypes getAccType(string input)
      {
      switch (input)
      {
      case "StandardLRS": return StorageAccountTypes.StandardLRS;
      case "PremiumLRS": return StorageAccountTypes.PremiumLRS;
      // case "StandardSSDLRS":return "StandardSSD_LRS";
      // case "UltraSSDLRS":return "UltraSSD_LRS";
      default: return StorageAccountTypes.StandardLRS;

      }
      }


      The Problem is that the AccType version in this namespace is limited to 2 (StandardLRS and PremiumLRS) . In the old version you had 4 different versions of it.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        ok, here is the answer...



        var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(ressgroup, virtualmname, null, new System.Threading.CancellationToken()).Result;
        var storagemanagementclient = new StorageManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };

        ObjVirtualMachines.StorageProfile.DataDisks.Add(new DataDisk(
        ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
        DiskCreateOptionTypes.Attach,
        name,
        null,
        null,
        null,
        null,
        new ManagedDiskParametersInner(resourceid, acctype)));

        var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(ressgroup, virtualmname, ObjVirtualMachines);


        and for the acctype use this.



         var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };
        var disk = computeClient.Disks.ListAsync().Result.FirstOrDefault(p => p.Name == name);
        var content = azure.Disks.GetById(disk.Id);
        var resourceid = disk.Id;
        var ressgroup = Resourcegroup.Get(context);
        var virtualmname = VirtualMachineName.Get(context);
        var acctype = getAccType(content.Sku.AccountType.ToString());


        getAccType Function is this...



         public StorageAccountTypes getAccType(string input)
        {
        switch (input)
        {
        case "StandardLRS": return StorageAccountTypes.StandardLRS;
        case "PremiumLRS": return StorageAccountTypes.PremiumLRS;
        // case "StandardSSDLRS":return "StandardSSD_LRS";
        // case "UltraSSDLRS":return "UltraSSD_LRS";
        default: return StorageAccountTypes.StandardLRS;

        }
        }


        The Problem is that the AccType version in this namespace is limited to 2 (StandardLRS and PremiumLRS) . In the old version you had 4 different versions of it.






        share|improve this answer












        ok, here is the answer...



        var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(ressgroup, virtualmname, null, new System.Threading.CancellationToken()).Result;
        var storagemanagementclient = new StorageManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };

        ObjVirtualMachines.StorageProfile.DataDisks.Add(new DataDisk(
        ObjVirtualMachines.StorageProfile.DataDisks.Count + 1,
        DiskCreateOptionTypes.Attach,
        name,
        null,
        null,
        null,
        null,
        new ManagedDiskParametersInner(resourceid, acctype)));

        var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(ressgroup, virtualmname, ObjVirtualMachines);


        and for the acctype use this.



         var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = credentials.DefaultSubscriptionId };
        var disk = computeClient.Disks.ListAsync().Result.FirstOrDefault(p => p.Name == name);
        var content = azure.Disks.GetById(disk.Id);
        var resourceid = disk.Id;
        var ressgroup = Resourcegroup.Get(context);
        var virtualmname = VirtualMachineName.Get(context);
        var acctype = getAccType(content.Sku.AccountType.ToString());


        getAccType Function is this...



         public StorageAccountTypes getAccType(string input)
        {
        switch (input)
        {
        case "StandardLRS": return StorageAccountTypes.StandardLRS;
        case "PremiumLRS": return StorageAccountTypes.PremiumLRS;
        // case "StandardSSDLRS":return "StandardSSD_LRS";
        // case "UltraSSDLRS":return "UltraSSD_LRS";
        default: return StorageAccountTypes.StandardLRS;

        }
        }


        The Problem is that the AccType version in this namespace is limited to 2 (StandardLRS and PremiumLRS) . In the old version you had 4 different versions of it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 10:03









        Glowhollow

        599




        599






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417327%2fazure-disk-attachment-to-vm-using-c-sharp-and-storagemanagementclient-computeman%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)