Forked from underscorephil/get_tickets_by_date.py
Last active
December 21, 2015 05:29
-
-
Save sudorandom/6257388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def ticket_iter(client, start_date, end_date): | |
mask = """ | |
mask[ | |
id, | |
billableFlag, | |
createDate, | |
groupId, | |
priority, | |
serverAdministrationBillingAmount, | |
serverAdministrationBillingInvoiceId, | |
serverAdministrationFlag, | |
statusId, | |
subjectId, | |
title | |
] | |
""" | |
date_filter = { | |
'tickets': { | |
'createDate': { | |
'operation': 'betweenDate', | |
'options': [ | |
# MM/DD/YYYY HH24:MI:SS | |
{'name': 'startDate', 'value': [start_date]}, | |
# MM/DD/YYYY HH24:MI:SS | |
{'name': 'endDate', 'value': [end_date]} | |
] | |
} | |
} | |
} | |
return client['Account'].getTickets( | |
mask=mask, filter=date_filter, iter=True, chunk=100) | |
def write_csv(tickets, csv_file): | |
import csv | |
with open(csv_file, 'wb') as f: | |
fields = [ | |
'id', 'billableFlag', 'createDate', 'groupId', 'priority', | |
'statusId', 'subjectId', 'title', | |
'serverAdministrationBillingInvoiceId', 'serverAdministrationFlag', | |
'serverAdministrationBillingAmount'] | |
writer = csv.DictWriter(f, fields, extrasaction='ignore') | |
writer.writeheader() | |
total_ticket_count = 0 | |
for ticket in tickets: | |
writer.writerow(ticket) | |
total_ticket_count += 1 | |
print "%s tickets written to %s" % (total_ticket_count, csv_file) | |
def main_cli(): | |
import SoftLayer | |
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter | |
parser = ArgumentParser( | |
description='Get tickets in a specified date range', | |
formatter_class=ArgumentDefaultsHelpFormatter) | |
parser.add_argument( | |
'start_date', help="Ticket search start date - MM/DD/YYYY HH24:MI:SS") | |
parser.add_argument( | |
'end_date', help="Ticket search end date - MM/DD/YYYY HH24:MI:SS") | |
parser.add_argument( | |
'--output-file', default="ticket.csv", | |
help="Output file for ticket csv") | |
parser.add_argument( | |
'--api-user', action='store', help="SoftLayer API User", dest="user", | |
default=None) | |
parser.add_argument( | |
'--api-key', action='store', help="SoftLayer API Key", dest="key", | |
default=None) | |
args = parser.parse_args() | |
client = SoftLayer.Client(username=args.user, api_key=args.key) | |
tickets = ticket_iter(client, args.start_date, args.end_date) | |
write_csv(tickets, args.output_file) | |
if __name__ == "__main__": | |
main_cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment