Skip to content
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

Implement mgmt resource #47944

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
f041f24
wip
live1206 Jan 16, 2025
badf1a5
wip
live1206 Jan 20, 2025
69fa28a
wip
live1206 Jan 21, 2025
615fec1
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Jan 21, 2025
d136be2
wip
live1206 Jan 23, 2025
a040627
wip
live1206 Jan 26, 2025
0296a71
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Jan 26, 2025
fda752a
generate operation methods for Resource
live1206 Jan 27, 2025
580508a
add package back
live1206 Jan 27, 2025
ba12225
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 6, 2025
ef83882
update to latest MGC
live1206 Feb 6, 2025
120e164
fix test
live1206 Feb 6, 2025
e4bbde0
Merge branch 'main' into implement-mgmt-resource
live1206 Feb 7, 2025
b2842c4
Add test for ResourceProvider
live1206 Feb 8, 2025
5f5cee9
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 8, 2025
41cf53c
Add RequestPath
live1206 Feb 8, 2025
7bba967
remove unused property
live1206 Feb 10, 2025
5d3a879
address comments
live1206 Feb 11, 2025
2fe508f
Include custom code for convenience methods
live1206 Feb 11, 2025
d9fd368
add TODO
live1206 Feb 11, 2025
17b2a0f
merge from main
live1206 Feb 13, 2025
45a10a7
implement resource LRO
live1206 Feb 13, 2025
b06fb59
implement LRO for resource
live1206 Feb 14, 2025
c98e231
Implement IOperationSouce and generated MgmtTypeSpec can build now
live1206 Feb 14, 2025
848e6d7
update test data
live1206 Feb 14, 2025
0db68cb
Add test for OperationSourceProvider
live1206 Feb 14, 2025
2b15650
update tests
live1206 Feb 14, 2025
51bd7f3
cleanup
live1206 Feb 14, 2025
a8b2b8f
update
live1206 Feb 14, 2025
dc2b83d
cleanup
live1206 Feb 14, 2025
adbcc1c
Address comments
live1206 Feb 15, 2025
bb92673
Add Mgmt-TypeSpec generation to Generate.ps1
live1206 Feb 15, 2025
af9bd9c
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 18, 2025
c5ca437
Update eng/packages/http-client-csharp/generator/Azure.Generator/src/…
live1206 Feb 19, 2025
3ed339d
update
live1206 Feb 19, 2025
d800d35
Merge branch 'main' into implement-mgmt-resource
live1206 Feb 24, 2025
8d78f7f
fix build
live1206 Feb 24, 2025
8fbc8aa
update to use CanonicalView
live1206 Feb 24, 2025
081696d
update test
live1206 Feb 24, 2025
f9e8afd
Use ModelSerializationExtensions.WireOptions instead of new instance
live1206 Feb 24, 2025
8d7b831
update test
live1206 Feb 24, 2025
4dcccc7
Address comments
live1206 Feb 25, 2025
d00ee3a
typo
live1206 Feb 25, 2025
0086b11
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 25, 2025
e34db7b
resolve comments
live1206 Feb 26, 2025
417af63
fix build
live1206 Feb 26, 2025
965e951
update test
live1206 Feb 27, 2025
579dd9b
separate tests for ResourceClientProvider
live1206 Feb 27, 2025
682e704
Add TODO
live1206 Feb 27, 2025
22e08c6
fix tests
live1206 Feb 28, 2025
541193c
Move resource related logic to ResourceBuilder
live1206 Feb 28, 2025
20cdaaf
Remove clientProvider and implement client methods in Resource directly
live1206 Mar 1, 2025
78b5cb5
update
live1206 Mar 1, 2025
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
1 change: 1 addition & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<PackageReference Update="ApprovalTests" Version="3.0.22" />
<PackageReference Update="ApprovalUtilities" Version="3.0.22" />
<PackageReference Update="Azure.Core" Version="1.44.1" />
<PackageReference Update="Azure.ResourceManager" Version="1.13.0" />
<PackageReference Update="Azure.Identity" Version="1.13.1" />
<PackageReference Update="Azure.Messaging.EventGrid" Version="4.17.0" />
<PackageReference Update="Azure.Messaging.EventHubs.Processor" Version="5.11.6" />
Expand Down
29 changes: 28 additions & 1 deletion eng/packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ if (-not $LaunchOnly) {
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'

$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Basic-TypeSpec"
$unbrandedTypespecTestProject = $unbrandedTypespecTestProject

Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Basic-TypeSpec.tsp" $unbrandedTypespecTestProject -forceNewProject $ForceNewProject)

Expand All @@ -36,6 +35,28 @@ if (-not $LaunchOnly) {
exit $LASTEXITCODE
}
}

