def create_ticket(self, title: str, description: str, urgency: int) -> int: """ Create one ticket with specified title """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError try: with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: result = glpi.add( TICKET, { "name": title, "content": description, "urgency": urgency }, ) if isinstance(result, list) and len(result) == 1 and "id" in result[0]: return result[0]["id"] raise StupidError("Failed to add ticket: {}".format(result)) except glpi_api.GLPIError as err: raise StupidError("Failed to add ticket: {}".format(err)) from err raise glpi_api.GLPIError
def check_cred(self) -> Optional[int]: """ Raise error if no login with provided credentials """ if self.login is None or self.password is None: raise glpi_api.GLPIError with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: result: Dict[str, Any] = glpi.get_full_session() # logging.info("get_full_session = %s", result) return result.get("glpiID", None) raise glpi_api.GLPIError
def get_one_ticket(self, ticket_id: int) -> Dict: """ Return one ticket with ticket_id """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: result = glpi.get_item(TICKET, item_id=ticket_id, get_hateoas=False) if "content" in result: result["content"] = html2markdown.convert( html2text.html2text(str(result["content"]))) return result raise glpi_api.GLPIError
def refuse_ticket_solition(self, ticket_id: int, text: str) -> List[Dict]: """ Add followup and reopen ticket """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: return glpi.add( "itilfollowup", { "itemtype": TICKET, "items_id": ticket_id, "content": text, "is_private": 0, "add_reopen": 1, }, ) raise glpi_api.GLPIError
def get_last_solution(self, ticket_id: int) -> str: """ Return last proposed solution for ticket with ticket_id """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: solution: Dict = glpi.get_sub_items(TICKET, ticket_id, SOLUTION, get_hateoas=False) logging.info("solution = %s", solution) if len(solution) < 1: return "" return html2markdown.convert( html2text.html2text(str(solution[-1].get("content")))) raise glpi_api.GLPIError
def approve_ticket_solution(self, ticket_id: int) -> None: """ Close ticket """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: # result = glpi.update( # "ticket", {"id": ticket_id, "status": CLOSED_TICKED_STATUS} # ) result = glpi.add( "itilfollowup", { "itemtype": TICKET, "items_id": ticket_id, "content": "", "is_private": 0, "add_close": 1, }, ) logging.info("result = %s", result)
def get_all_my_tickets(self, open_only: bool, full_info: bool) -> Dict[int, Dict]: """ Return all tickets """ if self.login is None or self.password is None or not self.is_logged_in: raise glpi_api.GLPIError # TODO error catch criteria = [{ "field": REQUEST_USER_ID, "searchtype": "equals", "value": str(self.glpi_id), }] forcedisplay = [ TICKET_NAME, TICKET_STATUS, TICKET_LAST_UPDATE, ASSIGNED_TO ] with glpi_api.GLPI( url=self.URL, auth=(self.login, self.password), apptoken=config.GLPI_APP_API_KEY, ) as glpi: glpi_tickets: List[Dict[str, Union[str, int, list, None]]] = glpi.search( TICKET, criteria=criteria, forcedisplay=forcedisplay, sort=TICKET_ID) logging.info("self.login = %s", self.login) logging.info("criteria = %s", criteria) logging.info("forcedisplay = %s", forcedisplay) logging.info("my_ticket_id = %s", glpi_tickets) if open_only: glpi_tickets = list( filter(lambda x: x[TICKET_STATUS] != 6, glpi_tickets)) result: Dict[int, Dict] = {} if full_info: for elem in glpi_tickets: ticket_id: int = int(elem[TICKET_ID]) result[ticket_id] = glpi.get_item(TICKET, item_id=ticket_id, get_hateoas=False) # assigned_user_id: List[int] = [] # if isinstance(elem[ASSIGNED_TO], int): # assigned_user_id = [elem[ASSIGNED_TO]] # elif isinstance(elem[ASSIGNED_TO], list): # assigned_user_id = elem[ASSIGNED_TO] # elif elem[ASSIGNED_TO] is not None: # raise StupidError( # "elem[ASSIGNED_TO] = " # + str(elem[ASSIGNED_TO]) # + " type = " # + str(type(elem[ASSIGNED_TO])) # ) # assigned_user: List[str] = [] # for user_id in assigned_user_id: # assigned_user.append( # glpi.get_item(USER, item_id=int(user_id), get_hateoas=False) # ) # result[ticket_id]["assigned_to"] = assigned_user else: for elem in glpi_tickets: result[int(elem[TICKET_ID])] = { "status": int(elem[TICKET_STATUS]), "name": elem[TICKET_NAME], "date_mod": elem[TICKET_LAST_UPDATE], } return result raise glpi_api.GLPIError