Skip to content

Commit 8daa2b6

Browse files
authored
Merge pull request #4687 from viananth/newpatch-env
Fix null ref value for adding env using ArmEndpoint
2 parents 458554f + c5bafad commit 8daa2b6

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/EnvironmentCmdletTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,93 @@ public void SetEnvironmentWithOnPremise()
611611
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
612612
}
613613

614+
[Fact]
615+
[Trait(Category.AcceptanceType, Category.CheckIn)]
616+
public void SetEnvironmentForStack()
617+
{
618+
Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
619+
SetupConfirmation(commandRuntimeMock);
620+
var cmdlet = new SetAzureRMEnvironmentCommand()
621+
{
622+
CommandRuntime = commandRuntimeMock.Object,
623+
Name = "Stack",
624+
ARMEndpoint = "https://management.local.azurestack.external/"
625+
};
626+
627+
Mock<EnvironmentHelper> envHelperMock = new Mock<EnvironmentHelper>();
628+
MetadataResponse metadataEndpoints = new MetadataResponse
629+
{
630+
GalleryEndpoint = "https://galleryendpoint",
631+
GraphEndpoint = "https://graphendpoint",
632+
PortalEndpoint = "https://portalendpoint",
633+
authentication = new Authentication
634+
{
635+
Audiences = new[] { "audience1", "audience2" },
636+
LoginEndpoint = "https://loginendpoint"
637+
}
638+
};
639+
envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny<string>())).ReturnsAsync(metadataEndpoints);
640+
envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny<string>())).Returns("domain");
641+
cmdlet.EnvHelper = envHelperMock.Object;
642+
cmdlet.SetParameterSet("ARMEndpoint");
643+
cmdlet.InvokeBeginProcessing();
644+
cmdlet.ExecuteCmdlet();
645+
cmdlet.InvokeEndProcessing();
646+
647+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
648+
IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Stack");
649+
Assert.Equal(env.Name, cmdlet.Name);
650+
Assert.Equal(cmdlet.ARMEndpoint, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
651+
Assert.Equal("https://loginendpoint/", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
652+
Assert.Equal("audience1", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
653+
Assert.Equal("https://graphendpoint", env.GetEndpoint(AzureEnvironment.Endpoint.GraphEndpointResourceId));
654+
envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny<string>()), Times.Once);
655+
envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny<string>()), Times.Once);
656+
657+
// Update onpremise to true
658+
var cmdlet2 = new SetAzureRMEnvironmentCommand()
659+
{
660+
CommandRuntime = commandRuntimeMock.Object,
661+
Name = "Stack",
662+
EnableAdfsAuthentication = true
663+
};
664+
665+
cmdlet2.MyInvocation.BoundParameters.Add("Name", "Stack");
666+
cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);
667+
668+
cmdlet2.InvokeBeginProcessing();
669+
cmdlet2.ExecuteCmdlet();
670+
cmdlet2.InvokeEndProcessing();
671+
672+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Exactly(2));
673+
IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");
674+
Assert.Equal(env2.Name, cmdlet2.Name);
675+
Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
676+
Assert.True(env2.OnPremise);
677+
678+
// Update gallery endpoint
679+
var cmdlet3 = new SetAzureRMEnvironmentCommand()
680+
{
681+
CommandRuntime = commandRuntimeMock.Object,
682+
Name = "Stack",
683+
GalleryEndpoint = "http://galleryendpoint.com",
684+
};
685+
686+
cmdlet3.MyInvocation.BoundParameters.Add("Name", "stack");
687+
cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");
688+
689+
cmdlet3.InvokeBeginProcessing();
690+
cmdlet3.ExecuteCmdlet();
691+
cmdlet3.InvokeEndProcessing();
692+
693+
// Ensure gallery endpoint is updated and OnPremise value is preserved
694+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Exactly(3));
695+
IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");
696+
Assert.Equal(env3.Name, cmdlet3.Name);
697+
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
698+
Assert.True(env3.OnPremise);
699+
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
700+
}
614701

