@@ -3,6 +3,8 @@ use crate::cli::common::CloudArg;
3
3
use camino:: Utf8PathBuf ;
4
4
use tedge_config:: OptionalConfigError ;
5
5
use tedge_config:: ProfileName ;
6
+ use tedge_config:: ReadError ;
7
+ use tedge_config:: TEdgeConfig ;
6
8
7
9
use super :: create:: CreateCertCmd ;
8
10
use super :: create_csr:: CreateCsrCmd ;
@@ -93,11 +95,14 @@ impl BuildCommand for TEdgeCertCli {
93
95
cloud,
94
96
} => {
95
97
let cloud: Option < Cloud > = cloud. map ( <_ >:: try_into) . transpose ( ) ?;
98
+
96
99
// 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) ?
100
104
} ;
105
+
101
106
let cmd = CreateCsrCmd {
102
107
id,
103
108
key_path : config. device_key_path ( cloud. as_ref ( ) ) ?. to_owned ( ) ,
@@ -186,3 +191,17 @@ pub enum UploadCertCli {
186
191
profile : Option < ProfileName > ,
187
192
} ,
188
193
}
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