Skip to content

Commit 14bd45d

Browse files
committed
Implement Watcher APIs
Run API generation to pull in REST specs from master Closes #2347 Cherry-pick from 519db80
1 parent b9698ae commit 14bd45d

File tree

231 files changed

+22466
-10885
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+22466
-10885
lines changed

src/CodeGeneration/ApiGenerator/ApiGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ private static KeyValuePair<string, ApiEndpoint> CreateApiEndpoint(string jsonFi
8282
return endpoint;
8383
}
8484

85-
private static string CreateMethodName(string apiEnpointKey)
85+
private static string CreateMethodName(string apiEndpointKey)
8686
{
87-
return PascalCase(apiEnpointKey);
87+
return PascalCase(apiEndpointKey);
8888
}
8989

9090
private static void GenerateClientInterface(RestApiSpec model)

src/CodeGeneration/ApiGenerator/ApiGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Overrides\Descriptors\SearchDescriptorOverrides.cs" />
7878
<Compile Include="Overrides\Descriptors\ReindexOnServerDescriptorOverrides.cs" />
7979
<Compile Include="Overrides\Descriptors\UpdateDescriptorOverrides.cs" />
80+
<Compile Include="Overrides\Descriptors\WatcherStatsDescriptorOverrides.cs" />
8081
<Compile Include="Overrides\Global\GlobalQueryParameters.cs" />
8182
<Compile Include="Program.cs" />
8283
<Compile Include="Properties\AssemblyInfo.cs" />

src/CodeGeneration/ApiGenerator/Domain/ApiEndpoint.cs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IEnumerable<string> MethodArguments
1919
get
2020
{
2121
var methodArgs = CsharpMethod.Parts
22-
.Select(p => p.Name != "body" ? "p.RouteValues." + p.Name.ToPascalCase() : "body")
22+
.Select(p => p.Name != "body" ? "p.RouteValues." + p.Name.ToPascalCase() + (p.Type == "enum" ? ".Value" : "") : "body")
2323
.Concat(new[] {"u => p.RequestParameters"});
2424
return methodArgs;
2525
}
@@ -44,6 +44,8 @@ public string IfCheck
4444

4545
public class ApiEndpoint
4646
{
47+
private List<CsharpMethod> _csharpMethods;
48+
4749
public string CsharpMethodName { get; set; }
4850
public string Documentation { get; set; }
4951
public IEnumerable<string> Methods { get; set; }
@@ -100,9 +102,20 @@ public IEnumerable<CsharpMethod> CsharpMethods
100102
{
101103
get
102104
{
103-
foreach (var method in this.Methods)
105+
if (_csharpMethods != null)
104106
{
107+
foreach (var csharpMethod in _csharpMethods)
108+
yield return csharpMethod;
109+
yield break;
110+
}
105111

112+
// enumerate once and cache
113+
_csharpMethods = new List<CsharpMethod>();
114+
115+
this.PatchEndpoint();
116+
117+
foreach (var method in this.Methods)
118+
{
106119
var methodName = this.CsharpMethodName + this.OptionallyAppendHttpMethod(this.Methods, method);
107120
//the distinctby here catches aliases routes i.e
108121
// /_cluster/nodes/{node_id}/hotthreads vs /_cluster/nodes/{node_id}/hot_threads
@@ -117,25 +130,8 @@ public IEnumerable<CsharpMethod> CsharpMethods
117130
return p.Value;
118131
})
119132
.ToList();
120-
var args = parts.Select(p =>
121-
{
122-
switch (p.Type)
123-
{
124-
case "int":
125-
case "string":
126-
return p.Type + " " + p.Name;
127-
case "list":
128-
return "string " + p.Name;
129-
case "enum":
130-
return this.PascalCase(p.Name) + p.Name;
131-
case "number":
132-
return "string " + p.Name;
133-
default:
134-
return p.Type + " " + p.Name;
135-
//return "string " + p.Name;
136133

137-
}
138-
});
134+
var args = parts.Select(p => p.Argument);
139135

140136
//.NET does not allow get requests to have a body payload.
141137
if (method != "GET" && this.Body != null)
@@ -184,6 +180,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
184180
"Func<"+apiMethod.QueryStringParamName+", " + apiMethod.QueryStringParamName + "> requestParameters = null"
185181
}).ToList();
186182
apiMethod.Arguments = string.Join(", ", args);
183+
_csharpMethods.Add(apiMethod);
187184
yield return apiMethod;
188185

