-
Notifications
You must be signed in to change notification settings - Fork 1
/
credentials.py
100 lines (86 loc) · 4.27 KB
/
credentials.py
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
####################################################################
######### Copyright 2016-2017 BigSQL ###########
####################################################################
from flask import Blueprint, request, jsonify, session
from flask.views import MethodView
from flask_security import login_required, roles_required, current_user, roles_accepted, auth_required
from flask import g
from responses import ServerErrorResult, Result, InvalidParameterResult
from Components import Components as pgc
credentials = Blueprint('credentials', 'credentials', url_prefix='/api/pgc/credentials')
class CreateCredential(MethodView):
def constructCmd(self, args, method_type=None):
cred_type = args.get('type')
cred_name = args.get('credential_name')
cred_user = args.get('user')
cred_pwd = args.get('password')
ssh_key = args.get('ssh_key')
ssh_sudo_pwd = args.get('ssh_sudo_pwd')
cloud_name = args.get('cloud_name')
credentials = args.get('credentials')
region = args.get('region')
cred_uuid = args.get('cred_uuid')
pgcCmd = "credentials ADD --type \"" + cred_type + "\" --name \"" + cred_name + "\""
if method_type=="UPDATE":
pgcCmd = "credentials UPDATE --type \"" + cred_type + "\" --name \"" + cred_name + "\" --cred_uuid \"" + cred_uuid + "\""
if cred_user:
pgcCmd = pgcCmd + " --user \"" + cred_user + "\""
if cred_pwd:
pgcCmd = pgcCmd + " --pwd \"" + cred_pwd + "\""
if ssh_key:
pgcCmd = pgcCmd + " --key \"" + ssh_key + "\""
if ssh_sudo_pwd:
pgcCmd = pgcCmd + " --sudo_pwd \"" + ssh_sudo_pwd + "\""
if cloud_name:
pgcCmd = pgcCmd + " --cloud \"" + cloud_name + "\""
if credentials:
import json
pgcCmd = pgcCmd + " --credentials \'" + json.dumps(credentials) + "\'"
return pgcCmd
@auth_required('token', 'session')
@roles_accepted('Administrator','User')
def post(self):
json_dict = {}
args= request.json.get('params')
pgcCmd = self.constructCmd(args, method_type = "ADD")
data = pgc.get_cmd_data(pgcCmd)
if len(data) == 0:
return ServerErrorResult().http_response()
if data[0]['state'] != 'info' or data[0]['state'] == 'completed':
return ServerErrorResult(state=data[0]['state'],message=data[0].get('msg')).http_response()
return Result(200,data[0]['state'], data[0]['msg']).http_response()
@auth_required('token', 'session')
@roles_accepted('Administrator','User')
def put(self):
json_dict = {}
args= request.json.get('params')
pgcCmd = self.constructCmd(args, method_type = "UPDATE")
data = pgc.get_cmd_data(pgcCmd)
if len(data) == 0:
return ServerErrorResult().http_response()
if data[0]['state'] != 'info' or data[0]['state'] == 'completed':
return ServerErrorResult(state=data[0]['state'],message=data[0].get('msg')).http_response()
return Result(200,data[0]['state'], data[0]['msg']).http_response()
@auth_required('token', 'session')
@roles_accepted('Administrator','User')
def get(self):
pg_response = pgc.get_cmd_data('credentials --list')
return Result(200,'SUCCESS','SUCCESS',extra_fields={"data":pg_response}).http_response()
credentials.add_url_rule('/', view_func=CreateCredential.as_view('create'))
class DeleteCredentials(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator','User')
def delete(self, uuids):
json_dict = {}
result = {}
for cred_uuid in uuids.split(','):
pgcCmd = "credentials DELETE "
if cred_uuid:
pgcCmd = pgcCmd + " --cred_uuid \"" + cred_uuid + "\""
data = pgc.get_cmd_data(pgcCmd)
if len(data) == 0:
return ServerErrorResult().http_response()
if data[0]['state'] != 'info' or data[0]['state'] == 'completed':
return ServerErrorResult(state=data[0]['state'],message=data[0].get('msg')).http_response()
return Result(200,data[0]['state'], data[0]['msg']).http_response()
credentials.add_url_rule('/<string:uuids>', view_func=DeleteCredentials.as_view('delete'))