@@ -3,6 +3,8 @@ use crate::cli::common::CloudArg;
33use camino:: Utf8PathBuf ;
44use tedge_config:: OptionalConfigError ;
55use tedge_config:: ProfileName ;
6+ use tedge_config:: ReadError ;
7+ use tedge_config:: TEdgeConfig ;
68
79use super :: create:: CreateCertCmd ;
810use super :: create_csr:: CreateCsrCmd ;
@@ -93,11 +95,14 @@ impl BuildCommand for TEdgeCertCli {
9395 cloud,
9496 } => {
9597 let cloud: Option < Cloud > = cloud. map ( <_ >:: try_into) . transpose ( ) ?;
98+
9699 // Use the current device id if no id is provided
97- let id = match id {
98- Some ( id) => id,
99- None => config. device . id ( ) ?. clone ( ) ,
100+ let id = if let Some ( id) = id {
101+ id
102+ } else {
103+ get_device_id_from_config ( & config, & cloud) ?
100104 } ;
105+
101106 let cmd = CreateCsrCmd {
102107 id,
103108 key_path : config. device_key_path ( cloud. as_ref ( ) ) ?. to_owned ( ) ,
@@ -186,3 +191,17 @@ pub enum UploadCertCli {
186191 profile : Option < ProfileName > ,
187192 } ,
188193}
194+
195+ fn get_device_id_from_config (
196+ config : & TEdgeConfig ,
197+ cloud : & Option < Cloud > ,
198+ ) -> Result < String , ReadError > {
199+ let id = match cloud {
200+ None => config. device . id ( ) ,
201+ Some ( Cloud :: C8y ( profile) ) => config. c8y . try_get ( profile. as_deref ( ) ) ?. device . id ( ) ,
202+ Some ( Cloud :: Azure ( profile) ) => config. az . try_get ( profile. as_deref ( ) ) ?. device . id ( ) ,
203+ Some ( Cloud :: Aws ( profile) ) => config. aws . try_get ( profile. as_deref ( ) ) ?. device . id ( ) ,
204+ } ?
205+ . to_owned ( ) ;
206+ Ok ( id)
207+ }
0 commit comments