615702
[Fact]
616703
[Trait(Category.AcceptanceType, Category.CheckIn)]

src/ResourceManager/Profile/Commands.Profile/Environment/AddAzureRMEnvironment.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1617
using Microsoft.Azure.Commands.Profile.Common;
1718
using Microsoft.Azure.Commands.Profile.Models;
1819
using Microsoft.Azure.Commands.Profile.Utilities;
@@ -97,6 +98,7 @@ public EnvironmentHelper EnvHelper
9798

9899
[Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 11, Mandatory = false, ValueFromPipelineByPropertyName = true,
99100
HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
101+
[Parameter(ParameterSetName = MetadataParameterSet, Position = 4, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
100102
public string AzureKeyVaultServiceEndpointResourceId { get; set; }
101103

102104
[Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true,
@@ -169,14 +171,20 @@ public override void ExecuteCmdlet()
169171
env.Value?.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager)?.ToLowerInvariant(),
170172
GeneralUtilities.EnsureTrailingSlash(ARMEndpoint)?.ToLowerInvariant(), StringComparison.CurrentCultureIgnoreCase));
171173

172-
var newEnvironment = new AzureEnvironment { Name = this.Name };
174+
var defProfile = GetDefaultProfile();
175+
IAzureEnvironment newEnvironment;
176+
if (!defProfile.TryGetEnvironment(this.Name, out newEnvironment))
177+
{
178+
newEnvironment = new AzureEnvironment { Name = this.Name };
179+
}
180+
173181
if (publicEnvironment.Key == null)
174182
{
175183
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ResourceManager, ARMEndpoint);
176184
try
177185
{
178186
EnvHelper = (EnvHelper == null ? new EnvironmentHelper() : EnvHelper);
179-
MetadataResponse metadataEndpoints = EnvHelper.RetrieveMetaDataEndpoints(ResourceManagerEndpoint).Result;
187+
MetadataResponse metadataEndpoints = EnvHelper.RetrieveMetaDataEndpoints(newEnvironment.ResourceManagerUrl).Result;
180188
string domain = EnvHelper.RetrieveDomain(ARMEndpoint);
181189

182190
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectory,

src/ResourceManager/Profile/Commands.Profile/Environment/SetAzureRMEnvironment.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public EnvironmentHelper EnvHelper
9999

100100
[Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 11, Mandatory = false, ValueFromPipelineByPropertyName = true,
101101
HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
102+
[Parameter(ParameterSetName = MetadataParameterSet, Position = 4, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
102103
public string AzureKeyVaultServiceEndpointResourceId { get; set; }
103104

104105
[Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true,
@@ -172,14 +173,20 @@ public override void ExecuteCmdlet()
172173
env.Value?.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager)?.ToLowerInvariant(),
173174
GeneralUtilities.EnsureTrailingSlash(ARMEndpoint)?.ToLowerInvariant(), StringComparison.CurrentCultureIgnoreCase));
174175

175-
var newEnvironment = new AzureEnvironment { Name = this.Name };
176+
var defProfile = GetDefaultProfile();
177+
IAzureEnvironment newEnvironment;
178+
if (!defProfile.TryGetEnvironment(this.Name, out newEnvironment))
179+
{
180+
newEnvironment = new AzureEnvironment { Name = this.Name };
181+
}
182+
176183
if (publicEnvironment.Key == null)
177184
{
178185
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ResourceManager, ARMEndpoint);
179186
try
180187
{
181188
EnvHelper = (EnvHelper == null ? new EnvironmentHelper() : EnvHelper);
182-
MetadataResponse metadataEndpoints = EnvHelper.RetrieveMetaDataEndpoints(ResourceManagerEndpoint).Result;
189+
MetadataResponse metadataEndpoints = EnvHelper.RetrieveMetaDataEndpoints(newEnvironment.ResourceManagerUrl).Result;
183190
string domain = EnvHelper.RetrieveDomain(ARMEndpoint);
184191

185192
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectory,

0 commit comments

Comments
 (0)