Azure entitlement get and cancel operations (#121)

* Azure entitlement get and cancel operations

* Updated PC SDK version to 3.3.0

* Fixed the object name
This commit is contained in:
kaminasy 2023-03-27 10:28:11 -07:00 коммит произвёл GitHub
Родитель 3a44c23afd
Коммит 8f5dc73721
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 161 добавлений и 7 удалений

Просмотреть файл

@ -0,0 +1,59 @@
// -----------------------------------------------------------------------
// <copyright file="CancelAzureEntitlement.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.Store.PartnerCenter.Samples.AzureEntitlement
{
using Microsoft.Store.PartnerCenter.Models.Subscriptions;
/// <summary>
/// Cancel an Azure entitlement.
/// </summary>
public class CancelAzureEntitlement : BasePartnerScenario
{
/// <summary>
/// Initializes a new instance of the <see cref="CancelAzureEntitlement"/> class.
/// </summary>
/// <param name="context">The context.</param>
public CancelAzureEntitlement(IScenarioContext context) : base("Cancel an Azure entitlement", context)
{
}
/// <summary>
/// Runs the scenario logic. This is delegated to the implementing sub class.
/// </summary>
protected override void RunScenario()
{
var partnerOperations = this.Context.UserPartnerOperations;
var customerId = this.ObtainCustomerId("Enter the ID of the customer whom to retrieve their Subscription");
var subscriptionId = this.ObtainSubscriptionId(customerId, "Enter the Azure plan ID to retrieve");
var azureEntitlementId = this.ObtainAzureEntitlementId(customerId, subscriptionId, "Enter the Azure entitlement ID to retrieve");
var selectedCancellationReasonCode = this.Context.ConsoleHelper.ReadNonEmptyString(
"Enter the cancellation reason code (ex: compromise): ",
"Cancellation reason code can't be empty");
if (selectedCancellationReasonCode != "compromise")
{
this.Context.ConsoleHelper.Error("Entered cancellation reason code is not supported. Please enter valid cancellation reason code.");
}
var azureEntitlementCancellationRequestContent = new AzureEntitlementCancellationRequestContent
{
CancellationReason = selectedCancellationReasonCode
};
this.Context.ConsoleHelper.StartProgress("Canceling customer's Azure entitlement");
var azureEntitlement = partnerOperations.Customers.ById(customerId)
.Subscriptions.ById(subscriptionId)
.AzureEntitlements.ById(azureEntitlementId)
.Cancel(azureEntitlementCancellationRequestContent);
this.Context.ConsoleHelper.StopProgress();
this.Context.ConsoleHelper.WriteObject(azureEntitlement, "Customer's Azure entitlement");
}
}
}

Просмотреть файл

@ -0,0 +1,43 @@
// -----------------------------------------------------------------------
// <copyright file="GetAzureEntitlement.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.Store.PartnerCenter.Samples.AzureEntitlement
{
/// <summary>
/// Get an Azure entitlement.
/// </summary>
public class GetAzureEntitlement : BasePartnerScenario
{
/// <summary>
/// Initializes a new instance of the <see cref="GetAzureEntitlement"/> class.
/// </summary>
/// <param name="context">The context.</param>
public GetAzureEntitlement(IScenarioContext context) : base("Get an Azure entitlement", context)
{
}
/// <summary>
/// Runs the scenario logic. This is delegated to the implementing sub class.
/// </summary>
protected override void RunScenario()
{
var partnerOperations = this.Context.UserPartnerOperations;
var customerId = this.ObtainCustomerId("Enter the ID of the customer whom to retrieve their Subscription");
var subscriptionId = this.ObtainSubscriptionId(customerId, "Enter the Azure plan ID to retrieve");
var azureEntitlementId = this.ObtainAzureEntitlementId(customerId, subscriptionId, "Enter the Azure entitlement ID to retrieve");
this.Context.ConsoleHelper.StartProgress("Retrieving customer's Azure entitlement");
var azureEntitlement = partnerOperations.Customers.ById(customerId)
.Subscriptions.ById(subscriptionId)
.AzureEntitlements.ById(azureEntitlementId)
.Get();
this.Context.ConsoleHelper.StopProgress();
this.Context.ConsoleHelper.WriteObject(azureEntitlement, "Customer's Azure entitlement");
}
}
}

Просмотреть файл

@ -8,6 +8,7 @@ namespace Microsoft.Store.PartnerCenter.Samples
{
using System;
using System.Collections.Generic;
using System.Linq;
using ScenarioExecution;
/// <summary>
@ -455,6 +456,52 @@ namespace Microsoft.Store.PartnerCenter.Samples
return subscriptionId.Trim();
}
/// <summary>
/// Obtains the Azure entitlement identifier.
/// </summary>
/// <param name="customerId">The customer identifier.</param>
/// <param name="subscriptionId">The subscription identifier.</param>
/// <param name="promptMessage">The prompt message.</param>
/// <returns>The Azure entitlement ID.</returns>
protected string ObtainAzureEntitlementId(string customerId, string subscriptionId, string promptMessage = default)
{
var partnerOperations = this.Context.UserPartnerOperations;
var azureEntitlementId = default(string);
var usageRecords = partnerOperations.Customers.ById(customerId).Subscriptions.UsageRecords.Get();
var selectedSubscriptionId = usageRecords.Items.FirstOrDefault(i => i.ResourceName == "Azure plan").ResourceId;
if (!string.IsNullOrWhiteSpace(selectedSubscriptionId))
{
this.Context.ConsoleHelper.StartProgress("Retrieving customer's Azure entitlements.");
var azureEntitlements = partnerOperations.Customers.ById(customerId)
.Subscriptions.ById(selectedSubscriptionId)
.AzureEntitlements.Get();
this.Context.ConsoleHelper.StopProgress();
this.Context.ConsoleHelper.WriteObject(azureEntitlements, "Customer's Azure entitlements.");
if (azureEntitlements.TotalCount > 0)
{
Console.WriteLine();
azureEntitlementId = this.Context.ConsoleHelper.ReadNonEmptyString(
string.IsNullOrWhiteSpace(promptMessage)
? "Enter the Azure entitlement ID"
: promptMessage,
"Azure entitlement ID can't be empty");
}
else
{
Console.WriteLine("Azure Plan with Id {0} contains no Azure entitlements!", selectedSubscriptionId);
}
}
else
{
Console.WriteLine("Provided Azure Plan Id {0} is incorrect", subscriptionId);
}
return azureEntitlementId.Trim();
}
/// <summary>
/// Obtains the product SKU ID by asking the user to enter it after displaying customer subscribed SKUs.
/// </summary>

Просмотреть файл

@ -21,6 +21,7 @@ namespace Microsoft.Store.PartnerCenter.Samples
using Entitlements;
using IndirectModel;
using Invoice;
using AzureEntitlement;
using Microsoft.Store.PartnerCenter.Samples.SelfServePolicies;
using Models.Auditing;
using Models.Customers;
@ -378,6 +379,8 @@ namespace Microsoft.Store.PartnerCenter.Samples
new TransitionSubscription(context),
new GetSubscriptionTransitions(context),
new UpdateOverage(context),
new GetAzureEntitlement(context),
new CancelAzureEntitlement(context),
};
return new AggregatePartnerScenario("Subscription samples", subscriptionScenarios, context);

