Skip to content

Supports overriding default subscription via -SubscriptionId #15659

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 4 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# How to: Enable Subscription ID Overriding in Your Module

## Background

## Steps

1. {add attribute}
2. if any cmdlet implememts IDynamicParameter

## Troubleshooting

### Static analysis fails
26 changes: 24 additions & 2 deletions src/Accounts/Accounts.Test/AzureRMProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
new AzureTenant() { Directory = DefaultDomain, Id = DefaultTenant.ToString() });
var profile = new AzureRmProfile();
profile.DefaultContext = Context;
profile.TrySetDefaultContext(Context);
return new RMProfileClient(profile);
}

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

Expand Down Expand Up @@ -1093,6 +1093,28 @@ public void CanRenewTokenLogin()
Assert.Contains(graphToken2, graphMessage.Headers.Authorization.Parameter);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ShouldShallowCopy()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var subscriptions = new List<string> { DefaultSubscription.ToString() };
var profile = SetupLogin(tenants, subscriptions, subscriptions);

var copy = profile.CopyForContextOverriding() as AzureRmProfile;

// Should act as shallow copy
// except for that `Contexts` should be a new dictionary
Assert.NotSame(profile, copy);
Assert.Same(profile.EnvironmentTable, copy.EnvironmentTable);
Assert.Same(profile.DefaultContext, copy.DefaultContext);
Assert.NotSame(profile.Contexts, copy.Contexts);

// Then deep copy default context of copy profile
copy.DefaultContext = copy.DefaultContext.DeepCopy();
Assert.NotSame(profile.DefaultContext, copy.DefaultContext);
}

private class MockPage<T> : IPage<T>
{
public MockPage(IList<T> Items)
Expand Down
7 changes: 5 additions & 2 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-->

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

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

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

## Version 2.2.6
* Upgrade Azure.Identity to fix the issue that Connect-AzAccount fails when ADFS credential is used [#13560]
Expand Down Expand Up @@ -83,7 +86,7 @@
* Added new cmdlet `Get-AzAccessToken`
* Fixed an issue that error happens if user profile path is inaccessible
* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419]
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
* Added parameter "ContainerRegistryEndpointSuffix" to: `Add-AzEnvironment`, `Set-AzEnvironment`
* Supported interrupting login by hitting <kbd>CTRL</kbd>+<kbd>C</kbd>
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]
* Fixed null reference and method case insensitive in `Invoke-AzRestMethod`
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Context/GetAzureRMContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ void WriteContext(IAzureContext azureContext, string name)
WriteObject(context);
}

