Наследование: global::System.ComponentModel.INotifyPropertyChanged
        public override void ExecuteCmdlet()
        {
            VirtualMachine pendingVirtualMachine = null;
            var virtualMachineOperations = new VirtualMachineOperations(this.WebClientFactory);
            Guid? jobId = Guid.Empty;

            var newVirtualMachine = new VirtualMachine()
            {
                Name = Name,
                VMTemplateId = Template.ID,
                UserName = VMCredential.UserName,
                Password = ExtractSecureString(VMCredential.Password),
            };

            pendingVirtualMachine = virtualMachineOperations.Create(newVirtualMachine, out jobId);

            if (!jobId.HasValue)
            {
                throw new WAPackOperationException(Resources.CreateFailedErrorMessage);
            }

            var jobInfo = new JobOperations(this.WebClientFactory).WaitOnJob(jobId.Value);
            if (jobInfo.jobStatus == JobStatusEnum.Failed)
            {
                this.WriteErrorDetails(new Exception(jobInfo.errorMessage));
            }

            var createdVM = virtualMachineOperations.Read(pendingVirtualMachine.ID);
           
            WriteObject(createdVM);
        }
Пример #2
0
        public void SerializeDeserializeVirtualMachine()
        {
            var helper = new JsonHelpers<VirtualMachine>();


            var toSerialize = new VirtualMachine
                {
                    ID = Guid.NewGuid(),
                    StampId = Guid.NewGuid(),
                    CreationTime = DateTime.Now,
                    PerfDiskBytesRead = 500,
                    Name = @"This is a test 这是一个测试 㐀㐁㐂㐃㐄㐅"
                };

            var serialized = helper.Serialize(toSerialize);

            var vmList = helper.Deserialize(serialized);
            Assert.NotNull(vmList);
            Assert.Equal(vmList.Count, 1);
            Assert.True(vmList[0] is VirtualMachine);

            Assert.Equal(toSerialize.ID, vmList[0].ID);
            Assert.Equal(toSerialize.StampId, vmList[0].StampId);
            Assert.Equal(toSerialize.CreationTime, vmList[0].CreationTime);
            Assert.Equal(toSerialize.PerfDiskBytesRead, vmList[0].PerfDiskBytesRead);
            Assert.Equal(toSerialize.Name, vmList[0].Name);
        }
        public override void ExecuteCmdlet()
        {
            VirtualMachine pendingVirtualMachine = null;
            var virtualMachineOperations = new VirtualMachineOperations(this.WebClientFactory);
            Guid? jobId = Guid.Empty;

            var newVirtualMachine = new VirtualMachine()
            {
                Name = Name,
                VMTemplateId = Template.ID,
                LocalAdminUserName = VMCredential.UserName,
                LocalAdminPassword = ExtractSecureString(VMCredential.Password),
            };

            pendingVirtualMachine = virtualMachineOperations.Create(newVirtualMachine, out jobId);

            if (!jobId.HasValue)
            {
                throw new WAPackOperationException(Resources.CreateFailedErrorMessage);
            }
            WaitForJobCompletion(jobId);

            var createdVM = virtualMachineOperations.Read(pendingVirtualMachine.ID);
           
            WriteObject(createdVM);
        }
