Skip to content

Commit

Permalink
Add Secrets endpoints (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
exvuma authored Feb 5, 2020
1 parent c70b3a6 commit 358de27
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cloudflare/src/endpoints/workers/create_secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::WorkersSecret;

use crate::framework::endpoint::{Endpoint, Method};

/// Create Secret
/// https://api.cloudflare.com/#worker-create-secret
pub struct CreateSecret<'a> {
/// account id of owner of the script
pub account_identifier: &'a str,
/// the name of the script to attach the secret to
pub script_name: &'a str,
/// secert's contents
pub params: CreateSecretParams,
}

impl<'a> Endpoint<WorkersSecret, (), CreateSecretParams> for CreateSecret<'a> {
fn method(&self) -> Method {
Method::Put
}
fn path(&self) -> String {
format!(
"accounts/{}/workers/scripts/{}/secrets",
self.account_identifier, self.script_name
)
}
fn body(&self) -> Option<CreateSecretParams> {
Some(self.params.clone())
}
}

#[derive(Serialize, Clone, Debug)]
pub struct CreateSecretParams {
/// the variable name of the secret that will be bound to the script
pub name: String,
/// the string value of the secret
pub text: String,
// type of binding (e.g.secret_text)
#[serde(rename = "type")]
pub secret_type: String,
}
26 changes: 26 additions & 0 deletions cloudflare/src/endpoints/workers/delete_secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::WorkersSecret;

use crate::framework::endpoint::{Endpoint, Method};

/// Delete Secret
/// https://api.cloudflare.com/#worker-delete-secret
pub struct DeleteSecret<'a> {
/// account id of owner of the script
pub account_identifier: &'a str,
/// the name of the script to remove the secret from
pub script_name: &'a str,
/// the variable name of the secret
pub secret_name: &'a str,
}

impl<'a> Endpoint<WorkersSecret> for DeleteSecret<'a> {
fn method(&self) -> Method {
Method::Delete
}
fn path(&self) -> String {
format!(
"accounts/{}/workers/scripts/{}/secrets/{}",
self.account_identifier, self.script_name, self.secret_name
)
}
}
23 changes: 23 additions & 0 deletions cloudflare/src/endpoints/workers/list_secrets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::WorkersSecret;

use crate::framework::endpoint::{Endpoint, Method};

/// List Secrets
/// Lists all secrets mappings for a given script
/// https://api.cloudflare.com/#worker-secrets-list-secrets
pub struct ListSecrets<'a> {
pub account_identifier: &'a str,
pub script_name: &'a str,
}

impl<'a> Endpoint<Vec<WorkersSecret>> for ListSecrets<'a> {
fn method(&self) -> Method {
Method::Get
}
fn path(&self) -> String {
format!(
"accounts/{}/workers/scripts/{}/secrets",
self.account_identifier, self.script_name
)
}
}
23 changes: 23 additions & 0 deletions cloudflare/src/endpoints/workers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use crate::framework::response::ApiResult;

use chrono::offset::Utc;
use chrono::DateTime;
use serde::Deserialize;

mod create_route;
mod create_secret;
mod delete_route;
mod delete_secret;
mod list_routes;
mod list_secrets;

pub use create_route::{CreateRoute, CreateRouteParams};
pub use create_secret::{CreateSecret, CreateSecretParams};
pub use delete_route::DeleteRoute;
pub use delete_secret::DeleteSecret;
pub use list_routes::ListRoutes;
pub use list_secrets::ListSecrets;

/// Workers KV Route
/// Routes are basic patterns used to enable or disable workers that match requests.
Expand Down Expand Up @@ -37,3 +45,18 @@ pub struct WorkersRouteIdOnly {
}

impl ApiResult for WorkersRouteIdOnly {}

/// Secrets attach to a single script to be readable in only the script
/// https://api.cloudflare.com/#worker-secrets-properties
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct WorkersSecret {
/// TODO: these fields depend on the API and may be wrong since unable to test
pub name: String,
#[serde(rename = "type")]
pub secret_type: String,
pub modified_on: DateTime<Utc>,
pub created_on: DateTime<Utc>,
}

impl ApiResult for WorkersSecret {}
impl ApiResult for Vec<WorkersSecret> {} // to parse arrays too

0 comments on commit 358de27

Please sign in to comment.