Skip to main content

openstack_cli_image/v2/metadef/namespace/
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//! Create Namespace command
19//!
20//! Wraps invoking of the `v2/metadefs/namespaces` with `POST` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use clap::ValueEnum;
31use openstack_cli_core::common::parse_key_val;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::image::v2::metadef::namespace::create;
34use openstack_types::image::v2::metadef::namespace::response;
35use serde_json::Value;
36
37/// Command without description in OpenAPI
38#[derive(Args)]
39pub struct NamespaceCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    /// Provides a user friendly description of the namespace.
49    #[arg(help_heading = "Body parameters", long)]
50    description: Option<String>,
51
52    /// The user friendly name for the namespace. Used by UI if available.
53    #[arg(help_heading = "Body parameters", long)]
54    display_name: Option<String>,
55
56    /// The unique namespace text.
57    #[arg(help_heading = "Body parameters", long)]
58    namespace: String,
59
60    /// Parameter is an array, may be provided multiple times.
61    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
62    objects: Option<Vec<Value>>,
63
64    /// Owner of the namespace.
65    #[arg(help_heading = "Body parameters", long)]
66    owner: Option<String>,
67
68    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
69    properties: Option<Vec<(String, Value)>>,
70
71    /// If true, namespace will not be deletable.
72    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
73    protected: Option<bool>,
74
75    /// Parameter is an array, may be provided multiple times.
76    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
77    resource_type_associations: Option<Vec<Value>>,
78
79    /// Parameter is an array, may be provided multiple times.
80    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
81    tags: Option<Vec<String>>,
82
83    /// Scope of namespace accessibility.
84    #[arg(help_heading = "Body parameters", long)]
85    visibility: Option<Visibility>,
86}
87
88/// Query parameters
89#[derive(Args)]
90struct QueryParameters {}
91
92/// Path parameters
93#[derive(Args)]
94struct PathParameters {}
95
96#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
97enum Visibility {
98    Private,
99    Public,
100}
101
102impl NamespaceCommand {
103    /// Perform command action
104    pub async fn take_action<C: CliArgs>(
105        &self,
106        parsed_args: &C,
107        client: &mut AsyncOpenStack,
108    ) -> Result<(), OpenStackCliError> {
109        info!("Create Namespace");
110
111        let op = OutputProcessor::from_args(
112            parsed_args,
113            Some("image.metadef/namespace"),
114            Some("create"),
115        );
116        op.validate_args(parsed_args)?;
117
118        let mut ep_builder = create::Request::builder();
119
120        // Set body parameters
121        // Set Request.description data
122        if let Some(arg) = &self.description {
123            ep_builder.description(arg);
124        }
125
126        // Set Request.display_name data
127        if let Some(arg) = &self.display_name {
128            ep_builder.display_name(arg);
129        }
130
131        // Set Request.namespace data
132        ep_builder.namespace(&self.namespace);
133
134        // Set Request.objects data
135        if let Some(arg) = &self.objects {
136            let objects_builder: Vec<create::Objects> = arg
137                .iter()
138                .flat_map(|v| serde_json::from_value::<create::Objects>(v.to_owned()))
139                .collect::<Vec<create::Objects>>();
140            ep_builder.objects(objects_builder);
141        }
142
143        // Set Request.owner data
144        if let Some(arg) = &self.owner {
145            ep_builder.owner(arg);
146        }
147
148        // Set Request.properties data
149        if let Some(arg) = &self.properties {
150            ep_builder.properties(
151                arg.iter()
152                    .map(|(k, v)| {
153                        serde_json::from_value(v.to_owned()).map(|v: create::Properties| (k, v))
154                    })
155                    .collect::<Result<Vec<_>, _>>()?
156                    .into_iter(),
157            );
158        }
159
160        // Set Request.protected data
161        if let Some(arg) = &self.protected {
162            ep_builder.protected(*arg);
163        }
164
165        // Set Request.resource_type_associations data
166        if let Some(arg) = &self.resource_type_associations {
167            let resource_type_associations_builder: Vec<create::ResourceTypeAssociations> = arg
168                .iter()
169                .flat_map(|v| {
170                    serde_json::from_value::<create::ResourceTypeAssociations>(v.to_owned())
171                })
172                .collect::<Vec<create::ResourceTypeAssociations>>();
173            ep_builder.resource_type_associations(resource_type_associations_builder);
174        }
175
176        // Set Request.tags data
177        if let Some(arg) = &self.tags {
178            let tags_builder: Vec<create::Tags> = arg
179                .iter()
180                .flat_map(|v| create::TagsBuilder::default().name(v).build())
181                .collect();
182            ep_builder.tags(tags_builder);
183        }
184
185        // Set Request.visibility data
186        if let Some(arg) = &self.visibility {
187            let tmp = match arg {
188                Visibility::Private => create::Visibility::Private,
189                Visibility::Public => create::Visibility::Public,
190            };
191            ep_builder.visibility(tmp);
192        }
193
194        let ep = ep_builder
195            .build()
196            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
197
198        let data: serde_json::Value = ep.query_async(client).await?;
199
200        op.output_single::<response::create::NamespaceResponse>(data.clone())?;
201        // Show command specific hints
202        op.show_command_hint()?;
203        Ok(())
204    }
205}