Skip to content

New Powershell commands for Subscription RP #12077

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 11 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -41,5 +41,12 @@ public void TestNewSubscription()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewSubscription");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestUpdateSubscription()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Test-UpdateSubscription");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function Test-NewSubscription
{
# $accounts = Get-AzEnrollmentAccount
$accounts = @(@{ ObjectId = "cdf813b6-bdc2-4df5-b150-00ccfd7580e2" })
$billingAccountId = "d6fd151e-2f30-50d5-34b2-fe40b1d64b7f:31670802-3752-4741-b5cc-683c71eca69b_2019-05-31"
$billingProfileId = "4S2P-T44P-BG7-TGB"
$InvoiceSectionId = "74EQ-I4QH-PJA-TGB"
$skuId ="0001"
$customerId = "JIMI-YGZA-BG7-TGB"

# Verify the caller has at least one enrollment account.
Assert-True { $accounts.Count -gt 0 }
Expand All @@ -31,3 +36,12 @@ function Test-NewSubscription
Assert-AreEqual $myNewSubName $newSub.Name
Assert-NotNull $newSub.SubscriptionId
}

function Test-UpdateSubscription
{
$subId = "8f99ecea-4536-468e-9cce-ac18497a353d"

$updateSub = Update-AzSubscription -SubscriptionId $subId

Assert-NotNull updateSub.SubscriptionId
}
67 changes: 59 additions & 8 deletions src/Subscription/Subscription/Cmdlets/NewAzureRmSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ public ISubscriptionClient SubscriptionClient
[Parameter(Mandatory = true, HelpMessage = "Name of the enrollment account to use when creating the subscription.")]
public string EnrollmentAccountObjectId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the billing account to use when creating the subscription.")]
public string BillingAccountId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the billing profile to use when creating the subscription.")]
public string BillingProfileId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the invoice section to use when creating the subscription.")]
public string InvoiceSectionId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Management Group Id.")]
public string ManagementGroupId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "SkuId of the subscription.")]
[PSArgumentCompleter("0001", "0002")]
public string SkuId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the customer to use when creating the subscription.")]
public string CustomerId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Reseller Id of the subscription")]
public string ResellerId { get; set; }

[Parameter(Mandatory = false, Position = 0, HelpMessage = "Name of the subscription. When not specified, a name will be generated based on the specified offer type.")]
public string Name { get; set; }

Expand All @@ -86,19 +108,48 @@ public ISubscriptionClient SubscriptionClient
[Alias("OwnerSPN", "OwnerServicePrincipalName")]
public string[] OwnerApplicationId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Type of the subscription.")]
[PSArgumentCompleter("Modern", "Csp")]
public string SubscriptionType { get; set; }

public override void ExecuteCmdlet()
{
if (this.ShouldProcess(target: this.Name, action: "Create subscription"))
{
var owners = this.ResolveObjectIds(this.OwnerObjectId, this.OwnerApplicationId, this.OwnerSignInName).Select(id => new AdPrincipal() { ObjectId = id }).ToArray();

// Create the subscription.
var result = this.SubscriptionClient.SubscriptionFactory.CreateSubscriptionInEnrollmentAccount(EnrollmentAccountObjectId, new SubscriptionCreationParameters()
SubscriptionCreationResult result = new SubscriptionCreationResult();
if (!string.IsNullOrWhiteSpace(EnrollmentAccountObjectId))
{
var owners = this
.ResolveObjectIds(this.OwnerObjectId, this.OwnerApplicationId, this.OwnerSignInName)
.Select(id => new AdPrincipal() {ObjectId = id}).ToArray();

// Create the subscription.
result = this.SubscriptionClient.Subscription.CreateSubscriptionInEnrollmentAccount(
EnrollmentAccountObjectId, new SubscriptionCreationParameters()
{
DisplayName = this.Name,
OfferType = this.OfferType,
Owners = owners
});
}
else if (SubscriptionType == "Modern")
{
DisplayName = this.Name,
OfferType = this.OfferType,
Owners = owners
});
result = this.SubscriptionClient.Subscription.CreateSubscription(BillingAccountId, BillingProfileId, InvoiceSectionId, new ModernSubscriptionCreationParameters()
{
DisplayName = this.Name,
SkuId = this.SkuId,
ManagementGroupId = this.ManagementGroupId
});
}
else if(SubscriptionType == "Csp")
{
result = this.SubscriptionClient.Subscription.CreateCspSubscription(BillingAccountId, CustomerId, new ModernCspSubscriptionCreationParameters()
{
DisplayName = this.Name,
SkuId = this.SkuId,
ResellerId = this.ResellerId
});
}

// Write output.
var createdSubscription = new AzureSubscription()
Expand Down
84 changes: 84 additions & 0 deletions src/Subscription/Subscription/Cmdlets/UpdateAzureRmSubscription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.Subscription.Models;
using Microsoft.Azure.Management.Subscription;
using Microsoft.Azure.Management.Subscription.Models;

namespace Microsoft.Azure.Commands.Subscription.Cmdlets
{
[Cmdlet("Update", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Subscription", SupportsShouldProcess =
true), OutputType(typeof(PSAzureSubscription))]
public class UpdateAzureRmSubscription : AzureRmLongRunningCmdlet
{
private ISubscriptionClient _subscriptionClient;

/// <summary>
/// Gets or sets the subscription client.
/// </summary>
public ISubscriptionClient SubscriptionClient
{
get
{
return _subscriptionClient ??
(_subscriptionClient =
AzureSession.Instance.ClientFactory.CreateArmClient<SubscriptionClient>(DefaultContext,
AzureEnvironment.Endpoint.ResourceManager));
}
set { _subscriptionClient = value; }
}

[Parameter(Mandatory = true, HelpMessage = "Subscription Id to update")]
public string SubscriptionId { get; set; }

[Parameter(Mandatory = true, HelpMessage = "Action to perform on subscription")]
[PSArgumentCompleter("Cancel", "Enable", "Rename")]
public string Action { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Name of the subscription")]
public string Name { get; set; }

public override void ExecuteCmdlet()
{
if (this.ShouldProcess(target: this.SubscriptionId, action: "Update subscription"))
{
// Updates the subscription.

if (Action == "Cancel")
{
var result = this.SubscriptionClient.Subscription.Cancel(SubscriptionId);
WriteObject(result);
}
else if (Action == "Enable")
{
var result = this.SubscriptionClient.Subscription.Enable(SubscriptionId);
WriteObject(result);
}
else
{
var result = this.SubscriptionClient.Subscription.Rename(SubscriptionId, new SubscriptionName()
{
SubscriptionNameProperty = Name
});
WriteObject(result);
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Subscription/Subscription/Subscription.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Subscription" Version="1.1.1-preview" />
<PackageReference Include="Microsoft.Azure.Management.Subscription" Version="1.1.4-preview" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/Subscription/Subscription/help/Az.Subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ The topics in this section document the Azure PowerShell cmdlets for Azure Subsc
### [New-AzSubscription](New-AzSubscription.md)
Creates an Azure subscription.

## Az.Subscription Cmdlets
### [New-AzModernSubscription](New-AzModernSubscription.md)
Creates Modern Azure subscription.

## Az.Subscription Cmdlets
### [New-AzCspSubscription](New-AzCspSubscription.md)
Creates Csp Azure subscription.

## Az.Subscription Cmdlets
### [Update-AzSubscription](Update-AzSubscription.md)
Updates an Azure subscription.