|
1 | 1 | // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
| 4 | +use std::future::{Future, IntoFuture}; |
| 5 | +use std::pin::Pin; |
| 6 | + |
4 | 7 | use serde::Deserialize; |
5 | 8 |
|
6 | 9 | use crate::context::Context; |
@@ -31,57 +34,126 @@ impl Context { |
31 | 34 | } |
32 | 35 |
|
33 | 36 | /// Create a provider called `name` of `provider_type` with `credentials`, |
34 | | - /// as pairs of environment variable and value. Cleanup deletes the |
35 | | - /// provider after the test's sandboxes. |
| 37 | + /// as pairs of environment variable and value, when awaited. Cleanup |
| 38 | + /// deletes the provider after the test's sandboxes. |
36 | 39 | /// |
37 | 40 | /// The CLI reads the credential values from its environment, so they |
38 | | - /// aren't in the reported command. |
39 | | - pub async fn create_provider( |
40 | | - &self, |
41 | | - name: &str, |
42 | | - provider_type: &str, |
43 | | - credentials: &[(&str, &str)], |
44 | | - ) -> Result { |
| 41 | + /// aren't in the reported command. [`ProviderBuilder`] sets other |
| 42 | + /// `provider create` options before the await. |
| 43 | + pub fn create_provider<'a>( |
| 44 | + &'a self, |
| 45 | + name: &'a str, |
| 46 | + provider_type: &'a str, |
| 47 | + credentials: &'a [(&'a str, &'a str)], |
| 48 | + ) -> ProviderBuilder<'a> { |
| 49 | + ProviderBuilder::new(self, name, ProviderType::Named(provider_type), credentials) |
| 50 | + } |
| 51 | + |
| 52 | + /// Import `profile` with [`Context::import_profile`] and create a provider |
| 53 | + /// called `name` of its type with [`Context::create_provider`], when |
| 54 | + /// awaited. |
| 55 | + pub fn install_provider<'a>( |
| 56 | + &'a self, |
| 57 | + name: &'a str, |
| 58 | + profile: &'a str, |
| 59 | + credentials: &'a [(&'a str, &'a str)], |
| 60 | + ) -> ProviderBuilder<'a> { |
| 61 | + ProviderBuilder::new(self, name, ProviderType::Profile(profile), credentials) |
| 62 | + } |
| 63 | + |
| 64 | + fn on_cleanup_delete(&self, kind: &'static str, name: &str) { |
| 65 | + let cli = self.cli_handle(); |
| 66 | + let owned = name.to_string(); |
| 67 | + self.on_cleanup(format!("delete {kind} {name}"), async move || { |
| 68 | + cli.run([kind, "delete", &owned]).await?.success()?; |
| 69 | + Ok(()) |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Where a [`ProviderBuilder`] gets its provider type. |
| 75 | +enum ProviderType<'a> { |
| 76 | + /// A profile already on the gateway. |
| 77 | + Named(&'a str), |
| 78 | + /// A profile, in YAML, to import first. |
| 79 | + Profile(&'a str), |
| 80 | +} |
| 81 | + |
| 82 | +/// A provider to create, from [`Context::create_provider`] or |
| 83 | +/// [`Context::install_provider`]. Awaiting it creates the provider. |
| 84 | +/// |
| 85 | +/// ```ignore |
| 86 | +/// cx.create_provider(&name, &provider_type, &[]) |
| 87 | +/// .config("project", "production") |
| 88 | +/// .await?; |
| 89 | +/// ``` |
| 90 | +#[must_use = "a ProviderBuilder does nothing until awaited"] |
| 91 | +pub struct ProviderBuilder<'a> { |
| 92 | + cx: &'a Context, |
| 93 | + name: &'a str, |
| 94 | + provider_type: ProviderType<'a>, |
| 95 | + credentials: &'a [(&'a str, &'a str)], |
| 96 | + config: Vec<String>, |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a> ProviderBuilder<'a> { |
| 100 | + fn new( |
| 101 | + cx: &'a Context, |
| 102 | + name: &'a str, |
| 103 | + provider_type: ProviderType<'a>, |
| 104 | + credentials: &'a [(&'a str, &'a str)], |
| 105 | + ) -> Self { |
| 106 | + Self { |
| 107 | + cx, |
| 108 | + name, |
| 109 | + provider_type, |
| 110 | + credentials, |
| 111 | + config: Vec::new(), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// Set the provider config `key` to `value` (`--config key=value`). |
| 116 | + pub fn config(mut self, key: &str, value: &str) -> Self { |
| 117 | + self.config.push(format!("{key}={value}")); |
| 118 | + self |
| 119 | + } |
| 120 | + |
| 121 | + async fn create(self) -> Result { |
| 122 | + let provider_type = match self.provider_type { |
| 123 | + ProviderType::Named(provider_type) => provider_type.to_string(), |
| 124 | + ProviderType::Profile(profile) => self.cx.import_profile(profile).await?, |
| 125 | + }; |
45 | 126 | // Deleting a missing provider succeeds, so register the delete before |
46 | 127 | // the create. |
47 | | - self.on_cleanup_delete("provider", name); |
| 128 | + self.cx.on_cleanup_delete("provider", self.name); |
48 | 129 | let mut args = vec![ |
49 | 130 | "provider", |
50 | 131 | "create", |
51 | 132 | "--name", |
52 | | - name, |
| 133 | + self.name, |
53 | 134 | "--type", |
54 | | - provider_type, |
| 135 | + &provider_type, |
55 | 136 | ]; |
56 | | - for (key, _) in credentials { |
| 137 | + for (key, _) in self.credentials { |
57 | 138 | args.extend(["--credential", key]); |
58 | 139 | } |
59 | | - self.cli_with_env(credentials, args) |
| 140 | + for setting in &self.config { |
| 141 | + args.extend(["--config", setting]); |
| 142 | + } |
| 143 | + self.cx |
| 144 | + .cli_with_env(self.credentials, args) |
60 | 145 | .await? |
61 | 146 | .success() |
62 | | - .context(format!("create provider {name}"))?; |
| 147 | + .context(format!("create provider {}", self.name))?; |
63 | 148 | Ok(()) |
64 | 149 | } |
| 150 | +} |
65 | 151 |
|
66 | | - /// Import `profile` with [`Context::import_profile`] and create a provider |
67 | | - /// called `name` of its type with [`Context::create_provider`]. |
68 | | - pub async fn install_provider( |
69 | | - &self, |
70 | | - name: &str, |
71 | | - profile: &str, |
72 | | - credentials: &[(&str, &str)], |
73 | | - ) -> Result { |
74 | | - let provider_type = self.import_profile(profile).await?; |
75 | | - self.create_provider(name, &provider_type, credentials) |
76 | | - .await |
77 | | - } |
| 152 | +impl<'a> IntoFuture for ProviderBuilder<'a> { |
| 153 | + type Output = Result; |
| 154 | + type IntoFuture = Pin<Box<dyn Future<Output = Result> + 'a>>; |
78 | 155 |
|
79 | | - fn on_cleanup_delete(&self, kind: &'static str, name: &str) { |
80 | | - let cli = self.cli_handle(); |
81 | | - let owned = name.to_string(); |
82 | | - self.on_cleanup(format!("delete {kind} {name}"), async move || { |
83 | | - cli.run([kind, "delete", &owned]).await?.success()?; |
84 | | - Ok(()) |
85 | | - }); |
| 156 | + fn into_future(self) -> Self::IntoFuture { |
| 157 | + Box::pin(self.create()) |
86 | 158 | } |
87 | 159 | } |
0 commit comments