public object GetDynamicParameters()
public new object GetDynamicParameters()
{
var parameters = new RuntimeDefinedParameterDictionary();
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
AzureRmProfile localProfile = DefaultProfile as AzureRmProfile;
if (localProfile != null && localProfile.Contexts != null && localProfile.Contexts.Count > 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Context/RemoveAzureRmContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class RemoveAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
[Parameter(Mandatory = false, HelpMessage = "Return the removed context")]
public SwitchParameter PassThru { get; set; }

public object GetDynamicParameters()
public new object GetDynamicParameters()
{
var parameters = new RuntimeDefinedParameterDictionary();
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
RuntimeDefinedParameter namedParameter;
if (TryGetExistingContextNameParameter("Name", NamedContextParameterSet, out namedParameter))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Context/RenameAzureRmContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class RenameAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
[Parameter(Mandatory=false, HelpMessage="Return the renamed context")]
public SwitchParameter PassThru { get; set; }

public object GetDynamicParameters()
public new object GetDynamicParameters()
{
var parameters = new RuntimeDefinedParameterDictionary();
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
RuntimeDefinedParameter sourceNameParameter;
if (TryGetExistingContextNameParameter(SourceParameterName, NameParameterSet, out sourceNameParameter))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Context/SelectAzureRmContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class SelectAzureRmContext : AzureContextModificationCmdlet, IDynamicPara
/// </summary>
protected override bool RequireDefaultContext() { return false; }

public object GetDynamicParameters()
public new object GetDynamicParameters()
{
var parameters = new RuntimeDefinedParameterDictionary();
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
RuntimeDefinedParameter nameParameter;
if (TryGetExistingContextNameParameter("Name", ContextNameParameterSet, out nameParameter))
{
Expand Down
7 changes: 7 additions & 0 deletions src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,5 +830,12 @@ public void RefreshContextsFromCache()

Save(ProfilePath, false);
}

public IAzureContextContainer CopyForContextOverriding()
{
var copy = MemberwiseClone() as AzureRmProfile;
copy.Contexts = new ConcurrentDictionary<string, IAzureContext>(copy.Contexts, StringComparer.CurrentCultureIgnoreCase);
return copy;
}
}
}
3 changes: 2 additions & 1 deletion src/Aks/Aks/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Made `-Subscription <String>` available in all Aks cmdlets. You can manage Aks resources in other subscriptions without switching the context.

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

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

## Version 2.0.2
Expand Down
3 changes: 2 additions & 1 deletion src/Aks/Aks/Commands/KubeCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
using Microsoft.Azure.Management.ContainerService;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Rest;

using Microsoft.WindowsAzure.Commands.Common.Attributes;
using CloudException = Microsoft.Rest.Azure.CloudException;

namespace Microsoft.Azure.Commands.Aks
{
[SupportsSubscriptionId]
public abstract class KubeCmdletBase : AzureRMCmdlet
{
private IContainerServiceClient _client;
Expand Down
23 changes: 12 additions & 11 deletions src/Aks/Aks/help/Az.Aks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ List Kubernetes managed clusters.
### [Get-AzAksNodePool](Get-AzAksNodePool.md)
List node pools in specified cluster.

### [Get-AzAksNodePoolUpgradeProfile](Get-AzAksNodePoolUpgradeProfile.md)
Gets the details of the upgrade profile for an agent pool with a specified resource group and managed cluster name.

### [Get-AzAksUpgradeProfile](Get-AzAksUpgradeProfile.md)
Gets the details of the upgrade profile for a managed cluster with a specified resource group and name.

### [Get-AzAksVersion](Get-AzAksVersion.md)
List available version for creating managed Kubernetes cluster.

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

### [Start-AzAksCluster](Start-AzAksCluster.md)
Starts a Stopped Managed Cluster

### [Start-AzAksDashboard](Start-AzAksDashboard.md)
Create a Kubectl SSH tunnel to the managed cluster's dashboard.

### [Stop-AzAksCluster](Stop-AzAksCluster.md)
Stops a Running Managed Cluster

### [Stop-AzAksDashboard](Stop-AzAksDashboard.md)
Stop the Kubectl SSH tunnel created in Start-AzKubernetesDashboard.

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

### [Get-AzAksNodePoolUpgradeProfile](Get-AzAksNodePoolUpgradeProfile.md)
Gets the details of the upgrade profile for an agent pool with a specified resource group and managed cluster name.

### [Get-AzAksUpgradeProfile](Get-AzAksUpgradeProfile.md)
Gets the details of the upgrade profile for a managed cluster with a specified resource group and name.

### [Start-AzAksCluster](Start-AzAksCluster.md)
Starts a Stopped Managed Cluster

### [Stop-AzAksCluster](Stop-AzAksCluster.md)
Stops a Running Managed Cluster
23 changes: 21 additions & 2 deletions src/Aks/Aks/help/Disable-AzAksAddOn.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ Disable the addons for aks.
### defaultParameterSet (Default)
```
Disable-AzAksAddOn [-ResourceGroupName] <String> [-ClusterName] <String> [-Name <String[]>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
[<CommonParameters>]
```

### InputObjectParameterSet
```
Disable-AzAksAddOn -ClusterObject <PSKubernetesCluster> [-Name <String[]>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -113,6 +115,23 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -SubscriptionId
The ID of the subscription.
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.
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.

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -Confirm
Prompts you for confirmation before running the cmdlet.

Expand Down
22 changes: 20 additions & 2 deletions src/Aks/Aks/help/Enable-AzAksAddOn.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ Enable the addons for aks.
```
Enable-AzAksAddOn [-WorkspaceResourceId <String>] [-SubnetName <String>] [-ResourceGroupName] <String>
[-ClusterName] <String> [-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
[-SubscriptionId <String>] [<CommonParameters>]
```

### InputObjectParameterSet
```
Enable-AzAksAddOn [-WorkspaceResourceId <String>] [-SubnetName <String>] -ClusterObject <PSKubernetesCluster>
[-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-Name <String[]>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [-SubscriptionId <String>]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -129,6 +130,23 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -SubscriptionId
The ID of the subscription.
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.
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.

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -WorkspaceResourceId
Resource Id of the workspace of Monitoring.

Expand Down
24 changes: 21 additions & 3 deletions src/Aks/Aks/help/Get-AzAksCluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ List Kubernetes managed clusters.
### ResourceGroupParameterSet (Default)
```
Get-AzAksCluster [[-ResourceGroupName] <String>] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-SubscriptionId <String>] [<CommonParameters>]
```

### IdParameterSet
```
Get-AzAksCluster [-Id] <String> [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
Get-AzAksCluster [-Id] <String> [-DefaultProfile <IAzureContextContainer>] [-SubscriptionId <String>]
[<CommonParameters>]
```

### NameParameterSet
```
Get-AzAksCluster [-ResourceGroupName] <String> [-Name] <String> [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-SubscriptionId <String>] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -113,6 +114,23 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -SubscriptionId
The ID of the subscription.
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.
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.

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### CommonParameters
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).

Expand Down
Loading