Last active
February 9, 2018 15:30
-
-
Save jdewinne/052ea1af27e11ffb87231e1d067ccf56 to your computer and use it in GitHub Desktop.
XLR Data inserter using python
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
import json | |
import urllib, urllib2, base64 | |
def get_config_id(title, username="admin", password="admin"): | |
params = {"configurationType": "configuration.HttpConnection", "title":title} | |
request = urllib2.Request("http://localhost:5516/api/v1/config/byTypeAndTitle?%s" % urllib.urlencode(params)) | |
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
return json.load(result)[0]["id"] | |
def replace_config_title_with_id(adict, configs): | |
for key, value in adict.iteritems(): | |
if key in configs.split(","): | |
adict[key] = get_config_id(value) | |
elif type(value) is dict and value: | |
replace_config_title_with_id(value, configs) | |
elif type(value) is list: | |
for ad in value: | |
if type(ad) is dict and ad: | |
replace_config_title_with_id(ad,configs) | |
def import_template(template, username="admin", password="admin", folder_id = None): | |
print "Inserting template with title [%s]" % template["title"] | |
url = "http://localhost:5516/api/v1/templates/import" | |
if folder_id: | |
url += "?folderId=%s" % folder_id | |
request = urllib2.Request(url, "[%s]" % json.dumps(template), {'Content-Type': 'application/json'}) | |
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
def import_release(release, username="admin", password="admin"): | |
print "Inserting release with title [%s]" % release["title"] | |
request = urllib2.Request("http://localhost:5516/fixtures/release", json.dumps(release), {'Content-Type': 'application/json'}) | |
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
def getopts(argv): | |
opts = {} # Empty dictionary to store key-value pairs. | |
while argv: # While there are arguments left to parse... | |
if argv[0][0] == '-': # Found a "-name value" pair. | |
opts[argv[0]] = argv[1] # Add key and value to the dictionary. | |
argv = argv[1:] # Reduce the argument list by copying it starting from index 1. | |
return opts | |
if __name__ == '__main__': | |
from sys import argv | |
myargs = getopts(argv) | |
username = "admin" | |
password = "admin" | |
folder = None | |
configs = None | |
if '-u' in myargs: | |
username = myargs['-u'] | |
if '-p' in myargs: | |
password = myargs['-p'] | |
if '-l' in myargs: | |
folder = myargs['-l'] | |
if '-c' in myargs: | |
configs = myargs['-c'] | |
if '-f' in myargs: # Example usage. | |
with open(myargs['-f']) as json_data: | |
d = json.load(json_data) | |
for item in d: | |
if configs: | |
replace_config_title_with_id(item, configs) | |
if item["status"] == "TEMPLATE": | |
import_template(item, username, password, folder) | |
else: | |
import_release(item, username, password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment