diff --git a/edgelet/Cargo.lock b/edgelet/Cargo.lock index 4881c292618..dfdb15e7f12 100644 --- a/edgelet/Cargo.lock +++ b/edgelet/Cargo.lock @@ -1306,6 +1306,7 @@ dependencies = [ "atty", "aziot-certd-config", "aziot-identity-common", + "aziot-identity-common-http", "aziot-identityd-config", "aziot-keyd-config", "aziot-keys-common", @@ -1331,6 +1332,7 @@ dependencies = [ "hyper", "hyper-proxy", "hyper-tls", + "identity-client", "lazy_static", "libc", "log", diff --git a/edgelet/identity-client/src/client.rs b/edgelet/identity-client/src/client.rs index 35437405c41..ad249fb3ce4 100644 --- a/edgelet/identity-client/src/client.rs +++ b/edgelet/identity-client/src/client.rs @@ -56,7 +56,7 @@ impl IdentityClient { let res = build_request_uri(&self.host, &uri) .into_future() .and_then(move |uri| { - request::<_, _, ()>(&client, hyper::Method::POST, &uri, Some(&body)) + request_no_content::<_, _>(&client, hyper::Method::POST, &uri, Some(&body)) }); Box::new(res) diff --git a/edgelet/iotedge/Cargo.toml b/edgelet/iotedge/Cargo.toml index e72b4511374..e3d64b0fc34 100644 --- a/edgelet/iotedge/Cargo.toml +++ b/edgelet/iotedge/Cargo.toml @@ -39,6 +39,7 @@ zip = "0.5.3" aziot-certd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } aziot-identity-common = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } +aziot-identity-common-http = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } aziot-identityd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } aziot-keyd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } aziot-keys-common = { git = "https://github.com/Azure/iot-identity-service", branch = "release/1.2" } @@ -51,6 +52,7 @@ edgelet-docker = { path = "../edgelet-docker" } edgelet-http = { path = "../edgelet-http" } edgelet-http-mgmt = { path = "../edgelet-http-mgmt" } edgelet-utils = { path = "../edgelet-utils" } +identity-client = { path = "../identity-client" } management = { path = "../management" } support-bundle = { path = "../support-bundle" } diff --git a/edgelet/iotedge/src/main.rs b/edgelet/iotedge/src/main.rs index 462b0476e84..b8673f9de2d 100644 --- a/edgelet/iotedge/src/main.rs +++ b/edgelet/iotedge/src/main.rs @@ -312,6 +312,10 @@ fn run() -> Result<(), Error> { .required(true), ) ) + .subcommand( + SubCommand::with_name("reprovision") + .about("Reprovision device with IoT Hub.") + ) ) .subcommand( SubCommand::with_name("support-bundle") @@ -518,6 +522,7 @@ fn run() -> Result<(), Error> { log::Level::from_str(args.value_of("log_level").expect("Value is required")) .expect("Value is restricted to parsable fields"), ), + ("reprovision", Some(_args)) => System::reprovision(&mut tokio_runtime), (command, _) => { eprintln!("Unknown system subcommand {:?}", command); diff --git a/edgelet/iotedge/src/system.rs b/edgelet/iotedge/src/system.rs index 70eae81ad2f..ae116c2a25d 100644 --- a/edgelet/iotedge/src/system.rs +++ b/edgelet/iotedge/src/system.rs @@ -9,6 +9,9 @@ use aziotctl_common::{ SERVICE_DEFINITIONS as IS_SERVICES, }; +use aziot_identity_common_http::ApiVersion; +use identity_client::IdentityClient; + use crate::error::{Error, ErrorKind}; lazy_static! { @@ -71,4 +74,26 @@ impl System { Error::from(ErrorKind::System) }) } + + pub fn reprovision(runtime: &mut tokio::runtime::Runtime) -> Result<(), Error> { + let uri = url::Url::parse("unix:///run/aziot/identityd.sock") + .expect("hard-coded URI should parse"); + let client = IdentityClient::new(ApiVersion::V2020_09_01, &uri); + + runtime + .block_on(client.reprovision_device()) + .map_err(|err| { + eprintln!("Failed to reprovision: {}", err); + Error::from(ErrorKind::System) + })?; + + println!("Successfully reprovisioned with IoT Hub."); + + restart(&[&IOTEDGED]).map_err(|err| { + eprintln!("{:#?}", err); + Error::from(ErrorKind::System) + })?; + + Ok(()) + } }