if ($null -eq $filter -or $filter -eq "Mgmt-TypeSpec") {
Write-Host "Generating MgmtTypeSpec" -ForegroundColor Cyan
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'

$mgmtTypespecTestProject = Join-Path $testProjectsLocalDir "Mgmt-TypeSpec"

Invoke (Get-TspCommand "$mgmtTypespecTestProject/main.tsp" $mgmtTypespecTestProject -forceNewProject $ForceNewProject)

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

Write-Host "Building MgmtTypeSpec" -ForegroundColor Cyan
Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Mgmt-TypeSpec/src/MgmtTypeSpec.csproj"

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
}

$specsDirectory = "$packageRoot/node_modules/@typespec/http-specs"
Expand Down Expand Up @@ -174,13 +195,19 @@ if ($null -eq $filter) {
$mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
$sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe"
$unbrandedSpec = "TestProjects/Local/Basic-TypeSpec"
$mgmtSpec = "TestProjects/Local/Mgmt-TypeSpec"

$launchSettings = @{}
$launchSettings.Add("profiles", @{})
$launchSettings["profiles"].Add("Basic-TypeSpec", @{})
$launchSettings["profiles"]["Basic-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.dll `$(SolutionDir)/$unbrandedSpec -p AzureClientPlugin")
$launchSettings["profiles"]["Basic-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Basic-TypeSpec"].Add("executablePath", "dotnet")

$launchSettings["profiles"].Add("Mgmt-TypeSpec", @{})
$launchSettings["profiles"]["Mgmt-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.dll `$(SolutionDir)/$mgmtSpec -p AzureClientPlugin")
$launchSettings["profiles"]["Mgmt-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Mgmt-TypeSpec"].Add("executablePath", "dotnet")

foreach ($kvp in $spectorLaunchProjects.GetEnumerator()) {
$launchSettings["profiles"].Add($kvp.Key, @{})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.ResourceManager;
using Azure.Generator.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.TypeSpec.Generator;
Expand Down Expand Up @@ -30,6 +31,9 @@ public class AzureClientPlugin : ClientModelPlugin
public override AzureOutputLibrary OutputLibrary => _azureOutputLibrary ??= new();

internal ResourceDetection ResourceDetection { get; } = new();
internal ParentDetection ParentDetection { get; } = new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't your parent just a property on the client from typespec?

Copy link
Member Author

@live1206 live1206 Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For TypeSpec, we should be able to get the resource hierarchy directly via #48281.
The resource detection logic is mainly for swagger input.
For now, given getArmResources API is not ready for integration. We use it as a temporary workaround.

Eventually, when we have the native way ready, we should get rid of them. And the resource detection should be part of the M4 output conversion to tspCodeModel.json in autorest.csharp.
Added TODO to remove them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As offline discussed, we need to clarify the contract of input types containing the resource hierarchy. And it should be an extensible way of holding it in Azure plugin only.
Then, we can put the temp workaround of resource detection close to the input instead of output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isolated Resource related logic in ResourceBuilder, so we have separated the temp workaround to a central place.

internal ScopeDetection ScopeDetection { get; } = new();
internal SingletonDetection SingletonDetection { get; } = new();

/// <summary>
/// The Azure client plugin to generate the Azure client SDK.
Expand All @@ -55,6 +59,8 @@ public override void Configure()
AddVisitor(new NamespaceVisitor());
if (IsAzureArm.Value)
{
// Include Azure.ResourceManager
AddMetadataReference(MetadataReference.CreateFromFile(typeof(ArmClient).Assembly.Location));
AddVisitor(new RestClientVisitor());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,56 @@
using Azure.Generator.Providers;
using Azure.Generator.Utilities;
using Microsoft.TypeSpec.Generator.ClientModel;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Providers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Azure.Generator
{
/// <inheritdoc/>
public class AzureOutputLibrary : ScmOutputLibrary
{
// TODO: categorize clients into operationSets, which contains operations sharing the same Path
private Dictionary<string, OperationSet> _pathToOperationSetMap;
private Dictionary<string, HashSet<OperationSet>> _resourceDataBySpecNameMap;
//TODO: Move these to InputLibrary instead
private Dictionary<RequestPath, OperationSet> _pathToOperationSetMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of code in here that is mgmt specific and it starts to get difficult to tell which is which have we considered a mgmt plugin?

Copy link
Member Author

@live1206 live1206 Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely.
Given I am fully working with MPG now, I think it would be better to split them when we have more implementation on DPG, then we can clarify the things in common for both.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#48481 to track the split, it will happen soon after this PR.

private Dictionary<string, HashSet<OperationSet>> _specNameToOperationSetsMap;
private Dictionary<string, InputModelType> _inputTypeMap;

/// <inheritdoc/>
public AzureOutputLibrary()
{
_pathToOperationSetMap = CategorizeClients();
_resourceDataBySpecNameMap = EnsureResourceDataMap();
_specNameToOperationSetsMap = EnsureOperationsetMap();
_inputTypeMap = AzureClientPlugin.Instance.InputLibrary.InputNamespace.Models.OfType<InputModelType>().ToDictionary(model => model.Name);
}

private Dictionary<string, HashSet<OperationSet>> EnsureResourceDataMap()
private MgmtLongRunningOperationProvider? _armOperation;
internal MgmtLongRunningOperationProvider ArmOperation => _armOperation ??= new MgmtLongRunningOperationProvider(false);

private MgmtLongRunningOperationProvider? _genericArmOperation;
internal MgmtLongRunningOperationProvider GenericArmOperation => _genericArmOperation ??= new MgmtLongRunningOperationProvider(true);

private IReadOnlyList<ResourceProvider> BuildResources()
{
var result = new List<ResourceProvider>();
foreach ((var schemaName, var operationSets) in _specNameToOperationSetsMap)
{
var model = _inputTypeMap[schemaName];
var resourceData = (ResourceDataProvider)AzureClientPlugin.Instance.TypeFactory.CreateModel(model)!;
foreach (var operationSet in operationSets)
{
var requestPath = operationSet.RequestPath;
var resourceType = ResourceDetection.GetResourceTypeFromPath(requestPath);
var resource = new ResourceProvider(operationSet, schemaName, resourceData, resourceType);
AzureClientPlugin.Instance.AddTypeToKeep(resource.Name);
result.Add(resource);
}
}
return result;
}

private Dictionary<string, HashSet<OperationSet>> EnsureOperationsetMap()
{
var result = new Dictionary<string, HashSet<OperationSet>>();
foreach (var operationSet in _pathToOperationSetMap.Values)
Expand All @@ -44,16 +74,14 @@ private Dictionary<string, HashSet<OperationSet>> EnsureResourceDataMap()
return result;
}

private Dictionary<string, OperationSet> CategorizeClients()
private Dictionary<RequestPath, OperationSet> CategorizeClients()
{
var result = new Dictionary<string, OperationSet>();
var result = new Dictionary<RequestPath, OperationSet>();
foreach (var inputClient in AzureClientPlugin.Instance.InputLibrary.InputNamespace.Clients)
{
var requestPathList = new HashSet<string>();
foreach (var operation in inputClient.Operations)
{
var path = operation.GetHttpPath();
requestPathList.Add(path);
if (result.TryGetValue(path, out var operationSet))
{
operationSet.Add(operation);
Expand All @@ -75,22 +103,20 @@ private Dictionary<string, OperationSet> CategorizeClients()
}

/// <inheritdoc/>
// TODO: generate resources and collections
// TODO: generate collections
protected override TypeProvider[] BuildTypeProviders()
{
var baseProviders = base.BuildTypeProviders();
if (AzureClientPlugin.Instance.IsAzureArm.Value == true)
{
var armOperation = new MgmtLongRunningOperationProvider(false);
var genericArmOperation = new MgmtLongRunningOperationProvider(true);

// TODO: remove them once they are referenced in Resource operation implementation
AzureClientPlugin.Instance.AddTypeToKeep(armOperation.Name);
AzureClientPlugin.Instance.AddTypeToKeep(genericArmOperation.Name);
return [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition(), armOperation, genericArmOperation];
var resources = BuildResources();
return [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition(), ArmOperation, GenericArmOperation, .. resources, .. resources.Select(r => r.Source)];
}
return [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition()];
}

internal bool IsResource(string name) => _resourceDataBySpecNameMap.ContainsKey(name);
internal bool IsResource(string name) => _specNameToOperationSetsMap.ContainsKey(name);

internal Lazy<IEnumerable<OperationSet>> ResourceOperationSets => new(() => _specNameToOperationSetsMap.Values.SelectMany(x => x));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace Azure.Generator.Mgmt.Models
/// </summary>
internal class OperationSet : IReadOnlyCollection<InputOperation>, IEquatable<OperationSet>
{
private readonly InputClient? _inputClient;

/// <summary>
/// The raw request path of string of the operations in this <see cref="OperationSet"/>
/// </summary>
public string RequestPath { get; }
public RequestPath RequestPath { get; }

public InputClient InputClient { get; }

/// <summary>
/// The operation set
Expand All @@ -31,9 +31,9 @@ internal class OperationSet : IReadOnlyCollection<InputOperation>, IEquatable<Op

public int Count => _operations.Count;

public OperationSet(string requestPath, InputClient? inputClient)
public OperationSet(RequestPath requestPath, InputClient inputClient)
{
_inputClient = inputClient;
InputClient = inputClient;
RequestPath = requestPath;
_operations = new HashSet<InputOperation>();
}
Expand All @@ -46,7 +46,7 @@ public OperationSet(string requestPath, InputClient? inputClient)
public void Add(InputOperation operation)
{
var path = operation.GetHttpPath();
if (path != RequestPath)
if (!path.Equals(RequestPath))
throw new InvalidOperationException($"Cannot add operation with path {path} to OperationSet with path {RequestPath}");
_operations.Add(operation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Azure.Generator.Mgmt.Models
{
internal class RequestPath : IEquatable<RequestPath>, IReadOnlyList<string>
{
private const string ProviderPath = "/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}";
private const string FeaturePath = "/subscriptions/{subscriptionId}/providers/Microsoft.Features/providers/{resourceProviderNamespace}/features";

public const string ManagementGroupScopePrefix = "/providers/Microsoft.Management/managementGroups";
public const string ResourceGroupScopePrefix = "/subscriptions/{subscriptionId}/resourceGroups";
public const string SubscriptionScopePrefix = "/subscriptions";
public const string TenantScopePrefix = "/tenants";
public const string Providers = "/providers";

public static readonly RequestPath ManagementGroup = new("/providers/Microsoft.Management/managementGroups/{managementGroupId}");
public static readonly RequestPath ResourceGroup = new("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
public static readonly RequestPath Subscription = new("/subscriptions/{subscriptionId}");
public static readonly RequestPath Tenant = new(string.Empty);

private string _path;
private IReadOnlyList<string> _segments;

public RequestPath(string path)
{
_path = path;
_segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
IndexOfLastProviders = _path.LastIndexOf(Providers);
}

public RequestPath(IEnumerable<string> segments)
{
_segments = segments.ToArray();
_path = string.Join("", _segments);
}

public int Count => _segments.Count;

public string SerializedPath => _path;

public int IndexOfLastProviders { get; }

public string this[int index] => _segments[index];

/// <summary>
/// Check if this <see cref="RequestPath"/> is a prefix path of the other request path.
/// Note that this.IsAncestorOf(this) will return false which indicates that this method is testing the "proper ancestor" like a proper subset.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool IsAncestorOf(RequestPath other)
{
// To be the parent of other, you must at least be shorter than other.
if (other.Count <= Count)
return false;
for (int i = 0; i < Count; i++)
{
// we need the segment to be identical when strict is true (which is the default value)
// when strict is false, we also need the segment to be identical if it is constant.
// but if it is a reference, we only require they have the same type, do not require they have the same variable name.
// This case happens a lot during the management group parent detection - different RP calls this different things
if (!this[i].Equals(other[i]))
return false;
}
return true;
}

/// <summary>
/// Trim this from the other and return the <see cref="RequestPath"/>that remain.
/// The result is "other - this" by removing this as a prefix of other.
/// If this == other, return empty request path
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">if this.IsAncestorOf(other) is false</exception>
public RequestPath TrimAncestorFrom(RequestPath other)
{
if (TryTrimAncestorFrom(other, out var diff))
return diff;

throw new InvalidOperationException($"Request path {this} is not parent of {other}");
}

private bool TryTrimAncestorFrom(RequestPath other, [MaybeNullWhen(false)] out RequestPath diff)
{
diff = default;
if (this == other)
{
diff = Tenant;
return true;
}
if (IsAncestorOf(other))
{
diff = new RequestPath(string.Join("", other._segments.Skip(Count)));
return true;
}
// Handle the special case of trim provider from feature
else if (_path == ProviderPath && other._path.StartsWith(FeaturePath))
{
diff = new RequestPath(string.Join("", other._segments.Skip(Count + 2)));
return true;
}
return false;
}

public bool Equals(RequestPath? other)
{
if (Count != other?.Count)
return false;
for (int i = 0; i < Count; i++)
{
if (!this[i].Equals(other[i]))
return false;
}
return true;
}

public override bool Equals(object? obj) => obj is RequestPath other && Equals(other);

public override int GetHashCode() => _path.GetHashCode();

public override string ToString() => _path;

public IEnumerator<String> GetEnumerator() => _segments.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => _segments.GetEnumerator();

public static bool operator ==(RequestPath left, RequestPath right)
{
return left.Equals(right);
}

public static bool operator !=(RequestPath left, RequestPath right)
{
return !(left == right);
}

public static implicit operator string(RequestPath requestPath)
{
return requestPath._path;
}

public static bool IsSegmentConstant(string segment)
{
var trimmed = segment.TrimStart('{').TrimEnd('}');
var isScope = trimmed == "scope";
return !isScope && !segment.StartsWith('{');
}
}
}
Loading
Loading