Skip to content

Commit 52d736a

Browse files
committed
Add ShouldProcess to new, update, and set cmdlets
1 parent d049d04 commit 52d736a

File tree

9 files changed

+282
-246
lines changed

9 files changed

+282
-246
lines changed

src/ResourceManager/Compute/Commands.Compute/Extension/ADDomain/SetAzureVMADDomainExtensionCommand.cs

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -74,56 +74,59 @@ public override void ExecuteCmdlet()
7474
{
7575
base.ExecuteCmdlet();
7676

77-
ExecuteClientAction(() =>
77+
if (ShouldProcess(VirtualMachineADDomainExtensionContext.ExtensionDefaultName, VerbsCommon.Set))
7878
{
79-
Hashtable publicSettings = new Hashtable();
80-
publicSettings.Add(nameKey, this.DomainName);
81-
82-
if (this.OUPath != null)
83-
{
84-
publicSettings.Add(ouPathKey, this.OUPath);
85-
}
86-
87-
if (this.JoinOption != null)
88-
{
89-
publicSettings.Add(optionKey, this.JoinOption);
90-
}
91-
92-
publicSettings.Add(restartKey, Restart.IsPresent);
93-
94-
Hashtable privateSettings = new Hashtable();
95-
if (Credential != null)
96-
{
97-
publicSettings.Add(userKey, Credential.UserName);
98-
privateSettings.Add(passwordKey, SecureStringExtensions.ConvertToString(this.Credential.Password));
99-
}
100-
101-
if (string.IsNullOrEmpty(this.Location))
102-
{
103-
this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
104-
}
105-
106-
var parameters = new VirtualMachineExtension
79+
ExecuteClientAction(() =>
10780
{
108-
Location = this.Location,
109-
Publisher = VirtualMachineADDomainExtensionContext.ExtensionDefaultPublisher,
110-
VirtualMachineExtensionType = VirtualMachineADDomainExtensionContext.ExtensionDefaultName,
111-
TypeHandlerVersion = this.TypeHandlerVersion ?? VirtualMachineADDomainExtensionContext.ExtensionDefaultVersion,
112-
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
113-
ForceUpdateTag = this.ForceRerun,
114-
Settings = publicSettings,
115-
ProtectedSettings = privateSettings
116-
};
117-
118-
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
119-
this.ResourceGroupName,
120-
this.VMName,
121-
this.Name ?? VirtualMachineADDomainExtensionContext.ExtensionDefaultName,
122-
parameters).GetAwaiter().GetResult();
123-
124-
var result = Mapper.Map<PSAzureOperationResponse>(op);
125-
WriteObject(result);
126-
});
81+
Hashtable publicSettings = new Hashtable();
82+
publicSettings.Add(nameKey, this.DomainName);
83+
84+
if (this.OUPath != null)
85+
{
86+
publicSettings.Add(ouPathKey, this.OUPath);
87+
}
88+
89+
if (this.JoinOption != null)
90+
{
91+
publicSettings.Add(optionKey, this.JoinOption);
92+
}
93+
94+
publicSettings.Add(restartKey, Restart.IsPresent);
95+
96+
Hashtable privateSettings = new Hashtable();
97+
if (Credential != null)
98+
{
99+
publicSettings.Add(userKey, Credential.UserName);
100+
privateSettings.Add(passwordKey, SecureStringExtensions.ConvertToString(this.Credential.Password));
101+
}
102+
103+
if (string.IsNullOrEmpty(this.Location))
104+
{
105+
this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
106+
}
107+
108+
var parameters = new VirtualMachineExtension
109+
{
110+
Location = this.Location,
111+
Publisher = VirtualMachineADDomainExtensionContext.ExtensionDefaultPublisher,
112+
VirtualMachineExtensionType = VirtualMachineADDomainExtensionContext.ExtensionDefaultName,
113+
TypeHandlerVersion = this.TypeHandlerVersion ?? VirtualMachineADDomainExtensionContext.ExtensionDefaultVersion,
114+
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
115+
ForceUpdateTag = this.ForceRerun,
116+
Settings = publicSettings,
117+
ProtectedSettings = privateSettings
118+
};
119+
120+
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
121+
this.ResourceGroupName,
122+
this.VMName,
123+
this.Name ?? VirtualMachineADDomainExtensionContext.ExtensionDefaultName,
124+
parameters).GetAwaiter().GetResult();
125+
126+
var result = Mapper.Map<PSAzureOperationResponse>(op);
127+
WriteObject(result);
128+
});
129+
}
127130
}
128131
}
129132
}

