-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAmbientSubscription.cs
102 lines (81 loc) · 3.97 KB
/
AmbientSubscription.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using static System.Environment;
namespace Pulumi.AzureNextGen
{
public sealed class AmbientSubscription : IDisposable
{
private static AsyncLocal<AmbientSubscription?> Ambient { get; } = new AsyncLocal<AmbientSubscription?>();
private static Dictionary<string, Provider> ProviderCache { get; } = new Dictionary<string, Provider>();
private string SubscriptionId { get; }
private Provider AzureNextGenProvider { get; }
public static AmbientSubscription? Current => Ambient.Value;
public AmbientSubscription(string subscriptionId, bool disableArmsAuxiliaryMitigation = false)
{
if (subscriptionId == null)
throw new ArgumentNullException(nameof(subscriptionId));
if (string.IsNullOrWhiteSpace(subscriptionId))
throw new ArgumentException("subscriptionId cannot be empty", nameof(subscriptionId));
if (Ambient.Value != null)
throw new InvalidOperationException("There is already an ambient subscription in effect - nesting ambient subscriptions is not supported, you must close the previous subscription before trying to switch to a new one");
if (AmbientResourceGroup.Current != null)
throw new InvalidOperationException("There is an ambient resource group in effect - nesting an ambient subscription inside an ambient resource group is not supported");
SubscriptionId = subscriptionId;
lock (ProviderCache)
{
AzureNextGenProvider = ProviderCache.TryGetValue(subscriptionId, out var provider)
? provider
: ProviderCache[subscriptionId] = GetProvider(subscriptionId, disableArmsAuxiliaryMitigation);
}
Ambient.Value = this;
}
private static Provider GetProvider(string subscriptionId, bool disableArmsAuxiliaryMitigation)
{
// Mitigation for https://github.com/pulumi/pulumi/issues/6358
if (disableArmsAuxiliaryMitigation)
{
return new Provider($"azurenextgen-ambientsubscription-{subscriptionId}", new ProviderArgs { SubscriptionId = subscriptionId });
}
else
{
var auxiliaryTenantIds = (GetEnvironmentVariable("ARM_AUXILIARY_TENANT_IDS") ?? string.Empty)
.Split(';', StringSplitOptions.RemoveEmptyEntries)
.Select(id => id.Trim())
.ToImmutableArray();
return new Provider($"azurenextgen-ambientsubscription-{subscriptionId}", new ProviderArgs { SubscriptionId = subscriptionId, AuxiliaryTenantIds = auxiliaryTenantIds });
}
}
internal static ResourceTransformation TransformDelegate { get; } = Transform;
private static ResourceTransformationResult? Transform(ResourceTransformationArgs args)
{
var ambientSubscription = Ambient.Value;
if (ambientSubscription == null)
return null;
if (!args.Resource.IsAzureNextGenResource() || args.Resource is Provider)
return null;
if (args.Options?.Provider != null)
{
Log.Debug($"The Ambient Subscription `{ambientSubscription.SubscriptionId}` could not be applied as this resource already has a provider applied to it");
return null;
}
var options = args.Options.CloneFor(args.Resource);
options.Provider = ambientSubscription.AzureNextGenProvider;
return new ResourceTransformationResult(args.Args, options);
}
private bool Disposed { get; set; }
void IDisposable.Dispose()
{
if (Disposed)
return;
if (Ambient.Value == null)
throw new InvalidOperationException("Tried to Dispose an AmbientSubscription when there doesn't seem to be one - this state shouldn't be possible - please file a bug report with details of the code you wrote to get to this state?");
if (Ambient.Value != this)
throw new InvalidOperationException("Tried to Dispose an AmbientSubscription, but this doesn't seem to be the current ambient subscription - this state shouldn't be possible - please file a bug report with details of the code you wrote to get to this state?");
Ambient.Value = null;
Disposed = true;
}
}
}