internal static void ValidateVMRunCommand(VirtualMachineRunCommand vmRunCommandExpected, VirtualMachineRunCommand vmRunCommandReturned)
 {
     Assert.True(!string.IsNullOrEmpty(vmRunCommandReturned.ProvisioningState));
     Assert.True(!string.IsNullOrEmpty(vmRunCommandReturned.Id));
     Assert.Equal(vmRunCommandExpected.ErrorBlobUri, vmRunCommandReturned.ErrorBlobUri);
     Assert.Equal(vmRunCommandExpected.OutputBlobUri, vmRunCommandReturned.OutputBlobUri);
     Assert.Equal(vmRunCommandExpected.Source?.Script, vmRunCommandReturned.Source?.Script);
     Assert.Equal(vmRunCommandExpected.Source?.ScriptUri, vmRunCommandReturned.Source?.ScriptUri);
     Assert.Equal(vmRunCommandExpected.Source?.CommandId, vmRunCommandReturned.Source?.CommandId);
     Assert.Equal(vmRunCommandExpected.Type, vmRunCommandReturned.Type);
     Assert.Equal(vmRunCommandExpected.Name, vmRunCommandReturned.Name);
     Assert.Equal(vmRunCommandExpected.AsyncExecution, vmRunCommandReturned.AsyncExecution);
     Assert.Equal(vmRunCommandExpected.RunAsUser, vmRunCommandReturned.RunAsUser);
     Assert.Equal(vmRunCommandExpected.TimeoutInSeconds, vmRunCommandReturned.TimeoutInSeconds);
     Assert.Equal(vmRunCommandExpected.RunAsUser, vmRunCommandReturned.RunAsUser);
     Assert.Equal(vmRunCommandExpected.RunAsUser, vmRunCommandReturned.RunAsUser);
     Assert.Equal(vmRunCommandExpected.RunAsUser, vmRunCommandReturned.RunAsUser);
     Assert.True(vmRunCommandExpected.Tags.SequenceEqual(vmRunCommandReturned.Tags));
     if (vmRunCommandExpected.Parameters != null)
     {
         Assert.NotNull(vmRunCommandReturned.Parameters);
         Assert.Equal(vmRunCommandExpected.Parameters.Count, vmRunCommandReturned.Parameters.Count);
         for (int i = 0; i < vmRunCommandExpected.Parameters.Count(); i++)
         {
             Assert.Equal(vmRunCommandExpected.Parameters[i].Name, vmRunCommandReturned.Parameters[i].Name);
             Assert.Equal(vmRunCommandExpected.Parameters[i].Value, vmRunCommandReturned.Parameters[i].Value);
         }
     }
 }
        internal static VirtualMachineRunCommand GetTestVMRunCommand(string type)
        {
            var vmRunCommand = new VirtualMachineRunCommand
            {
                Location   = ComputeManagementTestUtilities.DefaultLocation,
                Source     = new VirtualMachineRunCommandScriptSource("Write-Host Hello World!"),
                Parameters = new List <RunCommandInputParameter>()
                {
                    new RunCommandInputParameter("param1", "value1"),
                    new RunCommandInputParameter("param2", "value2")
                },
                ProtectedParameters = new List <RunCommandInputParameter>()
                {
                    new RunCommandInputParameter("secret", "protectedSecret"),
                },
                TimeoutInSeconds = 3600,
                AsyncExecution   = false,
                Tags             = new Dictionary <string, string>()
                {
                    { "Tag1", "1" }
                },
            };

            typeof(Resource).GetRuntimeProperty("Name").SetValue(vmRunCommand, "firstRunCommand");
            typeof(Resource).GetRuntimeProperty("Type").SetValue(vmRunCommand, type);

            return(vmRunCommand);
        }
Пример #3
0
 /// <summary>
 /// The operation to create or update the VMSS VM run command.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='vmScaleSetName'>
 /// The name of the VM scale set.
 /// </param>
 /// <param name='instanceId'>
 /// The instance ID of the virtual machine.
 /// </param>
 /// <param name='runCommandName'>
 /// The name of the virtual machine run command.
 /// </param>
 /// <param name='runCommand'>
 /// Parameters supplied to the Create Virtual Machine RunCommand operation.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <VirtualMachineRunCommand> CreateOrUpdateAsync(this IVirtualMachineScaleSetVMRunCommandsOperations operations, string resourceGroupName, string vmScaleSetName, string instanceId, string runCommandName, VirtualMachineRunCommand runCommand, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId, runCommandName, runCommand, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Пример #4
