Skip to main content

openstack_cli_network/v2/agent/
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 Agent command
19//!
20//! Wraps invoking of the `v2.0/agents/{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 openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::network::v2::agent::set;
33use openstack_types::network::v2::agent::response;
34
35/// Updates an agent.
36///
37/// Normal response codes: 200
38///
39/// Error response codes: 400, 401, 403, 404
40#[derive(Args)]
41#[command(about = "Update agent")]
42pub struct AgentCommand {
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    agent: Agent,
53}
54
55/// Query parameters
56#[derive(Args)]
57struct QueryParameters {}
58
59/// Path parameters
60#[derive(Args)]
61struct PathParameters {
62    /// id parameter for /v2.0/agents/{id} API
63    #[arg(
64        help_heading = "Path parameters",
65        id = "path_param_id",
66        value_name = "ID"
67    )]
68    id: String,
69}
70/// Agent Body data
71#[derive(Args, Clone)]
72struct Agent {
73    /// The administrative state of the resource, which is up (`true`) or down
74    /// (`false`). Default is `true`.
75    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
76    admin_state_up: Option<bool>,
77
78    /// A human-readable description for the resource. Default is an empty
79    /// string.
80    #[arg(help_heading = "Body parameters", long)]
81    description: Option<String>,
82
83    /// Set explicit NULL for the description
84    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
85    no_description: bool,
86}
87
88impl AgentCommand {
89    /// Perform command action
90    pub async fn take_action<C: CliArgs>(
91        &self,
92        parsed_args: &C,
93        client: &mut AsyncOpenStack,
94    ) -> Result<(), OpenStackCliError> {
95        info!("Set Agent");
96
97        let op = OutputProcessor::from_args(parsed_args, Some("network.agent"), Some("set"));
98        op.validate_args(parsed_args)?;
99
100        let mut ep_builder = set::Request::builder();
101
102        ep_builder.id(&self.path.id);
103
104        // Set body parameters
105        // Set Request.agent data
106        let args = &self.agent;
107        let mut agent_builder = set::AgentBuilder::default();
108        if let Some(val) = &args.admin_state_up {
109            agent_builder.admin_state_up(*val);
110        }
111
112        if let Some(val) = &args.description {
113            agent_builder.description(Some(val.into()));
114        } else if args.no_description {
115            agent_builder.description(None);
116        }
117
118        ep_builder.agent(
119            agent_builder
120                .build()
121                .wrap_err("error preparing the request data")?,
122        );
123
124        let ep = ep_builder
125            .build()
126            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
127
128        let data: serde_json::Value = ep.query_async(client).await?;
129
130        op.output_single::<response::set::AgentResponse>(data.clone())?;
131        // Show command specific hints
132        op.show_command_hint()?;
133        Ok(())
134    }
135}