Skip to content

Commit f651bcd

Browse files
grizzlytheodoregithub-actions
andauthored
IF-Match/IF-NOT-MATCH header support for VMSS creation/update (#24657)
* Autogen code * fix change log * fix etag adding to PS objects * unchange PSAzureOperationResponse * undo config cmdlts * new-azvmss, update-azvmss * update azvm * New-AzVm * new-azvm * add ValueFromPipelineByPropertyName * new-azvmss * md files * fix unnecessary syntax changes * example issues suppression for New-AzResourceGroup cmdlet use * undo NewAzureVMCommand format change * isolate changes to NewAzureVmCommand.cs file without format change * re-recorded vmss tests for regression test * improve helpmessages * fix changelog --------- Co-authored-by: github-actions <[email protected]>
1 parent c390e56 commit f651bcd

26 files changed

+3688
-2692
lines changed

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetDefaultToFlexibleOrchestrationMode.json

Lines changed: 163 additions & 281 deletions
Large diffs are not rendered by default.

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetEdgeZone.json

Lines changed: 972 additions & 546 deletions
Large diffs are not rendered by default.

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetSecurityTypeAndFlexDefaults.json

Lines changed: 912 additions & 737 deletions
Large diffs are not rendered by default.

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetSecurityTypeNoVMProfile.json

Lines changed: 181 additions & 353 deletions
Large diffs are not rendered by default.

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetSecurityTypeStandard.json

Lines changed: 1061 additions & 659 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
2121
-->
2222
## Upcoming Release
23+
* Added `Etag` property to PSVirtualMachine and PSVirtualMachineScaleSet objects.
24+
* Added parameters `-IfMatch` and `-IfNoneMatch` to `Update-AzVM`, `Update-AzVmss`, `New-AzVm`, `New-AzVmss`, `New-AzVmConfig`, and `New-AzVmssConfig` cmdlets.
2325

2426
## Version 7.3.0
2527
* Added cmdlet `Invoke-AzSpotPlacementRecommender`.

src/Compute/Compute/Custom/VirtualMachineExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Microsoft.Azure.Management.Compute.Models
99
static class VirtualMachineExtensions
1010
{
1111
private static IDictionary<VirtualMachine, Dictionary<string, List<string>>> mapping = new ConcurrentDictionary<VirtualMachine, Dictionary<string, List<string>>>();
12+
private static string ifMatchProperty = null;
13+
private static string ifNoneMatchProperty = null;
1214

1315
public static void SetAuxAuthHeader(this VirtualMachine vm, Dictionary<string, List<string>> auxAuthHeader)
1416
{
@@ -27,5 +29,21 @@ public static void RemoveAuxAuthHeader(this VirtualMachine vm)
2729
mapping.Remove(vm);
2830
}
2931

32+
public static void SetIfMatchIfNoneMatch(this VirtualMachine vm, string ifMatch, string ifNoneMatch)
33+
{
34+
ifMatchProperty = ifMatch;
35+
ifNoneMatchProperty = ifNoneMatch;
36+
}
37+
public static Tuple<string,string> GetIfMatchIfNoneMatch(this VirtualMachine vm)
38+
{
39+
return Tuple.Create(ifMatchProperty, ifNoneMatchProperty);
40+
}
41+
42+
public static void RemoveIfMatchIfNoneMatch(this VirtualMachine vm)
43+
{
44+
ifMatchProperty = null;
45+
ifNoneMatchProperty = null;
46+
}
47+
3048
}
3149
}

src/Compute/Compute/Custom/VirtualMachineScaleSetExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Microsoft.Azure.Management.Compute.Models
99
static class VirtualMachineScaleSetExtensions
1010
{
1111
private static IDictionary<VirtualMachineScaleSet, Dictionary<string, List<string>>> mapping = new ConcurrentDictionary<VirtualMachineScaleSet, Dictionary<string, List<string>>>();
12+
private static string ifMatchProperty = null;
13+
private static string ifNoneMatchProperty = null;
1214

1315
public static void SetAuxAuthHeader(this VirtualMachineScaleSet vmss, Dictionary<string, List<string>> auxAuthHeader)
1416
{
@@ -27,5 +29,21 @@ public static void RemoveAuxAuthHeader(this VirtualMachineScaleSet vmss)
2729
mapping.Remove(vmss);
2830
}
2931

32+
public static void SetIfMatchIfNoneMatch(this VirtualMachineScaleSet vmss, string ifMatch, string ifNoneMatch)
33+
{
34+
ifMatchProperty = ifMatch;
35+
ifNoneMatchProperty = ifNoneMatch;
36+
}
37+
public static Tuple<string, string> GetIfMatchIfNoneMatch(this VirtualMachineScaleSet vmss)
38+
{
39+
return Tuple.Create(ifMatchProperty, ifNoneMatchProperty);
40+
}
41+
42+
public static void RemoveIfMatchIfNoneMatch(this VirtualMachineScaleSet vmss)
43+
{
44+
ifMatchProperty = null;
45+
ifNoneMatchProperty = null;
46+
}
47+
3048
}
3149
}

