Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
2ca81d9
move mgmt test classes into mgmt
ArcturusZhang Oct 10, 2024
2c3d949
some in progress changes
ArcturusZhang Oct 11, 2024
30fe506
wip, now the new provider could generate those methods
ArcturusZhang Oct 11, 2024
1553bc9
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 11, 2024
066cd2d
follow up
ArcturusZhang Oct 11, 2024
3a03f71
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 14, 2024
6f25ddb
a few progress on creating id for a resource
ArcturusZhang Oct 14, 2024
d0d5df2
a few update
ArcturusZhang Oct 14, 2024
d209e5a
add get resource statement
ArcturusZhang Oct 14, 2024
1ac3fe2
add implementation for normal operations
ArcturusZhang Oct 16, 2024
36685a3
implmeent some bodies
ArcturusZhang Oct 16, 2024
834618d
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 16, 2024
ad43b46
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 17, 2024
403e9ab
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 17, 2024
77d3ebf
some more progress
ArcturusZhang Oct 17, 2024
ce40019
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Oct 21, 2024
64824ce
fix the scope issue
ArcturusZhang Oct 21, 2024
33bfeca
fix another scope path issue
ArcturusZhang Oct 21, 2024
f88e7c1
enable generation for collections
ArcturusZhang Oct 21, 2024
e0efd6e
implement part of the resource collection methods
ArcturusZhang Oct 21, 2024
fdeb1b0
fix inline parameter issue
ArcturusZhang Oct 21, 2024
ee9c3d8
some updates with issues
ArcturusZhang Oct 21, 2024
123c66c
fix the scope issue
ArcturusZhang Oct 22, 2024
12efc8e
fix isuses in mgmttestconfiguration
ArcturusZhang Oct 23, 2024
f1273f0
fix casing and empty lines
ArcturusZhang Oct 23, 2024
4576226
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 31, 2024
9cfffd1
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Oct 31, 2024
48f7f2f
honor the skipped operation config
ArcturusZhang Oct 31, 2024
3894a9b
regen
ArcturusZhang Nov 4, 2024
2cf792d
regen
ArcturusZhang Nov 4, 2024
2931a53
implement the extension operation samples
ArcturusZhang Nov 4, 2024
010f4cf
implement the property bag part 1
ArcturusZhang Nov 5, 2024
8468e13
implement the property bag part 2
ArcturusZhang Nov 5, 2024
600b4d0
remove the configureawait part
ArcturusZhang Nov 5, 2024
d1d9e0d
fix the null value issue in code model converter
ArcturusZhang Nov 5, 2024
e65f290
fix a case in resourcemanager
ArcturusZhang Nov 5, 2024
f5c2441
fix some issues found in real rps
ArcturusZhang Nov 5, 2024
e17b225
find another case that has configureawait
ArcturusZhang Nov 5, 2024
7936159
remove useless code
ArcturusZhang Nov 5, 2024
7461fff
more removals
ArcturusZhang Nov 5, 2024
c032d67
adjust namespaces
ArcturusZhang Nov 5, 2024
441dde0
clean up
ArcturusZhang Nov 5, 2024
c323542
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 6, 2024
c5704f9
refactor
ArcturusZhang Nov 6, 2024
fbf2968
more refactor
ArcturusZhang Nov 6, 2024
bf49918
regen
ArcturusZhang Nov 6, 2024
ad7549e
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Nov 11, 2024
9d7e62c
fix some more issues
ArcturusZhang Nov 11, 2024
97762f3
Merge remote-tracking branch 'origin/main' into refactor-mgmt-sample-…
ArcturusZhang Nov 12, 2024
855d575
fix userassignedidentity
ArcturusZhang Nov 12, 2024
8180473
add some notes
ArcturusZhang Nov 12, 2024
5027003
fix the optional parameter issue and regen
ArcturusZhang Nov 13, 2024
f1210ae
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 13, 2024
9ce9211
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 15, 2024
75098c2
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 21, 2024
c36dc84
fix the armclient issue
ArcturusZhang Nov 21, 2024
642a3bd
fix some minor issues in code
ArcturusZhang Nov 21, 2024
0754dde
regen
ArcturusZhang Nov 21, 2024
0e8aac7
resolve comments
ArcturusZhang Nov 22, 2024
f861682
refine
ArcturusZhang Nov 22, 2024
1f13f5b
Merge branch 'main' into refactor-mgmt-sample-generator
ArcturusZhang Nov 25, 2024
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
Prev Previous commit
Next Next commit
follow up
  • Loading branch information
