Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Add config option to switch usage model to unbound #1837

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mod tests {
site: None,
vars: None,
text_blobs: None,
usage_model: None,
};
assert!(kv::get_namespace_id(&target_with_dup_kv_bindings, "").is_err());
}
Expand Down
4 changes: 4 additions & 0 deletions src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use config::{Config, File};
use serde::{Deserialize, Serialize};
use serde_with::rust::string_empty_as_none;

use super::UsageModel;
use crate::commands::{validate_worker_name, whoami, DEFAULT_CONFIG_PATH};
use crate::deploy::{self, DeployTarget, DeploymentSet};
use crate::settings::toml::dev::Dev;
Expand Down Expand Up @@ -51,6 +52,8 @@ pub struct Manifest {
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub triggers: Option<Triggers>,
#[serde(default, with = "string_empty_as_none")]
pub usage_model: Option<UsageModel>,
}

impl Manifest {
Expand Down Expand Up @@ -297,6 +300,7 @@ impl Manifest {
site: self.site.clone(), // Inherited
vars: self.vars.clone(), // Not inherited
text_blobs: self.text_blobs.clone(), // Inherited
usage_model: self.usage_model, // Top level
};

let environment = self.get_environment(environment_name)?;
Expand Down
31 changes: 31 additions & 0 deletions src/settings/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mod target;
mod target_type;
mod triggers;

use serde::{Deserialize, Serialize};
use std::str::FromStr;

pub use environment::Environment;
pub use kv_namespace::{ConfigKvNamespace, KvNamespace};
pub use manifest::Manifest;
Expand All @@ -16,5 +19,33 @@ pub use site::Site;
pub use target::Target;
pub use target_type::TargetType;

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum UsageModel {
Bundled,
Unbound,
}

impl FromStr for UsageModel {
type Err = failure::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bundled" => Ok(UsageModel::Bundled),
"unbound" => Ok(UsageModel::Unbound),
_ => failure::bail!("Invalid usage model; must be either \"bundled\" or \"unbound\""),
}
}
}

impl AsRef<str> for UsageModel {
fn as_ref(&self) -> &str {
match self {
UsageModel::Bundled => "bundled",
UsageModel::Unbound => "unbound",
}
}
}

#[cfg(test)]
mod tests;
2 changes: 2 additions & 0 deletions src/settings/toml/target.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::kv_namespace::KvNamespace;
use super::site::Site;
use super::target_type::TargetType;
use super::UsageModel;

use std::collections::HashMap;
use std::env;
Expand All @@ -17,6 +18,7 @@ pub struct Target {
pub site: Option<Site>,
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub usage_model: Option<UsageModel>,
}

impl Target {
Expand Down
1 change: 1 addition & 0 deletions src/sites/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ mod tests {
site: Some(site),
vars: None,
text_blobs: None,
usage_model: None,
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/upload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ pub fn script(
failure::bail!(error_msg(res_status, res_text))
}

if let Some(usage_model) = target.usage_model {
let addr = format!(
"https://api.cloudflare.com/client/v4/accounts/{}/workers/scripts/{}/usage-model",
target.account_id, target.name,
);

let res = client
.put(&addr)
.json(&serde_json::json!({
"usage_model": usage_model.as_ref()
}))
.send()?;

let res_status = res.status();

if !res_status.is_success() {
let res_text = res.text()?;
failure::bail!(error_msg(res_status, res_text))
}
}

Ok(())
}

Expand All @@ -41,6 +62,8 @@ fn error_msg(status: reqwest::StatusCode, text: String) -> String {
"You need to verify your account's email address before you can publish. You can do this by checking your email or logging in to https://dash.cloudflare.com.".to_string()
} else if text.contains("\"code\":10000,") {
"Your user configuration is invalid, please run wrangler login or wrangler config and enter a new set of credentials.".to_string()
} else if text.contains("\"code\":10075,") {
"Setting a Usage Model requires a Paid plan with Unbound enabled. You can do this in the dash by logging in to https://dash.cloudflare.com/?account=workers/plans".to_string()
} else {
format!("Something went wrong! Status: {}, Details {}", status, text)
}
Expand Down