@@ -7,79 +7,82 @@ use crate::context::Context;
77use crate :: error:: { Error , Result , ResultExt as _} ;
88use crate :: yaml_file:: YamlFile ;
99
10- /// Import `profile`, a provider profile in YAML, and return its `id`, the
11- /// provider type it defines. Cleanup deletes the profile after the test's
12- /// sandboxes and the providers created after it.
13- pub async fn import_profile ( cx : & Context , profile : & str ) -> Result < String > {
14- #[ derive( Deserialize ) ]
15- struct Profile {
16- id : String ,
10+ impl Context {
11+ /// Import `profile`, a provider profile in YAML, and return its `id`, the
12+ /// provider type it defines. Cleanup deletes the profile after the test's
13+ /// sandboxes and the providers created after it.
14+ pub async fn import_profile ( & self , profile : & str ) -> Result < String > {
15+ #[ derive( Deserialize ) ]
16+ struct Profile {
17+ id : String ,
18+ }
19+ let id = serde_yml:: from_str :: < Profile > ( profile)
20+ . map_err ( |error| Error :: from_error ( & error) )
21+ . context ( "read the provider profile's id" ) ?
22+ . id ;
23+ // Deleting a missing profile succeeds, so register the delete before
24+ // the import.
25+ self . on_cleanup_delete ( "profile" , & id) ;
26+ let file = YamlFile :: new ( profile) ?;
27+ self . cli ( [ "profile" , "import" , "--file" , file. path ( ) ?] )
28+ . await ?
29+ . success ( )
30+ . context ( format ! ( "import profile {id}" ) ) ?;
31+ Ok ( id)
1732 }
18- let id = serde_yml:: from_str :: < Profile > ( profile)
19- . map_err ( |error| Error :: from_error ( & error) )
20- . context ( "read the provider profile's id" ) ?
21- . id ;
22- // Deleting a missing profile succeeds, so register the delete before the
23- // import.
24- on_cleanup_delete ( cx, "profile" , & id) ;
25- let file = YamlFile :: new ( profile) ?;
26- cx. cli ( [ "profile" , "import" , "--file" , file. path ( ) ?] )
27- . await ?
28- . success ( )
29- . context ( format ! ( "import profile {id}" ) ) ?;
30- Ok ( id)
31- }
3233
33- /// Create a provider called `name` of `provider_type` with `credentials`, as
34- /// pairs of environment variable and value. Cleanup deletes the provider
35- /// after the test's sandboxes.
36- ///
37- /// The CLI reads the credential values from its environment, so they aren't
38- /// in the reported command.
39- pub async fn create_provider (
40- cx : & Context ,
41- name : & str ,
42- provider_type : & str ,
43- credentials : & [ ( & str , & str ) ] ,
44- ) -> Result {
45- // Deleting a missing provider succeeds, so register the delete before the
46- // create.
47- on_cleanup_delete ( cx, "provider" , name) ;
48- let mut args = vec ! [
49- "provider" ,
50- "create" ,
51- "--name" ,
52- name,
53- "--type" ,
54- provider_type,
55- ] ;
56- for ( key, _) in credentials {
57- args. extend ( [ "--credential" , key] ) ;
34+ /// Create a provider called `name` of `provider_type` with `credentials`,
35+ /// as pairs of environment variable and value. Cleanup deletes the
36+ /// provider after the test's sandboxes.
37+ ///
38+ /// The CLI reads the credential values from its environment, so they
39+ /// aren't in the reported command.
40+ pub async fn create_provider (
41+ & self ,
42+ name : & str ,
43+ provider_type : & str ,
44+ credentials : & [ ( & str , & str ) ] ,
45+ ) -> Result {
46+ // Deleting a missing provider succeeds, so register the delete before
47+ // the create.
48+ self . on_cleanup_delete ( "provider" , name) ;
49+ let mut args = vec ! [
50+ "provider" ,
51+ "create" ,
52+ "--name" ,
53+ name,
54+ "--type" ,
55+ provider_type,
56+ ] ;
57+ for ( key, _) in credentials {
58+ args. extend ( [ "--credential" , key] ) ;
59+ }
60+ self . cli_with_env ( credentials, args)
61+ . await ?
62+ . success ( )
63+ . context ( format ! ( "create provider {name}" ) ) ?;
64+ Ok ( ( ) )
5865 }
59- cx. cli_with_env ( credentials, args)
60- . await ?
61- . success ( )
62- . context ( format ! ( "create provider {name}" ) ) ?;
63- Ok ( ( ) )
64- }
6566
66- /// Import `profile` with [`import_profile`] and create a provider called
67- /// `name` of its type with [`create_provider`].
68- pub async fn install_provider (
69- cx : & Context ,
70- name : & str ,
71- profile : & str ,
72- credentials : & [ ( & str , & str ) ] ,
73- ) -> Result {
74- let provider_type = import_profile ( cx, profile) . await ?;
75- create_provider ( cx, name, & provider_type, credentials) . await
76- }
67+ /// Import `profile` with [`Context::import_profile`] and create a provider
68+ /// called `name` of its type with [`Context::create_provider`].
69+ pub async fn install_provider (
70+ & self ,
71+ name : & str ,
72+ profile : & str ,
73+ credentials : & [ ( & str , & str ) ] ,
74+ ) -> Result {
75+ let provider_type = self . import_profile ( profile) . await ?;
76+ self . create_provider ( name, & provider_type, credentials)
77+ . await
78+ }
7779
78- fn on_cleanup_delete ( cx : & Context , kind : & ' static str , name : & str ) {
79- let cli = cx. cli_handle ( ) ;
80- let owned = name. to_string ( ) ;
81- cx. on_cleanup ( format ! ( "delete {kind} {name}" ) , async move || {
82- cli. run ( [ kind, "delete" , & owned] ) . await ?. success ( ) ?;
83- Ok ( ( ) )
84- } ) ;
80+ fn on_cleanup_delete ( & self , kind : & ' static str , name : & str ) {
81+ let cli = self . cli_handle ( ) ;
82+ let owned = name. to_string ( ) ;
83+ self . on_cleanup ( format ! ( "delete {kind} {name}" ) , async move || {
84+ cli. run ( [ kind, "delete" , & owned] ) . await ?. success ( ) ?;
85+ Ok ( ( ) )
86+ } ) ;
87+ }
8588}
0 commit comments