1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::{
    build_request,
    error::ToolError,
    settings::Core,
    workspace::{Workspace, WorkspaceVariables},
    BASE_URL,
};
use async_scoped::AsyncScope;
use log::{error, info};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
    fmt::{Display, Formatter},
    str::FromStr,
};
use surf::{http::Method, Client};
use url::Url;

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Variable {
    #[serde(rename = "type")]
    pub relationship_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub attributes: Attributes,
}

// the vars are in the format of key=value:description:category:hcl:sensitive
// we need to parse each one into a variable::Variable
// description, category, hcl, sensitive are all optional and will be None if not provided
// to skip a field just use a colon e.g. key=value::::true would only set key, value, and sensitive
// accepting the default for the rest
impl FromStr for Variable {
    type Err = ToolError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.contains(':') {
            let var_split: Vec<&str> = s.split(':').collect();
            let key_val = var_split[0].to_string();
            let key_val_split: Vec<&str> = key_val.split('=').collect();
            let key = key_val_split[0].to_string();
            let value = key_val_split[1].to_string();
            let description = if var_split[1].is_empty() {
                None
            } else {
                Some(var_split[1].to_string())
            };
            let category = if var_split[2].is_empty() {
                Category::default()
            } else {
                Category::from(var_split[2].to_string())
            };
            let hcl = if var_split[3].is_empty() {
                None
            } else {
                Some(var_split[3].parse::<bool>()?)
            };
            let sensitive = if var_split[4].is_empty() {
                None
            } else {
                Some(var_split[4].parse::<bool>()?)
            };
            Ok(Variable {
                relationship_type: "vars".to_string(),
                id: None,
                attributes: Attributes {
                    key,
                    value: Some(value),
                    description,
                    category,
                    hcl,
                    sensitive,
                },
            })
        } else {
            let key_val_split = s.split('=').collect::<Vec<&str>>();
            let key = key_val_split[0].to_string();
            let value = key_val_split[1].to_string();
            Ok(Variable {
                relationship_type: "vars".to_string(),
                id: None,
                attributes: Attributes {
                    key,
                    value: Some(value),
                    description: None,
                    category: Category::default(),
                    hcl: None,
                    sensitive: None,
                },
            })
        }
    }
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Category {
    #[default]
    Terraform,
    Env,
}

impl Display for Category {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Category::Terraform => write!(f, "terraform"),
            Category::Env => write!(f, "env"),
        }
    }
}

impl From<String> for Category {
    fn from(s: String) -> Self {
        match s.as_str() {
            "terraform" => Category::Terraform,
            "env" => Category::Env,
            _ => Category::Terraform,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Attributes {
    pub key: String,
    pub value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default)]
    pub category: Category,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hcl: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensitive: Option<bool>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct VariablesOuter {
    pub data: Vec<Variable>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct VariableOuter {
    pub data: Variable,
}

pub async fn create(
    workspace_id: &str,
    var: Variable,
    config: &Core,
    client: Client,
) -> Result<Variable, ToolError> {
    info!(
        "Creating variable: {} in workspace: {}",
        var.attributes.key, workspace_id
    );
    let url =
        Url::parse(&format!("{}/workspaces/{}/vars/", BASE_URL, workspace_id))?;
    let req = build_request(
        Method::Post,
        url,
        config,
        Some(json!(VariableOuter { data: var })),
    );
    match client.send(req).await {
        Ok(mut res) => {
            if res.status().is_success() {
                let body: VariableOuter =
                    res.body_json().await.map_err(|e| e.into_inner())?;
                Ok(body.data)
            } else {
                error!("Failed to create variable :(");
                let error =
                    res.body_string().await.map_err(|e| e.into_inner())?;
                Err(ToolError::General(anyhow::anyhow!(error)))
            }
        }
        Err(e) => Err(ToolError::General(e.into_inner())),
    }
}
pub async fn list(
    workspace_id: &str,
    config: &Core,
    client: Client,
) -> Result<Vec<Variable>, ToolError> {
    let url =
        Url::parse(&format!("{}/workspaces/{}/vars/", BASE_URL, workspace_id))?;
    let req = build_request(Method::Get, url, config, None);
    match client.send(req).await {
        Ok(mut res) => {
            let body: VariablesOuter =
                res.body_json().await.map_err(|e| e.into_inner())?;
            Ok(body.data)
        }
        Err(e) => Err(ToolError::General(e.into_inner())),
    }
}

pub async fn list_batch(
    config: &Core,
    client: Client,
    workspaces: Vec<Workspace>,
) -> Result<Vec<WorkspaceVariables>, ToolError> {
    // Get the variables for each workspace
    let (_, workspaces_variables) = AsyncScope::scope_and_block(|s| {
        for workspace in workspaces {
            let c = client.clone();
            let proc = || async move {
                match list(&workspace.id, config, c).await {
                    Ok(variables) => {
                        info!(
                            "Successfully retrieved variables for workspace {}",
                            workspace.attributes.name.clone().unwrap()
                        );
                        Some(WorkspaceVariables { workspace, variables })
                    }
                    Err(e) => {
                        error!(
                            "Unable to retrieve variables for workspace {}",
                            workspace.attributes.name.unwrap()
                        );
                        error!("{:#?}", e);
                        None
                    }
                }
            };
            s.spawn(proc());
        }
    });
    Ok(workspaces_variables.into_iter().flatten().collect())
}

pub async fn delete(
    variable_id: &str,
    workspace_id: &str,
    config: &Core,
    client: Client,
) -> Result<(), ToolError> {
    info!(
        "Deleting variable: {} from workspace: {}",
        variable_id, workspace_id
    );
    let url = Url::parse(&format!(
        "{}/workspaces/{}/vars/{}",
        BASE_URL, workspace_id, variable_id
    ))?;
    let req = build_request(Method::Delete, url, config, None);
    match client.send(req).await {
        Ok(mut res) => {
            if res.status().is_success() {
                info!("Successfully deleted variable!");
                Ok(())
            } else {
                error!("Failed to delete variable :(");
                let error =
                    res.body_string().await.map_err(|e| e.into_inner())?;
                Err(ToolError::General(anyhow::anyhow!(error)))
            }
        }
        Err(e) => Err(ToolError::General(e.into_inner())),
    }
}