Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 1 addition & 9 deletions src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,7 @@ private void WriteCSProjFiles(Action<string, string> writeFile)
if (Configuration.MgmtTestConfiguration is not null)
{
var testCSProjContent = GetTestCSProj();
string testGenProjectFolder;
if (Configuration.MgmtTestConfiguration.OutputFolder is { } testGenProjectOutputFolder)
{
testGenProjectFolder = Path.Combine(testGenProjectOutputFolder, "../");
}
else
{
testGenProjectFolder = "../";
}
string testGenProjectFolder = "../";
Console.WriteLine(Path.Combine(testGenProjectFolder, $"{Configuration.Namespace}.Tests.csproj"));
writeFile(FormatPath(Path.Combine(testGenProjectFolder, $"{Configuration.Namespace}.Tests.csproj")), testCSProjContent);
}
Expand Down
39 changes: 17 additions & 22 deletions src/AutoRest.CSharp/Mgmt/AutoRest/MgmtTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,15 @@ public static async Task ExecuteAsync(GeneratedCodeWorkspace project)
// write samples if enabled
if (Configuration.MgmtTestConfiguration?.Sample ?? Configuration.GenerateSampleProject)
{
string sampleOutputFolder = GetSampleOutputFolder(SAMPLE_DEFAULT_OUTPUT_PATH);
foreach (var sampleProvider in MgmtContext.Library.SampleProviders.Value)
var sampleOutputFolder = GetSampleOutputFolder();
if (sampleOutputFolder is not null)
{
var sampleWriter = new CodeWriter();
new ExpressionTypeProviderWriter(sampleWriter, sampleProvider).Write();
project.AddGeneratedTestFile(Path.Combine(sampleOutputFolder, $"Samples/{sampleProvider.Type.Name}.cs"), sampleWriter.ToString());
foreach (var sampleProvider in MgmtContext.Library.SampleProviders.Value)
{
var sampleWriter = new CodeWriter();
new ExpressionTypeProviderWriter(sampleWriter, sampleProvider).Write();
project.AddGeneratedTestFile(Path.Combine(sampleOutputFolder, $"Samples/{sampleProvider.Type.Name}.cs"), sampleWriter.ToString());
}
}
}
}
Expand Down Expand Up @@ -336,25 +339,17 @@ private static void WriteSerialization(GeneratedCodeWorkspace project, TypeProvi
AddGeneratedFile(project, serializationFileName, serializerCodeWriter.ToString());
}

private const string SOURCE_DEFAULT_OUTPUT_PATH = $"/src/Generated";
private const string MOCK_TEST_DEFAULT_OUTPUT_PATH = "/tests/Generated";
private const string SAMPLE_DEFAULT_OUTPUT_PATH = "/samples/Generated";

private static string GetSampleOutputFolder(string defaultOutputPath)
private static string? GetSampleOutputFolder()
{
if (!string.IsNullOrEmpty(Configuration.MgmtTestConfiguration?.OutputFolder))
return Configuration.MgmtTestConfiguration.OutputFolder;

string folder = FormatPath(Configuration.OutputFolder);
// if the output folder is not given explicitly, try to figure it out from general output folder if possible according to default folder structure:
// Azure.ResourceManager.XXX \ src \ Generated <- default sdk source output folder
// \ samples(or tests) \ Generated <- default sample output folder defined in msbuild
if (folder.EndsWith(SOURCE_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase))
return Path.Combine(folder, $"../../{defaultOutputPath}");
else if (folder.EndsWith(SAMPLE_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase) || folder.EndsWith(MOCK_TEST_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase))
return folder;
else
throw new InvalidOperationException("'sample-gen.output-folder' is not configured and can't figure it out from give general output-folder");
// we find the `src` part and change it to tests
var index = folder.IndexOf("/src/", StringComparison.InvariantCultureIgnoreCase);
if (index >= 0)
{
return folder.Replace("/src/", "/tests/", StringComparison.InvariantCultureIgnoreCase);
}
// indicates we cannot figure out an output folder for samples, we will not generate samples
return null;
}

private static string FormatPath(string? path)
Expand Down
12 changes: 0 additions & 12 deletions src/AutoRest.CSharp/Mgmt/AutoRest/MgmtTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using AutoRest.CSharp.AutoRest.Communication;
Expand All @@ -17,7 +16,6 @@ internal class MgmtTestConfiguration
private const string TestGenOptionsFormat = $"{TestGenOptionsRoot}.{{0}}";

