Skip to content

Commit

Permalink
Parse constant for optional operation paramter as well (#5162)
Browse files Browse the repository at this point in the history
  • Loading branch information
live1206 authored Nov 19, 2024
1 parent 9106ee0 commit bdfe268
Show file tree
Hide file tree
Showing 13 changed files with 1,584 additions and 223 deletions.
1 change: 0 additions & 1 deletion src/AutoRest.CSharp/Common/Output/Models/CmcRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Builders;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Output.Models.Requests;
using AutoRest.CSharp.Output.Models.Shared;
using AutoRest.CSharp.Output.Models.Types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private ReferenceOrConstant CreateReference(InputParameter requestParameter, Par
return (ReferenceOrConstant)_parameters[requestParameter.Name];
}

if (requestParameter is { Kind: InputOperationParameterKind.Constant, IsRequired: true })
if (requestParameter is { Kind: InputOperationParameterKind.Constant })
{
if (requestParameter.Type is InputLiteralType { Value: not null } literalType)
{
Expand Down
23 changes: 23 additions & 0 deletions test/TestProjects/MgmtTypeSpec/PrivateEndpointConnection.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,27 @@ interface PrivateLinks {
/** list private links on the given resource */
@clientName("GetAllPrivateLinkResources")
listByMongoCluster is ArmResourceListByParent<PrivateLinkResource>;

/**
* Starts the SAP Application Server Instance.
*/
start is ArmResourceActionAsync<
PrivateLinkResource,
{
@doc("SAP Application server instance start request body.")
@body
body?: StartRequest;
},
Foundations.OperationStatusResult
>;
}

/**
* Start SAP instance(s) request body.
*/
model StartRequest {
/**
* The boolean value indicates whether to start the virtual machines before starting the SAP instances.
*/
startVm?: boolean;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,75 @@ public static Pageable<MgmtTypeSpecPrivateLinkResourceData> GetAllPrivateLinkRes

return GetMockableMgmtTypeSpecResourceGroupResource(resourceGroupResource).GetAllPrivateLinkResources(cancellationToken);
}

/// <summary>
/// Starts the SAP Application Server Instance.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/MgmtTypeSpec/privateLinkResources/{privateLinkResourcenName}/start</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>PrivateLinks_Start</description>
/// </item>
/// <item>
/// <term>Default Api Version</term>
/// <description>2024-05-01</description>
/// </item>
/// </list>
/// <item>
/// <term>Mocking</term>
/// <description>To mock this method, please mock <see cref="MockableMgmtTypeSpecResourceGroupResource.StartPrivateLink(WaitUntil,string,StartContent,CancellationToken)"/> instead.</description>
/// </item>
/// </summary>
/// <param name="resourceGroupResource"> The <see cref="ResourceGroupResource" /> instance the method will execute against. </param>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="privateLinkResourcenName"> The name of the private link associated with the Azure resource. </param>
/// <param name="content"> SAP Application server instance start request body. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> <paramref name="privateLinkResourcenName"/> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="ArgumentNullException"> <paramref name="resourceGroupResource"/> or <paramref name="privateLinkResourcenName"/> is null. </exception>
public static async Task<ArmOperation<OperationStatusResult>> StartPrivateLinkAsync(this ResourceGroupResource resourceGroupResource, WaitUntil waitUntil, string privateLinkResourcenName, StartContent content = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(resourceGroupResource, nameof(resourceGroupResource));

return await GetMockableMgmtTypeSpecResourceGroupResource(resourceGroupResource).StartPrivateLinkAsync(waitUntil, privateLinkResourcenName, content, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Starts the SAP Application Server Instance.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/MgmtTypeSpec/privateLinkResources/{privateLinkResourcenName}/start</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>PrivateLinks_Start</description>
/// </item>
/// <item>
/// <term>Default Api Version</term>
/// <description>2024-05-01</description>
/// </item>
/// </list>
/// <item>
/// <term>Mocking</term>
/// <description>To mock this method, please mock <see cref="MockableMgmtTypeSpecResourceGroupResource.StartPrivateLink(WaitUntil,string,StartContent,CancellationToken)"/> instead.</description>
/// </item>
/// </summary>
/// <param name="resourceGroupResource"> The <see cref="ResourceGroupResource" /> instance the method will execute against. </param>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="privateLinkResourcenName"> The name of the private link associated with the Azure resource. </param>
/// <param name="content"> SAP Application server instance start request body. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> <paramref name="privateLinkResourcenName"/> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="ArgumentNullException"> <paramref name="resourceGroupResource"/> or <paramref name="privateLinkResourcenName"/> is null. </exception>
public static ArmOperation<OperationStatusResult> StartPrivateLink(this ResourceGroupResource resourceGroupResource, WaitUntil waitUntil, string privateLinkResourcenName, StartContent content = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(resourceGroupResource, nameof(resourceGroupResource));

return GetMockableMgmtTypeSpecResourceGroupResource(resourceGroupResource).StartPrivateLink(waitUntil, privateLinkResourcenName, content, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,93 @@ public virtual Pageable<MgmtTypeSpecPrivateLinkResourceData> GetAllPrivateLinkRe
HttpMessage NextPageRequest(int? pageSizeHint, string nextLink) => PrivateLinksRestClient.CreateGetAllPrivateLinkResourcesNextPageRequest(nextLink, Id.SubscriptionId, Id.ResourceGroupName);
return GeneratorPageableHelpers.CreatePageable(FirstPageRequest, NextPageRequest, e => MgmtTypeSpecPrivateLinkResourceData.DeserializeMgmtTypeSpecPrivateLinkResourceData(e), PrivateLinksClientDiagnostics, Pipeline, "MockableMgmtTypeSpecResourceGroupResource.GetAllPrivateLinkResources", "value", "nextLink", cancellationToken);
}

/// <summary>
/// Starts the SAP Application Server Instance.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/MgmtTypeSpec/privateLinkResources/{privateLinkResourcenName}/start</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>PrivateLinks_Start</description>
/// </item>
/// <item>
/// <term>Default Api Version</term>
/// <description>2024-05-01</description>
/// </item>
/// </list>
/// </summary>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="privateLinkResourcenName"> The name of the private link associated with the Azure resource. </param>
/// <param name="content"> SAP Application server instance start request body. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> <paramref name="privateLinkResourcenName"/> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="ArgumentNullException"> <paramref name="privateLinkResourcenName"/> is null. </exception>
public virtual async Task<ArmOperation<OperationStatusResult>> StartPrivateLinkAsync(WaitUntil waitUntil, string privateLinkResourcenName, StartContent content = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(privateLinkResourcenName, nameof(privateLinkResourcenName));

using var scope = PrivateLinksClientDiagnostics.CreateScope("MockableMgmtTypeSpecResourceGroupResource.StartPrivateLink");
scope.Start();
try
{
var response = await PrivateLinksRestClient.StartAsync(Id.SubscriptionId, Id.ResourceGroupName, privateLinkResourcenName, content, cancellationToken).ConfigureAwait(false);
var operation = new MgmtTypeSpecArmOperation<OperationStatusResult>(new OperationStatusResultOperationSource(), PrivateLinksClientDiagnostics, Pipeline, PrivateLinksRestClient.CreateStartRequest(Id.SubscriptionId, Id.ResourceGroupName, privateLinkResourcenName, content).Request, response, OperationFinalStateVia.Location);
if (waitUntil == WaitUntil.Completed)
await operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false);
return operation;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

/// <summary>
/// Starts the SAP Application Server Instance.
/// <list type="bullet">
/// <item>
/// <term>Request Path</term>
/// <description>/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/MgmtTypeSpec/privateLinkResources/{privateLinkResourcenName}/start</description>
/// </item>
/// <item>
/// <term>Operation Id</term>
/// <description>PrivateLinks_Start</description>
/// </item>
/// <item>
/// <term>Default Api Version</term>
/// <description>2024-05-01</description>
/// </item>
/// </list>
/// </summary>
/// <param name="waitUntil"> <see cref="WaitUntil.Completed"/> if the method should wait to return until the long-running operation has completed on the service; <see cref="WaitUntil.Started"/> if it should return after starting the operation. For more information on long-running operations, please see <see href="https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md"> Azure.Core Long-Running Operation samples</see>. </param>
/// <param name="privateLinkResourcenName"> The name of the private link associated with the Azure resource. </param>
/// <param name="content"> SAP Application server instance start request body. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> <paramref name="privateLinkResourcenName"/> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="ArgumentNullException"> <paramref name="privateLinkResourcenName"/> is null. </exception>
public virtual ArmOperation<OperationStatusResult> StartPrivateLink(WaitUntil waitUntil, string privateLinkResourcenName, StartContent content = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(privateLinkResourcenName, nameof(privateLinkResourcenName));

using var scope = PrivateLinksClientDiagnostics.CreateScope("MockableMgmtTypeSpecResourceGroupResource.StartPrivateLink");
scope.Start();
try
{
var response = PrivateLinksRestClient.Start(Id.SubscriptionId, Id.ResourceGroupName, privateLinkResourcenName, content, cancellationToken);
var operation = new MgmtTypeSpecArmOperation<OperationStatusResult>(new OperationStatusResultOperationSource(), PrivateLinksClientDiagnostics, Pipeline, PrivateLinksRestClient.CreateStartRequest(Id.SubscriptionId, Id.ResourceGroupName, privateLinkResourcenName, content).Request, response, OperationFinalStateVia.Location);
if (waitUntil == WaitUntil.Completed)
operation.WaitForCompletion(cancellationToken);
return operation;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using MgmtTypeSpec.Models;

namespace MgmtTypeSpec
{
internal class OperationStatusResultOperationSource : IOperationSource<OperationStatusResult>
{
OperationStatusResult IOperationSource<OperationStatusResult>.CreateResult(Response response, CancellationToken cancellationToken)
{
using var document = JsonDocument.Parse(response.ContentStream);
return OperationStatusResult.DeserializeOperationStatusResult(document.RootElement);
}

async ValueTask<OperationStatusResult> IOperationSource<OperationStatusResult>.CreateResultAsync(Response response, CancellationToken cancellationToken)
{
using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false);
return OperationStatusResult.DeserializeOperationStatusResult(document.RootElement);
}
}
}
Loading

0 comments on commit bdfe268

Please sign in to comment.