Skip to content

Commit caa42b0

Browse files
QingChenmsftshahabhijeet
authored andcommitted
Fix bugs and promote warning for remove node type (#4211)
1 parent 1c331ab commit caa42b0

File tree

7 files changed

+138
-20
lines changed

7 files changed

+138
-20
lines changed

src/ResourceManager/ServiceFabric/AzureRM.ServiceFabric.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ FunctionsToExport = @()
7575
CmdletsToExport = 'Add-AzureRmServiceFabricApplicationCertificate',
7676
'Add-AzureRmServiceFabricClientCertificate',
7777
'Add-AzureRmServiceFabricClusterCertificate',
78-
'Add-AzureRmServiceFabricNode', 'Add-AzureRmServiceFabricNodeType',
78+
'Add-AzureRmServiceFabricNode',
79+
'Add-AzureRmServiceFabricNodeType',
7980
'Get-AzureRmServiceFabricCluster',
8081
'New-AzureRmServiceFabricCluster',
8182
'Remove-AzureRmServiceFabricClientCertificate',

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/Commands.ServiceFabric.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484
<Compile Include="Commands\AddAzureRmServiceFabricClientCertificate.cs" />
8585
<Compile Include="Commands\AddAzureRmServiceFabricClusterCertificate.cs" />
8686
<Compile Include="Commands\AddAzureRmServiceFabricNodeType.cs" />
87+
<Compile Include="Commands\RemoveAzureRmServiceFabricNodeType.cs" />
8788
<Compile Include="Commands\ServiceFabricClientCertificateBase.cs" />
8889
<Compile Include="Common\CmdletNoun.cs" />
8990
<Compile Include="Common\Constants.cs" />
9091
<Compile Include="Commands\GetAzureRmServiceFabricClusterResouce.cs" />
9192
<Compile Include="Commands\NewAzureRmServiceFabricCluster.cs" />
92-
<Compile Include="Commands\RemoveAzureRmServiceFabricNodeType.cs" />
9393
<Compile Include="Commands\RemoveAzureRmServiceFabricClientCertificate.cs" />
9494
<Compile Include="Commands\RemoveAzureRmServiceFabricClusterCertificate.cs" />
9595
<Compile Include="Commands\RemoveAzureRmServiceFabricNode.cs" />

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/Commands/AddAzureRmServiceFabricNodeType.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using Microsoft.WindowsAzure.Commands.Common;
3232
using Newtonsoft.Json.Linq;
3333
using ServiceFabricProperties = Microsoft.Azure.Commands.ServiceFabric.Properties;
34+
using System.Text;
3435

3536
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
3637
{
@@ -41,6 +42,8 @@ public class AddAzureRmServiceFabricNodeType : ServiceFabricNodeTypeCmdletBase
4142
private const string BackendAddressIdFormat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/loadBalancers/{2}/backendAddressPools/{3}";
4243
private const string FrontendIDFormat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/loadBalancers/{2}/frontendIPConfigurations/{3}";
4344
private const string ProbeIDFormat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/loadBalancers/{2}/probes/{3}";
45+
private readonly HashSet<string> skusSupportGoldDurability =
46+
new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Standard_D15_v2", "Standard_G5" };
4447

4548
private string sku;
4649
private string diagnosticsStorageName;
@@ -94,16 +97,34 @@ public string VmSku
9497

9598
private string tier;
9699
[Parameter(Mandatory = false, ValueFromPipeline = true,
97-
HelpMessage = "Tier")]
100+
HelpMessage = "Vm Sku Tier")]
98101
[ValidateNotNullOrEmpty()]
99102
public string Tier
100103
{
101104
get { return string.IsNullOrWhiteSpace(this.tier) ? Constants.DefaultTier : this.tier; }
102105
set { this.tier = value; }
103106
}
104107

