pulumi/sdk/dotnet/Pulumi/Serialization/ResourcePackages.cs
Pat Gavlin 3d2e31289a
Add support for serialized resource references. (#5041)
Resources are serialized as their URN, ID, and package version. Each
Pulumi package is expected to register itself with the SDK. The package
will be invoked to construct appropriate instances of rehydrated
resources. Packages are distinguished by their name and their version.

This is the foundation of cross-process resources.

Related to #2430.

Co-authored-by: Mikhail Shilkov <github@mikhail.io>
Co-authored-by: Luke Hoban <luke@pulumi.com>
Co-authored-by: Levi Blackstone <levi@pulumi.com>
2020-10-27 10:12:12 -07:00

42 lines
1.4 KiB
C#

// Copyright 2016-2019, Pulumi Corporation
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
namespace Pulumi
{
public interface IResourcePackage
{
Resource Construct(string name, string type, IDictionary<string, object?>? args, string urn);
ProviderResource ConstructProvider(string name, string type, IDictionary<string, object?>? args, string urn);
}
internal static class ResourcePackages
{
internal static ConcurrentDictionary<string, IResourcePackage> _resourcePackages = new ConcurrentDictionary<string, IResourcePackage>();
private static string PackageKey(string name, string version)
{
return $"{name}@{version}";
}
internal static bool TryGetResourcePackage(string name, string version, [NotNullWhen(true)] out IResourcePackage? package)
{
return _resourcePackages.TryGetValue(PackageKey(name, version), out package);
}
public static void RegisterResourcePackage(string name, string version, IResourcePackage package)
{
var key = PackageKey(name, version);
if (!_resourcePackages.TryAdd(key, package))
{
throw new InvalidOperationException($"Cannot re-register package {key}.");
}
}
}
}