1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (c) Fortanix, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use super::*;

/// Type of an external role.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum ExternalRoleKind {
    LdapGroup,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExternalRole {
    pub external_role_id: Uuid,
    pub groups: HashMap<Uuid, UserGroupRole>,
    pub kind: ExternalRoleKind,
    pub last_synced: Time,
    pub name: String,
    pub source_id: Uuid,
    pub acct_id: Uuid,
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct ExternalRoleRequest {
    #[serde(default)]
    pub add_groups: Option<HashMap<Uuid, UserGroupRole>>,
    #[serde(default)]
    pub del_groups: Option<HashSet<Uuid>>,
    #[serde(default)]
    pub external_role_id: Option<Uuid>,
    #[serde(default)]
    pub kind: Option<ExternalRoleKind>,
    #[serde(default)]
    pub mod_groups: Option<HashMap<Uuid, UserGroupRole>>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub source_id: Option<Uuid>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct ListExternalRolesParams {
    pub group_id: Option<Uuid>,
}

impl UrlEncode for ListExternalRolesParams {
    fn url_encode(&self, m: &mut HashMap<&'static str, String>) {
        if let Some(ref v) = self.group_id {
            m.insert("group_id", v.to_string());
        }
    }
}

pub struct OperationListExternalRoles;
#[allow(unused)]
impl Operation for OperationListExternalRoles {
    type PathParams = ();
    type QueryParams = ListExternalRolesParams;
    type Body = ();
    type Output = Vec<ExternalRole>;

    fn method() -> Method {
        Method::Get
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles?{q}", q = q.encode())
    }
    fn to_body(body: &Self::Body) -> Option<serde_json::Value> {
        None
    }
}

impl SdkmsClient {
    pub fn list_external_roles(
        &self,
        query_params: Option<&ListExternalRolesParams>,
    ) -> Result<Vec<ExternalRole>> {
        self.execute::<OperationListExternalRoles>(&(), (), query_params)
    }
}

pub struct OperationGetExternalRole;
#[allow(unused)]
impl Operation for OperationGetExternalRole {
    type PathParams = (Uuid,);
    type QueryParams = ();
    type Body = ();
    type Output = ExternalRole;

    fn method() -> Method {
        Method::Get
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles/{id}", id = p.0)
    }
    fn to_body(body: &Self::Body) -> Option<serde_json::Value> {
        None
    }
}

impl SdkmsClient {
    pub fn get_external_role(&self, id: &Uuid) -> Result<ExternalRole> {
        self.execute::<OperationGetExternalRole>(&(), (id,), None)
    }
}

pub struct OperationCreateExternalRole;
#[allow(unused)]
impl Operation for OperationCreateExternalRole {
    type PathParams = ();
    type QueryParams = ();
    type Body = ExternalRoleRequest;
    type Output = ExternalRole;

    fn method() -> Method {
        Method::Post
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles")
    }
}

impl SdkmsClient {
    pub fn create_external_role(&self, req: &ExternalRoleRequest) -> Result<ExternalRole> {
        self.execute::<OperationCreateExternalRole>(req, (), None)
    }
}

pub struct OperationSyncExternalRole;
#[allow(unused)]
impl Operation for OperationSyncExternalRole {
    type PathParams = (Uuid,);
    type QueryParams = ();
    type Body = ();
    type Output = ExternalRole;

    fn method() -> Method {
        Method::Post
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles/{id}/sync", id = p.0)
    }
    fn to_body(body: &Self::Body) -> Option<serde_json::Value> {
        None
    }
}

impl SdkmsClient {
    pub fn sync_external_role(&self, id: &Uuid) -> Result<ExternalRole> {
        self.execute::<OperationSyncExternalRole>(&(), (id,), None)
    }
}

pub struct OperationUpdateExternalRole;
#[allow(unused)]
impl Operation for OperationUpdateExternalRole {
    type PathParams = (Uuid,);
    type QueryParams = ();
    type Body = ExternalRoleRequest;
    type Output = ExternalRole;

    fn method() -> Method {
        Method::Patch
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles/{id}", id = p.0)
    }
}

impl SdkmsClient {
    pub fn update_external_role(
        &self,
        id: &Uuid,
        req: &ExternalRoleRequest,
    ) -> Result<ExternalRole> {
        self.execute::<OperationUpdateExternalRole>(req, (id,), None)
    }
}

pub struct OperationDeleteExternalRole;
#[allow(unused)]
impl Operation for OperationDeleteExternalRole {
    type PathParams = (Uuid,);
    type QueryParams = ();
    type Body = ();
    type Output = ();

    fn method() -> Method {
        Method::Delete
    }
    fn path(p: <Self::PathParams as TupleRef>::Ref, q: Option<&Self::QueryParams>) -> String {
        format!("/sys/v1/external_roles/{id}", id = p.0)
    }
    fn to_body(body: &Self::Body) -> Option<serde_json::Value> {
        None
    }
}

impl SdkmsClient {
    pub fn delete_external_role(&self, id: &Uuid) -> Result<()> {
        self.execute::<OperationDeleteExternalRole>(&(), (id,), None)
    }
}