Skip to content

Commit d80101e

Browse files
russcamMpdreamz
authored andcommitted
Implement Watcher APIs (#2413)
* Implement Watcher APIs Update watcher rest api specs to 2.4 (cherry picked from commit 519db80) * touch up some System.Action's leftovers
1 parent b7aa0f9 commit d80101e

File tree

231 files changed

+25433
-13538
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

+25433
-13538
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Overrides\Descriptors\SearchDescriptorOverrides.cs" />
8282
<Compile Include="Overrides\Descriptors\ReindexOnServerDescriptorOverrides.cs" />
8383
<Compile Include="Overrides\Descriptors\UpdateDescriptorOverrides.cs" />
84+
<Compile Include="Overrides\Descriptors\WatcherStatsDescriptorOverrides.cs" />
8485
<Compile Include="Overrides\Global\GlobalQueryParameters.cs" />
8586
<Compile Include="Program.cs" />
8687
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -179,6 +180,7 @@
179180
<None Include="RestSpecification\Core\put_script.json" />
180181
<None Include="RestSpecification\Core\put_template.json" />
181182
<None Include="RestSpecification\Core\reindex.json" />
183+
<None Include="RestSpecification\Core\reindex_rethrottle.json" />
182184
<None Include="RestSpecification\Core\render_search_template.json" />
183185
<None Include="RestSpecification\Core\scroll.json" />
184186
<None Include="RestSpecification\Core\search.json" />

src/CodeGeneration/ApiGenerator/Domain/ApiEndpoint.cs

Lines changed: 50 additions & 25 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,7 @@ public string IfCheck
4444

4545
public class ApiEndpoint
4646
{
47+
private List<CsharpMethod> _csharpMethods;
4748
public string CsharpMethodName { get; set; }
4849
public string Documentation { get; set; }
4950
public IEnumerable<string> Methods { get; set; }
@@ -100,9 +101,20 @@ public IEnumerable<CsharpMethod> CsharpMethods
100101
{
101102
get
102103
{
103-
foreach (var method in this.Methods)
104+
if (_csharpMethods != null)
104105
{
106+
foreach (var csharpMethod in _csharpMethods)
107+
yield return csharpMethod;
108+
yield break;
109+
}
110+
111+
// enumerate once and cache
112+
_csharpMethods = new List<CsharpMethod>();
105113

114+
this.PatchEndpoint();
115+
116+
foreach (var method in this.Methods)
117+
{
106118
var methodName = this.CsharpMethodName + this.OptionallyAppendHttpMethod(this.Methods, method);
107119
//the distinctby here catches aliases routes i.e
108120
// /_cluster/nodes/{node_id}/hotthreads vs /_cluster/nodes/{node_id}/hot_threads
@@ -117,23 +129,8 @@ public IEnumerable<CsharpMethod> CsharpMethods
117129
return p.Value;
118130
})
119131
.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-
default:
132-
return p.Type + " " + p.Name;
133-
//return "string " + p.Name;
134132

135-
}
136-
});
133+
var args = parts.Select(p => p.Argument);
137134

138135
//.NET does not allow get requests to have a body payload.
139136
if (method != "GET" && this.Body != null)
@@ -182,6 +179,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
182179
"Func<"+apiMethod.QueryStringParamName+", " + apiMethod.QueryStringParamName + "> requestParameters = null"
183180
}).ToList();
184181
apiMethod.Arguments = string.Join(", ", args);
182+
_csharpMethods.Add(apiMethod);
185183
yield return apiMethod;
186184

187185
apiMethod = new CsharpMethod
@@ -202,6 +200,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
202200
Url = this.Url
203201
};
204202
PatchMethod(apiMethod);
203+
_csharpMethods.Add(apiMethod);
205204
yield return apiMethod;
206205

207206
//No need for deserialization state when returning dynamicdictionary
@@ -235,6 +234,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
235234
Url = this.Url
236235
};
237236
PatchMethod(apiMethod);
237+
_csharpMethods.Add(apiMethod);
238238
yield return apiMethod;
239239

240240
apiMethod = new CsharpMethod
@@ -256,12 +256,38 @@ public IEnumerable<CsharpMethod> CsharpMethods
256256
Url = this.Url
257257
};
258258
PatchMethod(apiMethod);
259+
_csharpMethods.Add(apiMethod);
259260
yield return apiMethod;
260261
}
261262
}
262263
}
263264
}
264265

266+
private void PatchEndpoint()
267+
{
268+
//rename the {metric} route param to something more specific on WatcherStats
269+
// TODO: find a better place to do this
270+
if (this.CsharpMethodName == "WatcherStats")
271+
{
272+
var metric = this.Url.Parts.First(p => p.Key == "metric");
273+
274+
var apiUrlPart = metric.Value;
275+
apiUrlPart.Name = "watcher_stats_metric";
276+
277+
if (this.Url.Parts.Remove("metric"))
278+
{
279+
this.Url.Parts.Add("watcher_stats_metric", apiUrlPart);
280+
}
281+
282+
this.Url.Path = RenameMetricUrlPathParam(this.Url.Path);
283+
this.Url.Paths = this.Url.Paths.Select(RenameMetricUrlPathParam);
284+
}
285+
}
286+
287+
private static string RenameMetricUrlPathParam(string path)
288+
{
289+
return path.Replace("{metric}", "{watcher_stats_metric}");
290+
}
265291