src/Compute/Compute/Custom/VirtualMachineScaleSetsOperationsExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public static partial class VirtualMachineScaleSetsOperationsExtensions
1313
public static async Task<VirtualMachineScaleSet> CreateOrUpdateWithCustomHeaderAsync(this IVirtualMachineScaleSetsOperations operations, string resourceGroupName, string vmssName, VirtualMachineScaleSet virtualMachineScaleSet, CancellationToken cancellationToken = default(CancellationToken))
1414
{
1515
var auxAuthHeader = virtualMachineScaleSet.GetAuxAuthHeader();
16+
var ifMatchResult = virtualMachineScaleSet.GetIfMatchIfNoneMatch();
17+
var ifMatch = ifMatchResult.Item1;
18+
var ifNoneMatch = ifMatchResult.Item2;
1619
if (auxAuthHeader == null)
1720
{
18-
return operations.CreateOrUpdate(resourceGroupName, vmssName, virtualMachineScaleSet);
21+
return operations.CreateOrUpdate(resourceGroupName, vmssName, virtualMachineScaleSet, ifMatch, ifNoneMatch);
1922
}
2023
virtualMachineScaleSet.RemoveAuxAuthHeader();
21-
using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmssName, virtualMachineScaleSet, null,null,auxAuthHeader, cancellationToken).ConfigureAwait(false))
24+
virtualMachineScaleSet.RemoveIfMatchIfNoneMatch();
25+
using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmssName, virtualMachineScaleSet, ifMatch ,ifNoneMatch,auxAuthHeader, cancellationToken).ConfigureAwait(false))
2226
{
2327
return _result.Body;
2428
}

src/Compute/Compute/Custom/VirtualMachinesOperationsExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public static partial class VirtualMachinesOperationsExtensions
1313
public static async Task<VirtualMachine> CreateOrUpdateWithCustomHeaderAsync(this IVirtualMachinesOperations operations, string resourceGroupName, string vmName, VirtualMachine virtualMachine, CancellationToken cancellationToken = default(CancellationToken))
1414
{
1515
var auxAuthHeader = virtualMachine.GetAuxAuthHeader();
16+
var ifMatchResult = virtualMachine.GetIfMatchIfNoneMatch();
17+
var ifMatch = ifMatchResult.Item1;
18+
var ifNoneMatch = ifMatchResult.Item2;
1619
if(auxAuthHeader == null)
1720
{
18-
return operations.CreateOrUpdate(resourceGroupName, vmName, virtualMachine);
21+
return operations.CreateOrUpdate(resourceGroupName, vmName, virtualMachine, ifMatch, ifNoneMatch);
1922
}
2023
virtualMachine.RemoveAuxAuthHeader();
21-
using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, virtualMachine, null,null,auxAuthHeader, cancellationToken).ConfigureAwait(false))
24+
virtualMachine.RemoveIfMatchIfNoneMatch();
25+
using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, virtualMachine, ifMatch, ifNoneMatch, auxAuthHeader, cancellationToken).ConfigureAwait(false))
2226
{
2327
return _result.Body;
2428
}

src/Compute/Compute/Generated/Models/PSVirtualMachineScaleSet.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ public string ResourceGroupName
7373
// Gets specifies the time at which the Virtual Machine Scale Set resource was created.&lt;br&gt;&lt;br&gt;Minimum
7474
// api-version: 2022-03-01.
7575
public DateTime? TimeCreated { get; private set; }
76+
public string Etag { get; private set; }
7677
}
7778
}