ArcturusZhang committed Oct 11, 2024
commit 066cd2d14161efa7a4c0cca0aae53355a3024c7f
4 changes: 0 additions & 4 deletions src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(CodeModel codeModel)
var inputNamespace = new CodeModelConverter(codeModel, schemaUsageProvider).CreateNamespace();
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(inputNamespace, sourceInputModel));
await MgmtTarget.ExecuteAsync(project);
//if (Configuration.MgmtTestConfiguration is not null && !Configuration.MgmtConfiguration.MgmtDebug.ReportOnly)
// await MgmtTestTarget.ExecuteAsync(project, inputNamespace, sourceInputModel);
GenerateMgmtReport(project);
}
else
Expand Down Expand Up @@ -88,8 +86,6 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(InputNamespace rootNamesp
InputNamespaceTransformer.Transform(rootNamespace);
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(rootNamespace, sourceInputModel));
await MgmtTarget.ExecuteAsync(project);
//if (Configuration.GenerateSampleProject)
// await MgmtTestTarget.ExecuteAsync(project, rootNamespace, sourceInputModel);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ internal record SingleLineCommentStatement(FormattableString Message) : MethodBo
{
public SingleLineCommentStatement(string message) : this(FormattableStringHelpers.FromString(message))
{ }

public override void Write(CodeWriter writer)
{
writer.Line($"// {Message}");
}
}
82 changes: 81 additions & 1 deletion src/AutoRest.CSharp/Mgmt/Output/Samples/NewMgmtSampleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Expressions.Statements;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Common.Output.Models;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Input.Source;
using AutoRest.CSharp.MgmtTest.Models;
using AutoRest.CSharp.Output.Models;
using AutoRest.CSharp.Output.Models.Shared;
using AutoRest.CSharp.Output.Models.Types;
using Azure.Core;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using NUnit.Framework;
using static AutoRest.CSharp.Common.Output.Models.Snippets;

