def test_dummy1_static_to_dict(self): dyn1 = DynamicField("firstname", "Jane") self.assertIsInstance(dyn1, DynamicField) self.assertDictEqual(dyn1.to_dct(), { 'Name': 'firstname', 'Value': 'Jane' })
def test_dct_search(self): dyn1 = DynamicField(name="SomeName", search_patterns=["foo", "bar"], search_operator="Equals") self.assertEqual(dyn1.to_dct_search(), {"DynamicField_SomeName": { "Equals": ["foo", "bar"] }})
def test_dct_search_date(self): dyn1 = DynamicField(name="SomeDate", search_patterns=[datetime(2011, 1, 1)], search_operator="GreaterThan") self.assertEqual(dyn1.to_dct_search(), { "DynamicField_SomeDate": { "GreaterThan": ["2011-01-01 00:00:00"] } })
def run(self, params={}): client = self.connection.client search_params = {} df = [] try: client.session_create() except Exception as e: raise Exception( "Unable to connect to OTRS webservice! Please check your connection information and \ that you have properly configured OTRS webservice. Information on configuring the webservice can be found \ in the Connection help") # Time search params # TODO: Add support for searching tickets with date #if params.get("date"): # if params.get("date").startswith("0001-01-01"): # del(params["date"]) # else: # ticket_date = maya.MayaDT.rfc3339(params.get("date")).datetime() # #search_params[""] = ticket_date # set queue if params.get("queue"): search_params["Queues"] = params.get("queue") # set customer id if params.get("cust_id"): search_params["CustomerID"] = params.get("cust_id") # set external search params external_params = params.get("external_params") if external_params: for param in external_params: for k, v in param.items(): search_params[k] = v # set dynamic fields dynamic_fields = params.get('dynamic_fields') if dynamic_fields: for field in dynamic_fields: df_payload = dict() df_payload["name"] = field.get("name") df_payload["value"] = field.get("value") df_payload["search_operator"] = field.get( "operation", "Equals") df_payload["search_patterns"] = field.get("patterns") df.append(DynamicField(**df_payload)) search_params["dynamic_fields"] = df tickets = client.ticket_search(**search_params) return {'ticket_ids': tickets}
def buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER): client = Client(OTRS_SERVER, OTRS_USER, OTRS_PASS) client.session_create() new_ticket = Ticket.create_basic(Title="Server " + HOST + " Down", Queue="" + QUEUE + "", State=u"" + STATE + "", Priority=u"" + PRIORITY + "", CustomerUser="" + CUSTOMERUSER + "") first_article = Article({ "Subject": "Server " + HOST + " Down", "Body": "Host: " + HOST + " Trigger ID: " + TRIGGERID + " Severity: " + SEVERITY + "Descricao: " + DESCRIPTION }) dynamic_field = DynamicField("TriggerID", value="" + TRIGGERID + "") client.ticket_create(new_ticket, first_article, None, dynamic_fields=[dynamic_field]) return "Ticket Build"
def update_ticket_command(): ticket_id = demisto.args().get('ticket_id') title = demisto.args().get('title') queue = demisto.args().get('queue') state = demisto.args().get('state') priority = demisto.args().get('priority') article_subject = demisto.args().get('article_subject') article_body = demisto.args().get('article_body') ticket_type = demisto.args().get('type') dynamic_fields = demisto.args().get('dynamic_fields') attachment = demisto.args().get('attachment') if all(v is None for v in [ title, queue, state, priority, article_subject, article_body, ticket_type, dynamic_fields, attachment ]): return_error('No fields to update were given') if (article_subject and article_body is None) or (article_subject is None and article_body): return_error( 'Both article subject and body are required in order to add article' ) elif article_subject and article_body: article_obj = {'Subject': article_subject, 'Body': article_body} article = Article(article_obj) else: article = None if state: state = translate_state(state) if priority: priority = translate_priority(priority) df = [] if dynamic_fields: dynamic_fields_list = argToList(dynamic_fields) for field in dynamic_fields_list: splitted_field = field.split('=') current_field, current_value = splitted_field[0], splitted_field[1] df.append(DynamicField(current_field, current_value)) attachments = [] if attachment: attachments_list = argToList(attachment) attachments = demisto_entry_to_otrs_attachment(attachments_list) ticket = update_ticket(ticket_id, title, queue, state, priority, article, ticket_type, df, attachments) context = { 'ID': ticket['TicketID'], } if priority: context['Priority'] = priority if queue: context['Queue'] = queue if state: context['State'] = state if title: context['Title'] = title if article: context['Article'] = article.to_dct() if ticket_type: context['Type'] = ticket_type ec = {'OTRS.Ticket(val.ID===obj.ID)': context} output = 'Updated ticket {} successfully'.format(ticket['TicketID']) demisto.results({ 'Type': entryTypes['note'], 'Contents': context, 'ContentsFormat': formats['json'], 'ReadableContentsFormat': formats['markdown'], 'HumanReadable': output, 'EntryContext': ec })
def create_ticket_command(): title = demisto.args().get('title') queue = demisto.args().get('queue') state = translate_state(demisto.args().get('state')) priority = translate_priority(demisto.args().get('priority')) customer_user = demisto.args().get('customer_user') article_subject = demisto.args().get('article_subject') article_body = demisto.args().get('article_body') ticket_type = demisto.args().get('type') dynamic_fields = demisto.args().get('dynamic_fields') attachment = demisto.args().get('attachment') df = [] df_output = [] if dynamic_fields: dynamic_fields_list = argToList(dynamic_fields) for field in dynamic_fields_list: splitted_field = field.split('=') current_field, current_value = splitted_field[0], splitted_field[1] df.append(DynamicField(current_field, current_value)) df_output.append({current_field: current_value}) attachments = [] if attachment: attachments_list = argToList(attachment) attachments = demisto_entry_to_otrs_attachment(attachments_list) new_ticket = Ticket.create_basic(Title=title, Queue=queue, State=state, Priority=priority, CustomerUser=customer_user, Type=ticket_type) article = Article({'Subject': article_subject, 'Body': article_body}) ticket = create_ticket(new_ticket, article, df, attachments) context = { 'ID': str(ticket['TicketID']), 'Number': ticket['TicketNumber'], 'CustomerUser': customer_user, 'Priority': priority, 'Queue': queue, 'State': state, 'Title': title, 'Article': { 'Subject': article_subject, 'Body': article_body }, 'Type': ticket_type, 'DynamicField': df_output } ec = {'OTRS.Ticket(val.ID===obj.ID)': context} output = 'Created ticket {} successfully'.format(ticket['TicketID']) demisto.results({ 'Type': entryTypes['note'], 'Contents': context, 'ContentsFormat': formats['json'], 'ReadableContentsFormat': formats['markdown'], 'HumanReadable': output, 'EntryContext': ec })
def run(self, params={}): client = self.connection.client try: client.session_create() except Exception as e: raise Exception( "Unable to connect to OTRS webservice! Please check your connection information and " "that you have properly configured OTRS webservice. Information on configuring the " "webservice can be found in the Connection help") # dynamic fields new_dynamic_fields = params.get("dynamic_fields") del (params["dynamic_fields"]) params = komand.helper.clean(params) # cleaning up params new_article = params.get("article") del (params["article"]) # helper.clean will not remove empty list. If these are left empty and not removed the plugin will fail if not new_article["ExcludeMuteNotificationToUserID"]: del (new_article["ExcludeMuteNotificationToUserID"]) if not new_article["ExcludeNotificationToUserID"]: del (new_article["ExcludeNotificationToUserID"]) if not new_article["ForceNotificationToUserID"]: del (new_article["ForceNotificationToUserID"]) # Set attachments new_attachments = params.get("attachments") del (params["attachments"]) # basics basics = {} for basic_param in [ "Title", "Queue", "Type", "State", "Priority", "CustomerUser" ]: if basic_param in params: basics[basic_param] = params.get(basic_param) del (params[basic_param]) else: raise Exception( "Parameter {} was not found. This is required to create a ticket" .format(basic_param)) # left over params added to other_basics other_basics = {} for k, v in params.items(): if v: other_basics[k] = v # build attachments attachments = [] for attachment in new_attachments: a = Attachment.create_basic(Content=attachment["content"], ContentType=mimetypes.guess_type( attachment["filename"])[0], Filename=attachment["filename"]) attachments.append(a) # build dynamic fields dynamic_fields = list( map(lambda f: DynamicField(**f), new_dynamic_fields)) # create ticket from basics and all other params new_ticket = Ticket.create_basic(**basics) new_article = Article(new_article) new_attachments = attachments new_dynamic_fields = dynamic_fields # Create Ticket ticket_results = client.ticket_create( ticket=new_ticket, article=new_article, attachments=new_attachments, dynamic_fields=new_dynamic_fields, **other_basics) ticket_id = ticket_results.get("TicketID") # check if pendingtime is left unmodified if params.get("PendingTime").startswith("0001-01-01"): del (params["PendingTime"]) else: PyOTRSUtils.add_pendingtime(self, client, params.get("PendingTime"), ticket_id) # update SLA if params.get("SLA"): PyOTRSUtils.add_sla(self, client=client, ticket_id=ticket_id, sla=params.get("SLA"), sla_service=params.get("Service")) ticket_details = client.ticket_get_by_id( ticket_id, articles=False, attachments=False, dynamic_fields=False, html_body_as_attachment=False) ticket_details = ticket_details.to_dct() if ticket_details["Ticket"]["SLAID"] == "": raise Exception( "SLA and Service was not set. Please check the SLA and Service parameters as they maybe set incorrectly!\n" f"SLA Parameter: {params.get('SLA')}\n" f"SLA Service parameter: {params.get('Service')}\n" f"Please note ticket ID:{ticket_id} was created but the SLA and Sevice parameters were not updated." ) # format TicketNumber as its required as an INT in other actions and returned here as a string return { "ticket_id": int(ticket_results["TicketID"]), "ticket_number": int(ticket_results["TicketNumber"]) }
def run(self, params={}): client = self.connection.client attachments = [] try: client.session_create() except Exception as e: raise Exception("Unable to connect to OTRS Web Service! Please check your connection information and \ that you have properly configured OTRS web service. Information on configuring the webservice can be found \ in the Connection help") # dynamic fields new_dynamic_fields = params.get("DynamicFields") del(params["DynamicFields"]) params = komand.helper.clean(params) # cleaning up params new_article = params.get("Article", None) if params.get("Article"): del(params["Article"]) # helper.clean will not remove empty list. If these are left empty and not removed the plugin will fail if "ExcludeMuteNotificationToUserID" in new_article: if not new_article["ExcludeMuteNotificationToUserID"]: del(new_article["ExcludeMuteNotificationToUserID"]) if "ExcludeNotificationToUserID" in new_article: if not new_article["ExcludeNotificationToUserID"]: del(new_article["ExcludeNotificationToUserID"]) if "ForceNotificationToUserID" in new_article: if not new_article["ForceNotificationToUserID"]: del(new_article["ForceNotificationToUserID"]) new_article = Article(new_article) # Set attachments if params.get("Attachements"): new_attachments = params.get("Attachments") del(params["Attachments"]) for attachment in new_attachments: filename = attachment['filename'] content = attachment['content'] mimetype = mimetypes.guess_type(filename) attachments.append(Attachment.create_basic(Content=content, ContentType= mimetype[0], Filename=filename)) ticket_id = params.get('TicketID') del(params["TicketID"]) dynamic_fields = [] for fields in new_dynamic_fields: dynamic_fields.append(DynamicField(**fields)) # Pending time if params.get("PendingTime").startswith("0001-01-01"): del(params['PendingTime']) else: # Format date PyOTRSUtils.add_pendingtime(self, client, params.get("PendingTime"), ticket_id) del(params['PendingTime']) other_options = params if new_article: ticket_update_results = client.ticket_update( ticket_id=ticket_id, article=new_article, attachments=attachments, dynamic_fields=dynamic_fields, **other_options ) else: ticket_update_results = client.ticket_update( ticket_id=ticket_id, dynamic_fields=dynamic_fields, **other_options ) return {'ticket_id': int(ticket_update_results["TicketID"]), 'ticket_number': int(ticket_update_results["TicketNumber"])}
def test_dummy1(self): dyn1 = DynamicField._dummy1() self.assertIsInstance(dyn1, DynamicField) self.assertEqual(dyn1.__repr__(), '<DynamicField: firstname: Jane>')
def test_dummy2_static(self): dyn2 = DynamicField.from_dct({'Name': 'lastname', 'Value': 'Doe'}) self.assertIsInstance(dyn2, DynamicField) self.assertEqual(dyn2.__repr__(), '<DynamicField: lastname: Doe>')
def test_dummy1_static(self): dyn1 = DynamicField(name="firstname", value="Jane") self.assertIsInstance(dyn1, DynamicField) self.assertEqual(dyn1.__repr__(), '<DynamicField: firstname: Jane>')
def test_init_list_pattern(self): dyn1 = DynamicField(name="SomeName", search_patterns=["foo", "bar"], search_operator="Equals") self.assertIsInstance(dyn1.search_patterns, list)
def test_init_operator_custom(self): dyn1 = DynamicField(name="somedate", search_operator="SmallerThanEquals") self.assertIsInstance(dyn1, DynamicField) self.assertEqual(dyn1.search_operator, "SmallerThanEquals")
def test_init_operator_default(self): dyn1 = DynamicField(name="firstname") self.assertIsInstance(dyn1, DynamicField) self.assertEqual(dyn1.search_operator, "Equals")
def test_dummy2(self): dyn2 = DynamicField._dummy2() self.assertIsInstance(dyn2, DynamicField) self.assertEqual(dyn2.__repr__(), '<DynamicField: lastname: Doe>')