Scheduler: adding/removing resources enforce warnings and recount number of unconfigu...
[myslice.git] / influxdb / client.py
1 from django.http import HttpResponse
2 from rest import error
3 import os,json
4 import ConfigParser
5 import string, random
6
7 from portal.models import MeasurementsDB
8
9 from manifold.core.query             import Query, AnalyzedQuery
10 from manifoldapi.manifoldapi         import execute_query
11
12 from influxdb import InfluxDBClient
13
14 def createDatabase(request, slicename):
15     result = {}
16     
17     Config = config(request)
18     
19     server = Config.get('influxdb', 'server')
20     #port = Config.get('influxdb', 'port', 8086)
21     port = 8086
22     user = Config.get('influxdb', 'user')
23     password = Config.get('influxdb', 'password')
24     
25     dbname = slicename
26     dbuser = request.user.username
27     dbpassword = generatePassword()
28     
29     query = Query().get('user').filter_by('user_email', '==', dbuser).select('slices')
30     slices = execute_query(request, query)
31     
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")
36
37     client = InfluxDBClient(server, port, user, password, dbname)
38     
39     try :
40         client.create_database(dbname)
41     except Exception as e:
42         print e
43     
44     # Add database user
45     try :
46         client.add_database_user(dbuser, dbpassword)
47     except Exception as e:
48         print e
49     
50     # Make user a database admin
51     client.set_database_admin(dbuser)
52     
53     
54     # Insert an entry in the Influxdb table
55     i = MeasurementsDB(
56         backend         = 'influxdb',
57         server          = server,
58         port            = port,
59         dbname          = dbname,
60         dbuser          = dbuser,
61         dbpassword      = dbpassword
62     )
63     i.save()
64     
65     
66     result['status'] = 'ok'
67
68     return HttpResponse(json.dumps(result), content_type="application/json")
69
70 def infoDatabase(request, slicename):
71     Config = config(request)
72     
73     res = MeasurementsDB.objects.get(dbname=slicename, dbuser=request.user.username)
74     result = {
75               'server' : res.server,
76               'port' : res.port,
77               'dbname' : res.dbname,
78               'dbuser' : res.dbuser,
79               'dbpassword' : res.dbpassword
80               }
81     return HttpResponse(json.dumps(result), content_type="application/json")
82
83 def config(request):
84     Config = ConfigParser.ConfigParser()
85     Config.read(os.getcwd() + "/myslice/measurements.ini")
86
87     if not request.user.is_authenticated :
88         return HttpResponse(json.dumps({'error' : 'not authenticated'}), content_type="application/json")
89     
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")
93     
94     if not Config.has_option('influxdb', 'user') :
95         return HttpResponse(json.dumps({'error' : 'user not specified'}), content_type="application/json")
96     
97     if not Config.has_option('influxdb', 'password') :
98         return HttpResponse(json.dumps({'error' : 'server not specified'}), content_type="application/json")
99     
100     return Config
101
102 def generatePassword():
103     password_len = 16
104     password = []
105     
106     for group in (string.ascii_letters, string.punctuation, string.digits):
107         password += random.sample(group, 3)
108     
109     password += random.sample(
110                      string.ascii_letters + string.punctuation + string.digits,
111                      password_len - len(password))
112     
113     random.shuffle(password)
114     password = ''.join(password)
115     
116     
117     return password
118     
119