Skip to content

Commit e79b497

Browse files
swallezrusscam
authored andcommitted
Simplify generated code module structure (#138)
Remove the `generated` directory and the various re-exports. - namespace docs have been extracted from the respective `mod.rs` to `.md` files in api_generator/docs, and are inserted in the generated source file. - namespace sources are generated directly in the `src` directory - the generator can now insert generated sections in regular source files based on `GENERATE-BEGIN` and `GENERATE-END` markers. This is used to generate the namespace module list in the main `lib.rs` and merge generated with manual code in `params.rs` - generated and merged source files are listed in `src/.generated.toml` so that the generator can cleanup on start. Before considering merging generated code with special markers we experimented with the `include!()` macro, but this didn't work well: - rustfmt will not reformat include files - `mod` statement look for files relative to the source file, so include files have to be located in the same directory as their includer, while locating them in a dedicated directory would have provided a nicer organization. Merging generated sections provides a more natural source code layout. (cherry picked from commit 9d9a4fb)
1 parent e02a892 commit e79b497

Some content is hidden

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

84 files changed

+9583
-870
lines changed

api_generator/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ globset = "~0.4"
1616
Inflector = "0.11.4"
1717
indicatif = "0.12.0"
1818
lazy_static = "1.4.0"
19+
path-slash = "0.1.3"
1920
quote = "~0.3"
2021
reduce = "0.1.2"
2122
regex = "1.3.1"
@@ -26,5 +27,9 @@ serde_json = "~1"
2627
serde_derive = "~1"
2728
syn = { version = "~0.11", features = ["full"] }
2829
tar = "~0.4"
30+
toml = "0.5.6"
2931
url = "2.1.1"
30-
void = "1.0.2"
32+
void = "1.0.2"
33+
34+
[dev-dependencies]
35+
tempfile = "3.1.0"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Async Search APIs
2+
3+
[Async search APIs](https://www.elastic.co/guide/en/elasticsearch/reference/master/async-search.html)
4+
let you asynchronously execute a search request, monitor its progress, and retrieve
5+
partial results as they become available.

api_generator/docs/namespaces/cat.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Cat APIs
2+
3+
The [Cat APIs](https://www.elastic.co/guide/en/elasticsearch/reference/master/cat.html) aim to
4+
meet the needs of humans when looking at data returned from Elasticsearch,
5+
formatting it as compact, column aligned text, making it easier on human eyes.
6+
7+
# Plain text responses
8+
9+
By default, all Cat APIs are configured to send requests with `text/plain` content-type
10+
and accept headers, returning plain text responses
11+
12+
```rust,no_run
13+
# use elasticsearch::{Elasticsearch, Error, SearchParts};
14+
# use url::Url;
15+
# use elasticsearch::auth::Credentials;
16+
# use serde_json::{json, Value};
17+
# async fn doc() -> Result<(), Box<dyn std::error::Error>> {
18+
# let client = Elasticsearch::default();
19+
let response = client
20+
.cat()
21+
.nodes()
22+
.send()
23+
.await?;
24+
25+
let response_body = response.text().await?;
26+
# Ok(())
27+
# }
28+
```
29+
30+
# JSON responses
31+
32+
JSON responses can be returned from Cat APIs either by using `.format("json")`
33+
34+
```rust,no_run
35+
# use elasticsearch::{Elasticsearch, Error, SearchParts};
36+
# use url::Url;
37+
# use elasticsearch::auth::Credentials;
38+
# use serde_json::{json, Value};
39+
# async fn doc() -> Result<(), Box<dyn std::error::Error>> {
40+
# let client = Elasticsearch::default();
41+
let response = client
42+
.cat()
43+
.nodes()
44+
.format("json")
45+
.send()
46+
.await?;
47+
48+
let response_body = response.json::<Value>().await?;
49+
# Ok(())
50+
# }
51+
```
52+
53+
Or by setting an accept header using `.headers()`
54+
55+
```rust,no_run
56+
# use elasticsearch::{Elasticsearch, Error, SearchParts, http::headers::{HeaderValue, DEFAULT_ACCEPT, ACCEPT}};
57+
# use url::Url;
58+
# use serde_json::{json, Value};
59+
# async fn doc() -> Result<(), Box<dyn std::error::Error>> {
60+
# let client = Elasticsearch::default();
61+
let response = client
62+
.cat()
63+
.nodes()
64+
.header(ACCEPT, HeaderValue::from_static(DEFAULT_ACCEPT))
65+
.send()
66+
.await?;
67+
68+
let response_body = response.json::<Value>().await?;
69+
# Ok(())
70+
# }
71+
```
72+
73+
# Column Headers
74+
75+
The column headers to return can be controlled with `.h()`
76+
77+
```rust,no_run
78+
# use elasticsearch::{Elasticsearch, Error, SearchParts};
79+
# use url::Url;
80+
# use serde_json::{json, Value};
81+
# async fn doc() -> Result<(), Box<dyn std::error::Error>> {
82+
# let client = Elasticsearch::default();
83+
let response = client
84+
.cat()
85+
.nodes()
86+
.h(&["ip", "port", "heapPercent", "name"])
87+
.send()
88+
.await?;
89+
90+
let response_body = response.json::<String>().await?;
91+
# Ok(())
92+
# }
93+
```
94+

api_generator/docs/namespaces/ccr.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Cross-cluster Replication APIs
2+
3+
[Enable replication of indices in remote clusters to a local cluster](https://www.elastic.co/guide/en/elasticsearch/reference/master/xpack-ccr.html).
4+
This functionality can be used in some common production use cases:
5+
6+
- Disaster recovery in case a primary cluster fails. A secondary cluster can serve as a hot backup
7+
- Geo-proximity so that reads can be served locally
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Cluster APIs
2+
3+
[Manage settings](https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster.html),
4+
perform operations, and retrieve information about an Elasticsearch cluster.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Dangling Index APIs
2+
3+
If Elasticsearch encounters index data that is absent from the current cluster state,
4+
those indices are considered to be _dangling_. For example, this can happen if you delete
5+
more than `cluster.indices.tombstones.size` number of indices while an Elasticsearch node
6+
is offline.
7+
8+
The dangling indices APIs can list, import and delete dangling indices.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Enrich APIs
2+
3+
Manage [enrich policies](https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest-enriching-data.html#enrich-policy)
4+
that can be used by the [enrich processor](https://www.elastic.co/guide/en/elasticsearch/reference/master/enrich-processor.html)
5+
as part of an [ingest pipeline](../ingest/index.html), to add data from your existing indices
6+
to incoming documents during ingest.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Graph APIs
2+
3+
Enable extraction and summarization of information about documents and terms in Elasticsearch
4+
indices, [inferring relationships across documents](https://www.elastic.co/what-is/elasticsearch-graph),
5+
and allowing the [exploration of such relationships](https://www.elastic.co/guide/en/elasticsearch/reference/master/graph-explore-api.html).

api_generator/docs/namespaces/ilm.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Index Lifecycle Management APIs
2+
3+
Automate how [indices are managed over time](https://www.elastic.co/guide/en/elasticsearch/reference/master/index-lifecycle-management.html).
4+
Rather than simply performing management actions on indices on a set schedule, actions can be based
5+
on other factors such as shard size and performance requirements.
6+
7+
Control how indices are handled as they age by attaching a lifecycle policy to the index
8+
template used to create them. Update the policy to modify the lifecycle of both new
9+
and existing indices.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Index APIs
2+
3+
[Manage individual indices](https://www.elastic.co/guide/en/elasticsearch/reference/master/indices.html),
4+
index settings, aliases, mappings, and index templates.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Ingest APIs
2+
3+
[Manage ingest pipelines](https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest-apis.html).
4+
Ingest pipelines can be used on a node with the `ingest` role to
5+
pre-process documents before indexing, to apply transformations and enrich data. Transformations are performed
6+
by [processors](https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest-processors.html)
7+
in the pipeline, and can include such operations as
8+
9+
- add, remove and append fields within the document
10+
- point documents to the right time-based index based on a timestamp within the document
11+
- extract details from fields with known formats and add new fields with extracted data
12+
13+
and many more.
14+
15+
All nodes enable ingest by default, so any node can handle ingest tasks. Ingest pipelines can
16+
be conditionally executed, and failures within pipelines can be explicitly handled by defining
17+
processors to execute in the event of failure.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Licensing APIs
2+
3+
[Manage Elastic Stack license](https://www.elastic.co/guide/en/elasticsearch/reference/master/licensing-apis.html), including
4+
5+
- Retrieve, update or delete a license
6+
- Start a 30 day trial of the Platinum license features
7+
- Start indefinite use of the Basic license features
8+
- Get the status of trial and basic license features
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Migration APIs
2+
3+
[Simplify upgrading X-Pack indices from one version to another](https://www.elastic.co/guide/en/elasticsearch/reference/master/migration-api.html).

api_generator/docs/namespaces/ml.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Machine Learning APIs
2+
3+
[Perform machine learning anomaly detection activities](https://www.elastic.co/guide/en/elasticsearch/reference/master/ml-apis.html).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Node APIs
2+
3+
Manage settings, perform operations, and retrieve information about the
4+
[nodes in an Elasticsearch cluster](https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster.html).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Security APIs
2+
3+
[Perform security related activities](https://www.elastic.co/guide/en/elasticsearch/reference/master/security-api.html), including
4+
5+
- Manage users
6+
- Manage application privileges
7+
- Manage roles
8+
- Manage role mappings
9+
- Manage API keys
10+
- Manage Bearer tokens
11+
- Authenticate users against an OpenID Connect or SAML authentication realm when using a
12+
custom web application other than Kibana

api_generator/docs/namespaces/slm.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Snapshot Lifecycle Management APIs
2+
3+
[Manage policies for the time and frequency of automatic snapshots](https://www.elastic.co/guide/en/elasticsearch/reference/master/snapshot-lifecycle-management-api.html).
4+
Snapshot Lifecycle Management (SLM)
5+
is related to [Index Lifecycle Management](../ilm/index.html), however, instead of managing a lifecycle of
6+
actions that are performed on a single index, SLM allows configuring policies spanning multiple
7+
indices. Snapshot Lifecycle Management can also perform deletion of older snapshots based on a
8+
configurable retention policy.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Snapshot APIs
2+
3+
[Manage snapshots taken from a running Elasticsearch cluster](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-snapshots.html).
4+
A snapshot is a backup of individual
5+
indices or the entire cluster, stored in a repository on a shared filesystem or a remote repository
6+
on S3, HDFS, Azure, Google Cloud storage, and more.

api_generator/docs/namespaces/sql.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SQL APIs
2+
3+
[Execute SQL queries against Elasticsearch indices and return results in tabular format](https://www.elastic.co/guide/en/elasticsearch/reference/master/xpack-sql.html).

api_generator/docs/namespaces/ssl.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SSL APIs
2+
3+
[Retrieve information about the X.509 certificates used to encrypt communications in the cluster](https://www.elastic.co/guide/en/elasticsearch/reference/master/security-api-ssl.html).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Task Management APIs
2+
3+
[Manage tasks currently executing on one or more nodes in the cluster](https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Transform APIs
2+
3+
[Transforms ](https://www.elastic.co/guide/en/elasticsearch/reference/master/transform-apis.html)
4+
can be used to copy data from source indices, transforms it, and persists it into an
5+
entity-centric destination index.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Watcher (Alerting) APIs
2+
3+
Enable [watching for changes or anomalies in data and perform the necessary actions in response](https://www.elastic.co/guide/en/elasticsearch/reference/master/xpack-alerting.html),
4+
by creating and managing watches that take action based on a met condition.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
X-Pack APIs
2+
3+
Provide general information about the installed X-Pack features and their usage.

api_generator/src/bin/run.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use std::{
2828
};
2929

3030
fn main() -> Result<(), failure::Error> {
31-
// This must be run from the src root directory, with cargo run -p api_generator
31+
// This must be run from the repo root directory, with cargo make generate-api
3232
let download_dir = fs::canonicalize(PathBuf::from("./api_generator/rest_specs"))?;
33-
let generated_dir = fs::canonicalize(PathBuf::from("./elasticsearch/src/generated"))?;
33+
let generated_dir = fs::canonicalize(PathBuf::from("./elasticsearch/src"))?;
3434
let last_downloaded_version =
3535
PathBuf::from("./api_generator/rest_specs/last_downloaded_version");
3636

@@ -96,12 +96,22 @@ fn main() -> Result<(), failure::Error> {
9696
}
9797

9898
if generate_code {
99-
// delete existing generated files if the exist
100-
if generated_dir.exists() {
101-
fs::remove_dir_all(&generated_dir)?;
99+
// Delete previously generated files
100+
let mut generated = generated_dir.clone();
101+
generated.push(generator::GENERATED_TOML);
102+
103+
if generated.exists() {
104+
let files =
105+
toml::from_str::<generator::GeneratedFiles>(&fs::read_to_string(generated)?)?;
106+
107+
for f in files.written {
108+
let mut generated_file = generated_dir.clone();
109+
generated_file.push(f);
110+
let _ = fs::remove_file(generated_file); // ignore missing files
111+
}
102112
}
103113

104-
fs::create_dir_all(&generated_dir)?;
114+
// and generate!
105115
generator::generate(&branch, &download_dir, &generated_dir)?;
106116
}
107117
}

api_generator/src/generator/code_gen/request/request_builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,8 @@ impl<'a> RequestBuilder<'a> {
687687

688688
let markdown_doc = {
689689
let mut path = docs_dir.clone();
690-
path.push(format!("{}.fn.{}.md", namespace_name, name));
690+
path.push("functions");
691+
path.push(format!("{}.{}.md", namespace_name, name));
691692
if path.exists() {
692693
let mut s = fs::read_to_string(&path)
693694
.unwrap_or_else(|_| panic!("Could not read file at {:?}", &path));

0 commit comments

Comments
 (0)