src/ResourceManager/Compute/Commands.Compute/Extension/BGInfo/SetAzureVMBGInfoExtension.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,35 @@ protected override void ProcessRecord()
3030
{
3131
base.ProcessRecord();
3232

33-
ExecuteClientAction(() =>
33+
if (ShouldProcess(VirtualMachineBGInfoExtensionContext.ExtensionDefaultName, VerbsCommon.Set))
3434
{
35-
if (string.IsNullOrEmpty(this.Location))
35+
ExecuteClientAction(() =>
3636
{
37-
this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
38-
}
37+
if (string.IsNullOrEmpty(this.Location))
38+
{
39+
this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
40+
}
3941

40-
var parameters = new VirtualMachineExtension
41-
{
42-
Location = this.Location,
43-
Publisher = VirtualMachineBGInfoExtensionContext.ExtensionDefaultPublisher,
44-
VirtualMachineExtensionType = VirtualMachineBGInfoExtensionContext.ExtensionDefaultName,
45-
TypeHandlerVersion = this.TypeHandlerVersion ?? VirtualMachineBGInfoExtensionContext.ExtensionDefaultVersion,
46-
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
47-
ForceUpdateTag = this.ForceRerun
48-
};
42+
var parameters = new VirtualMachineExtension
43+
{
44+
Location = this.Location,
45+
Publisher = VirtualMachineBGInfoExtensionContext.ExtensionDefaultPublisher,
46+
VirtualMachineExtensionType = VirtualMachineBGInfoExtensionContext.ExtensionDefaultName,
47+
TypeHandlerVersion = this.TypeHandlerVersion ?? VirtualMachineBGInfoExtensionContext.ExtensionDefaultVersion,
48+
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
49+
ForceUpdateTag = this.ForceRerun
50+
};
4951

50-
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
51-
this.ResourceGroupName,
52-
this.VMName,
53-
this.Name ?? VirtualMachineBGInfoExtensionContext.ExtensionDefaultName,
54-
parameters).GetAwaiter().GetResult();
52+
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
53+
this.ResourceGroupName,
54+
this.VMName,
55+
this.Name ?? VirtualMachineBGInfoExtensionContext.ExtensionDefaultName,
56+
parameters).GetAwaiter().GetResult();
5557

56-
var result = Mapper.Map<PSAzureOperationResponse>(op);
57-
WriteObject(result);
58-
});
58+
var result = Mapper.Map<PSAzureOperationResponse>(op);
59+
WriteObject(result);
60+
});
61+
}
5962
}
6063
}
6164
}

