Skip to main content

openstack_keystone_core/api/v3/
role_assignment.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
15pub use openstack_keystone_api_types::v3::role_assignment::*;
16
17use crate::api::error::KeystoneApiError;
18use crate::assignment::types;
19
20impl TryFrom<types::Assignment> for Assignment {
21    type Error = KeystoneApiError;
22
23    fn try_from(value: types::Assignment) -> Result<Self, Self::Error> {
24        let mut builder = AssignmentBuilder::default();
25        builder.role(Role {
26            id: value.role_id,
27            name: value.role_name,
28        });
29        match value.r#type {
30            types::AssignmentType::GroupDomain => {
31                builder.group(Group { id: value.actor_id });
32                builder.scope(Scope::Domain(Domain {
33                    id: value.target_id,
34                }));
35            }
36            types::AssignmentType::GroupProject => {
37                builder.group(Group { id: value.actor_id });
38                builder.scope(Scope::Project(Project {
39                    id: value.target_id,
40                }));
41            }
42            types::AssignmentType::UserDomain => {
43                builder.user(User { id: value.actor_id });
44                builder.scope(Scope::Domain(Domain {
45                    id: value.target_id,
46                }));
47            }
48            types::AssignmentType::UserProject => {
49                builder.user(User { id: value.actor_id });
50                builder.scope(Scope::Project(Project {
51                    id: value.target_id,
52                }));
53            }
54            types::AssignmentType::UserSystem => {
55                builder.user(User { id: value.actor_id });
56                builder.scope(Scope::System(System {
57                    id: value.target_id,
58                }));
59            }
60            types::AssignmentType::GroupSystem => {
61                builder.group(Group { id: value.actor_id });
62                builder.scope(Scope::System(System {
63                    id: value.target_id,
64                }));
65            }
66        }
67        Ok(builder.build()?)
68    }
69}
70
71impl TryFrom<RoleAssignmentListParameters> for types::RoleAssignmentListParameters {
72    type Error = KeystoneApiError;
73
74    fn try_from(value: RoleAssignmentListParameters) -> Result<Self, Self::Error> {
75        let mut builder = types::RoleAssignmentListParametersBuilder::default();
76        // Filter by role
77        if let Some(val) = &value.role_id {
78            builder.role_id(val);
79        }
80
81        // Filter by actor
82        if let Some(val) = &value.user_id {
83            builder.user_id(val);
84        } else if let Some(val) = &value.group_id {
85            builder.group_id(val);
86        }
87
88        // Filter by target
89        if let Some(val) = &value.project_id {
90            builder.project_id(val);
91        } else if let Some(val) = &value.domain_id {
92            builder.domain_id(val);
93        }
94
95        if let Some(val) = value.effective {
96            builder.effective(val);
97        }
98        if let Some(val) = value.include_names {
99            builder.include_names(val);
100        }
101        Ok(builder.build()?)
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108    use crate::assignment::types;
109
110    #[test]
111    fn test_assignment_conversion() {
112        assert_eq!(
113            Assignment {
114                role: Role {
115                    id: "role".into(),
116                    name: Some("role_name".into())
117                },
118                user: Some(User { id: "actor".into() }),
119                scope: Scope::Project(Project {
120                    id: "target".into()
121                }),
122                group: None,
123            },
124            Assignment::try_from(types::Assignment {
125                role_id: "role".into(),
126                role_name: Some("role_name".into()),
127                actor_id: "actor".into(),
128                target_id: "target".into(),
129                r#type: types::AssignmentType::UserProject,
130                inherited: false,
131                implied_via: None,
132            })
133            .unwrap()
134        );
135        assert_eq!(
136            Assignment {
137                role: Role {
138                    id: "role".into(),
139                    name: None
140                },
141                user: Some(User { id: "actor".into() }),
142                scope: Scope::Domain(Domain {
143                    id: "target".into()
144                }),
145                group: None,
146            },
147            Assignment::try_from(types::Assignment {
148                role_id: "role".into(),
149                role_name: None,
150                actor_id: "actor".into(),
151                target_id: "target".into(),
152                r#type: types::AssignmentType::UserDomain,
153                inherited: false,
154                implied_via: None,
155            })
156            .unwrap()
157        );
158        assert_eq!(
159            Assignment {
160                role: Role {
161                    id: "role".into(),
162                    name: None
163                },
164                group: Some(Group { id: "actor".into() }),
165                scope: Scope::Project(Project {
166                    id: "target".into()
167                }),
168                user: None,
169            },
170            Assignment::try_from(types::Assignment {
171                role_id: "role".into(),
172                role_name: None,
173                actor_id: "actor".into(),
174                target_id: "target".into(),
175                r#type: types::AssignmentType::GroupProject,
176                inherited: false,
177                implied_via: None,
178            })
179            .unwrap()
180        );
181        assert_eq!(
182            Assignment {
183                role: Role {
184                    id: "role".into(),
185                    name: None
186                },
187                group: Some(Group { id: "actor".into() }),
188                scope: Scope::Domain(Domain {
189                    id: "target".into()
190                }),
191                user: None,
192            },
193            Assignment::try_from(types::Assignment {
194                role_id: "role".into(),
195                role_name: None,
196                actor_id: "actor".into(),
197                target_id: "target".into(),
198                r#type: types::AssignmentType::GroupDomain,
199                inherited: false,
200                implied_via: None,
201            })
202            .unwrap()
203        );
204    }
205}