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

[Local extensibility] Expose new classes #14697

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Bicep.Cli.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Cli.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
56 changes: 52 additions & 4 deletions src/Bicep.Cli/Commands/LocalDeployCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Immutable;
using System.Web.Services.Description;
using Azure.Deployments.Core.Json;
using Bicep.Cli.Arguments;
using Bicep.Cli.Helpers;
Expand All @@ -12,12 +14,20 @@
using Bicep.Core.TypeSystem.Types;
using Bicep.Local.Deploy;
using Bicep.Local.Deploy.Extensibility;
using Bicep.Local.Extension.Protocol;
using Bicep.Local.Extension.Rpc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Bicep.Cli.Commands;

public class LocalDeployExtensionFactory : ILocalExtensionFactory
{
public Task<ILocalExtension> CreateLocalExtensionAsync(LocalExtensionKey extensionKey, Uri extensionBinaryUri)
=> GrpcProxyLocalExtension.CreateGrpcExtension(extensionBinaryUri);
}

public class LocalDeployCommand : ICommand
{
private readonly IModuleDispatcher moduleDispatcher;
Expand Down Expand Up @@ -65,16 +75,54 @@ parameters.Parameters is not { } parametersString ||
return 1;
}

await using LocalExtensibilityHostManager extensibilityHandler = new(moduleDispatcher, GrpcBuiltInLocalExtension.Start);
await extensibilityHandler.InitializeExtensions(compilation);
var serviceProvider = RegisterLocalDeployServices();
var localExtensionHostManager = serviceProvider.GetService<LocalExtensionHostManager>();
var localDeploy = serviceProvider.GetService<LocalDeploy>();

var result = await LocalDeployment.Deploy(extensibilityHandler, templateString, parametersString, cancellationToken);
if (localExtensionHostManager is null)
{
throw new ArgumentNullException(nameof(localExtensionHostManager));
}

if (localDeploy is null)
{
throw new ArgumentNullException(nameof(localDeploy));
}

localExtensionHostManager.InitializeLocalExtensions(GetBinaryExtensions(compilation).ToImmutableList());

var result = await localDeploy.Deploy(templateString, parametersString, cancellationToken);

await WriteSummary(result);
return 0;
}

private async Task WriteSummary(LocalDeployment.Result result)
private ServiceProvider RegisterLocalDeployServices()
{
var service = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
service.RegisterLocalDeployServices();
service.AddSingleton<ILocalExtensionFactory, LocalDeployExtensionFactory>();
return service.BuildServiceProvider();
}

private IEnumerable<BinaryExtensionReference> GetBinaryExtensions(Compilation compilation)
{
var namespaceTypes = compilation.GetAllBicepModels()
.Select(x => x.Root.NamespaceResolver)
.SelectMany(x => x.GetNamespaceNames().Select(x.TryGetNamespace))
.WhereNotNull();

foreach (var namespaceType in namespaceTypes)
{
if (namespaceType.Artifact is { } artifact &&
moduleDispatcher.TryGetProviderBinary(artifact) is { } binaryUri)
{
yield return new(namespaceType, binaryUri);
}
}
}

