Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added openapi spec generation to RestRouter #329

Merged
merged 5 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"wick-rpc/client",
"wick-packet/datetime",
"wick-packet/invocation",
"wick-packet/std"
"wick-packet/std",
"wick-interface-http/localgen",
"wick-interface-cli/localgen"
],
"rust-analyzer.rustfmt.overrideCommand": [
"rustup",
Expand Down
12 changes: 12 additions & 0 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 @@ -128,6 +128,7 @@ nuid = "0.4"
num-traits = "0.2"
oci-distribution = { version = "0.9", default-features = false }
once_cell = "1.8"
openapiv3 = "1.0"
openssl = "0.10"
option-utils = "0.1"
opentelemetry = { version = "0.18.0" }
Expand Down
6 changes: 3 additions & 3 deletions crates/bins/wick/src/commands/new/component/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use clap::Args;
use structured_output::StructuredOutput;
use wick_config::config::components::{Codec, HttpClientComponentConfigBuilder, HttpClientOperationDefinitionBuilder};
use wick_config::config::{self, ComponentConfiguration, ResourceBinding, ResourceDefinition, UrlResource};
use wick_config::config::components::{HttpClientComponentConfigBuilder, HttpClientOperationDefinitionBuilder};
use wick_config::config::{self, Codec, ComponentConfiguration, ResourceBinding, ResourceDefinition, UrlResource};
use wick_interface_types::{Field, Type};

use crate::io::File;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) async fn handle(
.resource(resource_name)
.operations([HttpClientOperationDefinitionBuilder::default()
.name("operation_name".to_owned())
.method(config::components::HttpMethod::Get)
.method(config::HttpMethod::Get)
.path("/user/{id:string}".to_owned())
.inputs([Field::new("id", Type::String)])
.build()
Expand Down
5 changes: 5 additions & 0 deletions crates/bins/wick/src/commands/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Subcommand;

pub(crate) mod login;
pub(crate) mod manifest;
pub(crate) mod pull;
pub(crate) mod push;

Expand All @@ -17,4 +18,8 @@ pub(crate) enum SubCommands {
/// Save the credentials for a registry.
#[clap(name = "login")]
Login(login::Options),

/// Retrieve the manifest for a package.
#[clap(name = "manifest")]
Manifest(manifest::Options),
}
123 changes: 123 additions & 0 deletions crates/bins/wick/src/commands/registry/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::collections::HashMap;

use anyhow::Result;
use clap::Args;
use serde_json::json;
use structured_output::StructuredOutput;
use wick_oci_utils::OciDescriptor;

use crate::options::get_auth_for_scope;
#[derive(Debug, Clone, Args)]
#[clap(rename_all = "kebab-case")]
#[group(skip)]
pub(crate) struct Options {
/// OCI reference to pull.
#[clap(action)]
pub(crate) reference: String,

/// Registry to use (overriding configured registry)
#[clap(long = "registry", action)]
pub(crate) registry: Option<String>,

#[clap(flatten)]
pub(crate) oci_opts: crate::oci::Options,
}

#[allow(clippy::unused_async)]
pub(crate) async fn handle(
opts: Options,
settings: wick_settings::Settings,
span: tracing::Span,
) -> Result<StructuredOutput> {
let _enter = span.enter();
let configured_creds = settings
.credentials
.iter()
.find(|c| opts.reference.starts_with(&c.scope));

let (username, password) = get_auth_for_scope(
configured_creds,
opts.oci_opts.username.as_deref(),
opts.oci_opts.password.as_deref(),
);

let mut oci_opts = wick_oci_utils::OciOptions::default();
oci_opts
.set_allow_insecure(opts.oci_opts.insecure_registries)
.set_allow_latest(true)
.set_username(username)
.set_password(password);

span.in_scope(|| debug!(options=?oci_opts, reference= opts.reference, "pulling reference"));

let (manifest, digest) = wick_oci_utils::fetch_image_manifest(&opts.reference, &oci_opts).await?;

let manifest_layers = manifest
.layers
.iter()
.enumerate()
.map(|(i, desc)| format!(" {}:\n{}", i, print_oci_descriptor(desc, 4)))
.collect::<Vec<_>>()
.join("\n");

let text = format!(
r#"# {}:

Digest: {}
Version: {}
Media Type: {}
Config:
{}
Annotations:
{}
Layers:
{}
"#,
opts.reference,
digest,
manifest.schema_version,
manifest.media_type.as_deref().unwrap_or_default(),
print_oci_descriptor(&manifest.config, 2),
print_annotations(&manifest.annotations, 2),
manifest_layers,
);

span.in_scope(|| debug!(%manifest, reference= opts.reference, "pulled manifest"));
let json = json!({"manifest":&manifest, "digest":digest});

Ok(StructuredOutput::new(text, json))
}

fn print_annotations(annotations: &Option<HashMap<String, String>>, indent: u8) -> String {
if let Some(annotations) = annotations {
return annotations
.iter()
.map(|(key, val)| format!("{}{}: {}", " ".repeat(indent as usize), key, val))
.collect::<Vec<_>>()
.join("\n");
}
"".to_owned()
}

fn print_oci_descriptor(descriptor: &OciDescriptor, indent: u8) -> String {
let mut text = vec![format!(
"{}Media Type: {}",
" ".repeat(indent as usize),
descriptor.media_type
)];
text.push(format!("{}Digest: {}", " ".repeat(indent as usize), descriptor.digest));
text.push(format!("{}Size: {}", " ".repeat(indent as usize), descriptor.size));
if let Some(urls) = &descriptor.urls {
if !urls.is_empty() {
text.push(format!("{}URLs: {}", " ".repeat(indent as usize), urls.join(", ")));
}
}
if descriptor.annotations.is_some() {
text.push(format!(
"{}Annotations:\n{}",
" ".repeat(indent as usize),
print_annotations(&descriptor.annotations, indent + 2)
));
}
text.join("\n")
}
1 change: 1 addition & 0 deletions crates/bins/wick/src/commands/registry/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) struct Options {
#[clap(flatten)]
pub(crate) oci_opts: crate::oci::Options,

/// Registry to use (overriding configured registry)
#[clap(long = "registry", action)]
pub(crate) registry: Option<String>,

Expand Down
1 change: 1 addition & 0 deletions crates/bins/wick/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ async fn async_main(cli: Cli, settings: wick_settings::Settings) -> Result<Struc
commands::registry::SubCommands::Push(cmd) => commands::registry::push::handle(cmd, settings, span).await,
commands::registry::SubCommands::Pull(cmd) => commands::registry::pull::handle(cmd, settings, span).await,
commands::registry::SubCommands::Login(cmd) => commands::registry::login::handle(cmd, settings, span).await,
commands::registry::SubCommands::Manifest(cmd) => commands::registry::manifest::handle(cmd, settings, span).await,
},
CliCommand::Rpc(cmd) => match cmd {
commands::rpc::SubCommands::Invoke(cmd) => commands::rpc::invoke::handle(cmd, settings, span).await,
Expand Down
4 changes: 1 addition & 3 deletions crates/components/wick-http-client/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ use serde_json::{Map, Value};
use tracing::Span;
use url::Url;
use wick_config::config::components::{
Codec,
ComponentConfig,
HttpClientComponentConfig,
HttpClientOperationDefinition,
HttpMethod,
OperationConfig,
};
use wick_config::config::{LiquidJsonConfig, Metadata, UrlResource};
use wick_config::config::{Codec, HttpMethod, LiquidJsonConfig, Metadata, UrlResource};
use wick_config::{ConfigValidation, Resolver};
use wick_interface_types::{ComponentSignature, OperationSignatures};
use wick_packet::{Base64Bytes, FluxChannel, Invocation, Observer, Packet, PacketSender, PacketStream, RuntimeConfig};
Expand Down
2 changes: 1 addition & 1 deletion crates/interfaces/wick-interface-http/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name: http
kind: wick/types@v1
metadata:
version: 0.2.0
version: 0.3.0
package:
registry:
host: registry.candle.dev
Expand Down
Loading