Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
hatchan committed Mar 4, 2024
1 parent 7b4ee18 commit 2dba624
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 49 deletions.
36 changes: 21 additions & 15 deletions src/integrations/pagerduty_receivers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::api_client_configuration;
use crate::interactive::{name_req, workspace_picker};
use crate::output::{output_details, output_json, output_list, GenericKeyValue};
use crate::utils::{clear_or_update, override_pagination, override_sorting};
use crate::utils::clear_or_update;
use anyhow::Result;
use clap::{Parser, ValueEnum};
use cli_table::Table;
Expand Down Expand Up @@ -77,7 +77,7 @@ struct CreateArgs {
/// An optional name referencing a Template. This template will be expanded
/// if a PagerDuty incident is created.
#[clap(long, short)]
incident_creation_template: Option<Name>,
incident_created_template: Option<Name>,

/// Output of the webhooks
#[clap(long, short, default_value = "table", value_enum)]
Expand Down Expand Up @@ -105,10 +105,10 @@ async fn handle_create(args: CreateArgs) -> Result<()> {
)?;

let new_pagerduty_receiver = NewPagerDutyReceiver::builder()
.creation_template(args.incident_creation_template)
.incident_created_template_name(args.incident_created_template)
.build();
let pagerduty_receiver = client
.pagerduty_receiver_create(workspace_id, name, new_pagerduty_receiver)
.pagerduty_receiver_create(workspace_id, &name, new_pagerduty_receiver)
.await?;

match args.output {
Expand Down Expand Up @@ -149,7 +149,7 @@ async fn handle_get(args: GetArgs) -> Result<()> {
let workspace_id = workspace_picker(&client, args.workspace_id).await?;
let name = name_req("PagerDuty webhook receiver name", args.name, None)?;

let pagerduty_receiver = client.pagerduty_receiver_get(workspace_id, name).await?;
let pagerduty_receiver = client.pagerduty_receiver_get(workspace_id, &name).await?;

match args.output {
PagerDutyWebhookOutput::Table => {
Expand Down Expand Up @@ -217,7 +217,7 @@ async fn handle_update(args: UpdateArgs) -> Result<()> {
.build();

let pagerduty_receiver = client
.pagerduty_receiver_update(workspace_id, name, update_pagerduty_receiver)
.pagerduty_receiver_update(workspace_id, &name, update_pagerduty_receiver)
.await?;

match args.output {
Expand Down Expand Up @@ -254,7 +254,9 @@ async fn handle_delete(args: DeleteArgs) -> Result<()> {
let workspace_id = workspace_picker(&client, args.workspace_id).await?;
let name = name_req("PagerDuty webhook receiver name", args.name, None)?;

client.pagerduty_receiver_delete(workspace_id, name).await?;
client
.pagerduty_receiver_delete(workspace_id, &name)
.await?;

info!("Deleted Pagerduty receiver");
Ok(())
Expand All @@ -276,11 +278,11 @@ struct ListArgs {

/// Page to display
#[clap(long)]
page: Option<u32>,
page: Option<i32>,

/// Amount of integrations to display per page
#[clap(long)]
limit: Option<u32>,
limit: Option<i32>,

/// Output of the webhooks
#[clap(long, short, default_value = "table", value_enum)]
Expand All @@ -300,11 +302,15 @@ async fn handle_list(args: ListArgs) -> Result<()> {
let client = api_client_configuration(args.token, args.config, args.base_url).await?;

let workspace_id = workspace_picker(&client, args.workspace_id).await?;
let sorting = override_sorting(args.sort_by, args.sort_direction);
let pagination = override_pagination(args.page, args.limit);

let pagerduty_receivers = client
.pagerduty_receiver_list(workspace_id, sorting, pagination)
.pagerduty_receiver_list(
workspace_id,
args.page,
args.limit,
args.sort_by.map(Into::into),
args.sort_direction.map(Into::into),
)
.await?;

if pagerduty_receivers.has_more_results {
Expand All @@ -326,8 +332,8 @@ pub struct PagerDutyReceiverRow {
#[table(title = "Name")]
pub name: String,

#[table(title = "Incident Creation Template")]
pub incident_creation_template: String,
#[table(title = "Incident Created Template")]
pub incident_created_template: String,

#[table(title = "Updated at")]
pub updated_at: String,
Expand All @@ -340,7 +346,7 @@ impl From<PagerDutyReceiver> for PagerDutyReceiverRow {
fn from(pagerduty_receiver: PagerDutyReceiver) -> Self {
Self {
name: pagerduty_receiver.name.to_string(),
incident_creation_template: pagerduty_receiver
incident_created_template: pagerduty_receiver
.incident_created_template_name
.map(|name| name.to_string())
.unwrap_or_else(|| String::from("<none>")),
Expand Down
34 changes: 0 additions & 34 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
use fiberplane::models::paging::Pagination;
use fiberplane::models::sorting::{SortDirection, SortField, Sorting};

pub fn clear_or_update<T>(clear: bool, update: Option<T>) -> Option<Option<T>> {
if clear {
Some(None)
} else {
update.map(Some)
}
}

pub fn override_sorting<T: SortField>(
sort_field: Option<T>,
sort_direction: Option<SortDirection>,
) -> Sorting<T> {
let mut sorting: Sorting<T> = Default::default();

if let Some(sort_field) = sort_field {
sorting.sort_by = sort_field;
}

if let Some(sort_direction) = sort_direction {
sorting.sort_direction = sort_direction;
}

sorting
}

pub fn override_pagination(page: Option<u32>, limit: Option<u32>) -> Pagination {
let mut pagination: Pagination = Default::default();

if let Some(page) = page {
pagination.page = page;
}

if let Some(limit) = limit {
pagination.limit = limit;
}

pagination
}

0 comments on commit 2dba624

Please sign in to comment.