189186
args = args.Concat(new[]
@@ -208,6 +205,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
208205
Url = this.Url
209206
};
210207
PatchMethod(apiMethod);
208+
_csharpMethods.Add(apiMethod);
211209
yield return apiMethod;
212210

213211
//No need for deserialization state when returning dynamicdictionary
@@ -241,7 +239,9 @@ public IEnumerable<CsharpMethod> CsharpMethods
241239
Url = this.Url
242240
};
243241
PatchMethod(apiMethod);
242+
_csharpMethods.Add(apiMethod);
244243
yield return apiMethod;
244+
245245
args = args.Concat(new[]
246246
{
247247
"CancellationToken cancellationToken = default(CancellationToken)"
@@ -265,12 +265,38 @@ public IEnumerable<CsharpMethod> CsharpMethods
265265
Url = this.Url
266266
};
267267
PatchMethod(apiMethod);
268+
_csharpMethods.Add(apiMethod);
268269
yield return apiMethod;
269270
}
270271
}
271272
}
272273
}
273274

275+
private void PatchEndpoint()
276+
{
277+
//rename the {metric} route param to something more specific on XpackWatcherStats
278+
// TODO: find a better place to do this
279+
if (this.CsharpMethodName == "XpackWatcherStats")
280+
{
281+
var metric = this.Url.Parts.First(p => p.Key == "metric");
282+
283+
var apiUrlPart = metric.Value;
284+
apiUrlPart.Name = "watcher_stats_metric";
285+
286+
if (this.Url.Parts.Remove("metric"))
287+
{
288+
this.Url.Parts.Add("watcher_stats_metric", apiUrlPart);
289+
}
290+
291+
this.Url.Path = RenameMetricUrlPathParam(this.Url.Path);
292+
this.Url.Paths = this.Url.Paths.Select(RenameMetricUrlPathParam);
293+
}
294+
}
295+
296+
private static string RenameMetricUrlPathParam(string path)
297+
{
298+
return path.Replace("{metric}", "{watcher_stats_metric}");
299+
}
274300

275301
//Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
276302
//or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
@@ -318,7 +344,7 @@ public static void PatchMethod(CsharpMethod method)
318344
try
319345
{
320346
IEnumerable<string> skipList = new List<string>();
321-
IDictionary<string, string> renameList = new Dictionary<string, string>();
347+
IDictionary<string, string> queryStringParamsRenameList = new Dictionary<string, string>();
322348

323349
var typeName = "ApiGenerator.Overrides.Descriptors." + method.DescriptorType + "Overrides";
324350
var type = CodeConfiguration.Assembly.GetType(typeName);
@@ -328,9 +354,8 @@ public static void PatchMethod(CsharpMethod method)
328354
if (overrides != null)
329355
{
330356
skipList = overrides.SkipQueryStringParams ?? skipList;
331-
renameList = overrides.RenameQueryStringParams ?? renameList;
332-
333-
overrides.PatchMethod(method);
357+
queryStringParamsRenameList = overrides.RenameQueryStringParams ?? queryStringParamsRenameList;
358+
method = overrides.PatchMethod(method);
334359
}
335360
}
336361

@@ -343,8 +368,8 @@ public static void PatchMethod(CsharpMethod method)
343368
};
344369