0
 /// <summary>
 /// The operation to create or update the VMSS VM run command.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='vmScaleSetName'>
 /// The name of the VM scale set.
 /// </param>
 /// <param name='instanceId'>
 /// The instance ID of the virtual machine.
 /// </param>
 /// <param name='runCommandName'>
 /// The name of the virtual machine run command.
 /// </param>
 /// <param name='runCommand'>
 /// Parameters supplied to the Create Virtual Machine RunCommand operation.
 /// </param>
 public static VirtualMachineRunCommand CreateOrUpdate(this IVirtualMachineScaleSetVMRunCommandsOperations operations, string resourceGroupName, string vmScaleSetName, string instanceId, string runCommandName, VirtualMachineRunCommand runCommand)
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, vmScaleSetName, instanceId, runCommandName, runCommand).GetAwaiter().GetResult());
 }
        public void TestVMScaleSetRunCommands()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create resource group
                string rgName             = TestUtilities.GenerateName(TestPrefix) + 1;
                var    vmssName           = TestUtilities.GenerateName("vmss");
                string storageAccountName = TestUtilities.GenerateName(TestPrefix);
                VirtualMachineScaleSet inputVMScaleSet;
                string instanceId = "0";
                bool   passed     = false;
                try
                {
                    EnsureClientsInitialized(context);

                    ImageReference imageRef             = GetPlatformVMImage(useWindowsImage: true);
                    var            storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

                    VirtualMachineScaleSet vmScaleSet = CreateVMScaleSet_NoAsyncTracking(
                        rgName,
                        vmssName,
                        storageAccountOutput,
                        imageRef,
                        out inputVMScaleSet,
                        vmScaleSetCustomizer: vmssModel =>
                    {
                        vmssModel.Tags = new Dictionary <string, string>()
                        {
                            { "supportsMultipleExtensions", "true" }
                        };
                    });

                    // Add a run command to the VMSS
                    VirtualMachineRunCommand vmssRunCommand = VMRunCommandsTests.GetTestVMRunCommand("Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommands");
                    var response = m_CrpClient.VirtualMachineScaleSetVMRunCommands.CreateOrUpdate(rgName, vmssName, instanceId, vmssRunCommand.Name, vmssRunCommand);
                    VMRunCommandsTests.ValidateVMRunCommand(vmssRunCommand, response);

                    // Perform a Get operation on the run command
                    var getVmssRunCommandResponse = m_CrpClient.VirtualMachineScaleSetVMRunCommands.Get(rgName, vmssName, instanceId, vmssRunCommand.Name);
                    VMRunCommandsTests.ValidateVMRunCommand(vmssRunCommand, getVmssRunCommandResponse);

                    // Validate the run command instance view
                    var getVmssWithInstanceViewResponse = m_CrpClient.VirtualMachineScaleSetVMRunCommands.Get(rgName, vmssName, instanceId, vmssRunCommand.Name, "instanceView");
                    VMRunCommandsTests.ValidateVMRunCommandInstanceView(getVmssWithInstanceViewResponse.InstanceView);

                    // Perform a List operation on vmss vm run command
                    var listVmssVMRunCommandsResponse = m_CrpClient.VirtualMachineScaleSetVMRunCommands.List(rgName, vmssName, instanceId);
                    VMRunCommandsTests.ValidateVMRunCommand(vmssRunCommand, listVmssVMRunCommandsResponse.FirstOrDefault(c => c.Name == vmssRunCommand.Name));

                    // Validate the run command delete API
                    m_CrpClient.VirtualMachineScaleSetVMRunCommands.Delete(rgName, vmssName, instanceId, vmssRunCommand.Name);

                    passed = true;
                }
                finally
                {
                    // Cleanup the created resources. But don't wait since it takes too long, and it's not the purpose
                    // of the test to cover deletion. CSM does persistent retrying over all RG resources.
                    m_ResourcesClient.ResourceGroups.Delete(rgName);
                }

                Assert.True(passed);
            }
        }
