Skip to content

Commit

Permalink
add role assignment parsing support (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
demoray authored Jul 8, 2024
1 parent 090c990 commit c820913
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/assignments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, Serialize)]
pub(crate) struct Assignments {
pub(crate) value: Vec<Assignment>,
}

#[derive(Deserialize, Debug, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Assignment {
pub id: String,
pub name: String,
pub properties: Properties,
#[serde(rename = "type")]
pub type_: String,
}

#[derive(Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct Properties {
#[serde(skip_serializing_if = "Option::is_none")]
pub condition: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub condition_version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_on: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_by: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_on: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_by: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub role_definition_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub delegated_managed_identity_resource_id: Option<String>,
pub principal_id: String,
pub principal_type: String,
pub scope: String,
}

#[cfg(test)]
mod tests {
use super::Assignments;
use anyhow::Result;
use insta::assert_json_snapshot;

#[test]
fn test_deserialization() -> Result<()> {
const DATA: &str = include_str!("../tests/data/assignments.json");
let data: Assignments = serde_json::from_str(DATA)?;
assert_json_snapshot!(data);
Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![allow(clippy::module_name_repetitions)]

pub mod activate;
mod assignments;
pub mod az_cli;
mod definitions;
pub mod interactive;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: src/assignments.rs
expression: data
---
{
"value": [
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000001",
"name": "00000000-0000-0000-0000-000000000001",
"properties": {
"createdOn": "2023-10-10T19:09:15.2878306Z",
"createdBy": "",
"updatedOn": "2023-10-10T19:09:15.2878306Z",
"updatedBy": "",
"roleDefinitionId": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/00000000-0000-0000-0000-000000000002",
"principalId": "c2104d9b-8c91-4f28-8a0a-5909d768818c",
"principalType": "ServicePrincipal",
"scope": "/subscriptions/00000000-0000-0000-0000-000000000000"
},
"type": "Microsoft.Authorization/roleAssignments"
}
]
}
23 changes: 23 additions & 0 deletions tests/data/assignments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"value": [
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000001",
"name": "00000000-0000-0000-0000-000000000001",
"properties": {
"condition": null,
"conditionVersion": null,
"createdBy": "",
"createdOn": "2023-10-10T19:09:15.2878306Z",
"delegatedManagedIdentityResourceId": null,
"description": null,
"principalId": "c2104d9b-8c91-4f28-8a0a-5909d768818c",
"principalType": "ServicePrincipal",
"roleDefinitionId": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/00000000-0000-0000-0000-000000000002",
"scope": "/subscriptions/00000000-0000-0000-0000-000000000000",
"updatedBy": "",
"updatedOn": "2023-10-10T19:09:15.2878306Z"
},
"type": "Microsoft.Authorization/roleAssignments"
}
]
}

0 comments on commit c820913

Please sign in to comment.