public string? SourceCodePath { get; }
public string? OutputFolder { get; }
public bool Sample { get; }
public IReadOnlyList<string> SkippedOperations { get; }
public bool ClearOutputFolder { get; }
Expand All @@ -26,13 +24,11 @@ public MgmtTestConfiguration(
IReadOnlyList<string> skippedOperations,
JsonElement? sourceCodePath = default,
JsonElement? sample = default,
JsonElement? outputFolder = default,
JsonElement? clearOutputFolder = default)
{
SkippedOperations = skippedOperations;
SourceCodePath = !Configuration.IsValidJsonElement(sourceCodePath) ? null : sourceCodePath.ToString();
Sample = Configuration.DeserializeBoolean(sample, true);
OutputFolder = !Configuration.IsValidJsonElement(outputFolder) ? null : Configuration.TrimFileSuffix(outputFolder.ToString() ?? "");
ClearOutputFolder = Configuration.DeserializeBoolean(clearOutputFolder, false);
}

Expand All @@ -46,7 +42,6 @@ public MgmtTestConfiguration(
testGenRoot.TryGetProperty(Options.SkippedOperations, out var skippedOperationsElement);
testGenRoot.TryGetProperty(Options.SourcePath, out var sourceCodePath);
testGenRoot.TryGetProperty(Options.Sample, out var sample);
testGenRoot.TryGetProperty(Options.OutputFolder, out var testGenOutputFolder);
testGenRoot.TryGetProperty(Options.ClearOutputFolder, out var testGenClearOutputFolder);

var skippedOperations = Configuration.DeserializeArray(skippedOperationsElement);
Expand All @@ -55,7 +50,6 @@ public MgmtTestConfiguration(
skippedOperations,
sourceCodePath: sourceCodePath,
sample: sample,
outputFolder: testGenOutputFolder,
clearOutputFolder: testGenClearOutputFolder);
}

Expand All @@ -68,7 +62,6 @@ public MgmtTestConfiguration(
skippedOperations: autoRest.GetValue<string[]?>(string.Format(TestGenOptionsFormat, "skipped-operations")).GetAwaiter().GetResult() ?? Array.Empty<string>(),
sourceCodePath: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "source-path")).GetAwaiter().GetResult(),
sample: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "sample")).GetAwaiter().GetResult(),
outputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "output-folder")).GetAwaiter().GetResult(),
clearOutputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "clear-output-folder")).GetAwaiter().GetResult());
}

Expand All @@ -80,8 +73,6 @@ private static class Options

internal const string Sample = "sample";

internal const string OutputFolder = "output-folder";

internal const string ClearOutputFolder = "clear-output-folder";
}

Expand All @@ -97,9 +88,6 @@ internal void SaveConfiguration(Utf8JsonWriter writer)
if (Sample)
writer.WriteBoolean(Options.Sample, Sample);

if (OutputFolder is not null)
writer.WriteString(Options.OutputFolder, Path.GetRelativePath(Configuration.OutputFolder, OutputFolder));

if (!ClearOutputFolder)
writer.WriteBoolean(Options.ClearOutputFolder, ClearOutputFolder);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
Comment thread
ArcturusZhang marked this conversation as resolved.
Outdated
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using NUnit.Framework;