345370
foreach (var kv in globalQueryStringRenames)
346-
if (!renameList.ContainsKey(kv.Key))
347-
renameList[kv.Key] = kv.Value;
371+
if (!queryStringParamsRenameList.ContainsKey(kv.Key))
372+
queryStringParamsRenameList[kv.Key] = kv.Value;
348373

349374
var patchedParams = new Dictionary<string, ApiQueryParameters>();
350375
foreach (var kv in method.Url.Params)
@@ -355,7 +380,7 @@ public static void PatchMethod(CsharpMethod method)
355380
continue;
356381

357382
string newName;
358-
if (!renameList.TryGetValue(kv.Key, out newName))
383+
if (!queryStringParamsRenameList.TryGetValue(kv.Key, out newName))
359384
{
360385
patchedParams.Add(kv.Key, kv.Value);
361386
continue;

src/CodeGeneration/ApiGenerator/Domain/ApiUrlPart.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ public string ClrTypeName
3535
case "target":
3636
return "IndexName";
3737
case "type": return this.Type == "string" ? "TypeName" : "Types";
38+
case "watch_id":
3839
case "id": return this.Type == "string" ? "Id" : "Ids";
3940
case "node_id": return this.Type == "string" ? "NodeId" : "NodeIds";
4041
case "scroll_id": return this.Type == "string" ? "ScrollId" : "ScrollIds";
4142
case "field":
4243
case "fields": return this.Type == "string" ? "Field" : "Fields";
4344
case "index_metric": return "IndexMetrics";
44-
case "metric": return "Metrics";
45+
case "metric":
46+
case "watcher_stats_metric":
47+
return "Metrics";
4548
case "feature": return "Features";
49+
case "action_id": return "ActionIds";
4650
case "repository":
4751
case "snapshot":
4852
case "lang":
@@ -73,5 +77,26 @@ public string InterfaceName
7377
}
7478
}
7579
}
80+
81+
public string Argument
82+
{
83+
get
84+
{
85+
switch (this.Type)
86+
{
87+
case "int":
88+
case "string":
89+
return this.Type + " " + this.Name;
90+
case "list":
91+
return "string " + this.Name;
92+
case "enum":
93+
return ApiGenerator.PascalCase(this.Name) + " " + this.Name;
94+
case "number":
95+
return "string " + this.Name;
96+
default:
97+
return this.Type + " " + this.Name;
98+
}
99+
}
100+
}
76101
}
77102
}

