openstack_cli_network/v2/rbac_policy/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 RbacPolicy command
19//!
20//! Wraps invoking of the `v2.0/rbac-policies` with `POST` method
21
22use clap::Args;
23use eyre::WrapErr;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::network::v2::rbac_policy::create;
33use openstack_types::network::v2::rbac_policy::response;
34
35/// Create RBAC policy for given tenant.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401
40#[derive(Args)]
41#[command(about = "Create RBAC policy")]
42pub struct RbacPolicyCommand {
43 /// Request Query parameters
44 #[command(flatten)]
45 query: QueryParameters,
46
47 /// Path parameters
48 #[command(flatten)]
49 path: PathParameters,
50
51 #[command(flatten)]
52 rbac_policy: RbacPolicy,
53}
54
55/// Query parameters
56#[derive(Args)]
57struct QueryParameters {}
58
59/// Path parameters
60#[derive(Args)]
61struct PathParameters {}
62/// RbacPolicy Body data
63#[derive(Args, Clone)]
64struct RbacPolicy {
65 /// Action for the RBAC policy which is `access_as_external` or
66 /// `access_as_shared`.
67 #[arg(help_heading = "Body parameters", long)]
68 action: Option<String>,
69
70 /// The ID of the `object_type` resource. An `object_type` of `network`
71 /// returns a network ID, an `object_type` of `qos-policy` returns a QoS
72 /// policy ID, an `object_type` of `security-group` returns a security
73 /// group ID, an `object_type` of `address-scope` returns a address scope
74 /// ID, an `object_type` of `subnetpool` returns a subnetpool ID and an
75 /// `object_type` of `address-group` returns an address group ID.
76 #[arg(help_heading = "Body parameters", long)]
77 object_id: Option<String>,
78
79 /// The type of the object that the RBAC policy affects. Types include
80 /// `qos-policy`, `network`, `security-group`, `address-scope`,
81 /// `subnetpool` or `address-group`.
82 #[arg(help_heading = "Body parameters", long)]
83 object_type: Option<String>,
84
85 /// The ID of the tenant to which the RBAC policy will be enforced. Please
86 /// note that Neutron does not perform any type of validation that the
87 /// value provided is actually the ID of the existing project. If, for
88 /// example, the name of the project is provided here, it will be accepted
89 /// by the Neutron API, but the RBAC rule created will not work as
90 /// expected.
91 #[arg(help_heading = "Body parameters", long)]
92 target_tenant: Option<String>,
93
94 #[arg(help_heading = "Body parameters", long)]
95 tenant_id: Option<String>,
96}
97
98impl RbacPolicyCommand {
99 /// Perform command action
100 pub async fn take_action<C: CliArgs>(
101 &self,
102 parsed_args: &C,
103 client: &mut AsyncOpenStack,
104 ) -> Result<(), OpenStackCliError> {
105 info!("Create RbacPolicy");
106
107 let op =
108 OutputProcessor::from_args(parsed_args, Some("network.rbac_policy"), Some("create"));
109 op.validate_args(parsed_args)?;
110
111 let mut ep_builder = create::Request::builder();
112
113 // Set body parameters
114 // Set Request.rbac_policy data
115 let args = &self.rbac_policy;
116 let mut rbac_policy_builder = create::RbacPolicyBuilder::default();
117 if let Some(val) = &args.action {
118 rbac_policy_builder.action(val);
119 }
120
121 if let Some(val) = &args.object_id {
122 rbac_policy_builder.object_id(val);
123 }
124
125 if let Some(val) = &args.object_type {
126 rbac_policy_builder.object_type(val);
127 }
128
129 if let Some(val) = &args.target_tenant {
130 rbac_policy_builder.target_tenant(val);
131 }
132
133 if let Some(val) = &args.tenant_id {
134 rbac_policy_builder.tenant_id(val);
135 }
136
137 ep_builder.rbac_policy(
138 rbac_policy_builder
139 .build()
140 .wrap_err("error preparing the request data")?,
141 );
142
143 let ep = ep_builder
144 .build()
145 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
146
147 let data: serde_json::Value = ep.query_async(client).await?;
148
149 op.output_single::<response::create::RbacPolicyResponse>(data.clone())?;
150 // Show command specific hints
151 op.show_command_hint()?;
152 Ok(())
153 }
154}