Skip to content

Commit

Permalink
Move Meta's rest model to a separate crate
Browse files Browse the repository at this point in the history
Tiny bonus: crate imports are re-organised in a more uniform way (std, local, restate, third-party)
  • Loading branch information
AhmedSoliman committed Nov 27, 2023
1 parent d28a41d commit bf04449
Show file tree
Hide file tree
Showing 21 changed files with 391 additions and 255 deletions.
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ restate-ingress-kafka = { path = "crates/ingress-kafka" }
restate-invoker-api = { path = "crates/invoker-api" }
restate-invoker-impl = { path = "crates/invoker-impl" }
restate-meta = { path = "crates/meta" }
restate-meta-rest-model = { path = "crates/meta-rest-model" }
restate-network = { path = "crates/network" }
restate-pb = { path = "crates/pb" }
restate-queue = { path = "crates/queue" }
Expand Down
3 changes: 1 addition & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ build = "build.rs"
default = []

[dependencies]
restate-meta = { workspace = true }
restate-schema-api = { workspace = true }
restate-meta-rest-model = { workspace = true }
restate-serde-util = { workspace = true }

anyhow = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions cli/src/commands/services/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::console::c_println;
use crate::meta_client::{MetaClient, MetaClientInterface};
use crate::ui::console::{Styled, StyledTable};
use crate::ui::stylesheet::Style;
use restate_meta::rest_api::endpoints::{ServiceEndpoint, ServiceEndpointResponse};

use restate_meta_rest_model::endpoints::{ProtocolType, ServiceEndpoint, ServiceEndpointResponse};

use anyhow::Result;

