Skip to content

Commit

Permalink
Add support for device identity operations (#142)
Browse files Browse the repository at this point in the history
* Initial work on identities for modules and devices

* Add create/update device identity feature

* Add missing docs to all enums/structs members and derive default for DeviceCapabilities

* Fix formatting
  • Loading branch information
corollaries authored Jan 18, 2021
1 parent e1da0cd commit 5aa5e8e
Show file tree
Hide file tree
Showing 4 changed files with 567 additions and 42 deletions.
56 changes: 56 additions & 0 deletions sdk/iothub/examples/device_identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use iothub::service::identity::{DesiredCapability, Status};
use iothub::service::ServiceClient;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");

let device_id = std::env::args()
.nth(1)
.expect("Please pass the device id as the first parameter");

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

let service_client = ServiceClient::from_connection_string(iothub_connection_string, 3600)?;
let device = service_client
.create_device_identity()
.device_id(device_id)
.authentication_using_sas(
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk=",
)
.status(Status::Enabled)
.execute()
.await?;

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

println!(
"Setting status to disabled and set IoT Edge capability of device '{}'",
device.device_id
);
let device = service_client
.update_device_identity(device.etag)
.device_id(device.device_id)
.device_capability(DesiredCapability::IotEdge)
.authentication_using_sas(
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk=",
)
.status(Status::Disabled)
.execute()
.await?;

println!("Getting device identity of '{}'", device.device_id);
let device = service_client.get_device_identity(device.device_id).await?;
println!("Identity is: {:?}", device);

println!("Deleting device '{}'", device.device_id);
service_client
.delete_device_identity(device.device_id, None)
.await?;

Ok(())
}
Loading

0 comments on commit 5aa5e8e

Please sign in to comment.