src/ResourceManager/Compute/Commands.Compute/Extension/CustomScript/SetAzureVMCustomScriptExtensionCommand.cs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -120,69 +120,72 @@ public override void ExecuteCmdlet()
120120
{
121121
base.ExecuteCmdlet();
122122

123-
ExecuteClientAction(() =>
123+
if (ShouldProcess(VirtualMachineCustomScriptExtensionContext.ExtensionDefaultName, VerbsCommon.Set))
124124
{
125-
if (string.Equals(this.ParameterSetName, SetCustomScriptExtensionByContainerBlobsParamSetName))
125+
ExecuteClientAction(() =>
126126
{
127-
this.StorageEndpointSuffix = string.IsNullOrEmpty(this.StorageEndpointSuffix) ?
128-
DefaultProfile.Context.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix) : this.StorageEndpointSuffix;
129-
var sName = string.IsNullOrEmpty(this.StorageAccountName) ? GetStorageName() : this.StorageAccountName;
130-
var sKey = string.IsNullOrEmpty(this.StorageAccountKey) ? GetStorageKey(sName) : this.StorageAccountKey;
131-
132-
if (this.FileName != null && this.FileName.Any())
127+
if (string.Equals(this.ParameterSetName, SetCustomScriptExtensionByContainerBlobsParamSetName))
133128
{
134-
this.FileUri = (from blobName in this.FileName
135-
select GetSasUrlStr(sName, sKey, this.ContainerName, blobName)).ToArray();
129+
this.StorageEndpointSuffix = string.IsNullOrEmpty(this.StorageEndpointSuffix) ?
130+
DefaultProfile.Context.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix) : this.StorageEndpointSuffix;
131+
var sName = string.IsNullOrEmpty(this.StorageAccountName) ? GetStorageName() : this.StorageAccountName;
132+
var sKey = string.IsNullOrEmpty(this.StorageAccountKey) ? GetStorageKey(sName) : this.StorageAccountKey;
136133

137-
if (string.IsNullOrEmpty(this.Run))
134+
if (this.FileName != null && this.FileName.Any())
138135
{
139-
WriteWarning(Microsoft.Azure.Commands.Compute.Properties.Resources.CustomScriptExtensionTryToUseTheFirstSpecifiedFileAsRunScript);
140-
this.Run = this.FileName[0];
136+
this.FileUri = (from blobName in this.FileName
137+
select GetSasUrlStr(sName, sKey, this.ContainerName, blobName)).ToArray();
138+
139+
if (string.IsNullOrEmpty(this.Run))
140+
{
141+
WriteWarning(Microsoft.Azure.Commands.Compute.Properties.Resources.CustomScriptExtensionTryToUseTheFirstSpecifiedFileAsRunScript);
142+
this.Run = this.FileName[0];
143+
}
141144
}
142145
}
143-
}
144146

145-
var policyStr = string.Format(policyFormatStr, defaultPolicyStr);
146-
var commandToExecute = string.Format(poshCmdFormatStr, policyStr, this.Run, this.Argument);
147+
var policyStr = string.Format(policyFormatStr, defaultPolicyStr);
148+
var commandToExecute = string.Format(poshCmdFormatStr, policyStr, this.Run, this.Argument);
147149

148-
var privateSettings = GetPrivateConfiguration();
150+
var privateSettings = GetPrivateConfiguration();
149151

150-
var publicSettings = new Hashtable();
151-
publicSettings.Add(fileUrisKey, FileUri ?? new string[] { });
152+
var publicSettings = new Hashtable();
153+
publicSettings.Add(fileUrisKey, FileUri ?? new string[] { });
152154

153-
if (this.SecureExecution.IsPresent)
154-
{
155-
if (privateSettings == null)
155+
if (this.SecureExecution.IsPresent)
156156
{
157-
privateSettings = new Hashtable();
157+
if (privateSettings == null)
158+
{
159+
privateSettings = new Hashtable();
160+
}
161+
privateSettings.Add(commandToExecuteKey, commandToExecute ?? "");
162+
}
163+
else
164+
{
165+
publicSettings.Add(commandToExecuteKey, commandToExecute ?? "");
158166
}
159-
privateSettings.Add(commandToExecuteKey, commandToExecute ?? "");
160-
}
161-
else
162-
{
163-
publicSettings.Add(commandToExecuteKey, commandToExecute ?? "");
164-
}
165167

166-
var parameters = new VirtualMachineExtension
167-
{
168-
Location = this.Location,
169-
Publisher = VirtualMachineCustomScriptExtensionContext.ExtensionDefaultPublisher,
170-
VirtualMachineExtensionType = VirtualMachineCustomScriptExtensionContext.ExtensionDefaultName,
171-
TypeHandlerVersion = (this.TypeHandlerVersion) ?? VirtualMachineCustomScriptExtensionContext.ExtensionDefaultVersion,
172-
Settings = publicSettings,
173-
ProtectedSettings = privateSettings,
174-
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
175-
ForceUpdateTag = this.ForceRerun
176-
};
177-
178-
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
179-
this.ResourceGroupName,
180-
this.VMName,
181-
this.Name,
182-
parameters).GetAwaiter().GetResult();
183-
var result = Mapper.Map<PSAzureOperationResponse>(op);
184-
WriteObject(result);
185-
});
168+
var parameters = new VirtualMachineExtension
169+
{
170+
Location = this.Location,
171+
Publisher = VirtualMachineCustomScriptExtensionContext.ExtensionDefaultPublisher,
172+
VirtualMachineExtensionType = VirtualMachineCustomScriptExtensionContext.ExtensionDefaultName,
173+
TypeHandlerVersion = (this.TypeHandlerVersion) ?? VirtualMachineCustomScriptExtensionContext.ExtensionDefaultVersion,
174+
Settings = publicSettings,
175+
ProtectedSettings = privateSettings,
176+
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
177+
ForceUpdateTag = this.ForceRerun
178+
};
179+
180+
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
181+
this.ResourceGroupName,
182+
this.VMName,
183+
this.Name,
184+
parameters).GetAwaiter().GetResult();
185+
var result = Mapper.Map<PSAzureOperationResponse>(op);
186+
WriteObject(result);
187+
});
188+
}
186189
}
187190

