Skip to content

Instantly share code, notes, and snippets.

@melvinpetix
Last active December 15, 2022 13:51
Show Gist options
  • Save melvinpetix/8ba0a15e2338a2a236d93e6224018506 to your computer and use it in GitHub Desktop.
Save melvinpetix/8ba0a15e2338a2a236d93e6224018506 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nnit.Automation.Framework.NET.Common;
using System.IO;
using UAAutomationNNIT.Model;
using System.Net;
using System.Security;
using System.Configuration;
using System.Collections;
using System.Reflection;
namespace UAAutomationNNIT.TicketAnaylsis
{
class ClientCherwell : ClientObject
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
const string PENDING = "Pending";
const string INPROGRESS = "In progress";
const string ars_hostname = "https://nnititsm-intgr.nnitcorp.com/CherwellAPI";
public bool ClearFlag(string incidentID)
{
bool hasError = true;
string serviceName = "cherwell_incident";
string operation = "MODIFY";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid", AFType.From(incidentID)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
{"actionneeded", AFType.From("$NULL$")}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info(incidentID + " flag cleared.");
}
else
{
logger.Info(incidentID + " flag cleared failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
return hasError;
}
public Dictionary<string, AFType> GetInprogressTicket(string ticketNo)
{
List<Dictionary<string, AFType>> ticketList = new List<Dictionary<string, AFType>>();
string status = "In progress";
string serviceName = "cherwell_incident";
string operation = "LIST";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid",AFType.From (ticketNo)},
{"status",AFType.From (status)},
{"worklog_count",new AFType(20)},
{"include_attachments",new AFType("true")},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
Dictionary<string, AFType> incidents = reqs.Outputs["incidents"].AsDictionary();
return incidents;
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public Dictionary<string, AFType> GetInprogressOrPendingTicket(string ticketNo)
{
var incident = this.GetPendingTicket(ticketNo);
if (incident.Count < 1)
{
incident = this.GetInprogressTicket(ticketNo);
if (incident.Count < 1)
{
return null;
}
}
return incident;
}
public bool IsInprogressOrPending(string ticketNo)
{
List<Dictionary<string, AFType>> ticketList = new List<Dictionary<string, AFType>>();
string serviceName = "cherwell_incident";
string operation = "LIST";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid",AFType.From (ticketNo)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
Dictionary<string, AFType> incidents = reqs.Outputs["incidents"].AsDictionary();
if (incidents.Count < 1)
{
return false;
}
var incStatus = (incidents.Values.First().AsDictionary())["status"].ToString().ToUpper();
if (incStatus == PENDING.ToUpper() || incStatus == INPROGRESS.ToUpper())
{
return true;
}
return false;
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public Dictionary<string, AFType> GetTicketByNumber(string ticketNo, bool include_work_log = false)
{
List<Dictionary<string, AFType>> ticketList = new List<Dictionary<string, AFType>>();
string serviceName = "cherwell_incident";
string operation = "LIST";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid",AFType.From (ticketNo)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)}
};
if (include_work_log)
{
inputs.Add("worklog_count", new AFType(20));
inputs.Add("include_attachments", new AFType("true"));
}
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
Dictionary<string, AFType> incidents = reqs.Outputs["incidents"].AsDictionary();
return incidents;
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public Dictionary<string, AFType> GetPendingTicket(string ticketNo)
{
List<Dictionary<string, AFType>> ticketList = new List<Dictionary<string, AFType>>();
string status = "Pending";
string serviceName = "cherwell_incident";
string operation = "LIST";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid",AFType.From (ticketNo)},
{"status",AFType.From (status)},
{"worklog_count",new AFType(20)},
{"include_attachments",new AFType("true")},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
Dictionary<string, AFType> incidents = reqs.Outputs["incidents"].AsDictionary();
return incidents;
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public Dictionary<string, AFType> GetTicketsBySummary(string summary, bool include_work_log = true)
{
string assignee = TicketAnaylsis.Properties.Settings.Default.TicketAssignee;
string status = "In progress";
string assigned_group = TicketAnaylsis.Properties.Settings.Default.TicketAssignedGroup;
List<Dictionary<string, AFType>> ticketList = new List<Dictionary<string, AFType>>();
string serviceName = "cherwell_incident";
string operation = "LIST";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
{"ownedbyteam",new AFType(assigned_group)},
{"summary",new AFType(summary)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
//{ "slm_measurement", new AFType("true")}
};
if (include_work_log)
{
inputs.Add("worklog_count",new AFType(20));
inputs.Add("include_attachments", new AFType("true"));
}
if (!string.IsNullOrEmpty(assignee))
{
inputs.Add("ownedby", new AFType(assignee));
}
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
Dictionary<string, AFType> incidents = reqs.Outputs["incidents"].AsDictionary();
return incidents;
//foreach (var item in incidents.Values)
//{
// var incident = item.AsDictionary();
// //string notes = incident.ContainsKey("notes") ? incident["notes"].AsString() : " ";
// //string ticketID = incident["incidentid"].AsString();
// //string summaryFromTicket = incident["summary"].AsString();
// //string corporate_id = incident["corporate_id"].AsString();
// //string requester = incident["requester"].AsString();
// ticketList.Add(incident);
//}
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public bool CloseTickets(Dictionary<string, string> tickets, string resolution)
{
string status = "Resolved";
string status_reason = "No Further Action Required";
bool hasError = true;
string serviceName = "cherwell_incident";
string operation = "MODIFY";
foreach (var ticket in tickets)
{
string incidentID = ticket.Key;
string assignee = ticket.Value;
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
{"status_reason",AFType.From (status_reason)},
{"resolution",AFType.From (resolution)},
{"incidentid", AFType.From(incidentID)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
{"actionneeded", AFType.From("$NULL$")}
};
if (!string.IsNullOrEmpty(assignee))
{
inputs.Add("ownedby", AFType.From(assignee));
}
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info(incidentID + " closed.");
}
else
{
logger.Info(incidentID + " closed failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
return hasError;
}
public bool CloseTicket(string incidentID, string assignee, string resolution)
{
string status = "Resolved";
string status_reason = "No Further Action Required";
bool hasError = false;
string serviceName = "cherwell_incident";
string operation = "MODIFY";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
{"status_reason",AFType.From (status_reason)},
{"resolution",AFType.From (resolution)},
{"incidentid", AFType.From(incidentID)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
{"actionneeded", AFType.From("$NULL$")}
};
if (!string.IsNullOrEmpty(assignee))
{
inputs.Add("ownedby", AFType.From(assignee));
}
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info(incidentID + " closed.");
}
else
{
logger.Info(incidentID + " closed failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
return hasError;
}
//sysAccount CreateNewTicket to IS
public bool CreateOneTicketToIS(string taskRequester, int taskType, string accountName)
{
string status_reason = "No Further Action Required";
string assignedGroup = Properties.Settings.Default.SysATicketAssignedGroup;
string status = "Assigned";
string summary = "Creation SA <" + accountName + "> <" + taskRequester + ">";
string notes = "";
bool hasError = true;
string serviceName = "cherwell_incident";
string requester = "CustomerInitials";
string operation = "CREATE";
string urgency = "LOW";
string reported_source = "Web"
string impact = "LOW";
string service_type = "Service";
string company = "NovoNordisk";
string production_cat_tier1 = "Business Application";
string production_cat_tier2 = "novoAccess";
string service_ci = "SO-novoAccess";
string ci_id = "s";
//notes
if (taskType == (int)TaskEnum.SystemAccountTaskType.Create)
{
notes += "Service Account Creation\n" + "________________________\n";
}
else if (taskType == (int)TaskEnum.SystemAccountTaskType.Delete)
{
summary = "Deletion SA <" + accountName + "> <" + taskRequester + ">";
notes += "Service Account Deletion\n" + "________________________\n";
}
notes += "Account: " + accountName + "\n\n";
notes += "Owner: " + taskRequester;
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
{"cause",AFType.From (status_reason)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
{"OwnedByTeam",new AFType(assignedGroup)},
{"impact",new AFType(impact)},
{"summary",new AFType(summary)},
{"description",new AFType(notes)},
{"urgency",new AFType(urgency)},
{"Source",new AFType(reported_source)},
{"companyname",new AFType(company)},
{"incidenttype",new AFType(service_type)},
{"requester",new AFType(requester)},
{"Category",new AFType(production_cat_tier1)},
{"Subcategory",new AFType(production_cat_tier2)},
{"service_ci",new AFType(service_ci)},
{"clientid",new AFType(ci_id)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string incidentID = reqs.Outputs["incidentid"].ToString();
logger.Info(timestamp + incidentID + " created and assigned to " + assignedGroup);
}
else
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string incidentID = reqs.Outputs["incidentid"].ToString();
logger.Info(timestamp + incidentID + " created failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
return hasError;
}
public void SetTargetDateAndPendingTicekt(string incidentID, DateTime targetDate)
{
string status = "Pending";
string status_reason = "Client Action Required";
string serviceName = "cherwell_incident";
string operation = "MODIFY";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"target_date",AFType.From (targetDate)},
{"status",AFType.From (status)},
{"status_reason", AFType.From (status_reason) },
{"incidentid", AFType.From(incidentID)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info(incidentID + " is set target date");
}
else
{
logger.Info(incidentID + " closed failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public bool ReassignTicket(string incidentID, string assigned_group, string description, string attachmentPath = null)
{
string status = "Assigned";
bool hasError = true;
string serviceName = "cherwell_incident";
string operation = "MODIFY";
string assignee = "$NULL$";
List<AFType> worklogs = new List<AFType>();
var worklog = new Dictionary<string, AFType>
{
{"description",AFType.From (description)}
};
if (!string.IsNullOrEmpty(attachmentPath))
{
string attahmentBase64 = Convert.ToBase64String(File.ReadAllBytes(attachmentPath));
string fileName = new FileInfo(attachmentPath).Name;
worklog.Add("attachments", AFType.From(fileName + ";" + attahmentBase64));
}
worklogs.Add(new AFType(worklog));
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
//{"resolution",AFType.From (resolution)},
{"incidentid", AFType.From(incidentID)},
{"ownedbyteam",new AFType(assigned_group)},
{"ownedby",new AFType(assignee)},
{"worklogs", new AFType(worklogs)},
{"username", AFType.From(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password", AFType.From(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info(incidentID + " reasssigned to " + assigned_group);
}
else
{
logger.Info(incidentID + " reasssigned to " + assigned_group + "failed.");
throw new Exception(reqs.Outputs["error_message"].ToString());
}
return hasError;
}
public string GetTxtAttachment(Dictionary<string, AFType> ticket)
{
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if(!ticket.Keys.Contains("worklogs"))
{
ticket = GetTicketByNumber(ticket["incidentid"].ToString(), true).FirstOrDefault().Value.AsDictionary();
}
if (ticket.Keys.Contains("worklogs"))
{
Dictionary<string, AFType> worklogs = ticket["worklogs"].AsDictionary();
//Dictionary<string, AFType> theFirstWorklog= worklogs.Values.First().AsDictionary();
foreach (KeyValuePair<string, AFType> worklog in worklogs)
{
Dictionary<string, AFType> log = worklog.Value.AsDictionary();
if (log.Keys.Contains("attachments"))
{
var attach = log["attachments"].AsString().Replace("[", "").Replace("]", "");
string attachmentName = attach.Split(';')[0];
if (attachmentName.EndsWith(".txt"))
{
string filePath = Path.Combine(assemblyFolder, "attchements", attachmentName);
string attachment = attach.Split(';')[1];
File.WriteAllBytes(filePath, Convert.FromBase64String(attachment));
return filePath;
}
}
}
}
return "";
}
public string CreateOneTicket(string summary, string notes)
{
string assignedGroup = Properties.Settings.Default.SysATicketAssignedGroup;
string status = "Assigned";
string serviceName = "cherwell_incident";
string requester = "nn000";
string operation = "CREATE";
string urgency = "4-Low";
string reported_source = "Web";
string impact = "4-Minor/Localized";
string service_type = "User Service Request";
string company = "NovoNordisk";
string production_cat_tier1 = "Business Application";
string production_cat_tier2 = "novoAccess";
string service_ci = "SO-novoAccess";
string ci_id = "REGJUD42AR2VLAQCLWZ3AHOIH81B42";
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"status",AFType.From (status)},
{"username",new AFType(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password",new AFType(_password)},
{"ownedbyteam",new AFType(assignedGroup)},
{"impact",new AFType(impact)},
{"summary",new AFType(summary)},
{"description",new AFType(notes)},
{"urgency",new AFType(urgency)},
{"source",new AFType(reported_source)},
{"companyname",new AFType(company)},
{"service_type",new AFType(service_type)},
{"requester",new AFType(requester)},
{"category",new AFType(production_cat_tier1)},
{"subcategory",new AFType(production_cat_tier2)},
{"service_ci",new AFType(service_ci)},
{"clientid",new AFType(ci_id)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string incidentID = reqs.Outputs["incidentid"].ToString();
logger.Info(timestamp + incidentID + " created and assigned to " + assignedGroup);
return incidentID;
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
public void AddingWorklog(string incidentID, string summary, string description, string attachmentPath = null)
{
string serviceName = "cherwell_incident";
string operation = "MODIFY";
List<AFType> worklogs = new List<AFType>();
var worklog = new Dictionary<string, AFType>
{
{"summary",AFType.From(summary)},
{"description",AFType.From(description)}
};
if (!string.IsNullOrEmpty(attachmentPath))
{
string attahmentBase64 = Convert.ToBase64String(File.ReadAllBytes(attachmentPath));
string fileName = new FileInfo(attachmentPath).Name;
worklog.Add("attachments", AFType.From(fileName + ";" + attahmentBase64));
}
worklogs.Add(new AFType(worklog));
var inputs = new Dictionary<string, AFType>
{
{"url",AFType.From (ars_hostname)},
{"incidentid", AFType.From(incidentID)},
{"worklogs", new AFType(worklogs)},
{"username", AFType.From(_userName.Substring(0,_userName.IndexOf("@")).ToLower())},
{"password", AFType.From(_password)}
};
AFResponse reqs = CallAService(serviceName, operation, inputs);
if (reqs.Result == AFResult.SUCCESS)
{
logger.Info("Adding worklog for incident:" + incidentID);
}
else
{
throw new Exception(reqs.Outputs["error_message"].ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment