Skip to main content

openstack_cli_load_balancer/v2/l7policy/rule/
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//! Set Rule command
19//!
20//! Wraps invoking of the `v2/lbaas/l7policies/{l7policy_id}/rules/{rule_id}` with `PUT` 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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::load_balancer::v2::l7policy::rule::set;
34use openstack_types::load_balancer::v2::l7policy::rule::response;
35
36/// Updates a L7 rule.
37///
38/// If the request is valid, the service returns the `Accepted (202)` response
39/// code. To confirm the update, check that the L7 rule provisioning status is
40/// `ACTIVE`. If the status is `PENDING_UPDATE`, use a GET operation to poll
41/// the L7 rule object for changes.
42///
43/// This operation returns the updated L7 rule object with the `ACTIVE`,
44/// `PENDING_UPDATE`, or `ERROR` provisioning status.
45#[derive(Args)]
46#[command(about = "Update a L7 Rule")]
47pub struct RuleCommand {
48    /// Request Query parameters
49    #[command(flatten)]
50    query: QueryParameters,
51
52    /// Path parameters
53    #[command(flatten)]
54    path: PathParameters,
55
56    /// Defines attributes that are acceptable of a PUT request.
57    #[command(flatten)]
58    rule: Rule,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {
68    /// l7policy_id parameter for
69    /// /v2/lbaas/l7policies/{l7policy_id}/rules/{rule_id} API
70    #[arg(
71        help_heading = "Path parameters",
72        id = "path_param_l7policy_id",
73        value_name = "L7POLICY_ID"
74    )]
75    l7policy_id: String,
76
77    /// rule_id parameter for
78    /// /v2/lbaas/l7policies/{l7policy_id}/rules/{rule_id} API
79    #[arg(
80        help_heading = "Path parameters",
81        id = "path_param_id",
82        value_name = "ID"
83    )]
84    id: String,
85}
86
87#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
88enum CompareType {
89    Contains,
90    EndsWith,
91    EqualTo,
92    Regex,
93    StartsWith,
94}
95
96#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
97enum Type {
98    Cookie,
99    FileType,
100    Header,
101    HostName,
102    Path,
103    SslConnHasCert,
104    SslDnField,
105    SslVerifyResult,
106}
107
108/// Rule Body data
109#[derive(Args, Clone)]
110struct Rule {
111    /// The administrative state of the resource, which is up (`true`) or down
112    /// (`false`). Default is `true`.
113    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
114    admin_state_up: Option<bool>,
115
116    /// The comparison type for the L7 rule. One of `CONTAINS`, `ENDS_WITH`,
117    /// `EQUAL_TO`, `REGEX`, or `STARTS_WITH`.
118    #[arg(help_heading = "Body parameters", long)]
119    compare_type: Option<CompareType>,
120
121    /// When `true` the logic of the rule is inverted. For example, with invert
122    /// `true`, equal to would become not equal to. Default is `false`.
123    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
124    invert: Option<bool>,
125
126    /// The key to use for the comparison. For example, the name of the cookie
127    /// to evaluate.
128    #[arg(help_heading = "Body parameters", long)]
129    key: Option<String>,
130
131    /// A list of simple strings assigned to the resource.
132    ///
133    /// **New in version 2.5**
134    ///
135    /// Parameter is an array, may be provided multiple times.
136    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
137    tags: Option<Vec<String>>,
138
139    /// The L7 rule type. One of `COOKIE`, `FILE_TYPE`, `HEADER`, `HOST_NAME`,
140    /// `PATH`, `SSL_CONN_HAS_CERT`, `SSL_VERIFY_RESULT`, or `SSL_DN_FIELD`.
141    #[arg(help_heading = "Body parameters", long)]
142    _type: Option<Type>,
143
144    /// The value to use for the comparison. For example, the file type to
145    /// compare.
146    #[arg(help_heading = "Body parameters", long)]
147    value: Option<String>,
148}
149
150impl RuleCommand {
151    /// Perform command action
152    pub async fn take_action<C: CliArgs>(
153        &self,
154        parsed_args: &C,
155        client: &mut AsyncOpenStack,
156    ) -> Result<(), OpenStackCliError> {
157        info!("Set Rule");
158
159        let op = OutputProcessor::from_args(
160            parsed_args,
161            Some("load-balancer.l7policy/rule"),
162            Some("set"),
163        );
164        op.validate_args(parsed_args)?;
165
166        let mut ep_builder = set::Request::builder();
167
168        ep_builder.l7policy_id(&self.path.l7policy_id);
169        ep_builder.id(&self.path.id);
170
171        // Set body parameters
172        // Set Request.rule data
173        let args = &self.rule;
174        let mut rule_builder = set::RuleBuilder::default();
175        if let Some(val) = &args.admin_state_up {
176            rule_builder.admin_state_up(*val);
177        }
178
179        if let Some(val) = &args.compare_type {
180            let tmp = match val {
181                CompareType::Contains => set::CompareType::Contains,
182                CompareType::EndsWith => set::CompareType::EndsWith,
183                CompareType::EqualTo => set::CompareType::EqualTo,
184                CompareType::Regex => set::CompareType::Regex,
185                CompareType::StartsWith => set::CompareType::StartsWith,
186            };
187            rule_builder.compare_type(tmp);
188        }
189
190        if let Some(val) = &args.invert {
191            rule_builder.invert(*val);
192        }
193
194        if let Some(val) = &args.key {
195            rule_builder.key(val);
196        }
197
198        if let Some(val) = &args.tags {
199            rule_builder.tags(val.iter().map(Into::into).collect::<Vec<_>>());
200        }
201
202        if let Some(val) = &args._type {
203            let tmp = match val {
204                Type::Cookie => set::Type::Cookie,
205                Type::FileType => set::Type::FileType,
206                Type::Header => set::Type::Header,
207                Type::HostName => set::Type::HostName,
208                Type::Path => set::Type::Path,
209                Type::SslConnHasCert => set::Type::SslConnHasCert,
210                Type::SslDnField => set::Type::SslDnField,
211                Type::SslVerifyResult => set::Type::SslVerifyResult,
212            };
213            rule_builder._type(tmp);
214        }
215
216        if let Some(val) = &args.value {
217            rule_builder.value(val);
218        }
219
220        ep_builder.rule(
221            rule_builder
222                .build()
223                .wrap_err("error preparing the request data")?,
224        );
225
226        let ep = ep_builder
227            .build()
228            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
229
230        let data: serde_json::Value = ep.query_async(client).await?;
231
232        op.output_single::<response::set::RuleResponse>(data.clone())?;
233        // Show command specific hints
234        op.show_command_hint()?;
235        Ok(())
236    }
237}