Пример #4
0
 private void SetSizeProfile(VirtualMachine vm)
 {
     vm.CPUCount = VMSizeProfile.CPUCount;
     vm.Memory = VMSizeProfile.Memory;
 }
        private static void CheckVirtualMachineOperationResult(string operation, MockRequestChannel mockChannel, VirtualMachine testVM)
        {
            var requests = mockChannel.ClientRequests;
            Assert.AreEqual(requests.Count, 2);
            Assert.AreEqual(requests[1].Item1.Method, HttpMethod.Put.ToString());

            var clientSentVM = mockChannel.DeserializeClientPayload<VirtualMachine>(requests[1].Item2);
            Assert.IsNotNull(clientSentVM);
            Assert.IsTrue(clientSentVM.Count == 1);
            Assert.AreEqual(testVM.ID, clientSentVM[0].ID);
            Assert.AreEqual(testVM.StampId, clientSentVM[0].StampId);
            Assert.AreEqual(clientSentVM[0].Operation, operation);
        }
        private static VirtualMachine InitVirtualMachineOperation(MockRequestChannel mockChannel, out VirtualMachineOperations vmOperations)
        {
            //Cloud for return value of first request (client gets cloud to get stampId)
            var testCloud = new Cloud {ID = Guid.NewGuid(), StampId = Guid.NewGuid()};
            mockChannel.AddReturnObject(testCloud);

            //VM for return value of second request (client updates VM with operation)
            var testVM = new VirtualMachine {ID = Guid.NewGuid(), StampId = testCloud.StampId};
            mockChannel.AddReturnObject(testVM, new WebHeaderCollection {"x-ms-request-id:" + Guid.NewGuid()});

            var factory = new WebClientFactory(new Subscription(), mockChannel);
            vmOperations = new VirtualMachineOperations(factory);

            return testVM;
        }
        public void VmUpdateShouldThrowWhenNoObjectReturned()
        {
            var mockChannel = new MockRequestChannel();

            var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel));

            var vmToUpdate = new VirtualMachine { VirtualHardDiskId = Guid.NewGuid(), Name = "Test" };

            Guid? jobOut;
            vmOps.Update(vmToUpdate, out jobOut);
        }
        public void VmCreateShouldThrowWhenNoObjectReturned()
        {
            var mockChannel = new MockRequestChannel();

            var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() };
            mockChannel.AddReturnObject(testCloud);

            var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel));

            var vmToCreate = new VirtualMachine { VirtualHardDiskId = Guid.NewGuid(), Name = "Test" };

            Guid? jobOut;
            vmOps.Create(vmToCreate, out jobOut);
        }
        public void VmCreateShouldThrowIfNoVhdAndNoTemplateSupplied()
        {
            var channel = new MockRequestChannel();
            var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() };
            channel.AddReturnObject(testCloud);

            var sub = new Subscription();
            var vmOps = new VirtualMachineOperations(new WebClientFactory(sub, channel));

            var vmToCreate = new VirtualMachine {Name = "Test"};

            Guid? jobOut;
            vmOps.Create(vmToCreate, out jobOut);
        }
        public void CreateVMFromTemplate()
        {
            var mockChannel = new MockRequestChannel();

            var testCloud = new Cloud { ID = Guid.NewGuid(), StampId = Guid.NewGuid() };
            mockChannel.AddReturnObject(testCloud);

            var vmToCreate = new VirtualMachine { VMTemplateId = Guid.NewGuid(), Name = "Test" };
            var vmToReturn = new VirtualMachine
            {
                ID = Guid.NewGuid(),
                Name = vmToCreate.Name,
                CloudId = testCloud.ID,
                StampId = testCloud.StampId
            };
            mockChannel.AddReturnObject(vmToReturn, new WebHeaderCollection { "x-ms-request-id:" + Guid.NewGuid() });

            var vmOps = new VirtualMachineOperations(new WebClientFactory(new Subscription(), mockChannel));

            Guid? jobOut;
            var resultVM = vmOps.Create(vmToCreate, out jobOut);

            //Check the results that client returns
            Assert.IsNotNull(resultVM);
            Assert.IsInstanceOfType(resultVM, typeof(VirtualMachine));
            Assert.AreEqual(resultVM.ID, vmToReturn.ID);
            Assert.AreEqual(resultVM.Name, vmToReturn.Name);
            Assert.AreEqual(resultVM.CloudId, vmToReturn.CloudId);
            Assert.AreEqual(resultVM.StampId, vmToReturn.StampId);

            //Check the requests that the client made
            var requestList = mockChannel.ClientRequests;
            Assert.AreEqual(requestList.Count, 2);
            Assert.AreEqual(requestList[1].Item1.Method, HttpMethod.Post.ToString());
            Assert.IsTrue(requestList[1].Item1.RequestUri.ToString().TrimEnd(new[] { '/' }).EndsWith("/VirtualMachines"));

            var sentVM = mockChannel.DeserializeClientPayload<VirtualMachine>(requestList[1].Item2);
            Assert.IsNotNull(sentVM);
            Assert.IsTrue(sentVM.Count == 1);
            Assert.AreEqual(sentVM[0].CloudId, testCloud.ID);
            Assert.AreEqual(sentVM[0].StampId, testCloud.StampId);
            Assert.AreEqual(sentVM[0].Name, vmToCreate.Name);
            Assert.AreEqual(sentVM[0].VMTemplateId, vmToCreate.VMTemplateId);
        }
Пример #11
0
        public override void ExecuteCmdlet()
        {
            VirtualMachine newVirtualMachine = null;
            var virtualMachineOperations = new VirtualMachineOperations(this.WebClientFactory);
            Guid? jobId = Guid.Empty;
            
            var virtualNetworkAdaptersWithVNet = this.CustomizeVnaInput();

            if (this.ParameterSetName == WAPackCmdletParameterSets.CreateWindowsVMFromTemplate)
            {
                newVirtualMachine = new VirtualMachine()
                {
                    Name = Name,
                    VMTemplateId = Template.ID,
                    LocalAdminUserName = VMCredential.UserName,
                    LocalAdminPassword = ExtractSecureString(VMCredential.Password),
                    NewVirtualNetworkAdapterInput = virtualNetworkAdaptersWithVNet,
                    ProductKey = ProductKey,
                };
            }
            else if (this.ParameterSetName == WAPackCmdletParameterSets.CreateLinuxVMFromTemplate)
            {
                newVirtualMachine = new VirtualMachine()
                {
                    Name = Name,
                    VMTemplateId = Template.ID,
                    LocalAdminUserName = VMCredential.UserName,
                    LocalAdminPassword = ExtractSecureString(VMCredential.Password),
                    NewVirtualNetworkAdapterInput = virtualNetworkAdaptersWithVNet,
                    LinuxAdministratorSSHKeyString = AdministratorSSHKey
                };
            }
            else if (this.ParameterSetName == WAPackCmdletParameterSets.CreateVMFromOSDisks)
            {
                newVirtualMachine = new VirtualMachine()
                {
                    Name = Name,
                    HardwareProfileId = VMSizeProfile.ID,
                    VirtualHardDiskId = OSDisk.ID,
                    NewVirtualNetworkAdapterInput = virtualNetworkAdaptersWithVNet
                };
            }

            var createdVirtualMachine = virtualMachineOperations.Create(newVirtualMachine, out jobId);

            if (!jobId.HasValue)
            {
                throw new WAPackOperationException(Resources.CreateFailedErrorMessage);
            }
            WaitForJobCompletion(jobId);

            createdVirtualMachine = virtualMachineOperations.Read(createdVirtualMachine.ID);
            WriteObject(createdVirtualMachine);
        }