netmito/config/client/
workers.rs

1use clap::{Args, Subcommand};
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5use crate::{
6    entity::role::GroupWorkerRole,
7    schema::{
8        RemoveGroupWorkerRoleReq, ReplaceWorkerLabelsReq, ReplaceWorkerTagsReq,
9        UpdateGroupWorkerRoleReq, WorkersQueryReq,
10    },
11};
12
13use super::parse_key_val_colon;
14
15#[derive(Serialize, Debug, Deserialize, Args, derive_more::From, Clone)]
16pub struct WorkersArgs {
17    #[command(subcommand)]
18    pub command: WorkersCommands,
19}
20
21#[derive(Subcommand, Serialize, Debug, Deserialize, derive_more::From, Clone)]
22pub enum WorkersCommands {
23    /// Cancel a worker
24    Cancel(CancelWorkerArgs),
25    /// Replace tags of a worker
26    UpdateTags(WorkerUpdateTagsArgs),
27    /// Replace labels of a worker
28    UpdateLabels(WorkerUpdateLabelsArgs),
29    /// Update the roles of groups to a worker
30    UpdateRoles(UpdateWorkerGroupArgs),
31    /// Remove the accessibility of groups from a worker
32    RemoveRoles(RemoveWorkerGroupArgs),
33    /// Get information about a worker
34    Get(GetWorkerArgs),
35    /// Query workers subject to the filter
36    Query(WorkersQueryArgs),
37}
38
39#[derive(Serialize, Debug, Deserialize, Args, Clone)]
40pub struct CancelWorkerArgs {
41    /// The UUID of the worker
42    pub uuid: Uuid,
43    /// Whether to force the worker to shutdown.
44    /// If not specified, the worker will be try to shutdown gracefully
45    #[arg(short, long)]
46    pub force: bool,
47}
48
49#[derive(Serialize, Debug, Deserialize, Args, Clone)]
50pub struct WorkerUpdateTagsArgs {
51    /// The UUID of the worker
52    pub uuid: Uuid,
53    /// The tags to replace
54    #[arg(num_args = 0..)]
55    pub tags: Vec<String>,
56}
57
58#[derive(Serialize, Debug, Deserialize, Args, Clone)]
59pub struct WorkerUpdateLabelsArgs {
60    /// The UUID of the worker
61    pub uuid: Uuid,
62    /// The labels to replace
63    #[arg(num_args = 0..)]
64    pub labels: Vec<String>,
65}
66
67#[derive(Serialize, Debug, Deserialize, Args, Clone)]
68pub struct UpdateWorkerGroupArgs {
69    /// The UUID of the worker
70    pub uuid: Uuid,
71    /// The name and role of the group on the worker to update
72    #[arg(num_args = 0.., value_parser = parse_key_val_colon::<String, GroupWorkerRole>)]
73    pub roles: Vec<(String, GroupWorkerRole)>,
74}
75
76#[derive(Serialize, Debug, Deserialize, Args, Clone)]
77pub struct RemoveWorkerGroupArgs {
78    /// The UUID of the worker
79    pub uuid: Uuid,
80    /// The name of the group to update
81    #[arg(num_args = 0..)]
82    pub groups: Vec<String>,
83}
84
85#[derive(Serialize, Debug, Deserialize, Args, Clone)]
86pub struct GetWorkerArgs {
87    /// The UUID of the worker
88    pub uuid: Uuid,
89}
90
91#[derive(Serialize, Debug, Deserialize, Args, Clone)]
92pub struct WorkersQueryArgs {
93    /// The name of the group has access to the workers
94    #[arg(short, long)]
95    pub group: Option<String>,
96    /// The role of the group on the workers
97    #[arg(short, long, num_args = 0.., value_delimiter = ',')]
98    pub role: Vec<GroupWorkerRole>,
99    /// The tags of the workers
100    #[arg(short, long, num_args = 0.., value_delimiter = ',')]
101    pub tags: Vec<String>,
102    /// The labels of the workers
103    #[arg(short, long, num_args = 0.., value_delimiter = ',')]
104    pub labels: Vec<String>,
105    /// The username of the creator
106    #[arg(long)]
107    pub creator: Option<String>,
108    /// Whether to output the verbose information of workers
109    #[arg(short, long)]
110    pub verbose: bool,
111    /// Only count the number of workers
112    #[arg(long)]
113    pub count: bool,
114}
115
116impl From<WorkersQueryArgs> for WorkersQueryReq {
117    fn from(args: WorkersQueryArgs) -> Self {
118        Self {
119            group_name: args.group,
120            role: if args.role.is_empty() {
121                None
122            } else {
123                Some(args.role.into_iter().collect())
124            },
125            tags: if args.tags.is_empty() {
126                None
127            } else {
128                Some(args.tags.into_iter().collect())
129            },
130            labels: if args.labels.is_empty() {
131                None
132            } else {
133                Some(args.labels.into_iter().collect())
134            },
135            creator_username: args.creator,
136            count: args.count,
137        }
138    }
139}
140
141impl From<UpdateWorkerGroupArgs> for UpdateGroupWorkerRoleReq {
142    fn from(args: UpdateWorkerGroupArgs) -> Self {
143        Self {
144            relations: args.roles.into_iter().collect(),
145        }
146    }
147}
148
149impl From<WorkerUpdateTagsArgs> for ReplaceWorkerTagsReq {
150    fn from(args: WorkerUpdateTagsArgs) -> Self {
151        Self {
152            tags: args.tags.into_iter().collect(),
153        }
154    }
155}
156
157impl From<WorkerUpdateLabelsArgs> for ReplaceWorkerLabelsReq {
158    fn from(args: WorkerUpdateLabelsArgs) -> Self {
159        Self {
160            labels: args.labels.into_iter().collect(),
161        }
162    }
163}
164
165impl From<RemoveWorkerGroupArgs> for RemoveGroupWorkerRoleReq {
166    fn from(args: RemoveWorkerGroupArgs) -> Self {
167        Self {
168            groups: args.groups.into_iter().collect(),
169        }
170    }
171}