Skip to main content

openstack_sdk_identity/v3/project/
create.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Creates a project, where the project may act as a domain.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/projects`
22//!
23use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use openstack_sdk_core::api::rest_endpoint_prelude::*;
27
28use serde::Deserialize;
29use serde::Serialize;
30use std::borrow::Cow;
31
32/// The resource options for the project. Available resource options are
33/// `immutable`.
34#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
35#[builder(setter(strip_option))]
36pub struct Options {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[builder(default, setter(into))]
39    pub(crate) immutable: Option<bool>,
40}
41
42/// A `project` object
43#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
44#[builder(setter(strip_option))]
45pub struct Project<'a> {
46    /// The description of the project.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[builder(default, setter(into))]
49    pub(crate) description: Option<Option<Cow<'a, str>>>,
50
51    /// The ID of the domain for the project.
52    ///
53    /// For projects acting as a domain, the `domain_id` must not be specified,
54    /// it will be generated by the Identity service implementation.
55    ///
56    /// For regular projects (i.e. those not acing as a domain), if `domain_id`
57    /// is not specified, but `parent_id` is specified, then the domain ID of
58    /// the parent will be used. If neither `domain_id` or `parent_id` is
59    /// specified, the Identity service implementation will default to the
60    /// domain to which the client’s token is scoped. If both `domain_id` and
61    /// `parent_id` are specified, and they do not indicate the same domain, an
62    /// `Bad Request (400)` will be returned.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    #[builder(default, setter(into))]
65    pub(crate) domain_id: Option<Option<Cow<'a, str>>>,
66
67    /// If set to `true`, project is enabled. If set to `false`, project is
68    /// disabled. The default is `true`.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    #[builder(default, setter(into))]
71    pub(crate) enabled: Option<bool>,
72
73    /// If set to `true`, project is enabled. If set to `false`, project is
74    /// disabled. The default is `true`.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    #[builder(default, setter(into))]
77    pub(crate) is_domain: Option<bool>,
78
79    /// The name of the project, which must be unique within the owning domain.
80    /// A project can have the same name as its domain.
81    #[serde()]
82    #[builder(setter(into))]
83    pub(crate) name: Cow<'a, str>,
84
85    /// The resource options for the project. Available resource options are
86    /// `immutable`.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    #[builder(default, setter(into))]
89    pub(crate) options: Option<Options>,
90
91    /// The ID of the parent of the project.
92    ///
93    /// If specified on project creation, this places the project within a
94    /// hierarchy and implicitly defines the owning domain, which will be the
95    /// same domain as the parent specified. If `parent_id` is not specified
96    /// and `is_domain` is `false`, then the project will use its owning domain
97    /// as its parent. If `is_domain` is `true` (i.e. the project is acting as
98    /// a domain), then `parent_id` must not specified (or if it is, it must be
99    /// `null`) since domains have no parents.
100    ///
101    /// `parent_id` is immutable, and can’t be updated after the project is
102    /// created - hence a project cannot be moved within the hierarchy.
103    ///
104    /// **New in version 3.4**
105    #[serde(skip_serializing_if = "Option::is_none")]
106    #[builder(default, setter(into))]
107    pub(crate) parent_id: Option<Option<Cow<'a, str>>>,
108
109    /// A list of simple strings assigned to a project. Tags can be used to
110    /// classify projects into groups.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    #[builder(default, setter(into))]
113    pub(crate) tags: Option<Vec<Cow<'a, str>>>,
114}
115
116#[derive(Builder, Debug, Clone)]
117#[builder(setter(strip_option))]
118pub struct Request<'a> {
119    /// A `project` object
120    #[builder(setter(into))]
121    pub(crate) project: Project<'a>,
122
123    #[builder(setter(name = "_headers"), default, private)]
124    _headers: Option<HeaderMap>,
125}
126impl<'a> Request<'a> {
127    /// Create a builder for the endpoint.
128    pub fn builder() -> RequestBuilder<'a> {
129        RequestBuilder::default()
130    }
131}
132
133impl<'a> RequestBuilder<'a> {
134    /// Add a single header to the Project.
135    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
136    where
137        K: Into<HeaderName>,
138        V: Into<HeaderValue>,
139    {
140        self._headers
141            .get_or_insert(None)
142            .get_or_insert_with(HeaderMap::new)
143            .insert(header_name.into(), header_value.into());
144        self
145    }
146
147    /// Add multiple headers.
148    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
149    where
150        I: Iterator<Item = T>,
151        T: Into<(Option<HeaderName>, HeaderValue)>,
152    {
153        self._headers
154            .get_or_insert(None)
155            .get_or_insert_with(HeaderMap::new)
156            .extend(iter.map(Into::into));
157        self
158    }
159}
160
161impl RestEndpoint for Request<'_> {
162    fn method(&self) -> http::Method {
163        http::Method::POST
164    }
165
166    fn endpoint(&self) -> Cow<'static, str> {
167        "projects".to_string().into()
168    }
169
170    fn parameters(&self) -> QueryParams<'_> {
171        QueryParams::default()
172    }
173
174    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
175        let mut params = JsonBodyParams::default();
176
177        params.push("project", serde_json::to_value(&self.project)?);
178
179        params.into_body()
180    }
181
182    fn service_type(&self) -> ServiceType {
183        ServiceType::Identity
184    }
185
186    fn response_key(&self) -> Option<Cow<'static, str>> {
187        Some("project".into())
188    }
189
190    /// Returns headers to be set into the request
191    fn request_headers(&self) -> Option<&HeaderMap> {
192        self._headers.as_ref()
193    }
194
195    /// Returns required API version
196    fn api_version(&self) -> Option<ApiVersion> {
197        Some(ApiVersion::new(3, 0))
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204    use http::{HeaderName, HeaderValue};
205    use httpmock::MockServer;
206    #[cfg(feature = "sync")]
207    use openstack_sdk_core::api::Query;
208    use openstack_sdk_core::test::client::FakeOpenStackClient;
209    use openstack_sdk_core::types::ServiceType;
210    use serde_json::json;
211
212    #[test]
213    fn test_service_type() {
214        assert_eq!(
215            Request::builder()
216                .project(ProjectBuilder::default().name("foo").build().unwrap())
217                .build()
218                .unwrap()
219                .service_type(),
220            ServiceType::Identity
221        );
222    }
223
224    #[test]
225    fn test_response_key() {
226        assert_eq!(
227            Request::builder()
228                .project(ProjectBuilder::default().name("foo").build().unwrap())
229                .build()
230                .unwrap()
231                .response_key()
232                .unwrap(),
233            "project"
234        );
235    }
236
237    #[cfg(feature = "sync")]
238    #[test]
239    fn endpoint() {
240        let server = MockServer::start();
241        let client = FakeOpenStackClient::new(server.base_url());
242        let mock = server.mock(|when, then| {
243            when.method(httpmock::Method::POST)
244                .path("/projects".to_string());
245
246            then.status(200)
247                .header("content-type", "application/json")
248                .json_body(json!({ "project": {} }));
249        });
250
251        let endpoint = Request::builder()
252            .project(ProjectBuilder::default().name("foo").build().unwrap())
253            .build()
254            .unwrap();
255        let _: serde_json::Value = endpoint.query(&client).unwrap();
256        mock.assert();
257    }
258
259    #[cfg(feature = "sync")]
260    #[test]
261    fn endpoint_headers() {
262        let server = MockServer::start();
263        let client = FakeOpenStackClient::new(server.base_url());
264        let mock = server.mock(|when, then| {
265            when.method(httpmock::Method::POST)
266                .path("/projects".to_string())
267                .header("foo", "bar")
268                .header("not_foo", "not_bar");
269            then.status(200)
270                .header("content-type", "application/json")
271                .json_body(json!({ "project": {} }));
272        });
273
274        let endpoint = Request::builder()
275            .project(ProjectBuilder::default().name("foo").build().unwrap())
276            .headers(
277                [(
278                    Some(HeaderName::from_static("foo")),
279                    HeaderValue::from_static("bar"),
280                )]
281                .into_iter(),
282            )
283            .header(
284                HeaderName::from_static("not_foo"),
285                HeaderValue::from_static("not_bar"),
286            )
287            .build()
288            .unwrap();
289        let _: serde_json::Value = endpoint.query(&client).unwrap();
290        mock.assert();
291    }
292}