108+
private DurabilityLevel durabilityLevel = DurabilityLevel.Bronze;
109+
[Parameter(Mandatory = false, ValueFromPipeline = true,
110+
HelpMessage = "Specify the durability level of the NodeType.")]
111+
[ValidateNotNullOrEmpty()]
112+
public DurabilityLevel DurabilityLevel
113+
{
114+
get { return this.durabilityLevel; }
115+
set
116+
{
117+
this.durabilityLevel = value;
118+
}
119+
}
120+
105121
public override void ExecuteCmdlet()
106122
{
123+
if (this.DurabilityLevel == DurabilityLevel.Gold && !skusSupportGoldDurability.Contains(this.VmSku))
124+
{
125+
throw new PSArgumentException("Only Standard_D15_v2 and Standard_G5 supports Gold durability,please specify -VmSku to right value");
126+
}
127+
107128
if (CheckNodeTypeExistence())
108129
{
109130
throw new PSArgumentException(string.Format("{0} exists", this.NodeType));
@@ -160,7 +181,7 @@ private PSCluster AddNodeTypeToSfrp()
160181
EndPort = Constants.DefaultApplicationEndPort
161182
},
162183
ClientConnectionEndpointPort = Constants.DefaultClientConnectionEndpoint,
163-
DurabilityLevel = Constants.DefaultDurabilityLevel,
184+
DurabilityLevel = this.DurabilityLevel.ToString(),
164185
EphemeralPorts = new Management.ServiceFabric.Models.EndpointRangeDescription()
165186
{
166187
StartPort = Constants.DefaultEphemeralStart,
@@ -327,6 +348,7 @@ private VirtualMachineScaleSetExtension GetFabriExtension(VirtualMachineScaleSet
327348
}
328349

329350
settings["nodeTypeRef"] = this.NodeType;
351+
settings["durabilityLevel"] = this.DurabilityLevel.ToString();
330352

331353
if (settings["nicPrefixOverride"] != null)
332354
{
@@ -423,13 +445,27 @@ private List<StorageAccount> CreateStorageAccount()
423445

424446
private string GetStorageRandomName()
425447
{
426-
var name = string.Empty;
448+
var name = this.NodeType.ToLower();
427449

428450
if (!dontRandom)
429451
{
430-
name = string.Concat(
431-
this.NodeType,
432-
System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName()));
452+
do
453+
{
454+
name = string.Concat(
455+
name,
456+
System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName()));
457+
458+
StringBuilder sb = new StringBuilder();
459+
foreach (var n in name)
460+
{
461+
if ((n >= 'a' && n <= 'z') || (n >= '0' && n <= '9'))
462+
{
463+
sb.Append(n);
464+
}
465+
}
466+
467+
name = sb.ToString();
468+
} while (name.Length < 3);
433469
}
434470
else
435471
{

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/Commands/RemoveAzureRmServiceFabricNodeType.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
using Microsoft.Azure.Commands.ServiceFabric.Models;
2020
using Microsoft.Azure.Management.Compute;
2121
using Microsoft.Azure.Management.ServiceFabric;
22+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2223
using ServiceFabricProperties = Microsoft.Azure.Commands.ServiceFabric.Properties;
2324

25+
2426
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
2527
{
2628
[Cmdlet(VerbsCommon.Remove, CmdletNoun.AzureRmServiceFabricNodeType, SupportsShouldProcess = true), OutputType(typeof(PSCluster))]
@@ -42,6 +44,10 @@ public class RemoveAzureRmServiceFabricNodeType : ServiceFabricNodeTypeCmdletBas
4244

4345
public override void ExecuteCmdlet()
4446
{
47+
WriteWarning("After the NodeType is removed, you may see the nodes of the NodeType are in error state," +
48+
"you need to run 'Remove-ServiceFabricNodeState' on those nodes to fix them, read this document for details on how to " +
49+
" https://docs.microsoft.com/powershell/module/servicefabric/remove-servicefabricnodestate?view=azureservicefabricps");
50+
4551
if (!CheckNodeTypeExistence())
4652
{
4753
throw new PSArgumentException(this.NodeType);
@@ -87,9 +93,8 @@ public override void ExecuteCmdlet()
8793

8894
if (ShouldProcess(target: this.NodeType, action: string.Format("Remove a nodetype {0} ", this.NodeType)))
8995
{
90-
this.ComputeClient.VirtualMachineScaleSets.Delete(this.ResourceGroupName, this.NodeType);
91-
9296
cluster = RemoveNodeTypeFromSfrp();
97+
this.ComputeClient.VirtualMachineScaleSets.Delete(this.ResourceGroupName, this.NodeType);
9398

9499
WriteObject((PSCluster)cluster, true);
95100
}

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/Commands/ServiceFabricClusterCertificateCmdlet.cs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using System.Security.Cryptography.X509Certificates;
2323
using System.Text;
2424
using System.Threading.Tasks;
25-
using Microsoft.Azure.Commands.Common.Authentication.Models;
2625
using Microsoft.Azure.Commands.ServiceFabric.Models;
2726
using Microsoft.Azure.Commands.Common.Authentication;
2827
using Microsoft.Azure.KeyVault.Models;
@@ -155,7 +154,6 @@ public ServiceFabricClusterCertificateCmdlet()
155154
public override void ExecuteCmdlet()
156155
{
157156
this.Validate();
158-
159157
}
160158

161159
protected virtual List<string> GetPfxSrcFiles()
@@ -298,7 +296,7 @@ internal List<CertificateInformation> GetOrCreateCertificateInformation()
298296

299297
if (string.IsNullOrEmpty(this.KeyVaultName))
300298
{
301-
this.KeyVaultName = this.ResourceGroupName;
299+
this.KeyVaultName = CreateDefaultKeyVaultName(this.ResourceGroupName);
302300
}
303301

304302
if (ParameterSetName != ExistingKeyVault)
@@ -471,7 +469,7 @@ protected void GetKeyVaultReady(out Vault vault, out CertificateBundle certifica
471469
WriteVerboseWithTimestamp(string.Format("Key Vault is created: {0}", vault.Id));
472470
}
473471

474-
SetCertificateName();
472+
this.keyVaultCertificateName = CreateDefaultCertificateName(this.ResourceGroupName);
475473

476474
if (!string.IsNullOrEmpty(srcPfxPath))
477475
{
@@ -494,10 +492,68 @@ protected void GetKeyVaultReady(out Vault vault, out CertificateBundle certifica
494492
}
495493
}
496494

497-
protected void SetCertificateName()
495+
protected static string CreateDefaultCertificateName(string resourceGroupName)
498496
{
499-
this.keyVaultCertificateName = string.Format("{0}{1}", this.ResourceGroupName,
500-
DateTime.Now.ToString("yyyyMMddHHmmss"));
497+
StringBuilder sb = new StringBuilder();
498+
foreach (var c in resourceGroupName)
499+
{
500+
if (IsValidKeyVaultObjectChar(c))
501+
{
502+
sb.Append(c);
503+
}
504+
}
505+
506+
return string.Format("{0}{1}", sb.ToString(), DateTime.Now.ToString("yyyyMMddHHmmss"));
507+
}
508+
509+
protected static string CreateDefaultKeyVaultName(string resourceGroupName)
510+
{
511+
StringBuilder sb = new StringBuilder();
512+
var targetCopy = resourceGroupName;
513+
while (sb.Length < 3)
514+
{
515+
foreach (var c in targetCopy)
516+
{
517+
if (IsValidKeyVaultChar(c))
518+
{
519+
sb.Append(c);
520+
}
521+
}
522+
523+
// resource group name can't be used for key vault name
524+
// use random string instread
525+
if (sb.Length == 0)
526+
{
527+
targetCopy = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
528+
}
529+
}
530+
531+
if (sb.Length > 24)
532+
{
533+
return sb.ToString().Substring(0, 24);
534+
}
535+
536+
return sb.ToString();
537+
}
538+
539+
private static bool IsValidKeyVaultChar(char name)
540+
{
541+
if (name >= 'a' && name <= 'z') return true;
542+
if (name >= 'A' && name <= 'Z') return true;
543+
if (name >= '0' && name <= '9') return true;
544+
if (name == '-') return true;
545+
546+
return false;
547+
}
548+
549+
private static bool IsValidKeyVaultObjectChar(char name)
550+
{
551+
if (name >= 'a' && name <= 'z') return true;
552+
if (name >= 'A' && name <= 'Z') return true;
553+
if (name >= '0' && name <= '9') return true;
554+
if (name == '-') return true;
555+
556+
return false;
501557
}
502558

503559
private string GetThumbprintFromSecret(string secretUrl)
@@ -583,4 +639,4 @@ private void ExtractSecretNameFromSecretIdentifier(string secretIdentifier, out
583639
version = tokens[4];
584640
}
585641
}
586-
}
642+
}

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/Commands/ServiceFabricSettingsCmdletBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ protected Dictionary<string, Dictionary<string, string>> FabricSettingsToDiction
110110
{
111111
foreach (var setting in fabricSettings)
112112
{
113-
settings[setting.Name] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
113+
if (!settings.ContainsKey(setting.Name))
114+
{
115+
settings[setting.Name] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
116+
}
117+
114118
foreach (var ps in setting.Parameters)
115119
{
116120
if (settings[setting.Name].ContainsKey(ps.Name))

src/ResourceManager/ServiceFabric/Commands.ServiceFabric/help/Add-AzureRmServiceFabricNodeType.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Accept wildcard characters: False
9393
```
9494
9595
### -Tier
96-
Tier.
96+
Vm Sku Tier.
9797
9898
```yaml
9999
Type: String
@@ -107,6 +107,22 @@ Accept pipeline input: True (ByValue)
107107
Accept wildcard characters: False
108108
```
109109
110+
### -DurabilityLevel
111+
Specify the durability level of the NodeType.
112+
113+
```yaml
114+
Type: DurabilityLevel
115+
Parameter Sets: (All)
116+
Aliases: Level
117+
Accepted values: Bronze, Silver, Gold
118+
119+
Required: False
120+
Position: Named
121+
Default value: None
122+
Accept pipeline input: True (ByValue)
123+
Accept wildcard characters: False
124+
```
125+
110126
### -VmPassword
111127
The password of login to the Vm.
112128

0 commit comments

Comments
 (0)