public async Task <AzureOperationResponse <string> > CreateVirtialMachineWithManagedDisk(
     string resourceGroupName,
     string virtualMachineName,
     string nicId,
     string diskId,
     string location,
     Profile2018Compute.Models.ImageReference imageReference = null)
 {
     if (String.Equals(environment, "azurestack", StringComparison.CurrentCultureIgnoreCase))
     {
         return(await CreateVirtialMachineWithManagedDiskStack
                    (resourceGroupName, virtualMachineName, nicId, diskId, location, imageReference));
     }
     else
     {
         return(await CreateVirtialMachineWithManagedDiskAzure
                    (resourceGroupName, virtualMachineName, nicId, diskId, location));
     }
 }
        private async Task <AzureOperationResponse <string> > CreateVirtialMachineWithManagedDiskStack(
            string resourceGroupName,
            string virtualMachineName,
            string nicId,
            string diskId,
            string location,
            Profile2018Compute.Models.ImageReference imageReference = null)
        {
            if (client == null)
            {
                return(new AzureOperationResponse <string>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.ExpectationFailed,
                        ReasonPhrase = "Client is not instantiated"
                    }
                });
            }

            try
            {
                var vmParameters = new Profile2018Compute.Models.VirtualMachine
                {
                    Location       = location,
                    NetworkProfile = new Profile2018Compute.Models.NetworkProfile
                    {
                        NetworkInterfaces = new List <Profile2018Compute.Models.NetworkInterfaceReference>
                        {
                            new Profile2018Compute.Models.NetworkInterfaceReference
                            {
                                Id      = nicId,
                                Primary = true
                            }
                        }
                    },
                    StorageProfile = new Profile2018Compute.Models.StorageProfile
                    {
                        DataDisks = new List <Profile2018Compute.Models.DataDisk>
                        {
                            new Profile2018Compute.Models.DataDisk
                            {
                                CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.Attach,
                                ManagedDisk  = new Profile2018Compute.Models.ManagedDiskParameters
                                {
                                    StorageAccountType = Profile2018Compute.Models.StorageAccountTypes.StandardLRS,
                                    Id = diskId
                                }
                            }
                        },
                        OsDisk = new Profile2018Compute.Models.OSDisk
                        {
                            Name         = "osDisk",
                            CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage
                        }
                    },
                    OsProfile = new Profile2018Compute.Models.OSProfile
                    {
                        ComputerName  = virtualMachineName,
                        AdminUsername = "******",
                        AdminPassword = "******"
                    },
                    HardwareProfile = new Profile2018Compute.Models.HardwareProfile
                    {
                        VmSize = "Standard_A1"
                    }
                };

                if (imageReference == null)
                {
                    vmParameters.StorageProfile.ImageReference = linuxImageReference;
                }
                else
                {
                    vmParameters.StorageProfile.ImageReference = imageReference;
                }

                var virtualMachineTask = await client.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, virtualMachineName, vmParameters);

                return(new AzureOperationResponse <string>
                {
                    Body = virtualMachineTask.Body.Id
                });
            }
            catch (Exception ex)
            {
                return(new AzureOperationResponse <string>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        ReasonPhrase = ex.Message
                    }
                });
            }
        }
        public async Task <AzureOperationResponse <Profile2018Compute.Models.VirtualMachine> > CreateVirtialMachine(
            string resourceGroupName,
            string virtualMachineName,
            string storageAccountName,
            string storagePrefix,
            string nicId,
            string location,
            Profile2018Compute.Models.ImageReference imageReference = null)
        {
            if (client == null)
            {
                return(new AzureOperationResponse <Profile2018Compute.Models.VirtualMachine>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.ExpectationFailed,
                        ReasonPhrase = "Client is not instantiated"
                    }
                });
            }

            try
            {
                var vmParameters = new Profile2018Compute.Models.VirtualMachine
                {
                    Location       = location,
                    NetworkProfile = new Profile2018Compute.Models.NetworkProfile
                    {
                        NetworkInterfaces = new List <Profile2018Compute.Models.NetworkInterfaceReference>
                        {
                            new Profile2018Compute.Models.NetworkInterfaceReference
                            {
                                Id      = nicId,
                                Primary = true
                            }
                        }
                    },
                    StorageProfile = new Profile2018Compute.Models.StorageProfile
                    {
                        OsDisk = new Profile2018Compute.Models.OSDisk
                        {
                            Name = "osDisk",
                            Vhd  = new Profile2018Compute.Models.VirtualHardDisk
                            {
                                Uri = string.Format(vhdURItemplate, storageAccountName, storagePrefix, virtualMachineName)
                            },
                            CreateOption = Profile2018Compute.Models.DiskCreateOptionTypes.FromImage
                        }
                    },
                    OsProfile = new Profile2018Compute.Models.OSProfile
                    {
                        ComputerName  = virtualMachineName,
                        AdminUsername = "******",
                        AdminPassword = "******"
                    },
                    HardwareProfile = new Profile2018Compute.Models.HardwareProfile
                    {
                        VmSize = "Standard_A1"
                    }
                };

                if (imageReference == null)
                {
                    vmParameters.StorageProfile.ImageReference = linuxImageReference;
                }
                else
                {
                    vmParameters.StorageProfile.ImageReference = imageReference;
                }
                var virtualMachineTask = await client.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, virtualMachineName, vmParameters);

                return(virtualMachineTask);
            }
            catch (Exception ex)
            {
                return(new AzureOperationResponse <Profile2018Compute.Models.VirtualMachine>
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        ReasonPhrase = ex.Message
                    }
                });
            }
        }