Skip to content

Migrate iothub to pipelines #972

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

Merged
merged 3 commits into from
Aug 4, 2022
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
2 changes: 2 additions & 0 deletions sdk/core/src/policies/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod custom_headers_policy;
mod retry_policies;
mod telemetry_policy;
mod timeout_policy;
mod transport;

pub use custom_headers_policy::{CustomHeaders, CustomHeadersPolicy};
pub use retry_policies::*;
pub use telemetry_policy::*;
pub use timeout_policy::*;
pub use transport::*;

use crate::{Context, Request, Response};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::request_options::Timeout;
use crate::{AppendToUrlQuery, Context, Policy, PolicyResult, Request};
use std::sync::Arc;

use azure_core::{prelude::*, Context, Policy, PolicyResult, Request};

#[derive(Debug, Clone, Default)]
pub struct TimeoutPolicy {
default_timeout: Option<Timeout>,
Expand Down
3 changes: 2 additions & 1 deletion sdk/iot_hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "Azure IoT Hub"
license = "MIT"

[dependencies]
async-trait = "0.1"
azure_core = { path = "../core", version = "0.3", default_features = false }
base64 = "0.13"
bytes = "1.0"
Expand All @@ -28,4 +29,4 @@ reqwest = "0.11.0"
tokio = { version = "1.0", features = ["macros"] }

[features]
default = ["azure_core/default"]
default = ["azure_core/default"]
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
}
});

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
service_client
.apply_on_edge_device(device_id)
.modules_content(modules_content)
Expand Down
7 changes: 2 additions & 5 deletions sdk/iot_hub/examples/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use azure_iot_hub::service::{resources::Configuration, ServiceClient};
use azure_iot_hub::service::ServiceClient;
use std::error::Error;

#[tokio::main]
Expand All @@ -10,9 +10,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.nth(1)
.expect("Please pass the configuration id as the first parameter");

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;

println!("Creating a new configuration with id: {}", configuration_id);

Expand Down Expand Up @@ -42,7 +40,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.get_configuration(configuration_id)
.into_future()
.await?;
let configuration: Configuration = configuration.try_into()?;

println!(
"Successfully retrieved the new configuration '{:?}'",
Expand Down
5 changes: 2 additions & 3 deletions sdk/iot_hub/examples/device_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.expect("Please pass the device id as the first parameter");

println!("Getting device twin for device '{}'", device_id);
let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
let device = service_client
.create_device_identity(
&device_id,
Expand All @@ -27,6 +25,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
)
.into_future()
.await?;
let device: DeviceIdentityResponse = device.try_into()?;

println!("Successfully created a new device '{}'", device.device_id);

Expand Down
4 changes: 1 addition & 3 deletions sdk/iot_hub/examples/directmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.nth(4)
.expect("Please pass the payload as the fourth parameter");

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
println!(
"Sending direct method {} to {}:{} on: {}",
method_name, device_id, module_id, service_client.iot_hub_name
Expand Down
4 changes: 1 addition & 3 deletions sdk/iot_hub/examples/gettwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

println!("Getting device twin for device: {}", device_id);

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
let twin = service_client
.get_device_twin(device_id)
.into_future()
Expand Down
4 changes: 1 addition & 3 deletions sdk/iot_hub/examples/module_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.nth(2)
.expect("Please pass the module id as the second parameter");

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
let module = service_client
.create_module_identity(
&device_id,
Expand Down
4 changes: 1 addition & 3 deletions sdk/iot_hub/examples/query_iothub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let query = "SELECT * FROM devices";
println!("Invoking query '{}' on the IoT Hub", query);

let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;

let response = service_client
.query(query)
Expand Down
4 changes: 1 addition & 3 deletions sdk/iot_hub/examples/updatetwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.expect("Please pass the payload as the second parameter");

println!("Updating device twin for device: {}", device_id);
let http_client = azure_core::new_http_client();

let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let service_client = ServiceClient::new_connection_string(iot_hub_connection_string, 3600)?;
let updated_twin = service_client
.update_device_twin(device_id)
.desired_properties(serde_json::from_str(&payload)?)
Expand Down
60 changes: 60 additions & 0 deletions sdk/iot_hub/src/authorization_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::service::IoTHubCredentials;
use azure_core::error::{ErrorKind, ResultExt};
use azure_core::{
headers::{self, *},
Context, Policy, PolicyResult, Request,
};
use std::sync::Arc;

const IOTHUB_TOKEN_SCOPE: &str = "https://iothubs.azure.net";

#[derive(Debug, Clone)]
pub struct AuthorizationPolicy {
credentials: IoTHubCredentials,
}

impl AuthorizationPolicy {
pub(crate) fn new(credentials: IoTHubCredentials) -> Self {
Self { credentials }
}
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl Policy for AuthorizationPolicy {
async fn send(
&self,
ctx: &Context,
request: &mut Request,
next: &[Arc<dyn Policy>],
) -> PolicyResult {
assert!(
!next.is_empty(),
"Authorization policies cannot be the last policy of a pipeline"
);
let request = match &self.credentials {
IoTHubCredentials::SASToken(sas_token) => {
request.insert_header(headers::AUTHORIZATION, sas_token);
request
}
IoTHubCredentials::BearerToken(token) => {
request.insert_header(AUTHORIZATION, format!("Bearer {}", token));
request
}
IoTHubCredentials::TokenCredential(token_credential) => {
let bearer_token = token_credential
.get_token(IOTHUB_TOKEN_SCOPE)
.await
.context(ErrorKind::Credential, "failed to get bearer token")?;

request.insert_header(
AUTHORIZATION,
format!("Bearer {}", bearer_token.token.secret()),
);
request
}
};

next[0].send(ctx, request, &next[1..]).await
}
}
1 change: 1 addition & 0 deletions sdk/iot_hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#![deny(missing_docs)]
//! The IoT Hub crate contains a client that can be used to manage the IoT Hub.

mod authorization_policy;
/// The service module contains the IoT Hub Service Client that can be used to manage the IoT Hub.
pub mod service;
Loading