openstack_sdk/api/image/v2/image/member/
set.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//! Sets the status for an image member. *(Since Image API v2.1)*
19//!
20//! This call allows an image member to change his or her *member status*.
21//!
22//! When an image is shared with you, you have immediate access to the image.
23//! What updating your member status on the image does for you is that it
24//! affects whether the image will appear in your image list response.
25//!
26//! For a more detailed discussion of image sharing, please consult
27//! [Image API v2 Sharing](http://specs.openstack.org/openstack/glance-specs/specs/api/v2/sharing-image-api-v2.html).
28//!
29//! Preconditions
30//!
31//! Synchronous Postconditions
32//!
33//! Normal response codes: 200
34//!
35//! Error response codes: 400, 401, 404, 403
36//!
37use derive_builder::Builder;
38use http::{HeaderMap, HeaderName, HeaderValue};
39
40use crate::api::rest_endpoint_prelude::*;
41
42use serde_json::Value;
43use std::borrow::Cow;
44use std::collections::BTreeMap;
45
46#[derive(Builder, Debug, Clone)]
47#[builder(setter(strip_option))]
48pub struct Request<'a> {
49    /// member_id parameter for /v2/images/{image_id}/members/{member_id} API
50    #[builder(default, setter(into))]
51    id: Cow<'a, str>,
52
53    /// image_id parameter for /v2/images/{image_id}/members/{member_id} API
54    #[builder(default, setter(into))]
55    image_id: Cow<'a, str>,
56
57    #[builder(setter(name = "_headers"), default, private)]
58    _headers: Option<HeaderMap>,
59
60    #[builder(setter(name = "_properties"), default, private)]
61    _properties: BTreeMap<Cow<'a, str>, Value>,
62}
63impl<'a> Request<'a> {
64    /// Create a builder for the endpoint.
65    pub fn builder() -> RequestBuilder<'a> {
66        RequestBuilder::default()
67    }
68}
69
70impl<'a> RequestBuilder<'a> {
71    /// Add a single header to the Member.
72    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
73    where
74        K: Into<HeaderName>,
75        V: Into<HeaderValue>,
76    {
77        self._headers
78            .get_or_insert(None)
79            .get_or_insert_with(HeaderMap::new)
80            .insert(header_name.into(), header_value.into());
81        self
82    }
83
84    /// Add multiple headers.
85    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
86    where
87        I: Iterator<Item = T>,
88        T: Into<(Option<HeaderName>, HeaderValue)>,
89    {
90        self._headers
91            .get_or_insert(None)
92            .get_or_insert_with(HeaderMap::new)
93            .extend(iter.map(Into::into));
94        self
95    }
96
97    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
98    where
99        I: Iterator<Item = (K, V)>,
100        K: Into<Cow<'a, str>>,
101        V: Into<Value>,
102    {
103        self._properties
104            .get_or_insert_with(BTreeMap::new)
105            .extend(iter.map(|(k, v)| (k.into(), v.into())));
106        self
107    }
108}
109
110impl RestEndpoint for Request<'_> {
111    fn method(&self) -> http::Method {
112        http::Method::PUT
113    }
114
115    fn endpoint(&self) -> Cow<'static, str> {
116        format!(
117            "images/{image_id}/members/{id}",
118            image_id = self.image_id.as_ref(),
119            id = self.id.as_ref(),
120        )
121        .into()
122    }
123
124    fn parameters(&self) -> QueryParams<'_> {
125        QueryParams::default()
126    }
127
128    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
129        let mut params = JsonBodyParams::default();
130
131        for (key, val) in &self._properties {
132            params.push(key.clone(), val.clone());
133        }
134
135        params.into_body()
136    }
137
138    fn service_type(&self) -> ServiceType {
139        ServiceType::Image
140    }
141
142    fn response_key(&self) -> Option<Cow<'static, str>> {
143        None
144    }
145
146    /// Returns headers to be set into the request
147    fn request_headers(&self) -> Option<&HeaderMap> {
148        self._headers.as_ref()
149    }
150
151    /// Returns required API version
152    fn api_version(&self) -> Option<ApiVersion> {
153        Some(ApiVersion::new(2, 0))
154    }
155}
156
157#[cfg(test)]
158mod tests {
159    use super::*;
160    #[cfg(feature = "sync")]
161    use crate::api::Query;
162    use crate::test::client::FakeOpenStackClient;
163    use crate::types::ServiceType;
164    use http::{HeaderName, HeaderValue};
165    use httpmock::MockServer;
166    use serde_json::json;
167
168    #[test]
169    fn test_service_type() {
170        assert_eq!(
171            Request::builder().build().unwrap().service_type(),
172            ServiceType::Image
173        );
174    }
175
176    #[test]
177    fn test_response_key() {
178        assert!(Request::builder().build().unwrap().response_key().is_none())
179    }
180
181    #[cfg(feature = "sync")]
182    #[test]
183    fn endpoint() {
184        let server = MockServer::start();
185        let client = FakeOpenStackClient::new(server.base_url());
186        let mock = server.mock(|when, then| {
187            when.method(httpmock::Method::PUT).path(format!(
188                "/images/{image_id}/members/{id}",
189                image_id = "image_id",
190                id = "id",
191            ));
192
193            then.status(200)
194                .header("content-type", "application/json")
195                .json_body(json!({ "dummy": {} }));
196        });
197
198        let endpoint = Request::builder()
199            .image_id("image_id")
200            .id("id")
201            .build()
202            .unwrap();
203        let _: serde_json::Value = endpoint.query(&client).unwrap();
204        mock.assert();
205    }
206
207    #[cfg(feature = "sync")]
208    #[test]
209    fn endpoint_headers() {
210        let server = MockServer::start();
211        let client = FakeOpenStackClient::new(server.base_url());
212        let mock = server.mock(|when, then| {
213            when.method(httpmock::Method::PUT)
214                .path(format!(
215                    "/images/{image_id}/members/{id}",
216                    image_id = "image_id",
217                    id = "id",
218                ))
219                .header("foo", "bar")
220                .header("not_foo", "not_bar");
221            then.status(200)
222                .header("content-type", "application/json")
223                .json_body(json!({ "dummy": {} }));
224        });
225
226        let endpoint = Request::builder()
227            .image_id("image_id")
228            .id("id")
229            .headers(
230                [(
231                    Some(HeaderName::from_static("foo")),
232                    HeaderValue::from_static("bar"),
233                )]
234                .into_iter(),
235            )
236            .header(
237                HeaderName::from_static("not_foo"),
238                HeaderValue::from_static("not_bar"),
239            )
240            .build()
241            .unwrap();
242        let _: serde_json::Value = endpoint.query(&client).unwrap();
243        mock.assert();
244    }
245}