Skip to content

Commit f606f49

Browse files
authored
Supports overriding default subscription via -SubscriptionId (Azure#15659)
* impl. and test * add `new` to `GetDynamicParameters()` * enable custom sub id for Aks * changelog & document
1 parent 8229206 commit f606f49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+442
-96
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# How to: Enable Subscription ID Overriding in Your Module
2+
3+
## Background
4+
5+
## Steps
6+
7+
1. {add attribute}
8+
2. if any cmdlet implememts IDynamicParameter
9+
10+
## Troubleshooting
11+
12+
### Static analysis fails

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
7979
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
8080
new AzureTenant() { Directory = DefaultDomain, Id = DefaultTenant.ToString() });
8181
var profile = new AzureRmProfile();
82-
profile.DefaultContext = Context;
82+
profile.TrySetDefaultContext(Context);
8383
return new RMProfileClient(profile);
8484
}
8585

@@ -107,7 +107,7 @@ private static AzureRmProfile SetupLogin(List<string> tenants, params List<strin
107107
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
108108
new AzureTenant() { Directory = DefaultDomain, Id = DefaultTenant.ToString() });
109109
var profile = new AzureRmProfile();
110-
profile.DefaultContext = Context;
110+
profile.TrySetDefaultContext(Context);
111111
return profile;
112112
}
113113

@@ -1093,6 +1093,28 @@ public void CanRenewTokenLogin()
10931093
Assert.Contains(graphToken2, graphMessage.Headers.Authorization.Parameter);
10941094
}
10951095

