aliyun_log_rust_sdk/client/project/
create_project.rs

1use super::*;
2use crate::RequestErrorKind;
3use serde::Serialize;
4
5impl crate::client::Client {
6    /// Create a new project.
7    ///
8    /// Project names must be globally unique within the Alibaba Cloud region and cannot be modified after creation.
9    ///
10    /// # Arguments
11    ///
12    /// * `project_name` - Name of the project to create. Must be globally unique and follow naming rules:
13    ///   - Only lowercase letters, numbers, and hyphens (-)
14    ///   - Must start and end with lowercase letter or number
15    ///
16    /// # Examples
17    ///
18    /// ```no_run
19    /// # async fn example(client: aliyun_log_rust_sdk::Client) -> Result<(), aliyun_log_rust_sdk::Error> {
20    /// client.create_project("test-project")
21    ///     .description("this is test")
22    ///     .resource_group_id("rg-aekzf******sxby")
23    ///     .data_redundancy_type("LRS")
24    ///     .recycle_bin_enabled(true)
25    ///     .send()
26    ///     .await?;
27    /// # Ok(())
28    /// # }
29    /// ```
30    pub fn create_project(&self, project_name: impl AsRef<str>) -> CreateProjectRequestBuilder {
31        CreateProjectRequestBuilder {
32            handle: self.handle.clone(),
33            project_name: project_name.as_ref().to_string(),
34            description: None,
35            resource_group_id: None,
36            data_redundancy_type: None,
37            recycle_bin_enabled: None,
38        }
39    }
40}
41
42pub struct CreateProjectRequestBuilder {
43    handle: HandleRef,
44    project_name: String,
45    description: Option<String>,
46    resource_group_id: Option<String>,
47    data_redundancy_type: Option<String>,
48    recycle_bin_enabled: Option<bool>,
49}
50
51impl CreateProjectRequestBuilder {
52    #[must_use = "the result future must be awaited"]
53    pub fn send(self) -> ResponseResultBoxFuture<()> {
54        Box::pin(async move {
55            let (handle, request) = self.build()?;
56            handle.send(request).await
57        })
58    }
59
60    /// Set the project description (required).
61    ///
62    /// # Arguments
63    ///
64    /// * `description` - Description of the project
65    pub fn description(mut self, description: impl Into<String>) -> Self {
66        self.description = Some(description.into());
67        self
68    }
69
70    /// Set the resource group ID (optional).
71    ///
72    /// # Arguments
73    ///
74    /// * `resource_group_id` - Resource group ID to create project into.
75    pub fn resource_group_id(mut self, resource_group_id: impl Into<String>) -> Self {
76        self.resource_group_id = Some(resource_group_id.into());
77        self
78    }
79
80    /// Set the data redundancy type (optional).
81    ///
82    /// # Arguments
83    ///
84    /// * `data_redundancy_type` - Data redundancy type. Valid values:
85    ///   - `LRS`: Local redundant storage
86    ///   - `ZRS`: Zone redundant storage
87    pub fn data_redundancy_type(mut self, data_redundancy_type: impl Into<String>) -> Self {
88        self.data_redundancy_type = Some(data_redundancy_type.into());
89        self
90    }
91
92    /// Set whether to enable recycle bin (optional).
93    ///
94    /// # Arguments
95    ///
96    /// * `enabled` - Whether to enable recycle bin
97    pub fn recycle_bin_enabled(mut self, enabled: bool) -> Self {
98        self.recycle_bin_enabled = Some(enabled);
99        self
100    }
101
102    fn build(self) -> BuildResult<CreateProjectRequest> {
103        check_required!(("description", self.description));
104        Ok((
105            self.handle,
106            CreateProjectRequest {
107                project_name: self.project_name,
108                description: self.description.unwrap(),
109                resource_group_id: self.resource_group_id,
110                data_redundancy_type: self.data_redundancy_type,
111                recycle_bin_enabled: self.recycle_bin_enabled,
112            },
113        ))
114    }
115}
116
117#[derive(Serialize)]
118struct CreateProjectRequest {
119    #[serde(rename = "projectName")]
120    project_name: String,
121    description: String,
122    #[serde(rename = "resourceGroupId", skip_serializing_if = "Option::is_none")]
123    resource_group_id: Option<String>,
124    #[serde(rename = "dataRedundancyType", skip_serializing_if = "Option::is_none")]
125    data_redundancy_type: Option<String>,
126    #[serde(rename = "recycleBinEnabled", skip_serializing_if = "Option::is_none")]
127    recycle_bin_enabled: Option<bool>,
128}
129
130impl Request for CreateProjectRequest {
131    const HTTP_METHOD: http::Method = http::Method::POST;
132    const CONTENT_TYPE: Option<http::HeaderValue> = Some(LOG_JSON);
133    type ResponseBody = ();
134
135    fn project(&self) -> Option<&str> {
136        None
137    }
138
139    fn path(&self) -> &str {
140        "/"
141    }
142
143    fn body(&self) -> crate::Result<Option<bytes::Bytes>, RequestError> {
144        let json = serde_json::to_string(&self).map_err(RequestErrorKind::JsonEncode)?;
145        Ok(Some(bytes::Bytes::from(json)))
146    }
147}