Expand All @@ -29,13 +37,85 @@ public NewMgmtSampleProvider(string defaultNamespace, MgmtTypeProvider client, S

protected override string DefaultName { get; }

protected override IEnumerable<string> BuildUsings()
{
yield return "Azure.Identity"; // we need this using because we might need to call `new DefaultAzureCredential` from `Azure.Identity` package, but Azure.Identity package is not a dependency of the generator project.
}

private Method BuildSampleMethod(MgmtOperationSample sample, bool isAsync)
{
var signature = sample.GetMethodSignature(false);

return new Method(signature, EmptyStatement);
return new Method(signature, BuildSampleMethodBody(sample, isAsync));
}

private MethodBodyStatement BuildSampleMethodBody(MgmtOperationSample sample, bool isAsync)
{
var statements = new List<MethodBodyStatement>()
{
new SingleLineCommentStatement($"Generated from example definition: {sample.ExampleFilepath}"),
new SingleLineCommentStatement($"this example is just showing the usage of \"{sample.OperationId}\" operation, for the dependent resource, they will have to be created separately."),
EmptyLine,
new SingleLineCommentStatement("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"),
Declare(typeof(TokenCredential), "cred", new FormattableStringToExpression($"new DefaultAzureCredential()"), out var cred), // this project does not use Azure.Identity as a dependency, therefore this is the only way to write this.
new SingleLineCommentStatement("authenticate your client"),
Declare(typeof(ArmClient), "client", New.Instance(typeof(ArmClient), cred), out var client),
EmptyLine,
sample.Carrier switch
{
Resource resource => BuildSampleOperationForResource(client, resource, sample),
_ => throw new InvalidOperationException("This should never happen")
},
};

return statements;
}

private MethodBodyStatement BuildSampleOperationForResource(ValueExpression client, Resource resource, MgmtOperationSample sample)
{
var resourceName = GetResourceName(resource);
var statements = new List<MethodBodyStatement>()
{
new SingleLineCommentStatement($"this example assumes you already have this {resourceName} created on azure"),
new SingleLineCommentStatement($"for more information of creating {resourceName}, please refer to the document of {resourceName}"),
BuildGetResourceStatement(resource, sample, client)
};

return statements;
}

private MethodBodyStatement BuildGetResourceStatement(MgmtTypeProvider carrierResource, OperationExample example, ValueExpression client)
=> carrierResource switch
{
ResourceCollection => throw new InvalidOperationException($"ResourceCollection is not supported here"),
Resource parentResource => BuildGetResourceStatementFromResource(parentResource, example, client),
//MgmtExtension parentExtension => WriteGetResourceStatementFromExtension(parentExtension, example, client),
_ => throw new InvalidOperationException($"Unknown parent {carrierResource.GetType()}"),
};

private MethodBodyStatement BuildGetResourceStatementFromResource(Resource carrierResource, OperationExample example, ValueExpression client)
{
// Can't use CSharpType.Equals(typeof(...)) because the CSharpType.Equals(Type) would assume itself is a FrameworkType, but here it's generated when IsArmCore=true
if (Configuration.MgmtConfiguration.IsArmCore && carrierResource.Type.Name == nameof(TenantResource))
{
//return BuildGetResourceStatementFromTenantResource(carrierResource, example, client);
throw new InvalidOperationException("WIP");
}
else
{
var id = new VariableReference(typeof(ResourceIdentifier), "id");
// WIP here
return EmptyLine;
}
}

private string GetResourceName(MgmtTypeProvider provider) => provider switch
{
Resource resource => resource.Type.Name,
MgmtExtension extension => extension.ArmCoreType.Name,
_ => throw new InvalidOperationException("Should never happen")
};

protected override IEnumerable<Method> BuildMethods()
{
foreach (var sample in _client.AllOperations.SelectMany(o => o.Samples))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#nullable disable

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

namespace MgmtTypeSpec.Samples
Expand All @@ -16,30 +19,80 @@ public partial class Sample_FooResource
[Ignore("Only validating compilation of examples")]
public async Task Get_GetAFoo()
{
// Generated from example definition: 2024-05-01/Foos_Get.json
// this example is just showing the usage of "Foos_Get" operation, for the dependent resource, 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 FooResource created on azure
// for more information of creating FooResource, please refer to the document of FooResource
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task Update_CreateAFoo()
{
// Generated from example definition: 2024-05-01/Foos_CreateOrUpdate.json
// this example is just showing the usage of "Foos_CreateOrUpdate" operation, for the dependent resource, 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 FooResource created on azure
// for more information of creating FooResource, please refer to the document of FooResource
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task AddTag_GetAFoo()
{
// Generated from example definition: 2024-05-01/Foos_Get.json
// this example is just showing the usage of "Foos_Get" operation, for the dependent resource, 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 FooResource created on azure
// for more information of creating FooResource, please refer to the document of FooResource
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task SetTags_GetAFoo()
{
// Generated from example definition: 2024-05-01/Foos_Get.json
// this example is just showing the usage of "Foos_Get" operation, for the dependent resource, 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 FooResource created on azure
// for more information of creating FooResource, please refer to the document of FooResource
}

[Test]
[Ignore("Only validating compilation of examples")]
public async Task RemoveTag_GetAFoo()
{
// Generated from example definition: 2024-05-01/Foos_Get.json
// this example is just showing the usage of "Foos_Get" operation, for the dependent resource, 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 FooResource created on azure
// for more information of creating FooResource, please refer to the document of FooResource
}
}
}