Struct zero_sdk::Zero

source ·
pub struct Zero { /* private fields */ }
Expand description

Zero API client. Instantiate with a token, than call the .fetch() method to download secrets.

Implementations§

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();

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;
}

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;
}

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;
}

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more