188191
protected string GetStorageName()

src/ResourceManager/Compute/Commands.Compute/Extension/SetAzureVMExtensionCommand.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,42 @@ public override void ExecuteCmdlet()
7979
{
8080
base.ExecuteCmdlet();
8181

82-
ExecuteClientAction(() =>
82+
if (ShouldProcess(this.ExtensionType, VerbsCommon.Set))
8383
{
84-
if (this.ParameterSetName.Equals(SettingStringParamSet))
84+
ExecuteClientAction(() =>
8585
{
86-
this.Settings = string.IsNullOrEmpty(this.SettingString)
87-
? null
88-
: JsonConvert.DeserializeObject<Hashtable>(this.SettingString);
89-
this.ProtectedSettings = string.IsNullOrEmpty(this.ProtectedSettingString)
90-
? null
91-
: JsonConvert.DeserializeObject<Hashtable>(this.ProtectedSettingString);
92-
}
86+
if (this.ParameterSetName.Equals(SettingStringParamSet))
87+
{
88+
this.Settings = string.IsNullOrEmpty(this.SettingString)
89+
? null
90+
: JsonConvert.DeserializeObject<Hashtable>(this.SettingString);
91+
this.ProtectedSettings = string.IsNullOrEmpty(this.ProtectedSettingString)
92+
? null
93+
: JsonConvert.DeserializeObject<Hashtable>(this.ProtectedSettingString);
94+
}
9395

94-
var parameters = new VirtualMachineExtension
95-
{
96-
Location = this.Location,
97-
Publisher = this.Publisher,
98-
VirtualMachineExtensionType = this.ExtensionType,
99-
TypeHandlerVersion = this.TypeHandlerVersion,
100-
Settings = this.Settings,
101-
ProtectedSettings = this.ProtectedSettings,
102-
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
103-
ForceUpdateTag = this.ForceRerun
104-
};
96+
var parameters = new VirtualMachineExtension
97+
{
98+
Location = this.Location,
99+
Publisher = this.Publisher,
100+
VirtualMachineExtensionType = this.ExtensionType,
101+
TypeHandlerVersion = this.TypeHandlerVersion,
102+
Settings = this.Settings,
103+
ProtectedSettings = this.ProtectedSettings,
104+
AutoUpgradeMinorVersion = !this.DisableAutoUpgradeMinorVersion.IsPresent,
105+
ForceUpdateTag = this.ForceRerun
106+
};
105107

106-
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
107-
this.ResourceGroupName,
108-
this.VMName,
109-
this.Name,
110-
parameters).GetAwaiter().GetResult();
108+
var op = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
109+
this.ResourceGroupName,
110+
this.VMName,
111+
this.Name,
112+
parameters).GetAwaiter().GetResult();
111113

112-
var result = Mapper.Map<PSAzureOperationResponse>(op);
113-
WriteObject(result);
114-
});
114+
var result = Mapper.Map<PSAzureOperationResponse>(op);
115+
WriteObject(result);
116+
});
117+
}
115118
}
116119
}
117120
}

0 commit comments

Comments
 (0)