Skip to content

Commit

Permalink
Implemented select-azuresubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Tavares committed Sep 25, 2013
1 parent 238c98f commit 289e10a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ public WindowsAzureSubscription CurrentSubscription
{
get
{
if (currentSubscription == null)
{
currentSubscription = DefaultSubscription;
}
return currentSubscription;
return currentSubscription ?? DefaultSubscription;
}

set { currentSubscription = value; }
Expand Down Expand Up @@ -204,8 +200,9 @@ public void UpdateSubscription(WindowsAzureSubscription s)
{
if (!subscriptions.Contains(s))
{
throw new ArgumentException(string.Format(Resources.CannotUpdateUnknownSubscription, s.Name,
s.SubscriptionId));
throw new ArgumentException(
string.Format(Resources.CannotUpdateUnknownSubscription,
s.Name, s.SubscriptionId));
}

if (s.IsDefault)
Expand Down Expand Up @@ -245,7 +242,7 @@ public void ImportPublishSettings(Stream stream)
}
}

if (DefaultSubscription == null && subscriptions.Count > 0)
if (DefaultSubscription == null && subscriptions.Count > 0)
{
subscriptions[0].IsDefault = true;
}
Expand Down Expand Up @@ -335,5 +332,15 @@ private void LoadSubscriptionData(ProfileData data)
}
}
}

private void GuardKnownSubscription(WindowsAzureSubscription s)
{
if (!subscriptions.Contains(s))
{
throw new ArgumentException(
string.Format(Resources.CannotUpdateUnknownSubscription,
s.Name,s.SubscriptionId));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,95 @@

namespace Microsoft.WindowsAzure.Commands.Subscription
{
using System;
using System.Linq;
using System.Management.Automation;
using Utilities.Common;
[Cmdlet(VerbsCommon.Select, "AzureSubscription")]
using Utilities.Properties;

[Cmdlet(VerbsCommon.Select, "AzureSubscription", DefaultParameterSetName = "Current")]
[OutputType(typeof(bool))]
public class SelectAzureSubscriptionCommand : CmdletWithSubscriptionBase
{

[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Current", HelpMessage = "Name of subscription to select")]
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Default", HelpMessage = "Name of subscription to select")]
[ValidateNotNullOrEmpty]
public string SubscriptionName { get; set; }

[Parameter(Mandatory = false, ParameterSetName = "Current", HelpMessage = "Switch to set the chosen subscription as the current one")]
public SwitchParameter Current { get; set; }

[Parameter(Mandatory = true, ParameterSetName = "Default", HelpMessage = "Switch to set the chosen subscription as the default one")]
public SwitchParameter Default { get; set; }

[Parameter(Mandatory = true, ParameterSetName = "NoCurrent", HelpMessage = "Switch to clear the current subscription")]
public SwitchParameter NoCurrent { get; set; }

[Parameter(Mandatory = true, ParameterSetName = "NoDefault", HelpMessage = "Switch to clear the default subscription")]
public SwitchParameter NoDefault { get; set; }

[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
{
switch (ParameterSetName)
{
case "Current":
SetCurrent();
break;

case "Default":
SetDefault();
break;

case "NoCurrent":
ClearCurrent();
break;

case "NoDefault":
ClearDefault();
break;
}

if (PassThru.IsPresent)
{
WriteObject(true);
}
}

public void SetCurrent()
{
Profile.CurrentSubscription = FindNamedSubscription();
}

public void SetDefault()
{
var newDefault = FindNamedSubscription();
newDefault.IsDefault = true;
Profile.UpdateSubscription(newDefault);
}

public void ClearCurrent()
{
Profile.CurrentSubscription = null;
}

public void ClearDefault()
{
var defaultSubscription = Profile.DefaultSubscription;
defaultSubscription.IsDefault = false;
Profile.UpdateSubscription(defaultSubscription);
}

private WindowsAzureSubscription FindNamedSubscription()
{
var subscription = Profile.Subscriptions.FirstOrDefault(s => s.Name == SubscriptionName);
if (subscription == null)
{
throw new Exception(string.Format(Resources.InvalidSubscription, SubscriptionName));
}
return subscription;
}
}
}

0 comments on commit 289e10a

Please sign in to comment.