private async Task WriteSummary(LocalDeploy.Result result)
{
if (result.Deployment.Properties.Outputs is { } outputs)
{
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Cli/Helpers/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.Utils;
using Bicep.Decompiler;
using Bicep.Local.Deploy.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using Environment = Bicep.Core.Utils.Environment;
using IOFileSystem = System.IO.Abstractions.FileSystem;
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Cli/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core.Samples/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Decompiler.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Decompiler.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.LangServer.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.LangServer.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.LangServer/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
46 changes: 39 additions & 7 deletions src/Bicep.Local.Deploy.IntegrationTests/EndToEndDeploymentTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Immutable;
using System.IO.Abstractions;
using System.Text;
using System.Text.Json.Nodes;
using Azure.Deployments.Core.Definitions;
using Azure.Deployments.Core.Extensions;
using Azure.Deployments.Engine.Host.Azure.ExtensibilityV2.Contract.Models;
using Azure.Deployments.Extensibility.Core.V2.Models;
using Azure.Deployments.Extensibility.Messages;
using Bicep.Core.Configuration;
using Bicep.Core.Features;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
using Bicep.Core.Semantics;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Features;
Expand All @@ -21,6 +24,8 @@
using Bicep.Local.Deploy.Extensibility;
using Bicep.Local.Extension;
using FluentAssertions;
using Microsoft.Azure.Deployments.Service.Shared.Jobs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.ResourceStack.Common.Json;
using Moq;
Expand Down Expand Up @@ -101,7 +106,7 @@ param coords {
{ "namespace", "someNamespace" }
};

var providerMock = StrictMock.Of<LocalExtensibilityHost>();
var providerMock = StrictMock.Of<Extension.Protocol.ILocalExtension>();
providerMock.Setup(x => x.CreateOrUpdate(It.Is<ResourceSpecification>(req => req.Properties["uri"]!.ToString() == "https://api.weather.gov/points/47.6363726,-122.1357068"), It.IsAny<CancellationToken>()))
.Returns<ResourceSpecification, CancellationToken>((req, _) =>
{
Expand All @@ -114,7 +119,7 @@ param coords {
}
}
""";
return Task.FromResult(new LocalExtensibilityOperationResponse(new Resource(req.Type, req.ApiVersion, identifiers, req.Properties, "Succeeded"), null));
return Task.FromResult(new Extension.Protocol.LocalExtensionOperationResponse(new Resource(req.Type, req.ApiVersion, identifiers, req.Properties, "Succeeded"), null));
});

providerMock.Setup(x => x.CreateOrUpdate(It.Is<ResourceSpecification>(req => req.Properties["uri"]!.ToString() == "https://api.weather.gov/gridpoints/SEW/131,68/forecast"), It.IsAny<CancellationToken>()))
Expand All @@ -138,14 +143,24 @@ param coords {
}
}
""";
return Task.FromResult(new LocalExtensibilityOperationResponse(new Resource(req.Type, req.ApiVersion, identifiers, req.Properties, "Succeeded"), null));
return Task.FromResult(new Extension.Protocol.LocalExtensionOperationResponse(new Resource(req.Type, req.ApiVersion, identifiers, req.Properties, "Succeeded"), null));
});

var dispatcher = BicepTestConstants.CreateModuleDispatcher(services.Build().Construct<IServiceProvider>());
await using LocalExtensibilityHostManager extensibilityHandler = new(dispatcher, uri => Task.FromResult(providerMock.Object));
await extensibilityHandler.InitializeExtensions(result.Compilation);
var localExtensionFactory = StrictMock.Of<ILocalExtensionFactory>();
localExtensionFactory
.Setup(x => x.CreateLocalExtensionAsync(It.IsAny<LocalExtensionKey>(), It.IsAny<Uri>()))
.ReturnsAsync(providerMock.Object);

var localDeployResult = await LocalDeployment.Deploy(extensibilityHandler, templateFile, parametersFile, TestContext.CancellationTokenSource.Token);
var moduleDispatcher = BicepTestConstants.CreateModuleDispatcher(services.Build().Construct<IServiceProvider>());

var extensionFactoryManager = new LocalExtensionFactoryManager(localExtensionFactory.Object);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most of the code remained the same, one of the biggest differences is the fact that there is an interface to create extension instances - ILocalExtensionFactory which is meant for clients to implement it - see LocalDeployCommand.cs and the other difference if LocalExtensionFactoryManager this class is in charge of initializing local extensions and load them on-demand and only once.

var extensionHostManager = new LocalExtensionHostManager(
extensionFactoryManager,
new LocalExtensionHost(extensionFactoryManager));

extensionHostManager.InitializeLocalExtensions(GetBinaryExtensions(result.Compilation, moduleDispatcher).ToImmutableList());

var localDeployResult = await LocalDeployment.Deploy(extensionHostManager, templateFile, parametersFile, TestContext.CancellationTokenSource.Token);

localDeployResult.Deployment.Properties.ProvisioningState.Should().Be(ProvisioningState.Succeeded);
localDeployResult.Deployment.Properties.Outputs["forecast"].Value.Should().DeepEqual(JToken.Parse("""
Expand All @@ -162,6 +177,23 @@ param coords {
"""));
}

private IEnumerable<BinaryExtensionReference> GetBinaryExtensions(Compilation compilation, IModuleDispatcher moduleDispatcher)
{
var namespaceTypes = compilation.GetAllBicepModels()
.Select(x => x.Root.NamespaceResolver)
.SelectMany(x => x.GetNamespaceNames().Select(x.TryGetNamespace))
.WhereNotNull();

foreach (var namespaceType in namespaceTypes)
{
if (namespaceType.Artifact is { } artifact &&
moduleDispatcher.TryGetProviderBinary(artifact) is { } binaryUri)
{
yield return new(namespaceType, binaryUri);
}
}
}

[TestMethod]
public async Task Provider_returning_resource_and_error_data_should_fail()
{
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Local.Deploy.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@
"Azure.Bicep.Local.Extension": {
"type": "Project",
"dependencies": {
"Azure.Deployments.Extensibility.Core": "[0.1.55, )",
"CommandLineParser": "[2.9.1, )",
"Google.Protobuf": "[3.27.1, )",
"Grpc.Net.Client": "[2.63.0, )"
Expand Down
Loading