Skip to content

Commit

Permalink
Rename functions to align with arm (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
demoray authored Jul 11, 2024
1 parent 144b9dd commit b03ef64
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 42 deletions.
6 changes: 3 additions & 3 deletions examples/cleanup-orphaned-assignments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use azure_pim_cli::{roles::RoleAssignments, ListFilter, PimClient};
use azure_pim_cli::{models::roles::RoleAssignments, ListFilter, PimClient};
use clap::Parser;
use std::{
io::{stderr, stdin},
Expand Down Expand Up @@ -74,7 +74,7 @@ fn main() -> Result<()> {
}

let mut objects = client
.list_assignments(&role_assignment.scope)
.role_assignments(&role_assignment.scope)
.context("unable to list active assignments")?;
let definitions = client.role_definitions(&role_assignment.scope)?;
debug!("{} total entries", objects.len());
Expand All @@ -97,7 +97,7 @@ fn main() -> Result<()> {
}

client
.delete_assignment(&entry.properties.scope, &entry.name)
.delete_role_assignment(&entry.properties.scope, &entry.name)
.context("unable to delete assignment")?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
az_cli::{extract_oid, get_token, TokenScope},
scope::Scope,
models::scope::Scope,
};
use anyhow::{anyhow, Context, Result};
use derive_setters::Setters;
Expand Down
18 changes: 10 additions & 8 deletions src/bin/az-pim.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anyhow::{bail, Context, Result};
use azure_pim_cli::{
assignments::Assignment,
check_latest_version,
interactive::{interactive_ui, Selected},
roles::{Role, RoleAssignments},
scope::Scope,
models::{
assignments::Assignment,
roles::{Role, RoleAssignments},
scope::Scope,
},
ListFilter, PimClient,
};
use clap::{ArgAction, Args, Command, CommandFactory, Parser, Subcommand, ValueHint};
Expand Down Expand Up @@ -576,7 +578,7 @@ impl AssignmentSubCommand {
let scope = build_scope(subscription, resource_group, scope, provider)?
.context("valid scope must be provided")?;
let objects = client
.list_assignments(&scope)
.role_assignments(&scope)
.context("unable to list active assignments")?;
output(&objects)?;
}
Expand All @@ -590,7 +592,7 @@ impl AssignmentSubCommand {
let scope = build_scope(subscription, resource_group, scope, provider)?
.context("valid scope must be provided")?;
client
.delete_assignment(&scope, &assignment_name)
.delete_role_assignment(&scope, &assignment_name)
.context("unable to delete assignment")?;
}
Self::DeleteSet { config } => {
Expand All @@ -599,7 +601,7 @@ impl AssignmentSubCommand {
.context("unable to parse config file")?;
for entry in entries {
client
.delete_assignment(&entry.properties.scope, &entry.name)
.delete_role_assignment(&entry.properties.scope, &entry.name)
.context("unable to delete assignment")?;
}
}
Expand All @@ -613,7 +615,7 @@ impl AssignmentSubCommand {
let scope = build_scope(subscription, resource_group, scope, provider)?
.context("valid scope must be provided")?;
let mut objects = client
.list_assignments(&scope)
.role_assignments(&scope)
.context("unable to list active assignments")?;
let definitions = client.role_definitions(&scope)?;
debug!("{} total entries", objects.len());
Expand All @@ -636,7 +638,7 @@ impl AssignmentSubCommand {
}

client
.delete_assignment(&entry.properties.scope, &entry.name)
.delete_role_assignment(&entry.properties.scope, &entry.name)
.context("unable to delete assignment")?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/interactive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::roles::{RoleAssignment, RoleAssignments};
use crate::models::roles::{RoleAssignment, RoleAssignments};
use anyhow::Result;
use ratatui::{
crossterm::{
Expand Down
47 changes: 32 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@
#![allow(clippy::module_name_repetitions)]

mod activate;
pub mod assignments;
mod az_cli;
mod backend;
mod definitions;
mod graph;
pub mod interactive;
mod latest;
pub mod resources;
pub mod roles;
pub mod scope;
pub mod models;

pub use crate::latest::check_latest_version;
use crate::{
activate::check_error_response,
assignments::{Assignment, Assignments},
backend::Backend,
definitions::{Definition, Definitions},
graph::get_objects_by_ids,
resources::ChildResource,
roles::{RoleAssignment, RoleAssignments},
scope::Scope,
models::{
assignments::{Assignment, Assignments},
definitions::{Definition, Definitions},
resources::ChildResource,
roles::{RoleAssignment, RoleAssignments},
scope::Scope,
},
};
use anyhow::{bail, ensure, Context, Result};
use backend::Operation;
Expand Down Expand Up @@ -161,6 +159,9 @@ impl PimClient {
}

/// List the roles active role assignments for the current user
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn list_active_role_assignments(
&self,
scope: Option<Scope>,
Expand Down Expand Up @@ -486,8 +487,11 @@ impl PimClient {
Ok(())
}

/// List all assignments (not just those managed by PIM)
pub fn list_assignments(&self, scope: &Scope) -> Result<Vec<Assignment>> {
/// List role assignments
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn role_assignments(&self, scope: &Scope) -> Result<Vec<Assignment>> {
info!("listing assignments for {scope}");
let value = self
.backend
Expand All @@ -509,6 +513,10 @@ impl PimClient {
Ok(assignments)
}

/// List eligible child resources for the specified scope
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn eligible_child_resources(&self, scope: &Scope) -> Result<BTreeSet<ChildResource>> {
info!("listing eligible child resources for {scope}");
let value = self
Expand All @@ -521,6 +529,9 @@ impl PimClient {
}

/// List all assignments (not just those managed by PIM)
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn role_definitions(&self, scope: &Scope) -> Result<Vec<Definition>> {
info!("listing role definitions for {scope}");
let definitions = self
Expand All @@ -533,7 +544,11 @@ impl PimClient {
Ok(definitions.value)
}

pub fn delete_assignment(&self, scope: &Scope, assignment_name: &str) -> Result<()> {
/// Delete a role assignment
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn delete_role_assignment(&self, scope: &Scope, assignment_name: &str) -> Result<()> {
info!("deleting assignment {assignment_name} from {scope}");
self.backend
.request(Method::DELETE, Operation::RoleAssignments)
Expand All @@ -544,11 +559,13 @@ impl PimClient {
Ok(())
}

/// Delete the specified role assignment
/// Delete eligibile role assignment
///
/// This removes role assignments that are available via PIM.
///
/// # Errors
/// Will return `Err` if the request fails or the response is not valid JSON
pub fn delete_role_assignment(&self, assignment: &RoleAssignment) -> Result<()> {
pub fn delete_eligible_role_assignment(&self, assignment: &RoleAssignment) -> Result<()> {
let RoleAssignment {
scope,
role_definition_id,
Expand Down
4 changes: 2 additions & 2 deletions src/assignments.rs → src/models/assignments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{graph::Object, scope::Scope};
use crate::{graph::Object, models::scope::Scope};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, Serialize)]
Expand Down Expand Up @@ -52,7 +52,7 @@ mod tests {

#[test]
fn test_deserialization() -> Result<()> {
const DATA: &str = include_str!("../tests/data/assignments.json");
const DATA: &str = include_str!("../../tests/data/assignments.json");
let data: Assignments = serde_json::from_str(DATA)?;
assert_json_snapshot!(data);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/definitions.rs → src/models/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod tests {

#[test]
fn test_deserialization() -> Result<()> {
const ROLES: &str = include_str!("../tests/data/definitions.json");
const ROLES: &str = include_str!("../../tests/data/definitions.json");
let definitions: Definitions = serde_json::from_str(ROLES)?;
assert_json_snapshot!(definitions);
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod assignments;
pub(crate) mod definitions;
pub(crate) mod resources;
pub mod roles;
pub mod scope;
5 changes: 2 additions & 3 deletions src/resources.rs → src/models/resources.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::models::scope::Scope;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeSet;

use crate::scope::Scope;

#[derive(Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq, Debug)]
pub struct ChildResource {
pub id: Scope,
Expand Down Expand Up @@ -39,7 +38,7 @@ mod tests {

#[test]
fn test_child_resource_parse() -> Result<()> {
let data: Value = from_str(include_str!("../tests/data/child-resources.json"))?;
let data: Value = from_str(include_str!("../../tests/data/child-resources.json"))?;
let result = ChildResource::parse(&data)?;
assert_json_snapshot!(result);
Ok(())
Expand Down
8 changes: 5 additions & 3 deletions src/roles.rs → src/models/roles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::graph::Object;
use crate::scope::{Scope, ScopeError};
use crate::{
graph::Object,
models::scope::{Scope, ScopeError},
};
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -172,7 +174,7 @@ mod tests {

#[test]
fn parse_active() -> Result<()> {
const ASSIGNMENTS: &str = include_str!("../tests/data/role-assignments.json");
const ASSIGNMENTS: &str = include_str!("../../tests/data/role-assignments.json");
let assignments = RoleAssignments::parse(&serde_json::from_str(ASSIGNMENTS)?, false)?;
assert_json_snapshot!(&assignments);
let assignments = RoleAssignments::parse(&serde_json::from_str(ASSIGNMENTS)?, true)?;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/assignments.rs
source: src/models/assignments.rs
expression: data
---
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/definitions.rs
source: src/models/definitions.rs
expression: definitions
---
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/resources.rs
source: src/models/resources.rs
expression: result
---
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/roles.rs
source: src/models/roles.rs
expression: "&assignments"
---
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/roles.rs
source: src/models/roles.rs
expression: "&assignments"
---
[
Expand Down

0 comments on commit b03ef64

Please sign in to comment.