Skip to content

Add Get/Set/Clear-AzureRmDefault Cmdlets #4707

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
Oct 20, 2017
Merged

Add Get/Set/Clear-AzureRmDefault Cmdlets #4707

merged 28 commits into from
Oct 20, 2017

Conversation

maddieclayton
Copy link
Contributor

@maddieclayton maddieclayton commented Oct 3, 2017

Description

Implements the cmdlets for maintaining azure defaults. Current default that can be set is resource group. PR includes cmdlets and tests (making resource group optional will come in a separate PR). See #3808.


This checklist is used to make sure that common guidelines for a pull request are followed. You can find a more complete discussion of PowerShell cmdlet best practices here.

General Guidelines

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.
  • The pull request does not introduce breaking changes (unless a major version change occurs in the assembly and module).

Testing Guidelines

  • Pull request includes test coverage for the included changes.
  • PowerShell scripts used in tests should do any necessary setup as part of the test or suite setup, and should not use hard-coded values for locations or existing resources.

Cmdlet Signature Guidelines

  • New cmdlets that make changes or have side effects should implement ShouldProcess and have SupportShouldProcess=true specified in the cmdlet attribute. You can find more information on ShouldProcess here.
  • Cmdlet specifies OutputType attribute if any output is produced - if the cmdlet produces no output, it should implement a PassThru parameter.

Cmdlet Parameter Guidelines

  • Parameter types should not expose types from the management library - complex parameter types should be defined in the module.
  • Complex parameter types are discouraged - a parameter type should be simple types as often as possible. If complex types are used, they should be shallow and easily creatable from a constructor or another cmdlet.
  • Cmdlet parameter sets should be mutually exclusive - each parameter set must have at least one mandatory parameter not in other parameter sets.

.SYNOPSIS
Tests Set-AzureRmDefault when resource group given does not exist
#>
function Test-SetAzureRmDefaultResourceGroupNonexistent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understanbd what this test is covering that isn't covered by the subsequent test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely redid the test - I figured I would test all of resource group in one test because all the commands are interconnected so you can't really test one alone effectively.

$output = Set-AzureRmDefault -ResourceGroupName "TestResourceGroup"
Assert-True { $output.Name -eq "TestResourceGroup" }
Remove-AzureRmResourceGroup -Name "TestResourceGroup" -Force
Clear-AzureRmDefault
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need some validation that the default was cleared

function Test-SetAzureRmDefaultResourceGroupNonexistent
{
$output = Set-AzureRmDefault -ResourceGroupName "TestResourceGroup"
Assert-True { $output.Name -eq "TestResourceGroup" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn';t seem like enough validation - where does the default resource group reside, was it actually stored there? Was it removed after Clear was executed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

#>
function Test-SetAzureRmDefaultResourceGroupExists
{
Set-AzureRmDefault -ResourceGroupName "TestResourceGroup"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments, I don't understand what this is testing

#>
function Test-GetAzureRmDefaultNoDefault
{
$output = Get-AzureRmDefault
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that there will be no defaults before this test runs? Do we need to clear the defaults before to ensure this?

public override void ExecuteCmdlet()
{
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should simply use the CreateServiceClient call here - we only need to pvide the context and the name of the resourecemanager endpiut. This call: https://github.com/Azure/azure-powershell/blob/preview/src/Common/Commands.Common.Authentication.Abstractions/Interfaces/IClientFactory.cs#L39

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will stay the same as discussed offline

// If no parameters are specified, show all defaults
if (!ResourceGroup)
{
if (context.ExtendedProperties.ContainsKey(Resources.DefaultResourceGroupKey))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the context extensions to make this simpler, for example. context.IsProeprtySet, or context.SetProperty would make this simpler:

// If any parameters are specified, show only defaults with switch parameters set to true
if (ResourceGroup)
{
if (context.ExtendedProperties.ContainsKey(Resources.DefaultResourceGroupKey))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

{
if (!client.ResourceGroups.CheckExistence(ResourceGroupName))
{
ResourceGroup parameters = new ResourceGroup("West US");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a reason for a ShouldContinue prompt - we should not create a resource group without teeling the user we are going to create it. This would also mean needing a -Force prompt.

I also wonder whether it is appropriate to to this here, or if this should be done lazily when the default resource group is applied (I am thinking it should be done lazily)

/// Cmdlet to get default options.
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureRmDefault", DefaultParameterSetName = ResourceGroupParameterSet)]
[OutputType(typeof(ResourceGroup))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shoudl create a PSResourceGroup object that has the proeprtieswe want, rather than relying on the REST API class

Copy link
Member

@markcowl markcowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of changes, otherwise, thjis looks good

// If no parameters are specified, clear all defaults
if (!ResourceGroup)
{
if (Force.IsPresent || ShouldContinue(Resources.RemoveDefaultsMessage, Resources.RemoveDefaultsCaption))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be inside a ShouldProcess prompt, when ShouldContinue is used, the pattern is always:

if (ShouldProcess(...) {
  if (Force.IsPresent || ShouldContinue(...) {
    // do something

[Cmdlet(VerbsCommon.Clear, "AzureRmDefault", DefaultParameterSetName = ResourceGroupParameterSet,
SupportsShouldProcess = true)]
[OutputType(typeof(bool))]
public class ClearAzureRMDefaultCommand : AzureRMCmdlet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want this to extend ContextModificationCmdlet, so that it will be able to write to the persisted scope

[Cmdlet(VerbsCommon.Set, "AzureRmDefault", DefaultParameterSetName = ResourceGroupNameParameterSet,
SupportsShouldProcess = true)]
[OutputType(typeof(PSResourceGroup))]
public class SetAzureRMDefaultCommand : AzureRMCmdlet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment, this needs to extend ContextModificationCmdlet, and changes to the context should be inside a ModifyContext call, see cmdlets liek Set-AzureRmContext for the pattern.

@maddieclayton
Copy link
Contributor Author

@maddieclayton maddieclayton merged commit 342cf64 into Azure:preview Oct 20, 2017
@maddieclayton maddieclayton deleted the Default branch October 30, 2017 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants