Skip to content

Add Az aliases #6987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Azure.PowerShell.Netcore.Test.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Commands.DeviceProvisioning
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Commands.PolicyInsights.Test.Netcore", "ResourceManager\PolicyInsights\Commands.PolicyInsights.Test\Commands.PolicyInsights.Test.Netcore.csproj", "{AFA65D3A-FB7E-4DAB-9867-1CA4CBA41BFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Commands.Resources.Test.Netcore", "ResourceManager\Resources\Commands.Resources.Test\Commands.Resources.Test.Netcore.csproj", "{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -223,6 +225,18 @@ Global
{AFA65D3A-FB7E-4DAB-9867-1CA4CBA41BFB}.Release|x64.Build.0 = Release|Any CPU
{AFA65D3A-FB7E-4DAB-9867-1CA4CBA41BFB}.Release|x86.ActiveCfg = Release|Any CPU
{AFA65D3A-FB7E-4DAB-9867-1CA4CBA41BFB}.Release|x86.Build.0 = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|x64.ActiveCfg = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|x64.Build.0 = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|x86.ActiveCfg = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Debug|x86.Build.0 = Debug|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|Any CPU.Build.0 = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|x64.ActiveCfg = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|x64.Build.0 = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|x86.ActiveCfg = Release|Any CPU
{50DEDB9D-D379-43CF-9A0F-6C366B4AE400}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,13 @@ private void SetupPowerShellModules(System.Management.Automation.PowerShell powe
foreach (string moduleName in modules)
{
powershell.AddScript(string.Format("Import-Module \"{0}\"", moduleName.AsAbsoluteLocation()));
if (moduleName.EndsWith(".psd1"))
{
#if NETSTANDARD
var moduleShortName = moduleName.Split(new string[] { "\\" }, StringSplitOptions.None).Last().Split(new string[] { "/" }, StringSplitOptions.None).Last();
powershell.AddScript("Enable-AzureRmAlias -Module " + moduleShortName.Substring(0, moduleShortName.Length - 5));
#endif
}
}

powershell.AddScript(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.Azure.Internal.Subscriptions;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;

namespace Commands.Network.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="9.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>

Expand Down
3 changes: 2 additions & 1 deletion src/ResourceManager/Profile/Commands.Profile/Az.Profile.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ CmdletsToExport = 'Disable-AzDataCollection', 'Disable-AzContextAutosave',
'Rename-AzContext', 'Remove-AzContext',
'Clear-AzContext', 'Disconnect-AzAccount',
'Get-AzContextAutosaveSetting', 'Set-AzDefault',
'Get-AzDefault', 'Clear-AzDefault'
'Get-AzDefault', 'Clear-AzDefault',
'Enable-AzureRmAlias', 'Disable-AzureRmAlias'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text.RegularExpressions;

namespace Microsoft.Azure.Commands.Profile.AzureRmAlias
{
public class AliasHelper
{
private const string STARTALIASIMPORTMARKER = "#Begin Azure PowerShell alias import";
private const string ENDALIASIMPORTMARKER = "#End Azure PowerShell alias import";

private IDataStore _dataStore;

public AliasHelper(IDataStore dataStore)
{
_dataStore = dataStore;
FileUtilities.DataStore = dataStore;
}

public AliasHelper() : this(AzureSession.Instance.DataStore)
{
}

public static string GetProfilePath(string Scope, SessionState sessionState)
{
var userprofile = "";
if (Scope != null && Scope.Equals("CurrentUser"))
{
var editionType = sessionState.PSVariable.GetValue("PSEdition") as string;
var psFolder = string.Equals(editionType, "Desktop", StringComparison.OrdinalIgnoreCase) ? "WindowsPowerShell" : "PowerShell";
userprofile = Path.Combine(sessionState.PSVariable.GetValue("env:USERPROFILE").ToString(), "Documents", psFolder, "profile.ps1");
}

else if (Scope != null && Scope.Equals("LocalMachine"))
{
userprofile = Path.Combine(sessionState.PSVariable.GetValue("PSHOME").ToString(), "profile.ps1");
}

return userprofile;
}

public void RemoveAliasesInProfile(string userprofile, string[] Module, Dictionary<string, object> mapping)
{
ParseFile(userprofile, Module, out string filecontent, out List<string> modulesToKeep, add: false);

// Add script to enable aliases to profile if there are any modules to keep
CreateFileEntry(modulesToKeep, filecontent, userprofile, mapping);
}

public void AddAliasesToProfile(string userprofile, string[] Module, Dictionary<string, object> mapping)
{
CreateProfileIfNotExists(userprofile);

ParseFile(userprofile, Module, out string filecontent, out List<string> modulesToKeep, add: true);

if (Module == null)
{
CreateFileEntry(mapping.Keys.ToList(), filecontent, userprofile, mapping);
}
else
{
CreateFileEntry(modulesToKeep, filecontent, userprofile, mapping);
}
}

public void ParseFile(string userprofile, string[] Module, out string filecontent, out List<string> modulesToKeep, bool add)
{
filecontent = "";
modulesToKeep = new List<String>();
if (add)
{
modulesToKeep = Module?.ToList();
}


string originalText = _dataStore.ReadFileAsText(userprofile.ToString());
if (originalText.Contains(STARTALIASIMPORTMARKER))
{
// Add back profile code unrelated to Azure PowerShell aliases
var splitOriginalText = originalText.Split(new string[] { STARTALIASIMPORTMARKER, ENDALIASIMPORTMARKER }, StringSplitOptions.None);
filecontent += splitOriginalText[0].Trim();
if (splitOriginalText.Length > 2)
{
filecontent += Environment.NewLine + splitOriginalText[2].Trim() + Environment.NewLine;
}

if (Module != null)
{
// Add modules currently in the profile to the list of modules to enable.
var regex = new Regex(@"Az\.[a-zA-Z0-9\.]+(,\s|\s-)");
Match match = regex.Match(splitOriginalText[1].Split(new string[] { "Import-Module Az.Profile" }, StringSplitOptions.None)[1]);
while (match.Success)
{
if (add)
{
if (!modulesToKeep.Contains(match.ToString().Substring(0, match.ToString().Length - 2), StringComparer.CurrentCultureIgnoreCase))
{
modulesToKeep.Add(match.ToString().Substring(0, match.ToString().Length - 2));
}
match = match.NextMatch();
}
else
{
if (!Module.Contains(match.ToString().Substring(0, match.ToString().Length - 2), StringComparer.CurrentCultureIgnoreCase))
{
modulesToKeep.Add(match.ToString().Substring(0, match.ToString().Length - 2));
}
match = match.NextMatch();
}
}
}
}
else
{
filecontent = originalText;
}
}

public void CreateFileEntry(List<string> modulesToKeep, string filecontent, string userprofile, Dictionary<string, object> mapping)
{
if (modulesToKeep.Count > 0)
{
filecontent += STARTALIASIMPORTMARKER + Environment.NewLine + "Import-Module Az.Profile -ErrorAction SilentlyContinue -ErrorVariable importError" +
Environment.NewLine + "if ($importerror.Count -eq 0) { " + Environment.NewLine;

var validModules = new List<string>();
foreach (var name in modulesToKeep)
{
if (mapping.ContainsKey(name))
{
validModules.Add(name);
}
}
filecontent += " Enable-AzureRmAlias -Module " + string.Join(", ", validModules) + " -ErrorAction SilentlyContinue; " + Environment.NewLine;

filecontent += "}" + Environment.NewLine + ENDALIASIMPORTMARKER;
}

ExecuteFileActionWithFriendlyError(() =>
{
FileUtilities.EnsureDirectoryExists(Path.GetDirectoryName(userprofile));
_dataStore.WriteFile(userprofile, filecontent);
});
}

public void CreateProfileIfNotExists(string userprofile)
{
ExecuteFileActionWithFriendlyError(() =>
{
FileUtilities.EnsureDirectoryExists(Path.GetDirectoryName(userprofile));
if (!_dataStore.FileExists(userprofile))
{
_dataStore.WriteFile(userprofile, Environment.NewLine);
}
});
}

private static void ExecuteFileActionWithFriendlyError(Action fileAction)
{
try
{
fileAction();
}
catch (UnauthorizedAccessException accessException) when (accessException?.Message != null && accessException.Message.ToLower().Contains("denied"))
{
throw new UnauthorizedAccessException(Properties.Resources.AliasImportFailure, accessException);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Profile.AzureRmAlias
{
/// <summary>
/// Cmdlet to clear default options.
/// </summary>
[Cmdlet("Disable", "AzureRmAlias", SupportsShouldProcess = true)]
[OutputType(typeof(string))]
public class DisableAzureRmAlias : AzureRMCmdlet
{
[Parameter(Mandatory = false, HelpMessage = "Indicates what scope aliases should be disabled for. Default is 'Process'")]
[ValidateSet("Process", "CurrentUser", "LocalMachine")]
public string Scope { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Indicates which modules to disable aliases for. If none are specified, default is all enabled modules.")]
public string[] Module { get; set; }

[Parameter(Mandatory = false, HelpMessage = "If specified, cmdlet will return all disabled aliases")]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
{
Dictionary<string, object> mapping = Mappings.GetCaseInsensitiveMapping();

// If no modules are specified, disable all aliases
if (Module == null)
{
DisableLocalAliases(mapping.Keys.ToArray(), mapping);
}
else
{
DisableLocalAliases(Module, mapping);
}

// Set path to user profile based on the Scope
string userprofile = AliasHelper.GetProfilePath(Scope, SessionState);

if (Scope != null && (Scope.Equals("CurrentUser") || Scope.Equals("LocalMachine")))
{
if (AzureSession.Instance.DataStore.FileExists(userprofile))
{
var helper = new AliasHelper();
helper.RemoveAliasesInProfile(userprofile, Module, mapping);
}
}
}

public void DisableLocalAliases(string[] modulesToDisable, Dictionary<string, object> mapping)
{
foreach (var module in modulesToDisable)
{
if (mapping.ContainsKey(module))
{
Dictionary<string, string> modulemapping =
(Dictionary<string, string>)JsonConvert.DeserializeObject(mapping[module].ToString(), typeof(Dictionary<string, string>));
foreach (var name in modulemapping.Keys)
{
// For every alias, remove pairing in the Alias provider if it exists
if (SessionState.PSVariable.GetValue("Alias:" + modulemapping[name]) != null)
{
SessionState.PSVariable.Remove("Alias:" + modulemapping[name]);
if (PassThru)
{
WriteObject("Removed: " + modulemapping[name] + " : " + name);
}
}
}
}
else
{
WriteWarning("Module '" + module + "' is not a valid Az module.");
}
}
}
}
}
Loading