-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
966 lines (894 loc) · 49 KB
/
Copy pathapp.py
File metadata and controls
966 lines (894 loc) · 49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
'''
############################# wunderDB #############################
wunderDB is a JSON-based micro Document DB, inspired by MongoDB.
version 0.1 Beta
developed by Tanmoy Sen Gupta
* Get started by downloading the app.py and db.json files.
* Create a Python Virtual Environment.
* Install Flask and shortuuid using pip.
* Run 'flask run' and your DB is ready to use!
When the development server starts access the instructions by going to localhost:5000/get-started
'''
import hashlib, json, string, random, secrets, shortuuid
from flask import Flask, request, jsonify, redirect, send_from_directory
from os import path, write
from flask_cors import CORS
from datetime import datetime
import smtplib, ssl, time, hashlib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
app = Flask(__name__)
CORS(app)
server_credentials = json.load(open('./server-config.json'))
port = server_credentials['port']
serverAddress = server_credentials['mail-server']
sender = server_credentials['sender']
password = server_credentials['password']
def write_json(data, filename):
with open(filename, 'w') as f:
json.dump(data, f, indent=4)
def hash_otp(mail, otp, timestamp):
complete_string = mail+"."+otp+"."+timestamp
return hashlib.md5(complete_string.encode()).hexdigest()
def generate_OTP():
return ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits , k = 6))
def send_mail(recipient, mail_template, content):
message = MIMEMultipart("alternative")
message["Subject"] = content["subject"]
message["From"] = sender
message["To"] = recipient
template = open(mail_template).read().replace(content["replacement_token"], content["replacement_string"])
html = MIMEText(template, "html")
message.attach(html)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(serverAddress, port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, recipient, message.as_string())
def generate_mail(recipient, mode, payload="none"):
if mode=="generate-otp-mail":
content = {
"subject" : "Verify you wunderDB account",
"replacement_token": "%otp%",
"replacement_string": payload["otp"]
}
send_mail(recipient, "otp_generation_template.txt", content)
elif mode=="verified-otp-mail":
content = {
"subject" : "Account Verified.",
"replacement_token": "%user%",
"replacement_string": recipient
}
send_mail(recipient, "otp_verified_template.txt", content)
def create_new_request(mail, otp):
with open("authlib.json") as authlib:
authdict = json.load(authlib)
if mail not in authdict.keys():
ts = time.time()
timestamp = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
authdict[mail] = {
"mail" : mail,
"timestampLatestOTP": timestamp ,
"latestOTP": hash_otp(mail, otp, timestamp),
"status" : "unverified"
}
write_json(authdict, "authlib.json")
generate_mail(mail, "generate-otp-mail", {"otp": otp})
return "Created"
else:
generate_mail(mail, "generate-otp-mail", {"otp": authdict[mail]["latestOTP"]})
print("Request Exists")
@app.route("/register/authenticate", methods = ['POST'])
def initialize_new_authentication():
incoming_data = request.get_json(force = True)
with open("users.json", 'r') as users:
user = json.load(users)
users_list = user['users']
if incoming_data['username'] not in users_list.keys():
otp = generate_OTP()
create_new_request(incoming_data['username'], otp)
return jsonify({
"status_code" : '1',
"response" : "OTP Sent."
})
else:
return jsonify({
"status_code" : '1',
"response" : "User Exists."
})
@app.route("/register/verify", methods = ['POST'])
def authenticate_OTP():
incoming_data = request.get_json(force = True)
mail = incoming_data['username']
otp = incoming_data['otp']
with open("authlib.json") as authlib:
temp = json.load(authlib)
if mail in temp.keys():
if temp[mail]["latestOTP"] == hash_otp(mail, otp, temp[mail]["timestampLatestOTP"]):
temp.pop(mail)
write_json(temp, "authlib.json")
generate_mail(mail, "verified-otp-mail")
print("Verified")
return register(incoming_data)
else:
print("Wrong OTP")
return jsonify({
"status_code" : '0',
"response" : "Wrong OTP."
})
else:
return jsonify({
"status_code" : '0',
"response" : "User already exists."
})
# GETTING STARTED
@app.route('/get-started', methods = ['GET'])
def get_started():
with open('templates.json') as templates:
instructions = json.load(templates)
return jsonify({
"_00_message" : "Welcome to wunderDB !",
"_01_about" : "wunderDB is a JSON-based micro Document DB inspired by MongoDB.",
"_02_version" : "0.1 Beta",
"_03_creator" : "Tanmoy Sen Gupta",
"_04_instructions" : instructions
})
# CREATE CLUSTER / REGISTER
# @app.route('/register', methods = ['POST'])
def register(user_data):
with open("users.json", 'r') as users:
user = json.load(users)
users_list = user['users']
if user_data['username'] not in users_list.keys():
salt = user_data['username'].encode('utf-8')
hashed_password = hashlib.sha512(user_data['password'].encode('utf-8') + salt).hexdigest()
tokens = []
cluster_id =shortuuid.uuid()
for i in range(3):
tokens.append(secrets.token_hex(16))
user_init = {
"_cluster_id" : cluster_id,
"name" : user_data['name'],
"username" : user_data['username'],
"password" : hashed_password,
"access_tokens": tokens
}
users_list[user_data['username']] = user_init
write_json(user, 'users.json')
cluster_data = {
"_cluster_id": cluster_id,
"access_tokens": tokens,
"databases":{}
}
cluster_init = {
"clusters" : {}
}
file = "./clusters/"+cluster_id+".json"
with open(file , 'w+' ) as json_file:
write_json(cluster_init, file)
data = json.load(json_file)
temp = data['clusters']
temp[cluster_id] = cluster_data
write_json(data, file)
return jsonify({
"status_code" : '1',
"response" : "Cluster Created",
"cluster_id" : cluster_id ,
"access_tokens" : tokens
})
else:
return jsonify({
"status_code" : '0',
"response" : "User already exists."
})
# LOGIN
@app.route('/login', methods = ['POST'])
def login():
user_data = request.get_json(force = True)
file = "users.json"
with open(file) as user_list:
users = json.load(user_list)
user = users['users']
if user_data["username"] in user.keys():
salt = user_data['username'].encode('utf-8')
hashed_password = hashlib.sha512(user_data['password'].encode('utf-8') + salt).hexdigest()
if user[user_data["username"]]['username'] == user_data['username'] and user[user_data["username"]]['password'] == hashed_password :
return jsonify({
"status_code" : '1',
"response" : "Logged-in",
"name" : user[user_data["username"]]['name'],
"cluster_id" : user[user_data["username"]]['_cluster_id'],
"access_token" : random.choice(user[user_data["username"]]['access_tokens'])
})
else:
return jsonify({
"status_code" : '0',
"response" : "Password is wrong."
})
else:
return jsonify({
"status_code" : '0',
"response" : "User doesn't exist"
})
# CREATE DATABASE
def create_database(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
db_json = payload
database_id =shortuuid.uuid()
db_data={
"_uuid": database_id,
"db_name": db_json['name'],
"collections": {}
}
temp = cluster[cluster_id]['databases']
temp[db_json['name']] = db_data
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Database Created with ID "+database_id
})
# CREATE COLLECTION
def create_collection(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
collection_id =shortuuid.uuid()
schema = cluster_json['schema']
schema.update({"_id" : "ID"})
collection_data={
"_uuid": collection_id,
"collection_name": cluster_json['name'],
"schema": schema,
"data": {}
}
temp = cluster[cluster_id]['databases'][cluster_json['database']]['collections']
temp[cluster_json['name']] = collection_data
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection created with ID "+ collection_id
})
else:
return jsonify({
"status_code" : '0',
"response" : "Database doesn't exist."
})
# DELETE COLLECTION
def delete_collection(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
database = cluster[cluster_id]["databases"][cluster_json['database']]
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
database["collections"].pop(cluster_json["collection"])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Deleted"
})
else:
return jsonify({
"status_code" : '0',
"response" : "Collection doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Database doesn't exist."
})
# CLONE ALL
def clone_all_collections(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['sourceDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['destinationDatabase'] in cluster[cluster_id]["databases"].keys():
destinationDatabase = cluster[cluster_id]["databases"][cluster_json['destinationDatabase']]
sourceDatabase = cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]
for collectionName in cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]["collections"].keys():
tempColName = "cloned-"+collectionName+"@"+cluster_json['sourceDatabase']
destinationDatabase["collections"][tempColName] = {}
destinationDatabase["collections"][tempColName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][tempColName]['collection_name'] = tempColName
destinationDatabase["collections"][tempColName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][tempColName]['data'] = sourceDatabase["collections"][collectionName].get('data')
write_json(clusters, file)
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collections Cloned."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Destination Database doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Source Database doesn't exist."
})
# CLONE COLLECTION
def clone_collection(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['collection'] == "clone-all":
return clone_all_collections(cluster_id, payload)
if cluster_json['sourceDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['destinationDatabase'] in cluster[cluster_id]["databases"].keys():
sourceDatabase = cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]
destinationDatabase = cluster[cluster_id]["databases"][cluster_json['destinationDatabase']]
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]["collections"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json["destinationDatabase"]]["collections"].keys():
if cluster_json['actionIfExists'] == "replace":
destinationDatabase["collections"].pop(cluster_json["collection"])
destinationDatabase["collections"][cluster_json['collection']] = {}
destinationDatabase["collections"][cluster_json['collection']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['collection']]['collection_name'] = sourceDatabase["collections"][cluster_json['collection']].get('collection_name')
destinationDatabase["collections"][cluster_json['collection']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['collection']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Copied!"
})
elif cluster_json['actionIfExists'] == "rename":
destinationDatabase["collections"][cluster_json['newName']] = {}
destinationDatabase["collections"][cluster_json['newName']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['newName']]['collection_name'] = cluster_json['newName']
destinationDatabase["collections"][cluster_json['newName']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['newName']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Copied!"
})
elif cluster_json['actionIfExists'] == "abort":
return jsonify({
"status_code" : '0',
"response" : "Process aborted as collection with same name exists in destination database."
})
else:
destinationDatabase["collections"][cluster_json['collection']] = {}
destinationDatabase["collections"][cluster_json['collection']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['collection']]['collection_name'] = sourceDatabase["collections"][cluster_json['collection']].get('collection_name')
destinationDatabase["collections"][cluster_json['collection']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['collection']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Copied!"
})
else:
return jsonify({
"status_code" : '0',
"response" : "Collection doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Destination Database doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Source Database doesn't exist."
})
# MIGRATE ALL
def migrate_all_collections(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['sourceDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['destinationDatabase'] in cluster[cluster_id]["databases"].keys():
destinationDatabase = cluster[cluster_id]["databases"][cluster_json['destinationDatabase']]
sourceDatabase = cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]
for collectionName in cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]["collections"].keys():
tempColName = "migrated-"+collectionName+"@"+cluster_json['sourceDatabase']
destinationDatabase["collections"][tempColName] = {}
destinationDatabase["collections"][tempColName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][tempColName]['collection_name'] = tempColName
destinationDatabase["collections"][tempColName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][tempColName]['data'] = sourceDatabase["collections"][collectionName].get('data')
write_json(clusters, file)
sourceDatabase["collections"] = {}
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collections migrated."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Destination Database doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Source Database doesn't exist."
})
# MIGRATE COLLECTION
def migrate_collection(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['collection'] == "migrate-all":
return migrate_all_collections(cluster_id, payload)
if cluster_json['sourceDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['destinationDatabase'] in cluster[cluster_id]["databases"].keys():
sourceDatabase = cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]
destinationDatabase = cluster[cluster_id]["databases"][cluster_json['destinationDatabase']]
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['sourceDatabase']]["collections"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json["destinationDatabase"]]["collections"].keys():
if cluster_json['actionIfExists'] == "replace":
destinationDatabase["collections"].pop(cluster_json["collection"])
destinationDatabase["collections"][cluster_json['collection']] = {}
destinationDatabase["collections"][cluster_json['collection']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['collection']]['collection_name'] = sourceDatabase["collections"][cluster_json['collection']].get('collection_name')
destinationDatabase["collections"][cluster_json['collection']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['collection']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
sourceDatabase["collections"].pop(cluster_json["collection"])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Moved!"
})
elif cluster_json['actionIfExists'] == "rename":
destinationDatabase["collections"][cluster_json['newName']] = {}
destinationDatabase["collections"][cluster_json['newName']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['newName']]['collection_name'] = cluster_json['newName']
destinationDatabase["collections"][cluster_json['newName']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['newName']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
sourceDatabase["collections"].pop(cluster_json["collection"])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Moved!"
})
elif cluster_json['actionIfExists'] == "abort":
return jsonify({
"status_code" : '0',
"response" : "Process aborted as collection with same name exists in destination database."
})
else:
destinationDatabase["collections"][cluster_json['collection']] = {}
destinationDatabase["collections"][cluster_json['collection']]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][cluster_json['collection']]['collection_name'] = sourceDatabase["collections"][cluster_json['collection']].get('collection_name')
destinationDatabase["collections"][cluster_json['collection']]['schema'] = sourceDatabase["collections"][cluster_json['collection']].get('schema')
destinationDatabase["collections"][cluster_json['collection']]['data'] = sourceDatabase["collections"][cluster_json['collection']].get('data')
sourceDatabase["collections"].pop(cluster_json["collection"])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Collection Moved!"
})
else:
return jsonify({
"status_code" : '0',
"response" : "Collection doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Destination Database doesn't exist."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Source Database doesn't exist."
})
# DELETE DATABASE
def delete_database(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['targetDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['migrateCollections'] == 'true':
destinationDatabase = cluster[cluster_id]["databases"][cluster_json['destinationDatabase']]
sourceDatabase = cluster[cluster_id]["databases"][cluster_json['targetDatabase']]
if cluster_json['destinationDatabase'] in cluster[cluster_id]["databases"].keys():
if cluster_json['ifCollectionExists'] == "replace":
for collectionName in cluster[cluster_id]["databases"][cluster_json['targetDatabase']]["collections"].keys():
if collectionName in cluster[cluster_id]["databases"][cluster_json["destinationDatabase"]]["collections"].keys():
destinationDatabase["collections"].pop(collectionName)
destinationDatabase["collections"][collectionName] = {}
destinationDatabase["collections"][collectionName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][collectionName]['collection_name'] = sourceDatabase["collections"][collectionName].get('collection_name')
destinationDatabase["collections"][collectionName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][collectionName]['data'] = sourceDatabase["collections"][collectionName].get('data')
else:
destinationDatabase["collections"][collectionName] = {}
destinationDatabase["collections"][collectionName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][collectionName]['collection_name'] = sourceDatabase["collections"][collectionName].get('collection_name')
destinationDatabase["collections"][collectionName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][collectionName]['data'] = sourceDatabase["collections"][collectionName].get('data')
write_json(clusters, file)
cluster[cluster_id]["databases"].pop(cluster_json['targetDatabase'])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Database deleted after migrating collections"
})
elif cluster_json['ifCollectionExists'] == "rename":
for collectionName in cluster[cluster_id]["databases"][cluster_json['targetDatabase']]["collections"].keys():
if collectionName in cluster[cluster_id]["databases"][cluster_json["destinationDatabase"]]["collections"].keys():
tempColName = "cloned-"+collectionName+"@"+cluster_json['targetDatabase']
destinationDatabase["collections"][tempColName] = {}
destinationDatabase["collections"][tempColName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][tempColName]['collection_name'] = tempColName
destinationDatabase["collections"][tempColName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][tempColName]['data'] = sourceDatabase["collections"][collectionName].get('data')
else:
destinationDatabase["collections"][collectionName] = {}
destinationDatabase["collections"][collectionName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][collectionName]['collection_name'] = sourceDatabase["collections"][collectionName].get('collection_name')
destinationDatabase["collections"][collectionName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][collectionName]['data'] = sourceDatabase["collections"][collectionName].get('data')
write_json(clusters, file)
cluster[cluster_id]["databases"].pop(cluster_json['targetDatabase'])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Database deleted after migrating collections"
})
elif cluster_json['ifCollectionExists'] == "skip":
for collectionName in cluster[cluster_id]["databases"][cluster_json['targetDatabase']]["collections"].keys():
if collectionName in cluster[cluster_id]["databases"][cluster_json["destinationDatabase"]]["collections"].keys():
pass
else:
destinationDatabase["collections"][collectionName] = {}
destinationDatabase["collections"][collectionName]['_uuid'] = shortuuid.uuid()
destinationDatabase["collections"][collectionName]['collection_name'] = sourceDatabase["collections"][collectionName].get('collection_name')
destinationDatabase["collections"][collectionName]['schema'] = sourceDatabase["collections"][collectionName].get('schema')
destinationDatabase["collections"][collectionName]['data'] = sourceDatabase["collections"][collectionName].get('data')
write_json(clusters, file)
cluster[cluster_id]["databases"].pop(cluster_json['targetDatabase'])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Database deleted after migrating collections"
})
else:
return jsonify({
"status_code" : '0',
"response" : "Destination Database doesn't exist"
})
elif cluster_json['migrateCollections'] == 'false':
cluster[cluster_id]["databases"].pop(cluster_json['targetDatabase'])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : "Database deleted without migration."
})
else:
return jsonify({
"status_code" : '0',
"response" : "Database doesn't exist."
})
# ADD DATA TO COLLECTION
def add_data(cluster_id , payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
schema = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]['schema']
_id = shortuuid.uuid()
data= cluster_json['data']
data.update({'_id': _id})
if set(data.keys()) == set(schema.keys()):
temp = cluster[cluster_id]['databases'][cluster_json['database']]['collections'][cluster_json['collection']]['data']
temp[_id] = data
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" :"Data Entered"
})
else:
return jsonify({
"status_code" : '0',
"response" : 'Collection Schema and Data Schema does not match'
})
else:
return jsonify({
"status_code" : '0',
"response" : "Collection doesn't exit."
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
# UPDATE DATA
def update_data(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
marker = cluster_json['marker'].split(" : ")
marker_key = marker[0]
marker_value = marker[1]
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
schema = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]['schema']
collection = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]
if marker_key in collection['schema'].keys():
for _id in collection['data'].keys():
if collection['data'][_id][marker_key] == marker_value:
collection['data'][_id].update(cluster_json['data'])
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" :'Data Updated!'
})
return jsonify({
"status_code" : '0',
"response" :'Data Not Found'
})
else:
return jsonify({
"status_code" : '0',
"response" :'Marker Invalid'
})
else:
return jsonify({
"status_code" : '0',
"response" :"Collection doesn't exit."
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
# DELETE DATA
def delete_data(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
marker = cluster_json['marker'].split(" : ")
marker_key = marker[0]
marker_value = marker[1]
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
schema = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]['schema']
collection = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]
if marker_key in collection['schema'].keys():
for _id in collection['data'].keys():
if collection['data'][_id][marker_key] == marker_value:
collection['data'].pop(_id)
write_json(clusters, file)
return jsonify({
"status_code" : '1',
"response" : 'Data Deleted!'
})
break
return jsonify({
"status_code" : '0',
"response" : 'Data Not Found'
})
else:
return jsonify({
"status_code" : '0',
"response" : 'Marker Invalid'
})
else:
return jsonify({
"status_code" : '0',
"response" :"Collection doesn't exit."
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
def view_data(cluster_id, payload):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
marker = cluster_json['marker'].split(" : ")
marker_key = marker[0]
marker_value = marker[1]
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
schema = cluster[cluster_id]["databases"][cluster_json['database']
]["collections"][cluster_json['collection']]['schema']
collection = cluster[cluster_id]["databases"][cluster_json['database']
]["collections"][cluster_json['collection']]
if marker_key in collection['schema'].keys():
for _id in collection['data'].keys():
if collection['data'][_id][marker_key] == marker_value:
targetData = collection['data'][_id]
return jsonify({
"status_code": '1',
"response": targetData
})
return jsonify({
"status_code": '0',
"response": 'Data Not Found'
})
else:
return jsonify({
"status_code": '0',
"response": 'Marker Invalid'
})
else:
return jsonify({
"status_code": '0',
"response": "Collection doesn't exit."
})
else:
return jsonify({
"status_code": '0',
"response": "Database doesn't exist."
})
# GET COMPLETE CLUSTER
def get_cluster(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
return jsonify({ "status_code" : "1" , "cluster" : cluster })
# GET DATABASES
def get_databases(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] == 'all' :
result = []
for i in cluster[cluster_id]["databases"].keys():
result.append({
"status_code" : "1",
"database_name" : cluster[cluster_id]["databases"][i]['db_name'],
"_uuid" : i,
"collections_count": len(cluster[cluster_id]["databases"][i]['collections'])
})
return jsonify({ "response" : result})
else:
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
return jsonify({
"status_code" : "1",
"database_name" : cluster[cluster_id]["databases"][cluster_json['database']]['db_name'],
"_uuid" : cluster_json['database'],
"collections_count": len(cluster[cluster_id]["databases"][cluster_json['database']]['collections'])
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
# GET COLLECTIONS
def get_collections(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] == 'all' :
result = []
for i in cluster[cluster_id]["databases"][cluster_json['database']]['collections'].keys():
result.append({
"collection_name" : cluster[cluster_id]["databases"][cluster_json['database']]['collections'][i]['collection_name'],
"_uuid" : i,
"data_count": len(cluster[cluster_id]["databases"][cluster_json['database']]['collections'][i]['data'])
})
return jsonify({"status_code" : "1", "response" : result})
else:
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]['collections'].keys():
return jsonify({
"collection_name" : cluster[cluster_id]["databases"][cluster_json['database']]['collections'][cluster_json['collection']]['collection_name'],
"_uuid" : cluster_json['collection'],
"data_count": len(cluster[cluster_id]["databases"][cluster_json['database']]['collections'][cluster_json['collection']]['data'])
})
else:
return jsonify({
"status_code" : '0',
"response" :"Collection doesn't exit."
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
# GET DATA
def get_data(cluster_id , payload ):
file = "./clusters/"+cluster_id+".json"
with open(file) as db:
clusters = json.load(db)
cluster = clusters['clusters']
cluster_json = payload
if cluster_json['database'] in cluster[cluster_id]["databases"].keys():
if cluster_json['collection'] in cluster[cluster_id]["databases"][cluster_json['database']]["collections"].keys():
data = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]['data']
schema = cluster[cluster_id]["databases"][cluster_json['database']]["collections"][cluster_json['collection']]['schema']
return jsonify({
"status_code" : "1",
"data" : data,
"schema" : schema
})
else:
return jsonify({
"status_code" : '0',
"response" :"Collection doesn't exit."
})
else:
return jsonify({
"status_code" : '0',
"response" :"Database doesn't exist."
})
# CONNECT - SINGLE ENDPOINT
@app.route('/connect', methods = ['GET' , 'POST'])
def connect():
cluster_id = request.args.get('cluster')
token = request.args.get('token')
file = "./clusters/"+cluster_id+".json"
if path.exists(file):
with open(file) as db:
body = json.load(db)
cluster = body['clusters']
if token in cluster[cluster_id]['access_tokens']:
action_json = request.get_json(force = True)
payload = action_json['payload']
if action_json['action'].lower() == "create-database" :
return create_database(cluster_id, payload)
elif action_json['action'].lower() == "create-collection" :
return create_collection(cluster_id, payload)
elif action_json['action'].lower() == "add-data" :
return add_data(cluster_id, payload)
elif action_json['action'].lower() == "update-data" :
return update_data(cluster_id, payload)
elif action_json['action'].lower() == "delete-data" :
return delete_data(cluster_id, payload)
elif action_json['action'].lower() == "get-cluster" :
return get_cluster(cluster_id, payload)
elif action_json['action'].lower() == "get-collection" :
return get_collections(cluster_id, payload)
elif action_json['action'].lower() == "get-database" :
return get_databases(cluster_id, payload)
elif action_json['action'].lower() == "get-data" :
return get_data(cluster_id, payload)
elif action_json['action'].lower() == "view-data":
return view_data(cluster_id, payload)
elif action_json["action"].lower() == "delete-collection":
return delete_collection(cluster_id, payload)
elif action_json["action"].lower() == "clone-collection":
return clone_collection(cluster_id, payload)
elif action_json["action"].lower() == "migrate-collection":
return migrate_collection(cluster_id, payload)
elif action_json["action"].lower() == "delete-database":
return delete_database(cluster_id, payload)
else:
return jsonify({ "status_code" : "0", "response" : 'Wrong Access token'})
else:
return jsonify({"status_code" : "0", "response" : 'Cluster does not exist.' })
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002)