1096+
[Fact]
1097+
[Trait(Category.AcceptanceType, Category.CheckIn)]
1098+
public void ShouldShallowCopy()
1099+
{
1100+
var tenants = new List<string> { DefaultTenant.ToString() };
1101+
var subscriptions = new List<string> { DefaultSubscription.ToString() };
1102+
var profile = SetupLogin(tenants, subscriptions, subscriptions);
1103+
1104+
var copy = profile.CopyForContextOverriding() as AzureRmProfile;
1105+
1106+
// Should act as shallow copy
1107+
// except for that `Contexts` should be a new dictionary
1108+
Assert.NotSame(profile, copy);
1109+
Assert.Same(profile.EnvironmentTable, copy.EnvironmentTable);
1110+
Assert.Same(profile.DefaultContext, copy.DefaultContext);
1111+
Assert.NotSame(profile.Contexts, copy.Contexts);
1112+
1113+
// Then deep copy default context of copy profile
1114+
copy.DefaultContext = copy.DefaultContext.DeepCopy();
1115+
Assert.NotSame(profile.DefaultContext, copy.DefaultContext);
1116+
}
1117+
10961118
private class MockPage<T> : IPage<T>
10971119
{
10981120
public MockPage(IList<T> Items)

src/Accounts/Accounts/ChangeLog.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Corrected the URLs to Azure Portal in the results of `Get-AzEnvironment` and `Get-AzContext`. [#15429]
23+
* Made infrastructural changes to support overriding default subscription via a `-SubscriptionId <String>` parameter.
24+
- [Az.Aks](https://docs.microsoft.com/powershell/module/az.aks/get-azakscluster) is the first module that supports it.
2225

2326
## Version 2.5.2
2427
* Disabled context auto saving when token cache persistence fails on Windows and macOS
@@ -53,7 +56,7 @@
5356

5457
## Version 2.2.7
5558
* Fixed incorrect warning message on Windows PowerShell [#14556]
56-
* Set Azure Environment variable `AzureKeyVaultServiceEndpointResourceId` according to the value of `AzureKeyVaultDnsSuffix` when discovering environment
59+
* Set Azure Environment variable `AzureKeyVaultServiceEndpointResourceId` according to the value of `AzureKeyVaultDnsSuffix` when discovering environment
5760

5861
## Version 2.2.6
5962
* Upgrade Azure.Identity to fix the issue that Connect-AzAccount fails when ADFS credential is used [#13560]
@@ -83,7 +86,7 @@
8386
* Added new cmdlet `Get-AzAccessToken`
8487
* Fixed an issue that error happens if user profile path is inaccessible
8588
* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419]
86-
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
89+
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
8790
* Supported interrupting login by hitting <kbd>CTRL</kbd>+<kbd>C</kbd>
8891
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]
8992
* Fixed null reference and method case insensitive in `Invoke-AzRestMethod`

src/Accounts/Accounts/Context/GetAzureRMContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ void WriteContext(IAzureContext azureContext, string name)
144144
WriteObject(context);
145145
}
146146

147-
public object GetDynamicParameters()
147+
public new object GetDynamicParameters()
148148
{
149-
var parameters = new RuntimeDefinedParameterDictionary();
149+
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
150150
AzureRmProfile localProfile = DefaultProfile as AzureRmProfile;
151151
if (localProfile != null && localProfile.Contexts != null && localProfile.Contexts.Count > 0)
152152
{

src/Accounts/Accounts/Context/RemoveAzureRmContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public class RemoveAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
4141
[Parameter(Mandatory = false, HelpMessage = "Return the removed context")]
4242
public SwitchParameter PassThru { get; set; }
4343

44-
public object GetDynamicParameters()
44+
public new object GetDynamicParameters()
4545
{
46-
var parameters = new RuntimeDefinedParameterDictionary();
46+
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
4747
RuntimeDefinedParameter namedParameter;
4848
if (TryGetExistingContextNameParameter("Name", NamedContextParameterSet, out namedParameter))
4949
{

src/Accounts/Accounts/Context/RenameAzureRmContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public class RenameAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
4141
[Parameter(Mandatory=false, HelpMessage="Return the renamed context")]
4242
public SwitchParameter PassThru { get; set; }
4343

44-
public object GetDynamicParameters()
44+
public new object GetDynamicParameters()
4545
{
46-
var parameters = new RuntimeDefinedParameterDictionary();
46+
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
4747
RuntimeDefinedParameter sourceNameParameter;
4848
if (TryGetExistingContextNameParameter(SourceParameterName, NameParameterSet, out sourceNameParameter))
4949
{

src/Accounts/Accounts/Context/SelectAzureRmContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class SelectAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
3535
/// </summary>
3636
protected override bool RequireDefaultContext() { return false; }
3737

38-
public object GetDynamicParameters()
38+
public new object GetDynamicParameters()
3939
{
40-
var parameters = new RuntimeDefinedParameterDictionary();
40+
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
4141
RuntimeDefinedParameter nameParameter;
4242
if (TryGetExistingContextNameParameter("Name", ContextNameParameterSet, out nameParameter))
4343
{

src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,5 +830,12 @@ public void RefreshContextsFromCache()
830830

831831
Save(ProfilePath, false);
832832
}
833+
834+
public IAzureContextContainer CopyForContextOverriding()
835+
{
836+
var copy = MemberwiseClone() as AzureRmProfile;
837+
copy.Contexts = new ConcurrentDictionary<string, IAzureContext>(copy.Contexts, StringComparer.CurrentCultureIgnoreCase);
838+
return copy;
839+
}
833840
}
834841
}

src/Aks/Aks/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Made `-Subscription <String>` available in all Aks cmdlets. You can manage Aks resources in other subscriptions without switching the context.
2122

2223
## Version 2.3.0
2324
* Added `Start-AzAksCluster`, `Stop-AzAksCluster`, `Get-AzAksUpgradeProfile` and `Get-AzAksNodePoolUpgradeProfile`. [#14194]
@@ -31,7 +32,7 @@
3132

3233
## Version 2.1.0
3334
* Added support `AcrNameToAttach` in `Set-AzAksCluster`. [#14692]
34-
* Added support `AcrNameToDetach` in `Set-AzAksCluster`. [#14693]
35+
* Added support `AcrNameToDetach` in `Set-AzAksCluster`. [#14693]
3536
* Added `Set-AzAksClusterCredential` to reset the ServicePrincipal of an existing AKS cluster.
3637

3738
## Version 2.0.2

src/Aks/Aks/Commands/KubeCmdletBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
using Microsoft.Azure.Management.ContainerService;
3030
using Microsoft.Azure.Management.Internal.Resources;
3131
using Microsoft.Rest;
32-
32+
using Microsoft.WindowsAzure.Commands.Common.Attributes;
3333
using CloudException = Microsoft.Rest.Azure.CloudException;
3434

3535
namespace Microsoft.Azure.Commands.Aks
3636
{
37+
[SupportsSubscriptionId]
3738
public abstract class KubeCmdletBase : AzureRMCmdlet
3839
{
3940
private IContainerServiceClient _client;

src/Aks/Aks/help/Az.Aks.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ List Kubernetes managed clusters.
2323
### [Get-AzAksNodePool](Get-AzAksNodePool.md)
2424
List node pools in specified cluster.
2525

26+
### [Get-AzAksNodePoolUpgradeProfile](Get-AzAksNodePoolUpgradeProfile.md)
27+
Gets the details of the upgrade profile for an agent pool with a specified resource group and managed cluster name.
28+
29+
### [Get-AzAksUpgradeProfile](Get-AzAksUpgradeProfile.md)
30+
Gets the details of the upgrade profile for a managed cluster with a specified resource group and name.
31+
2632
### [Get-AzAksVersion](Get-AzAksVersion.md)
2733
List available version for creating managed Kubernetes cluster.
2834

@@ -50,23 +56,18 @@ Update or create a managed Kubernetes cluster.
5056
### [Set-AzAksClusterCredential](Set-AzAksClusterCredential.md)
5157
Reset the ServicePrincipal of an existing AKS cluster.
5258

59+
### [Start-AzAksCluster](Start-AzAksCluster.md)
60+
Starts a Stopped Managed Cluster
61+
5362
### [Start-AzAksDashboard](Start-AzAksDashboard.md)
5463
Create a Kubectl SSH tunnel to the managed cluster's dashboard.
5564

65+
### [Stop-AzAksCluster](Stop-AzAksCluster.md)
66+
Stops a Running Managed Cluster
67+
5668
### [Stop-AzAksDashboard](Stop-AzAksDashboard.md)
5769
Stop the Kubectl SSH tunnel created in Start-AzKubernetesDashboard.
5870

5971
### [Update-AzAksNodePool](Update-AzAksNodePool.md)
6072
Update node pool in a managed cluster.
6173

62-
### [Get-AzAksNodePoolUpgradeProfile](Get-AzAksNodePoolUpgradeProfile.md)
63-
Gets the details of the upgrade profile for an agent pool with a specified resource group and managed cluster name.
64-
65-
### [Get-AzAksUpgradeProfile](Get-AzAksUpgradeProfile.md)
66-
Gets the details of the upgrade profile for a managed cluster with a specified resource group and name.
67-
68-
### [Start-AzAksCluster](Start-AzAksCluster.md)
69-
Starts a Stopped Managed Cluster
70-
71-
### [Stop-AzAksCluster](Stop-AzAksCluster.md)
72-
Stops a Running Managed Cluster

src/Aks/Aks/help/Disable-AzAksAddOn.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ Disable the addons for aks.
1515
### defaultParameterSet (Default)
1616
```
1717
Disable-AzAksAddOn [-ResourceGroupName] <String> [-ClusterName] <String> [-Name <String[]>]
18-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
18+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
19+
[<CommonParameters>]
1920
```
2021

2122
### InputObjectParameterSet
2223
```
2324
Disable-AzAksAddOn -ClusterObject <PSKubernetesCluster> [-Name <String[]>]
24-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
25+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
26+
[<CommonParameters>]
2527
```
2628

2729
## DESCRIPTION
@@ -113,6 +115,23 @@ Accept pipeline input: False
113115
Accept wildcard characters: False
114116
```
115117
118+
### -SubscriptionId
119+
The ID of the subscription.
120+
By default, cmdlets are executed in the subscription that is set in the current context. If the user specifies another subscription, the current cmdlet is executed in the subscription specified by the user.
121+
Overriding subscriptions only take effect during the lifecycle of the current cmdlet. It does not change the subscription in the context, and does not affect subsequent cmdlets.
122+
123+
```yaml
124+
Type: System.String
125+
Parameter Sets: (All)
126+
Aliases:
127+
128+
Required: False
129+
Position: Named
130+
Default value: None
131+
Accept pipeline input: True (ByPropertyName)
132+
Accept wildcard characters: False
133+
```
134+
116135
### -Confirm
117136
Prompts you for confirmation before running the cmdlet.
118137

src/Aks/Aks/help/Enable-AzAksAddOn.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Enable the addons for aks.
1616
```
1717
Enable-AzAksAddOn [-WorkspaceResourceId <String>] [-SubnetName <String>] [-ResourceGroupName] <String>
1818
[-ClusterName] <String> [-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
19-
[<CommonParameters>]
19+
[-SubscriptionId <String>] [<CommonParameters>]
2020
```
2121

2222
### InputObjectParameterSet
2323
```
2424
Enable-AzAksAddOn [-WorkspaceResourceId <String>] [-SubnetName <String>] -ClusterObject <PSKubernetesCluster>
25-
[-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
25+
[-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
26+
[<CommonParameters>]
2627
```
2728

2829
## DESCRIPTION
@@ -129,6 +130,23 @@ Accept pipeline input: True (ByPropertyName)
129130
Accept wildcard characters: False
130131
```
131132
133+
### -SubscriptionId
134+
The ID of the subscription.
135+
By default, cmdlets are executed in the subscription that is set in the current context. If the user specifies another subscription, the current cmdlet is executed in the subscription specified by the user.
136+
Overriding subscriptions only take effect during the lifecycle of the current cmdlet. It does not change the subscription in the context, and does not affect subsequent cmdlets.
137+
138+
```yaml
139+
Type: System.String
140+
Parameter Sets: (All)
141+
Aliases:
142+
143+
Required: False
144+
Position: Named
145+
Default value: None
146+
Accept pipeline input: True (ByPropertyName)
147+
Accept wildcard characters: False
148+
```
149+
132150
### -WorkspaceResourceId
133151
Resource Id of the workspace of Monitoring.
134152

src/Aks/Aks/help/Get-AzAksCluster.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ List Kubernetes managed clusters.
1515
### ResourceGroupParameterSet (Default)
1616
```
1717
Get-AzAksCluster [[-ResourceGroupName] <String>] [-DefaultProfile <IAzureContextContainer>]
18-
[<CommonParameters>]
18+
[-SubscriptionId <String>] [<CommonParameters>]
1919
```
2020

2121
### IdParameterSet
2222
```
23-
Get-AzAksCluster [-Id] <String> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
23+
Get-AzAksCluster [-Id] <String> [-DefaultProfile <IAzureContextContainer>] [-SubscriptionId <String>]
24+
[<CommonParameters>]
2425
```
2526

2627
### NameParameterSet
2728
```
2829
Get-AzAksCluster [-ResourceGroupName] <String> [-Name] <String> [-DefaultProfile <IAzureContextContainer>]
29-
[<CommonParameters>]
30+
[-SubscriptionId <String>] [<CommonParameters>]
3031
```
3132

3233
## DESCRIPTION
@@ -113,6 +114,23 @@ Accept pipeline input: False
113114
Accept wildcard characters: False
114115
```
115116
117+
### -SubscriptionId
118+
The ID of the subscription.
119+
By default, cmdlets are executed in the subscription that is set in the current context. If the user specifies another subscription, the current cmdlet is executed in the subscription specified by the user.
120+
Overriding subscriptions only take effect during the lifecycle of the current cmdlet. It does not change the subscription in the context, and does not affect subsequent cmdlets.
121+
122+
```yaml
123+
Type: System.String
124+
Parameter Sets: (All)
125+
Aliases:
126+
127+
Required: False
128+
Position: Named
129+
Default value: None
130+
Accept pipeline input: True (ByPropertyName)
131+
Accept wildcard characters: False
132+
```
133+
116134
### CommonParameters
117135
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
118136

0 commit comments

Comments
 (0)