Skip to content

Commit

Permalink
Update edgelet to use tokio 1 (#5385)
Browse files Browse the repository at this point in the history
Updates aziot-edged's tokio dependency from 0.1 to 1.
  • Loading branch information
gordonwang0 authored Sep 2, 2021
1 parent 2e04730 commit 4c2f173
Show file tree
Hide file tree
Showing 361 changed files with 15,125 additions and 36,877 deletions.
1,321 changes: 267 additions & 1,054 deletions edge-modules/edgehub-proxy/Cargo.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions edge-modules/edgehub-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ edition = "2018"
[dependencies]
clap = "2.31"
env_logger = "0.5"
failure = "0.1"
failure_derive = "0.1"
hyper = "0.12.36"
hyper = "0.14"
thiserror = "1"
log = "0.4"
serde_json = "1.0"
tokio = "0.1"
tokio = { version = "1", features = ["rt","macros"] }
url = "1.7"
chrono = "0.4"

edgelet-http = { path = "../../edgelet/edgelet-http" }
edgelet-utils = { path = "../../edgelet/edgelet-utils" }
workload = { path = "../../edgelet/workload" }
edgelet-client = { path = "../../mqtt/edgelet-client" }

[profile.release]
lto = true
154 changes: 48 additions & 106 deletions edge-modules/edgehub-proxy/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,55 @@
// Copyright (c) Microsoft. All rights reserved.

use std::fmt::{self, Display};
use std::io;

use failure::{Backtrace, Context, Fail};
use thiserror::Error as ThisError;
use hyper::Error as HyperError;
use serde_json;
use url::ParseError;

use edgelet_http::Error as EdgeletHttpError;
use workload::apis::Error as WorkloadError;

#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
use chrono::ParseError;
use edgelet_client::Error as ClientError;
use edgelet_client::WorkloadError;



#[derive(Debug, ThisError)]
pub enum Error {

#[error("no value for required '{0}'")]
MissingVal(&'static str),
#[error("Hyper error")]
Hyper(
#[from]
#[source]
HyperError,
),
#[error("An IO error occurred.")]
Io(
#[from]
#[source]
io::Error,
),
#[error("Chrono date parse error")]
Parse(
#[from]
#[source]
ParseError,
),
#[error("Serde error")]
Serde(
#[from]
#[source]
serde_json::Error,
),
#[error("Edgelet client error")]
Client(
#[from]
#[source]
ClientError,
),
#[error("Workload api error")]
Workload(
#[from]
#[source]
WorkloadError,
),
}

#[derive(Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "Client error")]
Client(WorkloadError<serde_json::Value>),
#[fail(display = "Error connecting to endpoint")]
Http,
#[fail(display = "Hyper error")]
Hyper,
#[fail(display = "An IO error occurred.")]
Io,
#[fail(display = "Url parse error")]
Parse,
#[fail(display = "Serde error")]
Serde,
}

impl Fail for Error {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}

fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.inner, f)
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Error {
inner: Context::new(kind),
}
}
}

impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Self {
Error { inner }
}
}

impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error {
inner: error.context(ErrorKind::Io),
}
}
}

impl From<EdgeletHttpError> for Error {
fn from(error: EdgeletHttpError) -> Self {
Error {
inner: error.context(ErrorKind::Http),
}
}
}

impl From<HyperError> for Error {
fn from(error: HyperError) -> Self {
Error {
inner: error.context(ErrorKind::Hyper),
}
}
}

impl From<ParseError> for Error {
fn from(error: ParseError) -> Self {
Error {
inner: error.context(ErrorKind::Parse),
}
}
}

impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error {
inner: error.context(ErrorKind::Serde),
}
}
}

impl From<WorkloadError<serde_json::Value>> for Error {
fn from(error: WorkloadError<serde_json::Value>) -> Self {
match error {
WorkloadError::Hyper(h) => From::from(h),
WorkloadError::Serde(s) => From::from(s),
WorkloadError::Api(_) => From::from(ErrorKind::Client(error)),
}
}
}
81 changes: 27 additions & 54 deletions edge-modules/edgehub-proxy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
// Copyright (c) Microsoft. All rights reserved.


use std::fs;
use std::process;

use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg, SubCommand};
use edgelet_http::UrlConnector;
use hyper::Client;

use log::info;
use url::Url;

use chrono::Utc;
use chrono::DateTime;
use edgehub_proxy::error::Error;
use edgehub_proxy::logging;
use workload::apis::client::APIClient;
use workload::apis::configuration::Configuration;
use workload::models::ServerCertificateRequest;
use edgelet_client::workload;

fn main() {
#[tokio::main]
async fn main() {
logging::init();
if let Err(e) = run() {
if let Err(e) = run().await {
logging::log_error(&e);
process::exit(1);
}
}

fn run() -> Result<(), Error> {
async fn run() -> Result<(), Error> {
let matches = App::new(crate_name!())
.version(crate_version!())
.about(crate_description!())
Expand Down Expand Up @@ -126,41 +127,34 @@ fn run() -> Result<(), Error> {
)
.get_matches();

let mut tokio_runtime = tokio::runtime::current_thread::Runtime::new()?;
let url = Url::parse(
matches
.value_of("host")
.expect("no value for required HOST"),
)?;
let client = client(&url)?;

let api_version = matches
.value_of("apiversion")
.expect("no default value for API_VERSION");

let url = matches
.value_of("host")
.ok_or(Error::MissingVal("HOST"))?;
let client = workload(url)?;

let module = matches
.value_of("moduleid")
.expect("no value for required MODULEID");
.ok_or(Error::MissingVal("MODULEID"))?;
let gen = matches
.value_of("genid")
.expect("no value for required GENID");
.ok_or(Error::MissingVal("GENID"))?;

if let ("cert-server", Some(args)) = matches.subcommand() {
let common_name = args
.value_of("common name")
.expect("no value for required COMMON_NAME");
let expiration = args
.value_of("expiration")
.expect("no value for required EXPIRATION");
.ok_or(Error::MissingVal("COMMON_NAME"))?;
let expiration = DateTime::parse_from_rfc3339(
args
.value_of("expiration")
.ok_or(Error::MissingVal("EXPIRATION"))?)?;
let expiration_utc = expiration.with_timezone(&Utc);
info!("Retrieving server certificate with common name \"{}\" and expiration \"{}\" from {}...", common_name, expiration, url);

let cert_request =
ServerCertificateRequest::new(common_name.to_string(), expiration.to_string());
let request =
client
.workload_api()
.create_server_certificate(api_version, module, gen, cert_request);

let response = tokio_runtime.block_on(request)?;
let response =
client.create_server_cert(module, gen, common_name, expiration_utc).await?;

info!("Retrieved server certificate.");

if let Some(crt_path) = args.value_of("crt file") {
Expand Down Expand Up @@ -199,24 +193,3 @@ fn run() -> Result<(), Error> {
}
Ok(())
}

fn client(url: &Url) -> Result<APIClient, Error> {
let hyper_client = Client::builder().build(UrlConnector::new(&url)?);
let base_path = get_base_path(url);
let mut configuration = Configuration::new(hyper_client);
configuration.base_path = base_path.to_string();

let scheme = url.scheme().to_string();
configuration.uri_composer = Box::new(move |base_path, path| {
Ok(UrlConnector::build_hyper_uri(&scheme, base_path, path)?)
});
let client = APIClient::new(configuration);
Ok(client)
}

fn get_base_path(url: &Url) -> &str {
match url.scheme() {
"unix" => url.path(),
_ => url.as_str(),
}
}
Loading

0 comments on commit 4c2f173

Please sign in to comment.