1 from django.http import HttpResponse
7 from portal.models import MeasurementsDB
9 from manifold.core.query import Query, AnalyzedQuery
10 from manifoldapi.manifoldapi import execute_query
12 from influxdb import InfluxDBClient
14 def createDatabase(request, slicename):
17 Config = config(request)
19 server = Config.get('influxdb', 'server')
20 #port = Config.get('influxdb', 'port', 8086)
22 user = Config.get('influxdb', 'user')
23 password = Config.get('influxdb', 'password')
26 dbuser = request.user.username
27 dbpassword = generatePassword()
29 query = Query().get('user').filter_by('user_email', '==', dbuser).select('slices')
30 slices = execute_query(request, query)
32 if not slicename in slices:
33 result['status'] = 'fail'
34 result['message'] = 'no permissions'
35 return HttpResponse(json.dumps(result), content_type="application/json")
37 client = InfluxDBClient(server, port, user, password, dbname)
40 client.create_database(dbname)
41 except Exception as e:
46 client.add_database_user(dbuser, dbpassword)
47 except Exception as e:
50 # Make user a database admin
51 client.set_database_admin(dbuser)
54 # Insert an entry in the Influxdb table
61 dbpassword = dbpassword
66 result['status'] = 'ok'
68 return HttpResponse(json.dumps(result), content_type="application/json")
70 def infoDatabase(request, slicename):
71 Config = config(request)
73 res = MeasurementsDB.objects.get(dbname=slicename, dbuser=request.user.username)
75 'server' : res.server,
77 'dbname' : res.dbname,
78 'dbuser' : res.dbuser,
79 'dbpassword' : res.dbpassword
81 return HttpResponse(json.dumps(result), content_type="application/json")
84 Config = ConfigParser.ConfigParser()
85 Config.read(os.getcwd() + "/myslice/measurements.ini")
87 if not request.user.is_authenticated :
88 return HttpResponse(json.dumps({'error' : 'not authenticated'}), content_type="application/json")
90 #if Config.has_section('influxdb') :
91 if not Config.has_option('influxdb', 'server') :
92 return HttpResponse(json.dumps({'error' : 'server not specified'}), content_type="application/json")
94 if not Config.has_option('influxdb', 'user') :
95 return HttpResponse(json.dumps({'error' : 'user not specified'}), content_type="application/json")
97 if not Config.has_option('influxdb', 'password') :
98 return HttpResponse(json.dumps({'error' : 'server not specified'}), content_type="application/json")
102 def generatePassword():
106 for group in (string.ascii_letters, string.punctuation, string.digits):
107 password += random.sample(group, 3)
109 password += random.sample(
110 string.ascii_letters + string.punctuation + string.digits,
111 password_len - len(password))
113 random.shuffle(password)
114 password = ''.join(password)