11use super :: getter:: Identifiers ;
22use crate :: agent_control:: agent_id:: AgentID ;
3- use crate :: agent_control:: defaults:: IDENTIFIERS_FILENAME ;
3+ use crate :: agent_control:: defaults:: { CM_NAME_OPAMP_DATA_PREFIX , INSTANCE_ID_FILENAME } ;
44use crate :: opamp:: instance_id:: getter:: DataStored ;
55use crate :: opamp:: instance_id:: storer:: InstanceIDStorer ;
66use fs:: LocalFile ;
2020 file_rw : F ,
2121 dir_manager : D ,
2222 agent_control_remote_dir : PathBuf ,
23- agent_remote_dir : PathBuf ,
2423}
2524
2625#[ derive( thiserror:: Error , Debug ) ]
@@ -67,17 +66,11 @@ where
6766 D : DirectoryManager ,
6867 F : FileWriter + FileReader ,
6968{
70- pub fn new (
71- file_rw : F ,
72- dir_manager : D ,
73- agent_control_remote_dir : PathBuf ,
74- agent_remote_dir : PathBuf ,
75- ) -> Self {
69+ pub fn new ( file_rw : F , dir_manager : D , agent_control_remote_dir : PathBuf ) -> Self {
7670 Self {
7771 file_rw,
7872 dir_manager,
7973 agent_control_remote_dir,
80- agent_remote_dir,
8174 }
8275 }
8376}
@@ -128,14 +121,14 @@ where
128121 }
129122 }
130123
124+ pub fn build_folder_name ( & self , agent_id : & AgentID , prefix : & str ) -> String {
125+ format ! ( "{prefix}{agent_id}" )
126+ }
127+
131128 fn get_instance_id_path ( & self , agent_id : & AgentID ) -> PathBuf {
132- if agent_id == & AgentID :: AgentControl {
133- self . agent_control_remote_dir . join ( IDENTIFIERS_FILENAME )
134- } else {
135- self . agent_remote_dir
136- . join ( agent_id)
137- . join ( "identifiers.yaml" )
138- }
129+ self . agent_control_remote_dir
130+ . join ( self . build_folder_name ( agent_id, CM_NAME_OPAMP_DATA_PREFIX ) )
131+ . join ( INSTANCE_ID_FILENAME )
139132 }
140133}
141134
@@ -156,26 +149,32 @@ mod tests {
156149 #[ test]
157150 fn basic_get_uild_path ( ) {
158151 let sa_dir = PathBuf :: from ( "/super" ) ;
159- let sub_agent_dir = PathBuf :: from ( "/sub" ) ;
160152 let storer = Storer :: new (
161153 MockLocalFile :: default ( ) ,
162154 MockDirectoryManager :: default ( ) ,
163155 sa_dir. clone ( ) ,
164- sub_agent_dir. clone ( ) ,
165156 ) ;
166157 let agent_id = AgentID :: try_from ( "test" ) . unwrap ( ) ;
167158 let path = storer. get_instance_id_path ( & agent_id) ;
168- assert_eq ! ( path, sub_agent_dir. join( "test" ) . join( "identifiers.yaml" ) ) ;
159+ assert_eq ! (
160+ path,
161+ sa_dir. join( "fleet-data-test" ) . join( "instance_id.yaml" )
162+ ) ;
169163
170164 let agent_control_id = AgentID :: AgentControl ;
171165 let path = storer. get_instance_id_path ( & agent_control_id) ;
172- assert_eq ! ( path, sa_dir. join( "identifiers.yaml" ) ) ;
166+ assert_eq ! (
167+ path,
168+ sa_dir
169+ . join( "fleet-data-agent-control" )
170+ . join( "instance_id.yaml" )
171+ ) ;
173172 }
174173
175174 #[ test]
176175 fn test_successful_write ( ) {
177176 // Data
178- let ( agent_id, sa_path, sub_agent_path , instance_id_path) = test_data ( ) ;
177+ let ( agent_id, sa_path, instance_id_path) = test_data ( ) ;
179178 let mut file_rw = MockLocalFile :: default ( ) ;
180179 let mut dir_manager = MockDirectoryManager :: default ( ) ;
181180 let instance_id = InstanceID :: create ( ) ;
@@ -188,14 +187,14 @@ mod tests {
188187 dir_manager. should_create ( instance_id_path. parent ( ) . unwrap ( ) ) ;
189188 file_rw. should_write ( & instance_id_path, expected_file ( instance_id) ) ;
190189
191- let storer = Storer :: new ( file_rw, dir_manager, sa_path, sub_agent_path ) ;
190+ let storer = Storer :: new ( file_rw, dir_manager, sa_path) ;
192191 assert ! ( storer. set( & agent_id, & ds) . is_ok( ) ) ;
193192 }
194193
195194 #[ test]
196195 fn test_unsuccessful_write ( ) {
197196 // Data
198- let ( agent_id, sa_path, sub_agent_path , instance_id_path) = test_data ( ) ;
197+ let ( agent_id, sa_path, instance_id_path) = test_data ( ) ;
199198 let mut file_rw = MockLocalFile :: default ( ) ;
200199 let mut dir_manager = MockDirectoryManager :: default ( ) ;
201200 let instance_id = InstanceID :: create ( ) ;
@@ -208,14 +207,14 @@ mod tests {
208207 file_rw. should_not_write ( & instance_id_path, expected_file ( instance_id) ) ;
209208 dir_manager. should_create ( instance_id_path. parent ( ) . unwrap ( ) ) ;
210209
211- let storer = Storer :: new ( file_rw, dir_manager, sa_path, sub_agent_path ) ;
210+ let storer = Storer :: new ( file_rw, dir_manager, sa_path) ;
212211 assert ! ( storer. set( & agent_id, & ds) . is_err( ) ) ;
213212 }
214213
215214 #[ test]
216215 fn test_successful_read ( ) {
217216 // Data
218- let ( agent_id, sa_path, sub_agent_path , instance_id_path) = test_data ( ) ;
217+ let ( agent_id, sa_path, instance_id_path) = test_data ( ) ;
219218 let mut file_rw = MockLocalFile :: default ( ) ;
220219 let dir_manager = MockDirectoryManager :: default ( ) ;
221220 let instance_id = InstanceID :: create ( ) ;
@@ -234,15 +233,15 @@ mod tests {
234233 . once ( )
235234 . return_once ( |_| Ok ( expected_file ( instance_id) ) ) ;
236235
237- let storer = Storer :: new ( file_rw, dir_manager, sa_path, sub_agent_path ) ;
236+ let storer = Storer :: new ( file_rw, dir_manager, sa_path) ;
238237 let actual = storer. get ( & agent_id) ;
239238 assert ! ( actual. is_ok( ) ) ;
240239 assert_eq ! ( expected, actual. unwrap( ) ) ;
241240 }
242241
243242 #[ test]
244243 fn test_unsuccessful_read ( ) {
245- let ( agent_id, sa_path, sub_agent_path , instance_id_path) = test_data ( ) ;
244+ let ( agent_id, sa_path, instance_id_path) = test_data ( ) ;
246245 let mut file_rw = MockLocalFile :: default ( ) ;
247246 let dir_manager = MockDirectoryManager :: default ( ) ;
248247
@@ -254,7 +253,7 @@ mod tests {
254253 . once ( )
255254 . return_once ( |_| Err ( io:: Error :: other ( "some error message" ) . into ( ) ) ) ;
256255
257- let storer = Storer :: new ( file_rw, dir_manager, sa_path, sub_agent_path ) ;
256+ let storer = Storer :: new ( file_rw, dir_manager, sa_path) ;
258257 let expected = storer. get ( & agent_id) ;
259258
260259 // As said above, we are not generating the error variant here
@@ -270,12 +269,11 @@ mod tests {
270269 const HOST_ID : & str = "test-host-id" ;
271270 const FLEET_ID : & str = "test-fleet-id" ;
272271
273- fn test_data ( ) -> ( AgentID , PathBuf , PathBuf , PathBuf ) {
272+ fn test_data ( ) -> ( AgentID , PathBuf , PathBuf ) {
274273 let agent_id = AgentID :: try_from ( "test" ) . unwrap ( ) ;
275274 let sa_path = PathBuf :: from ( "/super" ) ;
276- let sub_agent_path = PathBuf :: from ( "/sub" ) ;
277- let instance_id_path = PathBuf :: from ( "/sub/test/identifiers.yaml" ) ;
278- ( agent_id, sa_path, sub_agent_path, instance_id_path)
275+ let instance_id_path = PathBuf :: from ( "/super/fleet-data-test/instance_id.yaml" ) ;
276+ ( agent_id, sa_path, instance_id_path)
279277 }
280278
281279 fn expected_file ( instance_id : InstanceID ) -> String {
0 commit comments