forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertResourceHandler.cs
329 lines (296 loc) · 14.8 KB
/
InsertResourceHandler.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Deployments.Core.Comparers;
using Azure.Deployments.Core.Definitions.Identifiers;
using Bicep.Core.CodeAction;
using Bicep.Core.Diagnostics;
using Bicep.Core.Parsing;
using Bicep.Core.Resources;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem.Az;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Extensions;
using MediatR;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
using Bicep.Core.Extensions;
using Bicep.Core.PrettyPrint;
using Bicep.Core.PrettyPrint.Options;
using Bicep.Core.Workspaces;
using Bicep.LanguageServer.Utils;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol;
using Bicep.Core.Navigation;
using Bicep.Core.Rewriters;
using System.Text.RegularExpressions;
using Bicep.LanguageServer.Providers;
using Bicep.LanguageServer.Telemetry;
using System.Diagnostics;
namespace Bicep.LanguageServer.Handlers
{
[Method("textDocument/insertResource", Direction.ClientToServer)]
public record InsertResourceParams : TextDocumentPositionParams, IRequest
{
public string? ResourceId { get; init; }
}
public class InsertResourceHandler : IJsonRpcNotificationHandler<InsertResourceParams>
{
private readonly ILanguageServerFacade server;
private readonly ICompilationManager compilationManager;
private readonly IAzResourceProvider azResourceProvider;
private readonly IAzResourceTypeLoader azResourceTypeLoader;
private readonly TelemetryAndErrorHandlingHelper<Unit> helper;
public InsertResourceHandler(ILanguageServerFacade server, ICompilationManager compilationManager, IAzResourceProvider azResourceProvider, IAzResourceTypeLoader azResourceTypeLoader, ITelemetryProvider telemetryProvider)
{
this.server = server;
this.compilationManager = compilationManager;
this.azResourceProvider = azResourceProvider;
this.azResourceTypeLoader = azResourceTypeLoader;
this.helper = new TelemetryAndErrorHandlingHelper<Unit>(server.Window, telemetryProvider);
}
public Task<Unit> Handle(InsertResourceParams request, CancellationToken cancellationToken)
=> helper.ExecuteWithTelemetryAndErrorHandling(async () => {
var context = compilationManager.GetCompilation(request.TextDocument.Uri);
if (context is null)
{
return (Unit.Value, null);
}
var model = context.Compilation.GetEntrypointSemanticModel();
if (TryParseResourceId(request.ResourceId) is not { } resourceId)
{
throw helper.CreateException(
$"Failed to parse supplied resourceId \"{request.ResourceId}\".",
BicepTelemetryEvent.InsertResourceFailure("ParseResourceIdFailed"),
Unit.Value);
}
var matchedType = azResourceTypeLoader.GetAvailableTypes()
.Where(x => StringComparer.OrdinalIgnoreCase.Equals(resourceId.FullyQualifiedType, x.FormatType()))
.OrderByDescending(x => x.ApiVersion, ApiVersionComparer.Instance)
.FirstOrDefault();
if (matchedType is null || matchedType.ApiVersion is null)
{
throw helper.CreateException(
$"Failed to find a Bicep type definition for resource of type \"{resourceId.FullyQualifiedType}\".",
BicepTelemetryEvent.InsertResourceFailure($"MissingType({resourceId.FullyQualifiedType})"),
Unit.Value);
}
JsonElement? resource = null;
try
{
// First attempt a direct GET on the resource using the Bicep type API version.
resource = await azResourceProvider.GetGenericResource(
model.Configuration,
resourceId,
apiVersion: matchedType.ApiVersion,
cancellationToken);
}
catch (Exception exception)
{
// We want to keep going here - we'll try again without the API version.
Trace.WriteLine($"Failed to fetch resource '{resourceId}' with API version {matchedType.ApiVersion}: {exception}");
}
try
{
// If the direct GET fails, attempt to look it up without the API version.
// This will use the latest version from the /providers/<provider> API.
if (resource is null)
{
resource = await azResourceProvider.GetGenericResource(
model.Configuration,
resourceId,
apiVersion: null,
cancellationToken);
}
}
catch (Exception exception)
{
Trace.WriteLine($"Failed to fetch resource '{resourceId}' without API version: {exception}");
throw helper.CreateException(
$"Caught exception fetching resource: {exception.Message}.",
BicepTelemetryEvent.InsertResourceFailure($"FetchResourceFailure"),
Unit.Value);
}
var resourceDeclaration = CreateResourceSyntax(resource.Value, resourceId, matchedType);
var insertContext = GetInsertContext(context, request.Position);
var replacement = GenerateCodeReplacement(context.Compilation, resourceDeclaration, insertContext);
await server.Workspace.ApplyWorkspaceEdit(new ApplyWorkspaceEditParams
{
Edit = new()
{
Changes = new Dictionary<DocumentUri, IEnumerable<TextEdit>>
{
[request.TextDocument.Uri] = new[] {
new TextEdit
{
Range = replacement.ToRange(context.LineStarts),
NewText = replacement.Text,
},
},
},
},
}, cancellationToken);
return (Unit.Value, BicepTelemetryEvent.InsertResourceSuccess(resourceId.FullyQualifiedType, matchedType.ApiVersion));
});
private record InsertContext(
bool StartWithNewline,
bool EndWithNewline,
int InsertOffset);
private static InsertContext GetInsertContext(CompilationContext context, Position requestPosition)
{
var cursorOffset = PositionHelper.GetOffset(context.LineStarts, requestPosition);
var binder = context.Compilation.GetEntrypointSemanticModel().Binder;
var node = context.ProgramSyntax.TryFindMostSpecificNodeInclusive(cursorOffset, n => binder.GetParent(n) is ProgramSyntax);
if (node is null)
{
return new(true, false, context.ProgramSyntax.GetEndPosition());
}
var programChildren = context.ProgramSyntax.Children;
var index = programChildren.IndexOf(node);
var nextNode = index < (programChildren.Length - 1) ? programChildren[index + 1] : null;
var startNewline = node is not Token { Type: TokenType.NewLine };
var endNewline = nextNode is not null and not Token { Type: TokenType.NewLine };
var insertOffset = node.GetEndPosition();
return new(startNewline, endNewline, insertOffset);
}
private static CodeReplacement GenerateCodeReplacement(Compilation prevCompilation, ResourceDeclarationSyntax resourceDeclaration, InsertContext insertContext)
{
// Create a new document containing the resource to insert.
// This allows us to apply syntax rewriters and formatting, before generating the code replacement.
var printOptions = new PrettyPrintOptions(NewlineOption.LF, IndentKindOption.Space, 2, false);
var program = new ProgramSyntax(
new[] { resourceDeclaration },
SyntaxFactory.CreateToken(TokenType.EndOfFile),
ImmutableArray<IDiagnostic>.Empty);
var printed = PrettyPrinter.PrintProgram(program, printOptions);
var bicepFile = RewriterHelper.RewriteMultiple(
prevCompilation,
SourceFileFactory.CreateBicepFile(new Uri("inmemory:///generated.bicep"), printed),
rewritePasses: 5,
model => new TypeCasingFixerRewriter(model),
model => new ReadOnlyPropertyRemovalRewriter(model));
printed = PrettyPrinter.PrintProgram(bicepFile.ProgramSyntax, printOptions);
if (insertContext.StartWithNewline)
{
printed = "\n" + printed;
}
if (insertContext.EndWithNewline)
{
printed = printed + "\n";
}
return new CodeReplacement(new TextSpan(insertContext.InsertOffset, 0), printed);
}
private static ResourceDeclarationSyntax CreateResourceSyntax(JsonElement resource, IAzResourceProvider.AzResourceIdentifier resourceId, ResourceTypeReference typeReference)
{
var properties = new List<ObjectPropertySyntax>();
foreach (var property in resource.EnumerateObject())
{
switch (property.Name.ToLowerInvariant())
{
case "id":
case "type":
case "apiVersion":
// Don't add these to the resource properties - they're part of the resource declaration.
break;
case "name":
// Use the fully-qualified name instead of the name returned by the RP.
properties.Add(SyntaxFactory.CreateObjectProperty(
"name",
SyntaxFactory.CreateStringLiteral(resourceId.FullyQualifiedName)));
break;
default:
properties.Add(SyntaxFactory.CreateObjectProperty(
property.Name,
ConvertJsonElement(property.Value)));
break;
}
}
var description = SyntaxFactory.CreateDecorator(
"description",
SyntaxFactory.CreateStringLiteral($"Generated from {resourceId.FullyQualifiedId}"));
return new ResourceDeclarationSyntax(
new SyntaxBase[] { description, SyntaxFactory.NewlineToken, },
SyntaxFactory.CreateToken(TokenType.Identifier, "resource"),
SyntaxFactory.CreateIdentifier(Regex.Replace(resourceId.UnqualifiedName, "[^a-zA-Z]", "")),
SyntaxFactory.CreateStringLiteral(typeReference.FormatName()),
null,
SyntaxFactory.CreateToken(TokenType.Assignment),
SyntaxFactory.CreateObject(properties));
}
private static IAzResourceProvider.AzResourceIdentifier? TryParseResourceId(string? resourceIdString)
{
if (resourceIdString is null)
{
return null;
}
if (ResourceId.TryParse(resourceIdString, out var resourceId))
{
return new(
resourceId.FullyQualifiedId,
resourceId.FormatFullyQualifiedType(),
resourceId.FormatName(),
resourceId.NameHierarchy.Last(),
string.Empty);
}
var rgRegexOptions = RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant;
var rgRegex = new Regex(@"^/subscriptions/(?<subId>[^/]+)/resourceGroups/(?<rgName>[^/]+)$", rgRegexOptions);
var rgRegexMatch = rgRegex.Match(resourceIdString);
if (rgRegexMatch.Success)
{
return new(
resourceIdString,
"Microsoft.Resources/resourceGroups",
rgRegexMatch.Groups["rgName"].Value,
rgRegexMatch.Groups["rgName"].Value,
rgRegexMatch.Groups["subId"].Value);
}
return null;
}
private static SyntaxBase ConvertJsonElement(JsonElement element)
{
switch (element.ValueKind)
{
case JsonValueKind.Object:
var properties = new List<ObjectPropertySyntax>();
foreach (var property in element.EnumerateObject())
{
properties.Add(SyntaxFactory.CreateObjectProperty(property.Name, ConvertJsonElement(property.Value)));
}
return SyntaxFactory.CreateObject(properties);
case JsonValueKind.Array:
var items = new List<SyntaxBase>();
foreach (var value in element.EnumerateArray())
{
items.Add(ConvertJsonElement(value));
}
return SyntaxFactory.CreateArray(items);
case JsonValueKind.String:
return SyntaxFactory.CreateStringLiteral(element.GetString()!);
case JsonValueKind.Number:
if (element.TryGetInt64(out long intValue))
{
return SyntaxFactory.CreatePositiveOrNegativeInteger(intValue);
}
return SyntaxFactory.CreateFunctionCall(
"json",
SyntaxFactory.CreateStringLiteral(element.ToString()));
case JsonValueKind.True:
return SyntaxFactory.CreateToken(TokenType.TrueKeyword);
case JsonValueKind.False:
return SyntaxFactory.CreateToken(TokenType.FalseKeyword);
case JsonValueKind.Null:
return SyntaxFactory.CreateToken(TokenType.NullKeyword);
default:
throw new InvalidOperationException($"Failed to deserialize JSON");
}
}
}
}