src/Compute/Compute/Generated/VirtualMachineScaleSet/Config/NewAzureRmVmssConfigCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,11 @@ private void Run()
937937
Identity = vIdentity,
938938
OrchestrationMode = this.IsParameterBound(c => c.OrchestrationMode) ? this.OrchestrationMode : null,
939939
SpotRestorePolicy = this.IsParameterBound(c => c.EnableSpotRestore) ? new SpotRestorePolicy(true, this.SpotRestoreTimeout) : null,
940-
PriorityMixPolicy = vPriorityMixPolicy
940+
PriorityMixPolicy = vPriorityMixPolicy
941941
};
942942

943943
WriteObject(vVirtualMachineScaleSet);
944944
}
945945
}
946946
}
947+

src/Compute/Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetCreateOrUpdateMethod.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ public override void ExecuteCmdlet()
176176
var res = VirtualMachineScaleSetsClient.CreateOrUpdateWithHttpMessagesAsync(
177177
resourceGroupName,
178178
vmScaleSetName,
179-
parameters,null,null,
179+
parameters, this.IfMatch, this.IfNoneMatch,
180180
auxAuthHeader).GetAwaiter().GetResult();
181181

182182
result = res.Body;
183183
}
184184
else
185185
{
186-
result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
186+
result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters, this.IfMatch, this.IfNoneMatch);
187187
}
188188

189189
var psObject = new PSVirtualMachineScaleSet();
@@ -401,5 +401,16 @@ private int convertAPIVersionToInt(string networkAPIVersion)
401401
ParameterSetName = SimpleParameterSet,
402402
HelpMessage = "Whether OS upgrades should automatically be applied to scale set instances in a rolling fashion when a newer version of the image becomes available.")]
403403
public SwitchParameter EnableAutomaticOSUpgrade{ get; set; }
404+
405+
[Parameter(
406+
Mandatory = false,
407+
HelpMessage = "used to make a request conditional for the PUT and other non-safe methods. The server will only return the requested resources if the resource matches one of the listed ETag values. Omit this value to always overwrite the current resource. Specify the last-seen ETag value to prevent accidentally overwriting concurrent changes.")]
408+
public string IfMatch { get; set; }
409+
410+
[Parameter(
411+
Mandatory = false,
412+
HelpMessage = "Used to make a request conditional for the GET and HEAD methods. The server will only return the requested resources if none of the listed ETag values match the current entity. Used to make a request conditional for the GET and HEAD methods. The server will only return the requested resources if none of the listed ETag values match the current entity. Set to '*' to allow a new record set to be created, but to prevent updating an existing record set. Other values will result in error from server as they are not supported.")]
413+
public string IfNoneMatch { get; set; }
404414
}
405415
}
416+

src/Compute/Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetUpdateMethod.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public override void ExecuteCmdlet()
8181
ComputeAutomationAutoMapperProfile.Mapper.Map<PSVirtualMachineScaleSet, VirtualMachineScaleSet>(this.VirtualMachineScaleSet, parameters);
8282

8383
var result = (this.VirtualMachineScaleSetUpdate == null)
84-
? VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters)
85-
: VirtualMachineScaleSetsClient.Update(resourceGroupName, vmScaleSetName, parametersupdate);
84+
? VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters, this.IfMatch, this.IfNoneMatch)
85+
: VirtualMachineScaleSetsClient.Update(resourceGroupName, vmScaleSetName, parametersupdate, this.IfMatch, this.IfNoneMatch);
8686
var psObject = new PSVirtualMachineScaleSet();
8787
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
8888
WriteObject(psObject);
@@ -409,6 +409,18 @@ public override void ExecuteCmdlet()
409409
Mandatory = false)]
410410
public bool? EnableSecureBoot { get; set; } = null;
411411

