protected VirtualMachine CreateVM_NoAsyncTracking( string rgName, string asName, StorageAccount storageAccount, ImageReference imageRef, out VirtualMachine inputVM, Action <VirtualMachine> vmCustomizer = null, bool createWithPublicIpAddress = false) { try { // Create the resource Group, it might have been already created during StorageAccount creation. var resourceGroup = m_ResourcesClient.ResourceGroups.CreateOrUpdate( rgName, new ResourceGroup { Location = m_location }); PublicIpAddressGetResponse getPublicIpAddressResponse = createWithPublicIpAddress ? null : CreatePublicIP(rgName); SubnetGetResponse subnetResponse = CreateVNET(rgName); NetworkInterfaceGetResponse nicResponse = CreateNIC( rgName, subnetResponse.Subnet, getPublicIpAddressResponse != null ? getPublicIpAddressResponse.PublicIpAddress : null); string asetId = CreateAvailabilitySet(rgName, asName); inputVM = CreateDefaultVMInput(rgName, storageAccount.Name, imageRef, asetId, nicResponse.NetworkInterface.Id); if (vmCustomizer != null) { vmCustomizer(inputVM); } string expectedVMReferenceId = Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name); var createOrUpdateResponse = m_CrpClient.VirtualMachines.BeginCreatingOrUpdating( rgName, inputVM); Assert.True(createOrUpdateResponse.StatusCode == HttpStatusCode.Created); Assert.True(createOrUpdateResponse.VirtualMachine.Name == inputVM.Name); Assert.True(createOrUpdateResponse.VirtualMachine.Location == inputVM.Location.ToLower().Replace(" ", "") || createOrUpdateResponse.VirtualMachine.Location.ToLower() == inputVM.Location.ToLower()); Assert.True( createOrUpdateResponse.VirtualMachine.AvailabilitySetReference.ReferenceUri .ToLowerInvariant() == asetId.ToLowerInvariant()); ValidateVM(inputVM, createOrUpdateResponse.VirtualMachine, expectedVMReferenceId); var operationUri = new Uri(createOrUpdateResponse.AzureAsyncOperation); string operationId = operationUri.Segments.LastOrDefault(); var lroResponse = m_CrpClient.GetLongRunningOperationStatus(createOrUpdateResponse.AzureAsyncOperation.ToString()); ValidateLROResponse(lroResponse, operationId); // CONSIDER dropping this Get and ValidateVM call. Nothing changes in the VM model after it's accepted. // There might have been intent to track the async operation to completion and then check the VM is // still this and okay, but that's not what the code above does and still doesn't make much sense. var getResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name); Assert.True(getResponse.StatusCode == HttpStatusCode.OK); ValidateVM(inputVM, getResponse.VirtualMachine, expectedVMReferenceId); return(getResponse.VirtualMachine); } catch { var deleteRg1Response = m_ResourcesClient.ResourceGroups.Delete(rgName); Assert.True(deleteRg1Response.StatusCode == HttpStatusCode.OK); throw; } }
protected VirtualMachine CreateVM(string rgName, string asName, StorageAccount storageAccount, string imgRefId, out VirtualMachine inputVM, Action <VirtualMachine> vmCustomizer = null, bool createWithPublicIpAddress = false) { try { // Create the resource Group, it might have been already created during StorageAccount creation. var resourceGroup = m_ResourcesClient.ResourceGroups.CreateOrUpdate( rgName, new ResourceGroup { Location = m_location }); PublicIpAddressGetResponse getPublicIpAddressResponse = createWithPublicIpAddress ? null : CreatePublicIP(rgName); SubnetGetResponse subnetResponse = CreateVNET(rgName); NetworkInterfaceGetResponse nicResponse = CreateNIC(rgName, subnetResponse.Subnet, getPublicIpAddressResponse.PublicIpAddress); string asetId = CreateAvailabilitySet(rgName, asName); inputVM = CreateDefaultVMInput(rgName, storageAccount.Name, imgRefId, asetId, nicResponse.NetworkInterface.Id); if (vmCustomizer != null) { vmCustomizer(inputVM); } string expectedVMReferenceId = Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name); var createOrUpdateResponse = m_CrpClient.VirtualMachines.BeginCreatingOrUpdating( rgName, inputVM); Assert.True(createOrUpdateResponse.StatusCode == HttpStatusCode.Created); Assert.True(createOrUpdateResponse.VirtualMachine.Name == inputVM.Name); Assert.True(createOrUpdateResponse.VirtualMachine.Location == inputVM.Location.ToLower().Replace(" ", "") || createOrUpdateResponse.VirtualMachine.Location.ToLower() == inputVM.Location.ToLower()); Assert.True( createOrUpdateResponse.VirtualMachine.AvailabilitySetReference.ReferenceUri .ToLowerInvariant() == asetId.ToLowerInvariant()); ValidateVM(inputVM, createOrUpdateResponse.VirtualMachine, expectedVMReferenceId); var operationUri = new Uri(createOrUpdateResponse.AzureAsyncOperation); string operationId = operationUri.Segments.LastOrDefault(); var lroResponse = m_CrpClient.GetLongRunningOperationStatus(createOrUpdateResponse.AzureAsyncOperation.ToString()); ValidateLROResponse(lroResponse, operationId); var getResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name); Assert.True(getResponse.StatusCode == HttpStatusCode.OK); ValidateVM(inputVM, getResponse.VirtualMachine, expectedVMReferenceId); return(getResponse.VirtualMachine); } catch { var deleteRg1Response = m_ResourcesClient.ResourceGroups.Delete(rgName); Assert.True(deleteRg1Response.StatusCode == HttpStatusCode.OK); throw; } }