diff --git a/src/ServiceFabric/ServiceFabric/Commands/ServiceFabricCmdletBase.cs b/src/ServiceFabric/ServiceFabric/Commands/ServiceFabricCmdletBase.cs index 28af5d8596fa..264863d32304 100644 --- a/src/ServiceFabric/ServiceFabric/Commands/ServiceFabricCmdletBase.cs +++ b/src/ServiceFabric/ServiceFabric/Commands/ServiceFabricCmdletBase.cs @@ -181,33 +181,51 @@ private void InitializeAzureRmClients() #region VMSS - protected VirtualMachineScaleSet GetVmss(string name) + protected VirtualMachineScaleSet GetVmss(string name, string clusterId) { if (string.IsNullOrWhiteSpace(name)) { throw new PSArgumentNullException("Invalid vmss name"); } - var result = ComputeClient.VirtualMachineScaleSets.List(ResourceGroupName); - if (result == null || !result.Any()) + var vmssPages = ComputeClient.VirtualMachineScaleSets.List(ResourceGroupName); + if (vmssPages == null || !vmssPages.Any()) { throw new PSArgumentException(string.Format( ServiceFabricProperties.Resources.NoVMSSFoundInRG, this.ResourceGroupName)); } - var vmss = result.FirstOrDefault( - vm => vm.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - - if (vmss == null) + do { - throw new PSInvalidOperationException( + if (!vmssPages.Any()) + { + break; + } + + foreach (var vmss in vmssPages) + { + VirtualMachineScaleSetExtension sfExtension; + if (TryGetFabricVmExt(vmss.VirtualMachineProfile.ExtensionProfile?.Extensions, out sfExtension)) + { + if (string.Equals(GetClusterIdFromExtension(sfExtension), clusterId, StringComparison.OrdinalIgnoreCase)) + { + WriteVerboseWithTimestamp(string.Format("GetVmss: Found vmss {0} that corresponds to cluster id {1}", vmss.Id, clusterId)); + string nodeTypeRef = GetNodeTypeRefFromExtension(sfExtension); + if (string.Equals(nodeTypeRef, name, StringComparison.OrdinalIgnoreCase)) + { + return vmss; + } + } + } + } + } while (!string.IsNullOrEmpty(vmssPages.NextPageLink) && + (vmssPages = this.ComputeClient.VirtualMachineScaleSets.ListNext(vmssPages.NextPageLink)) != null); + + throw new PSInvalidOperationException( string.Format( - ServiceFabricProperties.Resources.CannotFindVMSS, + ServiceFabricProperties.Resources.CannotFindVMSS, name)); - } - - return vmss; } public bool TryGetFabricVmExt(IList extensions, out VirtualMachineScaleSetExtension sfExtension) @@ -230,12 +248,22 @@ public bool TryGetFabricVmExt(IList extensions, public string GetClusterIdFromExtension(VirtualMachineScaleSetExtension sfExtension) { - JObject extSettings = (JObject)sfExtension.Settings; - string clusterEndpoint = (string)extSettings.SelectToken("clusterEndpoint"); - string id = clusterEndpoint.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); + string clusterEndpoint = GetSettingFromExtension(sfExtension, "clusterEndpoint"); + string id = clusterEndpoint?.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); return id; } + public string GetNodeTypeRefFromExtension(VirtualMachineScaleSetExtension sfExtension) + { + return GetSettingFromExtension(sfExtension, "nodeTypeRef"); + } + + internal string GetSettingFromExtension(VirtualMachineScaleSetExtension sfExtension, string settingName) + { + JObject extSettings = sfExtension.Settings as JObject; + return (string)extSettings.SelectToken(settingName); + } + #endregion #region Key Vault diff --git a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricDurability.cs b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricDurability.cs index 38451487e0d7..bf9ab45d153d 100644 --- a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricDurability.cs +++ b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricDurability.cs @@ -68,14 +68,14 @@ public class UpdateAzureRmServiceFabricDurability : ServiceFabricClusterCmdlet public override void ExecuteCmdlet() { - var vmss = GetVmss(this.NodeType); + var cluster = GetCurrentCluster(); + var vmss = GetVmss(this.NodeType, cluster.ClusterId); VirtualMachineScaleSetExtension ext; if (!TryGetFabricVmExt(vmss.VirtualMachineProfile.ExtensionProfile.Extensions, out ext)) { throw new InvalidOperationException(string.Format(ServiceFabricProperties.Resources.SFExtensionNotFoundInVMSS, vmss.Name, vmss.Id)); } - var cluster = GetCurrentCluster(); var nodeType = GetNodeType(cluster, this.NodeType); var oldDurabilityLevel = GetDurabilityLevel(nodeType.DurabilityLevel); var newDurabilityLevel = this.DurabilityLevel; diff --git a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricNodeBase.cs b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricNodeBase.cs index f9abedc9899f..825e5f399669 100644 --- a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricNodeBase.cs +++ b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricNodeBase.cs @@ -56,7 +56,7 @@ protected virtual int Number public override void ExecuteCmdlet() { var cluster = GetCurrentCluster(); - var vmss = GetVmss(this.NodeType); + var vmss = GetVmss(this.NodeType, cluster.ClusterId); var nodeType = GetNodeType(cluster, this.NodeType); var durabilityLevel = (DurabilityLevel)Enum.Parse(typeof(DurabilityLevel), nodeType.DurabilityLevel); diff --git a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricReliability.cs b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricReliability.cs index 486fd2ea1606..bb0fa7fb5089 100644 --- a/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricReliability.cs +++ b/src/ServiceFabric/ServiceFabric/Commands/UpdateAzureRmServiceFabricReliability.cs @@ -126,7 +126,7 @@ protected VirtualMachineScaleSet GetPrimaryVmss() var clusterRes = GetCurrentCluster(); var nodeTypes = clusterRes.NodeTypes; var primaryNodeType = nodeTypes.Single(nt => nt.IsPrimary); - var vmss = GetVmss(primaryNodeType.Name); + var vmss = GetVmss(primaryNodeType.Name, clusterRes.ClusterId); return vmss; } }