266292
//Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
267293
//or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
@@ -309,7 +335,7 @@ public static void PatchMethod(CsharpMethod method)
309335
try
310336
{
311337
IEnumerable<string> skipList = new List<string>();
312-
IDictionary<string, string> renameList = new Dictionary<string, string>();
338+
IDictionary<string, string> queryStringParamsRenameList = new Dictionary<string, string>();
313339

314340
var typeName = "ApiGenerator.Overrides.Descriptors." + method.DescriptorType + "Overrides";
315341
var type = CodeConfiguration.Assembly.GetType(typeName);
@@ -319,9 +345,8 @@ public static void PatchMethod(CsharpMethod method)
319345
if (overrides != null)
320346
{
321347
skipList = overrides.SkipQueryStringParams ?? skipList;
322-
renameList = overrides.RenameQueryStringParams ?? renameList;
323-
324-
overrides.PatchMethod(method);
348+
queryStringParamsRenameList = overrides.RenameQueryStringParams ?? queryStringParamsRenameList;
349+
method = overrides.PatchMethod(method);
325350
}
326351
}
327352

@@ -334,8 +359,8 @@ public static void PatchMethod(CsharpMethod method)
334359
};
335360

336361
foreach (var kv in globalQueryStringRenames)
337-
if (!renameList.ContainsKey(kv.Key))
338-
renameList[kv.Key] = kv.Value;
362+
if (!queryStringParamsRenameList.ContainsKey(kv.Key))
363+
queryStringParamsRenameList[kv.Key] = kv.Value;
339364

340365
var patchedParams = new Dictionary<string, ApiQueryParameters>();
341366
foreach (var kv in method.Url.Params)
@@ -346,7 +371,7 @@ public static void PatchMethod(CsharpMethod method)
346371
continue;
347372

348373
string newName;
349-
if (!renameList.TryGetValue(kv.Key, out newName))
374+
if (!queryStringParamsRenameList.TryGetValue(kv.Key, out newName))
350375
{
351376
patchedParams.Add(kv.Key, kv.Value);
352377
continue;

src/CodeGeneration/ApiGenerator/Domain/ApiUrlPart.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ public string ClrTypeName
2929
{
3030
case "index": return this.Type == "string" ? "IndexName" : "Indices";
3131
case "type": return this.Type == "string" ? "TypeName" : "Types";
32+
case "watch_id":
3233
case "id": return this.Type == "string" ? "Id" : "Ids";
3334
case "node_id": return this.Type == "string" ? "NodeId" : "NodeIds";
3435
case "scroll_id": return this.Type == "string" ? "ScrollId" : "ScrollIds";
3536
case "field":
3637
case "fields": return this.Type == "string" ? "Field" : "Fields";
3738
case "index_metric": return "IndexMetrics";
38-
case "metric": return "Metrics";
39+
case "metric":
40+
case "watcher_stats_metric":
41+
return "Metrics";
3942
case "feature": return "Features";
43+
case "action_id": return "ActionIds";
4044
case "repository":
4145
case "snapshot":
4246
case "lang":
@@ -65,5 +69,26 @@ public string InterfaceName
6569
}
6670
}
6771
}
72+
73+
public string Argument
74+
{
75+
get
76+
{
77+
switch (this.Type)
78+
{
79+
case "int":
80+
case "string":
81+
return this.Type + " " + this.Name;
82+
case "list":
83+
return "string " + this.Name;
84+
case "enum":
85+
return ApiGenerator.PascalCase(this.Name) + " " + this.Name;
86+
case "number":
87+
return "string " + this.Name;
88+
default:
89+
return this.Type + " " + this.Name;
90+
}
91+
}
92+
}
6893
}
6994
}

src/CodeGeneration/ApiGenerator/Domain/CsharpMethod.cs

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

270-
if (paramName == "metric") routeValue = "(Metrics)metric";
270+
if (paramName == "metric" || paramName == "watcherStatsMetric") routeValue = "(Metrics)" + paramName;
271271
else if (paramName == "indexMetric") routeValue = "(IndexMetrics)indexMetric";
272272

273273
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Collections.Generic;
42

53
namespace ApiGenerator.Overrides.Allow404
64
{
7-
public static class ApiEndpointsThatAllow404
5+
public static class ApiEndpointsThatAllow404
86
{
97
public static readonly IEnumerable<string> Endpoints = new List<string>
108
{
@@ -17,6 +15,8 @@ public static class ApiEndpointsThatAllow404
1715
"Exists",
1816
"Get",
1917
"Delete",
18+
"GetWatch",
19+
"DeleteWatch",
2020
};
2121
}
2222
}
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ static void Main(string[] args)
3636
if (redownloadCoreSpecification)
3737
RestSpecDownloader.Download(downloadBranch);
3838

39-
ApiGenerator.Generate(downloadBranch, "Core", "DeleteByQuery", "Graph", "License", "Shield");
40-
//ApiGenerator.Generate("Core", "Graph", "License");
39+
ApiGenerator.Generate(downloadBranch, "Core", "DeleteByQuery", "Graph", "License", "Shield", "Watcher");
4140
//ApiGenerator.Generate(); //generates everything under ApiSpecification
4241
}
4342

0 commit comments

Comments
 (0)