Expand Down Expand Up @@ -89,8 +90,8 @@ fn add_endpoint(endpoint: &ServiceEndpointResponse, table: &mut Table) {
table.add_row(vec!["Endpoint Type:", "HTTP"]);
table.add_row(vec!["Endpoint URL:", &uri.to_string()]);
let protocol_type = match protocol_type {
restate_schema_api::endpoint::ProtocolType::RequestResponse => "RequestResponse",
restate_schema_api::endpoint::ProtocolType::BidiStream => "BidiStream",
ProtocolType::RequestResponse => "RequestResponse",
ProtocolType::BidiStream => "BidiStream",
}
.to_string();
table.add_row(vec!["Endpoint Protocol:", &protocol_type]);
Expand Down
7 changes: 4 additions & 3 deletions cli/src/commands/services/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use crate::cli_env::CliEnv;
use crate::console::{c_println, Icon};
use crate::meta_client::MetaClientInterface;
use crate::ui::console::StyledTable;

use restate_meta_rest_model::endpoints::{ProtocolType, ServiceEndpoint, ServiceEndpointResponse};
use restate_meta_rest_model::services::{InstanceType, MethodMetadata};

use anyhow::{Context, Result};
use cling::prelude::*;
use comfy_table::{Attribute, Cell, Table};
use restate_meta::rest_api::endpoints::{ServiceEndpoint, ServiceEndpointResponse};
use restate_schema_api::endpoint::ProtocolType;
use restate_schema_api::service::{InstanceType, MethodMetadata};

#[derive(Run, Parser, Collect, Clone)]
#[clap(visible_alias = "ls")]
Expand Down
12 changes: 5 additions & 7 deletions cli/src/meta_client/meta_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use async_trait::async_trait;
use restate_meta::rest_api::endpoints::ListServiceEndpointsResponse;
use restate_meta::rest_api::endpoints::ServiceEndpointResponse;

use restate_meta::rest_api::services::ListServicesResponse;
use restate_schema_api::service::ServiceMetadata;

use super::client::Envelope;
use super::MetaClient;

use restate_meta_rest_model::endpoints::*;
use restate_meta_rest_model::services::*;

use async_trait::async_trait;

#[async_trait]
pub trait MetaClientInterface {
/// Check if the meta service is healthy by invoking /health
Expand Down
23 changes: 23 additions & 0 deletions crates/meta-rest-model/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "restate-meta-rest-model"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
publish = false

[features]
default = []
schema = ["dep:schemars", "restate-schema-api/serde_schema", "restate-serde-util/schema", "restate-types/serde_schema"]

[dependencies]
restate-types = { workspace = true, features = ["serde"] }
restate-schema-api = { workspace = true, features = [ "service", "endpoint", "discovery", "serde", "subscription" ] }
restate-serde-util = { workspace = true }

http = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
150 changes: 150 additions & 0 deletions crates/meta-rest-model/src/endpoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use super::services::ServiceRevision;

use restate_serde_util::SerdeableHeaderHashMap;

use http::Uri;

use serde::{Deserialize, Serialize};
use serde_with::serde_as;

// Export schema types to be used by other crates without exposing the fact
// that we are using proxying to restate-schema-api or restate-types
pub use restate_schema_api::endpoint::{EndpointMetadata, ProtocolType};
pub use restate_types::identifiers::{EndpointId, LambdaARN};

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ServiceEndpoint {
Http {
#[serde(with = "serde_with::As::<serde_with::DisplayFromStr>")]
#[cfg_attr(feature = "schema", schemars(with = "String"))]
uri: Uri,
protocol_type: ProtocolType,
#[serde(skip_serializing_if = "SerdeableHeaderHashMap::is_empty")]
#[serde(default)]
additional_headers: SerdeableHeaderHashMap,
},
Lambda {
arn: LambdaARN,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
assume_role_arn: Option<String>,
#[serde(skip_serializing_if = "SerdeableHeaderHashMap::is_empty")]
#[serde(default)]
additional_headers: SerdeableHeaderHashMap,
},
}

impl From<EndpointMetadata> for ServiceEndpoint {
fn from(value: EndpointMetadata) -> Self {
match value {
EndpointMetadata::Http {
address,
protocol_type,
delivery_options,
} => Self::Http {
uri: address,
protocol_type,
additional_headers: delivery_options.additional_headers.into(),
},
EndpointMetadata::Lambda {
arn,
assume_role_arn,
delivery_options,
} => Self::Lambda {
arn,
assume_role_arn: assume_role_arn.map(Into::into),
additional_headers: delivery_options.additional_headers.into(),
},
}
}
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterServiceEndpointRequest {
#[serde(flatten)]
pub endpoint_metadata: RegisterServiceEndpointMetadata,
/// # Additional headers
///
/// Additional headers added to the discover/invoke requests to the service endpoint.
pub additional_headers: Option<SerdeableHeaderHashMap>,
/// # Force
///
/// If `true`, it will override, if existing, any endpoint using the same `uri`.
/// Beware that this can lead in-flight invocations to an unrecoverable error state.
///
/// By default, this is `true` but it might change in future to `false`.
///
/// See the [versioning documentation](https://docs.restate.dev/services/upgrades-removal) for more information.
#[serde(default = "restate_serde_util::default::bool::<true>")]
pub force: bool,
}

#[serde_as]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RegisterServiceEndpointMetadata {
Http {
/// # Uri
///
/// Uri to use to discover/invoke the http service endpoint.
#[serde_as(as = "serde_with::DisplayFromStr")]
#[cfg_attr(feature = "schema", schemars(with = "String"))]
uri: Uri,
},
Lambda {
/// # ARN
///
/// ARN to use to discover/invoke the lambda service endpoint.
arn: String,
/// # Assume role ARN
///
/// Optional ARN of a role to assume when invoking this endpoint, to support role chaining
assume_role_arn: Option<String>,
},
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterServiceResponse {
pub name: String,
pub revision: ServiceRevision,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterServiceEndpointResponse {
pub id: EndpointId,
pub services: Vec<RegisterServiceResponse>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct ListServiceEndpointsResponse {
pub endpoints: Vec<ServiceEndpointResponse>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct ServiceEndpointResponse {
pub id: EndpointId,
#[serde(flatten)]
pub service_endpoint: ServiceEndpoint,
/// # Services
///
/// List of services exposed by this service endpoint.
pub services: Vec<RegisterServiceResponse>,
}
14 changes: 14 additions & 0 deletions crates/meta-rest-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

pub mod endpoints;
pub mod methods;
pub mod services;
pub mod subscriptions;
21 changes: 21 additions & 0 deletions crates/meta-rest-model/src/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use serde::{Deserialize, Serialize};

// Export schema types to be used by other crates without exposing the fact
// that we are using proxying to restate-schema-api or restate-types
pub use restate_schema_api::service::MethodMetadata;

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct ListServiceMethodsResponse {
pub methods: Vec<MethodMetadata>,
}
32 changes: 32 additions & 0 deletions crates/meta-rest-model/src/services.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use serde::{Deserialize, Serialize};

// Export schema types to be used by other crates without exposing the fact
// that we are using proxying to restate-schema-api or restate-types
pub use restate_schema_api::service::{InstanceType, MethodMetadata, ServiceMetadata};
pub use restate_types::identifiers::ServiceRevision;

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct ListServicesResponse {
pub services: Vec<ServiceMetadata>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize)]
pub struct ModifyServiceRequest {
/// # Public
///
/// If true, the service can be invoked through the ingress.
/// If false, the service can be invoked only from another Restate service.
pub public: bool,
}
Loading

0 comments on commit bf04449

Please sign in to comment.