pub struct Zero { /* private fields */ }Expand description
Zero API client. Instantiate with a token, than call the .fetch() method to download secrets.
Implementations§
source§impl Zero
impl Zero
The main client for accessing Zero GraphQL API.
Example:
use zero_sdk::{Zero, Arguments};
let client = Zero::new(Arguments {
pick: Some(vec![String::from("my-secret")]),
token: String::from("my-zero-token"),
caller_name: None,
})
.unwrap();sourcepub fn set_api_url(self, new_api_url: String) -> Self
pub fn set_api_url(self, new_api_url: String) -> Self
Set the URL which will be called in fetch(). The method was added mostly for convenience of testing.
Examples found in repository?
src/tests/mock_and_fetch.rs (line 81)
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
pub fn mock_and_fetch(
arguments: Arguments,
options: Option<Options>,
) -> Result<std::collections::HashMap<String, std::collections::HashMap<String, String>>, String> {
const TOKEN: &str = "token";
const CALLER_NAME: &str = "cicd";
let caller_name_query = if !options.is_none() && options.as_ref().unwrap().is_caller_name_empty {
String::from("")
} else {
format!(", callerName: \\\"{}\\\"", CALLER_NAME)
};
let pick;
let pick_query;
let response_body;
// Empty response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_pick_empty {
pick = Some(vec![]);
pick_query = "";
} else {
pick = Some(vec![String::from("aws"), String::from("stripe")]);
pick_query = "\\\"aws\\\", \\\"stripe\\\"";
}
// Failed response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_response_failed {
response_body = serde_json::json!({
"data": null,
"errors": [
{
"message": "Could not establish connection with database",
"locations": [{"line": 2, "column": 2}],
"path": ["secrets"],
"extensions": {"internal_error": "Error occurred while creating a new object: error connecting to server: Connection refused (os error 61)"}
}
]
});
} else {
response_body = serde_json::json!({"data": {
"secrets": [
{"name": "aws", "fields": [{"name": "key", "value": "a"}, {"name": "secret", "value": "b"}]},
]
}});
}
// Generate mock
let mock = arguments.server.mock(|when, then| {
when.method(httpmock::prelude::POST)
.path("/v1/graphql")
.body_contains(&format!(
"secrets(zeroToken: \\\"{}\\\", pick: [{}]{})",
TOKEN, pick_query, caller_name_query,
));
then.status(200)
.header("content-type", "application/json")
.json_body(response_body);
});
// Instantiate Zero and fetch the secrets
let secrets = super::super::Zero::new(super::super::Arguments {
pick,
token: String::from(TOKEN),
caller_name: if caller_name_query.is_empty() { None } else { Some(String::from(CALLER_NAME)) },
})
.unwrap()
.set_api_url(arguments.server.url("/v1/graphql"))
.fetch();
mock.assert();
return secrets;
}sourcepub fn fetch(self) -> Result<HashMap<String, HashMap<String, String>>, String>
pub fn fetch(self) -> Result<HashMap<String, HashMap<String, String>>, String>
Fetch the secrets assigned to the token.
Examples found in repository?
src/tests/mock_and_fetch.rs (line 82)
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
pub fn mock_and_fetch(
arguments: Arguments,
options: Option<Options>,
) -> Result<std::collections::HashMap<String, std::collections::HashMap<String, String>>, String> {
const TOKEN: &str = "token";
const CALLER_NAME: &str = "cicd";
let caller_name_query = if !options.is_none() && options.as_ref().unwrap().is_caller_name_empty {
String::from("")
} else {
format!(", callerName: \\\"{}\\\"", CALLER_NAME)
};
let pick;
let pick_query;
let response_body;
// Empty response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_pick_empty {
pick = Some(vec![]);
pick_query = "";
} else {
pick = Some(vec![String::from("aws"), String::from("stripe")]);
pick_query = "\\\"aws\\\", \\\"stripe\\\"";
}
// Failed response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_response_failed {
response_body = serde_json::json!({
"data": null,
"errors": [
{
"message": "Could not establish connection with database",
"locations": [{"line": 2, "column": 2}],
"path": ["secrets"],
"extensions": {"internal_error": "Error occurred while creating a new object: error connecting to server: Connection refused (os error 61)"}
}
]
});
} else {
response_body = serde_json::json!({"data": {
"secrets": [
{"name": "aws", "fields": [{"name": "key", "value": "a"}, {"name": "secret", "value": "b"}]},
]
}});
}
// Generate mock
let mock = arguments.server.mock(|when, then| {
when.method(httpmock::prelude::POST)
.path("/v1/graphql")
.body_contains(&format!(
"secrets(zeroToken: \\\"{}\\\", pick: [{}]{})",
TOKEN, pick_query, caller_name_query,
));
then.status(200)
.header("content-type", "application/json")
.json_body(response_body);
});
// Instantiate Zero and fetch the secrets
let secrets = super::super::Zero::new(super::super::Arguments {
pick,
token: String::from(TOKEN),
caller_name: if caller_name_query.is_empty() { None } else { Some(String::from(CALLER_NAME)) },
})
.unwrap()
.set_api_url(arguments.server.url("/v1/graphql"))
.fetch();
mock.assert();
return secrets;
}sourcepub fn new(arguments: Arguments) -> Result<Self, &'static str>
pub fn new(arguments: Arguments) -> Result<Self, &'static str>
Instantiate new Zero struct. Requires token string to be non empty, other params are optional.
Examples found in repository?
src/tests/mock_and_fetch.rs (lines 75-79)
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
pub fn mock_and_fetch(
arguments: Arguments,
options: Option<Options>,
) -> Result<std::collections::HashMap<String, std::collections::HashMap<String, String>>, String> {
const TOKEN: &str = "token";
const CALLER_NAME: &str = "cicd";
let caller_name_query = if !options.is_none() && options.as_ref().unwrap().is_caller_name_empty {
String::from("")
} else {
format!(", callerName: \\\"{}\\\"", CALLER_NAME)
};
let pick;
let pick_query;
let response_body;
// Empty response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_pick_empty {
pick = Some(vec![]);
pick_query = "";
} else {
pick = Some(vec![String::from("aws"), String::from("stripe")]);
pick_query = "\\\"aws\\\", \\\"stripe\\\"";
}
// Failed response, depending on the flag
if !options.is_none() && options.as_ref().unwrap().is_response_failed {
response_body = serde_json::json!({
"data": null,
"errors": [
{
"message": "Could not establish connection with database",
"locations": [{"line": 2, "column": 2}],
"path": ["secrets"],
"extensions": {"internal_error": "Error occurred while creating a new object: error connecting to server: Connection refused (os error 61)"}
}
]
});
} else {
response_body = serde_json::json!({"data": {
"secrets": [
{"name": "aws", "fields": [{"name": "key", "value": "a"}, {"name": "secret", "value": "b"}]},
]
}});
}
// Generate mock
let mock = arguments.server.mock(|when, then| {
when.method(httpmock::prelude::POST)
.path("/v1/graphql")
.body_contains(&format!(
"secrets(zeroToken: \\\"{}\\\", pick: [{}]{})",
TOKEN, pick_query, caller_name_query,
));
then.status(200)
.header("content-type", "application/json")
.json_body(response_body);
});
// Instantiate Zero and fetch the secrets
let secrets = super::super::Zero::new(super::super::Arguments {
pick,
token: String::from(TOKEN),
caller_name: if caller_name_query.is_empty() { None } else { Some(String::from(CALLER_NAME)) },
})
.unwrap()
.set_api_url(arguments.server.url("/v1/graphql"))
.fetch();
mock.assert();
return secrets;
}