Skip to content

Commit

Permalink
Add proxy support for Jira
Browse files Browse the repository at this point in the history
Jira has the ability to take rest commands over a proxy
using a reqwest client.
  • Loading branch information
cholcombe973 committed Sep 21, 2017
1 parent 26c3525 commit dbc1e17
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ libc = "*"
log = "*"
mktemp = "*"
# protobuf = "*"
reqwest = "*"
rusqlite = "*"
serde = "*"
serde_derive = "*"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ though could prove useful.

## Configuration:
1. Create your configuration file. The utility takes json config
information. Edit the `/etc/ceph_dead_disk/config.json` file to configure it. Fields for this file are:
information. Edit the `/etc/ceph_dead_disk/config.json` file to configure it.
An optional proxy field can be configured to send JIRA REST API requests through.
Fields for this file are:
```
{
"backend": "Ceph",
"db_location": "/etc/ceph_dead_disk/disks.sqlite3",
"proxy": "https://my.proxy",
"jira_user": "test_user",
"jira_password": "user_password",
"jira_host": "https://tickets.jira.com",
Expand Down
83 changes: 57 additions & 26 deletions src/create_support_ticket.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
extern crate goji;
extern crate log;
extern crate reqwest;
extern crate serde_json;

use self::goji::{Credentials, Jira};
use self::goji::Error as GojiError;
use self::goji::issues::*;
use self::serde_json::value::Value;
use super::ConfigSettings;

/// Create a new JIRA support ticket and return the ticket ID associated with it
pub fn create_support_ticket(
host: &str,
user: &str,
pass: &str,
issue_type: &str,
priority: &str,
project_id: &str,
assignee: &str,
settings: &ConfigSettings,
title: &str,
description: &str,
environment: &str,
) -> Result<String, GojiError> {
let issue_description = CreateIssue {
fields: Fields {
assignee: Assignee { name: assignee.into() },
assignee: Assignee { name: settings.jira_ticket_assignee.clone() },
components: vec![Component { name: "Ceph".into() }],
description: description.into(),
environment: environment.into(),
issuetype: IssueType { id: issue_type.into() },
reporter: Assignee { name: user.to_string() },
priority: Priority { id: priority.into() },
project: Project { key: project_id.into() },
issuetype: IssueType { id: settings.jira_issue_type.clone() },
reporter: Assignee { name: settings.jira_user.clone() },
priority: Priority { id: settings.jira_priority.clone() },
project: Project { key: settings.jira_project_id.clone() },
summary: title.into(),
},
};
let jira: Jira = Jira::new(
host.to_string(),
Credentials::Basic(user.into(), pass.into()),
)?;
let jira: Jira = match settings.proxy {
Some(ref url) => {
let client = reqwest::Client::builder()?
.proxy(reqwest::Proxy::all(url)?)
.build()?;
Jira::from_client(
settings.jira_host.to_string(),
Credentials::Basic(
settings.jira_user.clone().into(),
settings.jira_password.clone().into(),
),
client,
)?
}
None => {
Jira::new(
settings.jira_host.clone().to_string(),
Credentials::Basic(
settings.jira_user.clone().into(),
settings.jira_password.clone().into(),
),
)?
}
};
let issue = Issues::new(&jira);

debug!(
Expand All @@ -48,16 +64,31 @@ pub fn create_support_ticket(
}

/// Check to see if a JIRA support ticket is marked as resolved
pub fn ticket_resolved(
host: &str,
user: &str,
pass: &str,
issue_id: &str,
) -> Result<bool, GojiError> {
let jira: Jira = Jira::new(
host.to_string(),
Credentials::Basic(user.into(), pass.into()),
)?;
pub fn ticket_resolved(settings: &ConfigSettings, issue_id: &str) -> Result<bool, GojiError> {
let jira: Jira = match settings.proxy {
Some(ref url) => {
let client = reqwest::Client::builder()?
.proxy(reqwest::Proxy::all(url)?)
.build()?;
Jira::from_client(
settings.jira_host.to_string(),
Credentials::Basic(
settings.jira_user.clone().into(),
settings.jira_password.clone().into(),
),
client,
)?
}
None => {
Jira::new(
settings.jira_host.clone().to_string(),
Credentials::Basic(
settings.jira_user.clone().into(),
settings.jira_password.clone().into(),
),
)?
}
};
let issue = Issues::new(&jira);
debug!("Fetching issue: {} for resolution information", issue_id);
let results = issue.get(issue_id)?;
Expand Down
31 changes: 12 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ use clap::{Arg, App};
use host_information::Host;
use simplelog::{Config, SimpleLogger};

#[derive(Debug, Deserialize)]
struct ConfigSettings {
#[derive(Clone, Debug, Deserialize)]
pub struct ConfigSettings {
backend: backend::BackendType,
db_location: String,
jira_user: String,
jira_password: String,
jira_host: String,
jira_issue_type: String,
jira_priority: String,
jira_project_id: String,
jira_ticket_assignee: String,
pub jira_user: String,
pub jira_password: String,
pub jira_host: String,
pub jira_issue_type: String,
pub jira_priority: String,
pub jira_project_id: String,
pub jira_ticket_assignee: String,
pub proxy: Option<String>,
}

fn load_config(config_dir: &str) -> Result<ConfigSettings, String> {
Expand Down Expand Up @@ -99,13 +100,7 @@ fn check_for_failed_disks(config_dir: &str, simulate: bool) -> Result<(), String
if !in_progress {
debug!("Creating support ticket");
let _ = create_support_ticket(
&config.jira_host,
&config.jira_user,
&config.jira_password,
&config.jira_issue_type,
&config.jira_priority,
&config.jira_project_id,
&config.jira_ticket_assignee,
&config,
"Dead disk",
&description,
&environment,
Expand Down Expand Up @@ -145,9 +140,7 @@ fn add_repaired_disks(config_dir: &str, simulate: bool) -> Result<(), String> {
info!("Checking for resolved repair tickets");
for ticket in tickets {
let resolved = ticket_resolved(
&config.jira_host,
&config.jira_user,
&config.jira_password,
&config,
&ticket.id.to_string(),
).map_err(|e| e.to_string())?;
if resolved {
Expand Down

0 comments on commit dbc1e17

Please sign in to comment.