src/CodeGeneration/ApiGenerator/Domain/CsharpMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public IEnumerable<Constructor> RequestConstructors()
9999
{
100100
route = p.Key,
101101
call = p.Value.Required ? "Required" : "Optional",
102-
v = p.Key == "metric"
102+
v = p.Key == "metric" || p.Key == "watcher_stats_metric"
103103
? $"(Metrics){p.Key}"
104104
: p.Key == "index_metric"
105105
? $"(IndexMetrics){p.Key}"
@@ -269,7 +269,7 @@ public IEnumerable<FluentRouteSetter> GetFluentRouteSetters()
269269
var routeValue = paramName;
270270
var routeSetter = p.Required ? "Required" : "Optional";
271271

272-
if (paramName == "metric") routeValue = "(Metrics)metric";
272+
if (paramName == "metric" || paramName == "watcherStatsMetric") routeValue = "(Metrics)" + paramName;
273273
else if (paramName == "indexMetric") routeValue = "(IndexMetrics)indexMetric";
274274

275275
var code = $"public {returnType} {p.InterfaceName}({ClrParamType(p.ClrTypeName)} {paramName}) => Assign(a=>a.RouteValues.{routeSetter}(\"{p.Name}\", {routeValue}));";

src/CodeGeneration/ApiGenerator/Domain/RestApiSpec.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
namespace ApiGenerator.Domain
@@ -11,6 +12,8 @@ public class EnumDescription
1112

1213
public class RestApiSpec
1314
{
15+
private IEnumerable<EnumDescription> _enumDescriptions;
16+
1417
public string Commit { get; set; }
1518
public IDictionary<string, ApiEndpoint> Endpoints { get; set; }
1619

@@ -21,30 +24,35 @@ public IEnumerable<EnumDescription> EnumsInTheSpec
2124
{
2225
get
2326
{
24-
var queryParamEnums = from m in this.CsharpMethodsWithQueryStringInfo.SelectMany(m => m.Url.Params)
25-
where m.Value.Type == "enum"
26-
select new EnumDescription
27-
{
28-
Name = m.Value.CsharpType(m.Key),
29-
Options = m.Value.Options
30-
31-
};
27+
if (_enumDescriptions == null)
28+
{
29+
var queryParamEnums = from m in this.CsharpMethodsWithQueryStringInfo.SelectMany(m => m.Url.Params)
30+
where m.Value.Type == "enum"
31+
select new EnumDescription
32+
{
33+
Name = m.Value.CsharpType(m.Key),
34+
Options = m.Value.Options
35+
};
3236

33-
var urlParamEnums = from data in this.Endpoints.Values
34-
.SelectMany(v => v.CsharpMethods.Select(m => new { m, n = v.CsharpMethodName }))
35-
.SelectMany(m => m.m.Parts.Select(part => new { m = m.n, p = part }))
36-
let p = data.p
37-
let m = data.m
38-
where p.Options != null && p.Options.Any()
39-
let name = p.Name.Contains("metric") ? m + p.Name.ToPascalCase() : p.Name.ToPascalCase()
40-
select new EnumDescription
41-
{
42-
Name = name,
43-
Options = p.Options
44-
};
37+
var urlParamEnums = from data in this.Endpoints.Values
38+
.SelectMany(v => v.CsharpMethods.Select(m => new { m, n = v.CsharpMethodName }))
39+
.SelectMany(m => m.m.Parts.Select(part => new { m = m.n, p = part }))
40+
let p = data.p
41+
let m = data.m
42+
where p.Options != null && p.Options.Any()
43+
let name = p.Name.Contains("metric") && p.Name != "watcher_stats_metric"
44+
? m + p.Name.ToPascalCase()
45+
: p.Name.ToPascalCase()
46+
select new EnumDescription
47+
{
48+
Name = name,
49+
Options = p.Options
50+
};
4551

46-
return queryParamEnums.Concat(urlParamEnums).DistinctBy(e => e.Name);
52+
_enumDescriptions = queryParamEnums.Concat(urlParamEnums).DistinctBy(e => e.Name);
53+
}
4754

55+
return _enumDescriptions;
4856
}
4957

5058
}

src/CodeGeneration/ApiGenerator/Overrides/Allow404/ApiEndpointsThatAllow404.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static class ApiEndpointsThatAllow404
1717
"Exists",
1818
"Get",
1919
"Delete",
20+
"GetWatch",
21+
"DeleteWatch",
2022
};
2123
}
2224
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using ApiGenerator.Domain;
4+
5+
namespace ApiGenerator.Overrides.Descriptors
6+
{
7+
public class WatcherStatsDescriptorOverrides : DescriptorOverridesBase
8+
{
9+
public override IEnumerable<string> SkipQueryStringParams => new[]
10+
{
11+
// this is already included in the url route
12+
"metric"
13+
};
14+
}
15+
}

src/CodeGeneration/ApiGenerator/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ static void Main(string[] args)
3737
if (redownloadCoreSpecification)
3838
RestSpecDownloader.Download(downloadBranch);
3939

40-
ApiGenerator.Generate(downloadBranch, "Core", "Graph", "License", "Security");
41-
//ApiGenerator.Generate("Core", "DeleteByQuery", "Graph", "License", "Shield");
42-
//ApiGenerator.Generate("Core", "Graph", "License");
40+
ApiGenerator.Generate(downloadBranch, "Core", "Graph", "License", "Security", "Watcher");
41+
4342
//ApiGenerator.Generate(); //generates everything under ApiSpecification
4443
}
4544

0 commit comments

Comments
 (0)