Просмотреть файл

@ -60,14 +60,14 @@
<Reference Include="Microsoft.Identity.Client, Version=4.31.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Identity.Client.4.31.0\lib\net461\Microsoft.Identity.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Store.PartnerCenter, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.2.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.dll</HintPath>
<Reference Include="Microsoft.Store.PartnerCenter, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.3.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Store.PartnerCenter.Extensions, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.2.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.Extensions.dll</HintPath>
<Reference Include="Microsoft.Store.PartnerCenter.Extensions, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.3.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.Extensions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Store.PartnerCenter.Models, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.2.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.Models.dll</HintPath>
<Reference Include="Microsoft.Store.PartnerCenter.Models, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Store.PartnerCenter.3.3.0\lib\netstandard2.0\Microsoft.Store.PartnerCenter.Models.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
@ -117,6 +117,8 @@
<Compile Include="Auditing\SearchAuditRecords.cs" />
<Compile Include="Auditing\SearchAuditRecordsByCustomerId.cs" />
<Compile Include="Auditing\SearchAuditRecordsByResourceType.cs" />
<Compile Include="AzureEntitlement\CancelAzureEntitlement.cs" />
<Compile Include="AzureEntitlement\GetAzureEntitlement.cs" />
<Compile Include="Carts\CheckoutCart.cs" />
<Compile Include="Carts\CreateCart.cs" />
<Compile Include="Carts\CreateCartAddonWithExistingSubscription.cs" />

Просмотреть файл

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Identity.Client" version="4.31.0" targetFramework="net461" />
<package id="Microsoft.Store.PartnerCenter" version="3.2.0" targetFramework="net461" />
<package id="Microsoft.Store.PartnerCenter" version="3.3.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net461" />
<package id="System.ComponentModel.Annotations" version="5.0.0" targetFramework="net461" />
<package id="System.Data.DataSetExtensions" version="4.5.0" targetFramework="net461" />