Skip to content

Commit

Permalink
make create entity and group closer to the specification
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-gt committed Mar 4, 2024
1 parent 660a4eb commit 030f518
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/api/identity/entity/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::responses::{
#[builder(setter(into, strip_option), default)]
pub struct CreateEntityRequest {
/// Name of the entity.
pub name: String,
pub name: Option<String>,
/// ID of the entity. If set, updates the corresponding existing entity.
pub id: Option<String>,
/// Metadata to be associated with the entity.
Expand Down
2 changes: 1 addition & 1 deletion src/api/identity/group/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{collections::HashMap, fmt::Debug};
#[builder(setter(into, strip_option), default)]
pub struct CreateGroupRequest {
/// Name of the group. If set (and ID is not set), updates the corresponding existing group.
pub name: String,
pub name: Option<String>,
/// ID of the group. If set, updates the corresponding existing group.
pub id: Option<String>,
/// Type of the group, internal or external. Defaults to internal.
Expand Down
3 changes: 1 addition & 2 deletions src/identity/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ use crate::{
#[instrument(skip(client, opts), err)]
pub async fn create(
client: &impl Client,
name: &str,
opts: Option<&mut CreateEntityRequestBuilder>,
) -> Result<(), ClientError> {
let mut t = CreateEntityRequest::builder();
let endpoint = opts.unwrap_or(&mut t).name(name).build().unwrap();
let endpoint = opts.unwrap_or(&mut t).build().unwrap();
api::exec_with_empty(client, endpoint).await
}

Expand Down
3 changes: 1 addition & 2 deletions src/identity/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ use crate::{
#[instrument(skip(client, opts), err)]
pub async fn create(
client: &impl Client,
name: &str,
opts: Option<&mut CreateGroupRequestBuilder>,
) -> Result<(), ClientError> {
let mut t = CreateGroupRequest::builder();
let endpoint = opts.unwrap_or(&mut t).name(name).build().unwrap();
let endpoint = opts.unwrap_or(&mut t).build().unwrap();
api::exec_with_empty(client, endpoint).await
}

Expand Down
63 changes: 42 additions & 21 deletions tests/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ fn test_group_and_group_alias() {
async fn test_create_entity(client: &VaultClient) -> String {
identity::entity::create(
client,
ENTITY_NAME,
Some(&mut CreateEntityRequestBuilder::default().policies(vec![POLICY.to_string()])),
Some(
&mut CreateEntityRequestBuilder::default()
.policies(vec![POLICY.to_string()])
.name(ENTITY_NAME),
),
)
.await
.unwrap();
Expand All @@ -106,10 +109,10 @@ async fn test_create_entity(client: &VaultClient) -> String {

identity::entity::create(
client,
ENTITY_NAME,
Some(
&mut CreateEntityRequestBuilder::default()
.disabled(true)
.name(ENTITY_NAME)
.id(&entity.id),
),
)
Expand Down Expand Up @@ -208,12 +211,18 @@ async fn test_delete_entity_by_name(client: &VaultClient) {
}

async fn test_batch_delete_entity(client: &VaultClient) {
identity::entity::create(client, "test-entity1", None)
.await
.unwrap();
identity::entity::create(client, "test-entity2", None)
.await
.unwrap();
identity::entity::create(
client,
Some(CreateEntityRequestBuilder::default().name("test-entity1")),
)
.await
.unwrap();
identity::entity::create(
client,
Some(CreateEntityRequestBuilder::default().name("test-entity2")),
)
.await
.unwrap();
let entity1 = identity::entity::read_by_name(client, "test-entity1")
.await
.unwrap();
Expand Down Expand Up @@ -242,15 +251,24 @@ async fn test_batch_delete_entity(client: &VaultClient) {
}

async fn test_merge_entity(client: &VaultClient) {
identity::entity::create(client, "test-entity1", None)
.await
.unwrap();
identity::entity::create(client, "test-entity2", None)
.await
.unwrap();
identity::entity::create(client, "test-entity3", None)
.await
.unwrap();
identity::entity::create(
client,
Some(CreateEntityRequestBuilder::default().name("test-entity1")),
)
.await
.unwrap();
identity::entity::create(
client,
Some(CreateEntityRequestBuilder::default().name("test-entity2")),
)
.await
.unwrap();
identity::entity::create(
client,
Some(CreateEntityRequestBuilder::default().name("test-entity3")),
)
.await
.unwrap();
let entity1 = identity::entity::read_by_name(client, "test-entity1")
.await
.unwrap();
Expand Down Expand Up @@ -376,8 +394,11 @@ async fn test_list_entity_alias_by_id(client: &VaultClient, alias_id: &str, expe
async fn test_create_group(client: &VaultClient) -> String {
identity::group::create(
client,
GROUP_NAME,
Some(&mut CreateGroupRequestBuilder::default().policies(vec![POLICY.to_string()])),
Some(
&mut CreateGroupRequestBuilder::default()
.policies(vec![POLICY.to_string()])
.name(GROUP_NAME),
),
)
.await
.unwrap();
Expand All @@ -391,10 +412,10 @@ async fn test_create_group(client: &VaultClient) -> String {
// This one it's called in "updating mode".
identity::group::create(
client,
GROUP_NAME,
Some(
&mut CreateGroupRequestBuilder::default()
.metadata(metadata.clone())
.name(GROUP_NAME)
.id(&group.id),
),
)
Expand Down

0 comments on commit 030f518

Please sign in to comment.