Пример #6
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.VMScaleSetName, "Add"))
                {
                    string resourceGroupName;
                    string vmScaleSetName;
                    switch (this.ParameterSetName)
                    {
                    case "ObjectParameter":
                        resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id);
                        vmScaleSetName    = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        break;

                    default:
                        resourceGroupName = this.ResourceGroupName;
                        vmScaleSetName    = this.VMScaleSetName;
                        break;
                    }
                    VirtualMachineRunCommand vmruncommand;

                    vmruncommand = new VirtualMachineRunCommand
                    {
                        OutputBlobUri    = this.IsParameterBound(c => c.OutputBlobUri) ? this.OutputBlobUri : null,
                        ErrorBlobUri     = this.IsParameterBound(c => c.ErrorBlobUri) ? this.ErrorBlobUri : null,
                        RunAsPassword    = this.IsParameterBound(c => c.RunAsPassword) ? this.RunAsPassword.ToString() : null,
                        RunAsUser        = this.IsParameterBound(c => c.RunAsUser) ? this.RunAsUser : null,
                        TimeoutInSeconds = this.IsParameterBound(c => c.TimeOutInSeconds) ? Convert.ToInt32(this.TimeOutInSeconds) : (int?)null,
                        Location         = this.IsParameterBound(c => c.Location) ? this.Location : null,
                        AsyncExecution   = this.IsParameterBound(c => c.AsyncExecution) ? this.AsyncExecution : null
                    };


                    if (this.IsParameterBound(c => c.ScriptUri))
                    {
                        vmruncommand.Source = new VirtualMachineRunCommandScriptSource
                        {
                            Script    = this.IsParameterBound(c => c.Script) ? this.Script : null,
                            ScriptUri = this.IsParameterBound(c => c.ScriptUri) ? this.ScriptUri : null,
                            CommandId = this.IsParameterBound(c => c.CommandId) ? this.CommandId : null
                        };
                    }
                    else if (this.ScriptPath != null && this.IsParameterBound(c => c.ScriptPath))
                    {
                        List <string> Script       = new List <string>();
                        PathIntrinsics currentPath = SessionState.Path;
                        var filePath       = new System.IO.FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(this.ScriptPath));
                        string fileContent = Commands.Common.Authentication.Abstractions.FileUtilities.DataStore.ReadFileAsText(filePath.FullName);
                        Script             = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList();

                        vmruncommand.Source = new VirtualMachineRunCommandScriptSource(this.Script, this.ScriptPath, this.CommandId);
                    }

                    if (this.Parameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.Parameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();

                            p.Name  = key.ToString();
                            p.Value = this.Parameter[key].ToString();
                            vParameter.Add(p);
                        }
                        vmruncommand.Parameters = vParameter;
                    }
                    else if (this.ProtectedParameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.ProtectedParameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();

                            p.Name  = key.ToString();
                            p.Value = this.ProtectedParameter[key].ToString();
                            vParameter.Add(p);
                        }
                        vmruncommand.ProtectedParameters = vParameter;
                    }

                    var expand       = new Microsoft.Rest.Azure.OData.ODataQuery <VirtualMachineScaleSetVM>();
                    expand.Expand    = "instanceView";
                    var vmList       = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName, expand);
                    var resultList   = vmList.ToList();
                    var nextPageLink = vmList.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    List <VirtualMachineScaleSetVM> listOfVms = new List <VirtualMachineScaleSetVM>(resultList);
                    List <PSVirtualMachineRunCommand> output  = new List <PSVirtualMachineRunCommand>();


                    foreach (VirtualMachineScaleSetVM vm in listOfVms)
                    {
                        var resultOfRunCmd = VirtualMachineScaleSetVMRunCommandsClient.BeginCreateOrUpdateAsync(resourceGroupName, vmScaleSetName, vm.InstanceId, this.Name, vmruncommand);
                        var Pstemp         = new PSVirtualMachineRunCommand();
                        ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineRunCommand, PSVirtualMachineRunCommand>(resultOfRunCmd.Result, Pstemp);
                        output.Add(Pstemp);
                    }

                    WriteObject(output);
                }
            });
        }
Пример #7
0
 /// <summary>
 /// The operation to create or update the run command.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='vmName'>
 /// The name of the virtual machine where the run command should be created or
 /// updated.
 /// </param>
 /// <param name='runCommandName'>
 /// The name of the virtual machine run command.
 /// </param>
 /// <param name='runCommand'>
 /// Parameters supplied to the Create Virtual Machine RunCommand operation.
 /// </param>
 public static VirtualMachineRunCommand BeginCreateOrUpdate(this IVirtualMachineRunCommandsOperations operations, string resourceGroupName, string vmName, string runCommandName, VirtualMachineRunCommand runCommand)
 {
     return(operations.BeginCreateOrUpdateAsync(resourceGroupName, vmName, runCommandName, runCommand).GetAwaiter().GetResult());
 }