412+
[Parameter(
413+
Mandatory = false,
414+
ValueFromPipelineByPropertyName = true,
415+
HelpMessage = "used to make a request conditional for the PUT and other non-safe methods. The server will only return the requested resources if the resource matches one of the listed ETag values. Omit this value to always overwrite the current resource. Specify the last-seen ETag value to prevent accidentally overwriting concurrent changes.")]
416+
public string IfMatch { get; set; }
417+
418+
[Parameter(
419+
Mandatory = false,
420+
ValueFromPipelineByPropertyName = true,
421+
HelpMessage = "Used to make a request conditional for the GET and HEAD methods. The server will only return the requested resources if none of the listed ETag values match the current entity. Used to make a request conditional for the GET and HEAD methods. The server will only return the requested resources if none of the listed ETag values match the current entity. Set to '*' to allow a new record set to be created, but to prevent updating an existing record set. Other values will result in error from server as they are not supported.")]
422+
public string IfNoneMatch { get; set; }
423+
412424
private void BuildPatchObject()
413425
{
414426
if (this.IsParameterBound(c => c.AutomaticOSUpgrade))

src/Compute/Compute/Manual/VirtualMachineScaleSetCreateOrUpdateMethod.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ private async Task<ResourceConfig<VirtualMachineScaleSet>> SimpleParameterSetNor
465465
securityType: _cmdlet.SecurityType,
466466
enableVtpm: _cmdlet.EnableVtpm,
467467
enableSecureBoot: _cmdlet.EnableSecureBoot,
468-
enableAutomaticOSUpgradePolicy: _cmdlet.EnableAutomaticOSUpgrade == true ? true : (bool?)null
468+
enableAutomaticOSUpgradePolicy: _cmdlet.EnableAutomaticOSUpgrade == true ? true : (bool?)null,
469+
ifMatch: _cmdlet.IfMatch,
470+
ifNoneMatch: _cmdlet.IfNoneMatch
469471
);
470472
}
471473

@@ -561,6 +563,25 @@ private async Task<ResourceConfig<VirtualMachineScaleSet>> SimpleParameterSetOrc
561563

562564
var hostGroup = resourceGroup.CreateDedicatedHostGroupSubResourceFunc(_cmdlet.HostGroupId);
563565

566+
Dictionary<string, List<string>> auxAuthHeader = null;
567+
if (!string.IsNullOrEmpty(_cmdlet.ImageReferenceId))
568+
{
569+
var resourceId = ResourceId.TryParse(_cmdlet.ImageReferenceId);
570+
571+
if (string.Equals(ComputeStrategy.Namespace, resourceId?.ResourceType?.Namespace, StringComparison.OrdinalIgnoreCase)
572+
&& string.Equals("galleries", resourceId?.ResourceType?.Provider, StringComparison.OrdinalIgnoreCase)
573+
&& !string.Equals(_cmdlet.ComputeClient?.ComputeManagementClient?.SubscriptionId, resourceId?.SubscriptionId, StringComparison.OrdinalIgnoreCase))
574+
{
575+
List<string> resourceIds = new List<string>();
576+
resourceIds.Add(_cmdlet.ImageReferenceId);
577+
var auxHeaderDictionary = _cmdlet.GetAuxilaryAuthHeaderFromResourceIds(resourceIds);
578+
if (auxHeaderDictionary != null && auxHeaderDictionary.Count > 0)
579+
{
580+
auxAuthHeader = new Dictionary<string, List<string>>(auxHeaderDictionary);
581+
}
582+
}
583+
}
584+
564585
return resourceGroup.CreateVirtualMachineScaleSetConfigOrchestrationModeFlexible(
565586
name: _cmdlet.VMScaleSetName,
566587
subnet: subnet,
@@ -591,7 +612,10 @@ private async Task<ResourceConfig<VirtualMachineScaleSet>> SimpleParameterSetOrc
591612
securityType: _cmdlet.SecurityType,
592613
enableVtpm: _cmdlet.EnableVtpm,
593614
enableSecureBoot: _cmdlet.EnableSecureBoot,
594-
enableAutomaticOSUpgradePolicy: _cmdlet.EnableAutomaticOSUpgrade == true ? true : (bool?)null
615+
enableAutomaticOSUpgradePolicy: _cmdlet.EnableAutomaticOSUpgrade == true ? true : (bool?)null,
616+
auxAuthHeader: auxAuthHeader,
617+
ifMatch: _cmdlet.IfMatch,
618+
ifNoneMatch: _cmdlet.IfNoneMatch
595619
);
596620
}
597621
}

src/Compute/Compute/Models/PSVirtualMachine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,6 @@ public string ResourceGroupName
154154
// Gets specifies the time at which the Virtual Machine resource was created.&lt;br&gt;&lt;br&gt;Minimum
155155
// api-version: 2022-03-01.
156156
public DateTime? TimeCreated { get; private set; }
157+
public string Etag { get; private set; }
157158
}
158159
}

0 commit comments

Comments
 (0)