Skip to content

Commit

Permalink
Adding Add-AzVmssRunCommand And Remove-AzVmssRunCommand (Azure#16318)
Browse files Browse the repository at this point in the history
* Adding manual run commands

* Adding Test and Recording

* small changes and changelog.md

* adding help

* fixing help

* change from parameters to parameter
  • Loading branch information
haagha authored Nov 2, 2021
1 parent 8d299b0 commit a26ef6a
Show file tree
Hide file tree
Showing 11 changed files with 4,032 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,12 @@ public void TestVirtualMachineScaleSetFlexibleOModeDefaulting()
{
TestRunner.RunTestScript("Test-VirtualMachineScaleSetFlexibleOModeDefaulting");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestAddAzVmssRunCommand()
{
TestRunner.RunTestScript("Test-AddAndRemoveAzVmssRunCommand");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2873,3 +2873,45 @@ function Test-VirtualMachineScaleSetFlexibleOModeDefaulting
}
}

<#
.SYNOPSIS
Test Add and remove Vmss Run Command.
#>
function Test-AddAndRemoveAzVmssRunCommand
{

# Setup
$rgname = Get-ComputeTestResourceName;

try
{
# Common
$loc = "eastus";

$loc = Get-ComputeVMLocation;
New-AzResourceGroup -Name $rgname -Location $loc -Force;

# Setup variables
$vmssname = "vmss" + $rgname;
$domainNameLabel = "dnl" + $rgname;
$username = "admin01"
$securePassword = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword);

# Create VMSS with minimal inputs to allow defaulting
$vmss = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssname -ImageName 'Win2016Datacenter' -Credential $credential -InstanceCount 1 -DomainNameLabel $domainNameLabel
#$vmss = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssname -Credential $credential -OrchestrationMode $omode -DomainNameLabel $domainNameLabel;

$runcmds = Add-AzVmssRunCommand -ResourceGroupName $rgname -VMScaleSetName $vmssname -Location eastus -Name myruncommand -Script "Write-Host Hello World" -TimeOutInSeconds 3600 -RunAsUser "admin01"
Assert-AreEqual $runcmds.RunAsUser "admin01";
Assert-AreEqual $runcmds.TimeOutInSeconds "3600";
Remove-AzVmssRunCommand -ResourceGroupName $rgname -VMScaleSetName $vmssname -Name myruncommand

}
finally
{
# Cleanup
Clean-ResourceGroup $rgname;
}
}

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/Compute/Compute/Az.Compute.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ CmdletsToExport = 'Remove-AzAvailabilitySet', 'Get-AzAvailabilitySet',
'New-AzRestorePointCollection', 'Get-AzRestorePointCollection',
'Update-AzRestorePointCollection',
'Remove-AzRestorePointCollection', 'New-AzRestorePoint',
'Get-AzRestorePoint', 'Remove-AzRestorePoint',
'Get-AzRestorePoint', 'Remove-AzRestorePoint',
'New-AzVmGalleryApplication', 'New-AzVmssGalleryApplication',
'Add-AzVmGalleryApplication', 'Add-AzVmssGalleryApplication',
'Remove-AzVmGalleryApplication', 'Remove-AzVmssGalleryApplication'
'Remove-AzVmGalleryApplication', 'Remove-AzVmssGalleryApplication',
'Add-AzVmssRunCommand', 'Remove-AzVmssRunCommand'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
3 changes: 3 additions & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
-->
## Upcoming Release
* Update-AzVM will update ApplicationProfile.
* Adding new cmdlets:
- Add-AzVmssRunCommand
- Remove-AzVmssRunCommand

## Version 4.18.0
* Added cmdlets for adding VMGalleryApplication property to VM/VMSS
Expand Down
8 changes: 8 additions & 0 deletions src/Compute/Compute/Generated/ComputeAutomationBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ public IVirtualMachineScaleSetVMsOperations VirtualMachineScaleSetVMsClient
}
}

public IVirtualMachineScaleSetVMRunCommandsOperations VirtualMachineScaleSetVMRunCommandsClient
{
get
{
return ComputeClient.ComputeManagementClient.VirtualMachineScaleSetVMRunCommands;
}
}

public IVirtualMachinesOperations VirtualMachinesClient
{
get
Expand Down
251 changes: 251 additions & 0 deletions src/Compute/Compute/Manual/AddAzVmssRunCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
//
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Warning: This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Automation.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Security;

namespace Microsoft.Azure.Commands.Compute.Automation
{
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VmssRunCommand", DefaultParameterSetName = "DefaultParameter", SupportsShouldProcess = true)]
[OutputType(typeof(List<PSVirtualMachineRunCommand>))]
public partial class AddAzVmssRunCommand : ComputeAutomationBaseCmdlet
{
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);
}
});
}

[Parameter(
ParameterSetName = "DefaultParameter",
Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
[ResourceGroupCompleter]
public string ResourceGroupName { get; set; }

[Parameter(
ParameterSetName = "DefaultParameter",
Position = 1,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
[ResourceNameCompleter("Microsoft.Compute/virtualMachineScaleSets", "ResourceGroupName")]
public string VMScaleSetName { get; set; }

[Parameter(
ParameterSetName = "DefaultParameter",
Position = 2,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
public string Location { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string CommandId { get; set; }

[Parameter(
Mandatory = true)]
[AllowNull]
public string Name { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string ScriptPath { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string ScriptUri { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string Script { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string TimeOutInSeconds { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string ErrorBlobUri { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string OutputBlobUri { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public string RunAsUser { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public SecureString RunAsPassword { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public Hashtable Parameter { get; set; }

[Parameter(
Mandatory = false)]
[AllowNull]
public Hashtable ProtectedParameter { get; set; }

[Parameter(
Mandatory = false)]
public Nullable<Boolean> AsyncExecution { get; set; }


[Parameter(
ParameterSetName = "ObjectParameter",
Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true)]
[ValidateNotNullOrEmpty]
public PSVirtualMachineScaleSetVM VirtualMachineScaleSetVM { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }
}
}
23 changes: 23 additions & 0 deletions src/Compute/Compute/Manual/PSVirtualMachineRunCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

using Microsoft.Rest.Serialization;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Microsoft.Azure.Management.Compute.Models
{

public class PSVirtualMachineRunCommand : Resource
{
public VirtualMachineRunCommandScriptSource Source { get; set; }
public IList<RunCommandInputParameter> Parameters { get; set; }
public IList<RunCommandInputParameter> ProtectedParameters { get; set; }
public bool? AsyncExecution { get; set; }
public string RunAsUser { get; set; }
public string RunAsPassword { get; set; }
public int? TimeoutInSeconds { get; set; }
public string OutputBlobUri { get; set; }
public string ErrorBlobUri { get; set; }
public string ProvisioningState { get; }
public VirtualMachineRunCommandInstanceView InstanceView { get; set; }
}
}
Loading

0 comments on commit a26ef6a

Please sign in to comment.