namespace MgmtMockAndSample.Samples
{
public partial class Sample_DeletedManagedHsmCollection
{
[Test]
[Ignore("Only validating compilation of examples")]
public async Task Get_RetrieveADeletedManagedHSM()
{
// Generated from example definition:
// this example is just showing the usage of "ManagedHsms_GetDeleted" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this SubscriptionResource created on azure
// for more information of creating SubscriptionResource, please refer to the document of SubscriptionResource
string subscriptionId = "00000000-0000-0000-0000-000000000000";
ResourceIdentifier subscriptionResourceId = SubscriptionResource.CreateResourceIdentifier(subscriptionId);
SubscriptionResource subscriptionResource = client.GetSubscriptionResource(subscriptionResourceId);

// get the collection of this DeletedManagedHsmResource
DeletedManagedHsmCollection collection = subscriptionResource.GetDeletedManagedHsms();

// invoke the operation
AzureLocation location = new AzureLocation("westus");
string name = "hsm1";
DeletedManagedHsmResource result = await collection.GetAsync(location, name);

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
DeletedManagedHsmData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task Exists_RetrieveADeletedManagedHSM()
{
// Generated from example definition:
// this example is just showing the usage of "ManagedHsms_GetDeleted" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this SubscriptionResource created on azure
// for more information of creating SubscriptionResource, please refer to the document of SubscriptionResource
string subscriptionId = "00000000-0000-0000-0000-000000000000";
ResourceIdentifier subscriptionResourceId = SubscriptionResource.CreateResourceIdentifier(subscriptionId);
SubscriptionResource subscriptionResource = client.GetSubscriptionResource(subscriptionResourceId);

// get the collection of this DeletedManagedHsmResource
DeletedManagedHsmCollection collection = subscriptionResource.GetDeletedManagedHsms();

// invoke the operation
AzureLocation location = new AzureLocation("westus");
string name = "hsm1";
bool result = await collection.ExistsAsync(location, name);

Console.WriteLine($"Succeeded: {result}");
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task GetIfExists_RetrieveADeletedManagedHSM()
{
// Generated from example definition:
// this example is just showing the usage of "ManagedHsms_GetDeleted" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this SubscriptionResource created on azure
// for more information of creating SubscriptionResource, please refer to the document of SubscriptionResource
string subscriptionId = "00000000-0000-0000-0000-000000000000";
ResourceIdentifier subscriptionResourceId = SubscriptionResource.CreateResourceIdentifier(subscriptionId);
SubscriptionResource subscriptionResource = client.GetSubscriptionResource(subscriptionResourceId);

// get the collection of this DeletedManagedHsmResource
DeletedManagedHsmCollection collection = subscriptionResource.GetDeletedManagedHsms();

// invoke the operation
AzureLocation location = new AzureLocation("westus");
string name = "hsm1";
NullableResponse<DeletedManagedHsmResource> response = await collection.GetIfExistsAsync(location, name);
DeletedManagedHsmResource result = response.HasValue ? response.Value : null;

if (result == null)
{
Console.WriteLine("Succeeded with null as result");
}
else
{
// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
DeletedManagedHsmData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using NUnit.Framework;

namespace MgmtMockAndSample.Samples
{
public partial class Sample_DeletedManagedHsmResource
{
[Test]
[Ignore("Only validating compilation of examples")]
public async Task Get_RetrieveADeletedManagedHSM()
{
// Generated from example definition:
// this example is just showing the usage of "ManagedHsms_GetDeleted" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this DeletedManagedHsmResource created on azure
// for more information of creating DeletedManagedHsmResource, please refer to the document of DeletedManagedHsmResource
string subscriptionId = "00000000-0000-0000-0000-000000000000";
AzureLocation location = new AzureLocation("westus");
string name = "hsm1";
ResourceIdentifier deletedManagedHsmResourceId = DeletedManagedHsmResource.CreateResourceIdentifier(subscriptionId, location, name);
DeletedManagedHsmResource deletedManagedHsm = client.GetDeletedManagedHsmResource(deletedManagedHsmResourceId);

// invoke the operation
DeletedManagedHsmResource result = await deletedManagedHsm.GetAsync();

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
DeletedManagedHsmData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task PurgeDeleted_PurgeAManagedHSMPool()
{
// Generated from example definition:
// this example is just showing the usage of "ManagedHsms_PurgeDeleted" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this DeletedManagedHsmResource created on azure
// for more information of creating DeletedManagedHsmResource, please refer to the document of DeletedManagedHsmResource
string subscriptionId = "00000000-0000-0000-0000-000000000000";
AzureLocation location = new AzureLocation("westus");
string name = "hsm1";
ResourceIdentifier deletedManagedHsmResourceId = DeletedManagedHsmResource.CreateResourceIdentifier(subscriptionId, location, name);
DeletedManagedHsmResource deletedManagedHsm = client.GetDeletedManagedHsmResource(deletedManagedHsmResourceId);

// invoke the operation
await deletedManagedHsm.PurgeDeletedAsync(WaitUntil.Completed);

Console.WriteLine("Succeeded");
}
}
}
Loading