cloudflare/endpoints/load_balancing/
create_lb.rs

1use surf::http::Method;
2
3use crate::endpoints::load_balancing::{
4    LbPoolId, LbPoolMapping, LoadBalancer, SessionAffinity, SessionAffinityAttributes,
5    SteeringPolicy,
6};
7use crate::framework::endpoint::Endpoint;
8
9/// Create Load Balancer
10/// https://api.cloudflare.com/#load-balancers-create-load-balancer
11#[derive(Debug)]
12pub struct CreateLoadBalancer<'a> {
13    /// The Zone to which this Load Balancer shall belong.
14    pub zone_identifier: &'a str,
15    /// Optional parameters for the API call
16    pub params: Params<'a>,
17}
18
19/// Mandatory parameters for creating a Load Balancer.
20#[serde_with::skip_serializing_none]
21#[derive(Serialize, Clone, Debug)]
22pub struct Params<'a> {
23    /// A short name (tag) for the load balancer.
24    /// Only alphanumeric characters, hyphens and underscores are allowed.
25    /// E.g. "lb-user-facing"
26    pub name: &'a str,
27    /// The list of LB Pools (by their IDs) ordered by their failover priority.
28    pub default_pools: &'a [LbPoolId],
29    /// The LB Pool ID to use when all other pools are detected as unhealthy.
30    pub fallback_pool: &'a LbPoolId,
31    #[serde(flatten)]
32    pub optional_params: Option<OptionalParams<'a>>,
33}
34
35/// Optional parameters for creating a Load Balancer.
36#[serde_with::skip_serializing_none]
37#[derive(Serialize, Clone, Debug, Default)]
38pub struct OptionalParams<'a> {
39    pub description: Option<&'a str>,
40    /// Time to live (TTL) of the DNS entry for the IP address returned by this load balancer. This
41    /// only applies to gray-clouded (unproxied) load balancers.
42    pub ttl: Option<u32>,
43    /// A mapping of Cloudflare PoP identifiers to a list of pool IDs (ordered by their failover
44    /// priority) for the PoP (datacenter). Any PoPs not explicitly defined will fall back to using
45    /// default_pools.
46    pub pop_pools: Option<LbPoolMapping>,
47    /// A mapping of region/country codes to a list of pool IDs (ordered by their failover priority)
48    /// for the given region. Any regions not explicitly defined will fall back to using
49    /// default_pools.
50    pub region_pools: Option<LbPoolMapping>,
51    pub proxied: Option<bool>,
52    pub steering_policy: Option<SteeringPolicy>,
53    pub session_affinity: Option<SessionAffinity>,
54    pub session_affinity_attributes: Option<SessionAffinityAttributes>,
55    pub session_affinity_ttl: Option<u32>,
56}
57
58impl<'a> Endpoint<LoadBalancer, (), Params<'a>> for CreateLoadBalancer<'a> {
59    fn method(&self) -> Method {
60        Method::Post
61    }
62    fn path(&self) -> String {
63        format!("zones/{}/load_balancers", self.zone_identifier)
64    }
65    fn body(&self) -> Option<Params<'a>> {
66        Some(self.params.clone())
67    }
68}