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












0















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 '18 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 '18 at 9:58













  • ok, cool, got it done

    – Glowhollow
    Nov 22 '18 at 13:03











  • If it is resolved, please add it as an answer to close this thread.

    – Tom Sun
    Nov 23 '18 at 1:17
















0















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 '18 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 '18 at 9:58













  • ok, cool, got it done

    – Glowhollow
    Nov 22 '18 at 13:03











  • If it is resolved, please add it as an answer to close this thread.

    – Tom Sun
    Nov 23 '18 at 1:17














0












0








0








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 '18 at 13:11









Pedro Gaspar

549321




549321










asked Nov 21 '18 at 17:11









GlowhollowGlowhollow

729




729













  • What does i have to translate to the other package mean? Could you please add more information about that?

    – Tom Sun
    Nov 22 '18 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 '18 at 9:58













  • ok, cool, got it done

    – Glowhollow
    Nov 22 '18 at 13:03











  • If it is resolved, please add it as an answer to close this thread.

    – Tom Sun
    Nov 23 '18 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 '18 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 '18 at 9:58













  • ok, cool, got it done

    – Glowhollow
    Nov 22 '18 at 13:03











  • If it is resolved, please add it as an answer to close this thread.

    – Tom Sun
    Nov 23 '18 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 '18 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 '18 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 '18 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 '18 at 9:58















ok, cool, got it done

– Glowhollow
Nov 22 '18 at 13:03





ok, cool, got it done

– Glowhollow
Nov 22 '18 at 13:03













If it is resolved, please add it as an answer to close this thread.

– Tom Sun
Nov 23 '18 at 1:17





If it is resolved, please add it as an answer to close this thread.

– Tom Sun
Nov 23 '18 at 1:17












1 Answer
1






active

oldest

votes


















0














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',
    autoActivateHeartbeat: false,
    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









    0














    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




























      0














      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


























        0












        0








        0







        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 '18 at 10:03









        GlowhollowGlowhollow

        729




        729
































            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.




            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

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory