-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·230 lines (204 loc) · 5.33 KB
/
app.py
File metadata and controls
executable file
·230 lines (204 loc) · 5.33 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
#!/usr/bin/env python
# encoding: utf-8
import os
from utils import *
from flask import Flask
from flask_cors import CORS
from flask import send_from_directory, jsonify
from flask_restful import Api, Resource, request
from flasgger import Swagger
'''
# Kill all processes running on port 5000
lsof -ti :5000 | xargs kill -9
'''
app = Flask(__name__)
CORS(app) # Enable CORS
api = Api(app)
app.config['SWAGGER'] = {
'swagger_ui': True,
'uiversion': 3,
'openapi': '3.0.2',
'specs_route': '/'
}
swagger = Swagger(app)
DAT_LIST = 'data/list-stacks.json'
DAT_DESC = 'data/describe-stacks.json'
class Stacks():
def __init__(self, path: str):
self.path = f'{path}' if path else '.'
self.list_file = f'{path}/list-stacks.json'
self.desc_file = f'{path}/describe-stacks.json'
def list(self):
warning('File:', self.list_file)
res = get_json(self.list_file)
return res if isinstance(res, list) else []
def describe(self, name: str):
warning('Name:', name)
try:
rs = get_json(self.desc_file)
warning('RSet:', rs)
for r in rs.get('Stacks', []):
if r['StackName'] == name:
return r
except Exception as e:
error(e)
warning('File:', self.desc_file)
return {}
def is_exist(self, name: str):
rs = self.list()
for r in rs:
if r['StackName'] == name:
return True
return False
def put(self, data):
rs = get_json(self.desc_file)
rs.append(data)
name = data['StackName']
if not self.is_exist(name):
rs.append(data)
return put_json(self.desc_file, rs)
return False
def to_json(self):
return {
'StackName': self.StackName,
'StackStatus': self.StackStatus
}
OBJ = Stacks('data')
@app.route('/stacks/list', methods=['GET'])
def list_records():
"""
Get a list of stack records
---
responses:
200:
description: List of stack records
400:
description: Error getting stack records
"""
res = OBJ.list()
return jsonify(res) if res else jsonify({'error': 'data not found'}), 400
@app.route('/stacks/<StackName>', methods=['GET'])
def query_records(StackName):
"""
Get a stack record
---
parameters:
- name: StackName
in: path
type: string
required: true
responses:
200:
description: Stack record returned
400:
description: Stack not found
"""
res = OBJ.describe(StackName)
return jsonify(res) if res else jsonify({'error': 'data not found'}), 400
@app.route('/stacks', methods=['PUT'])
def create_record():
"""
Create a new stack record
---
consumes:
application/json
parameters:
- in: body
name: body
schema:
type: object
required:
- StackName
- StackStatus
properties:
StackName:
type: string
StackStatus:
type: string
responses:
200:
description: Stack record created
400:
description: Stack already exists
"""
warning(request)
# cextract request attributes
warning(request.form)
record = request.get_json(force=True)
warning(record)
record = request.get_json()
name = record.get('StackName')
if OBJ.is_exist(name):
return jsonify({'error': 'data already exists'}), 400
OBJ.put(record)
return jsonify(record)
@app.route('/stacks', methods=['POST'])
def put_record():
"""
Update a stack record
---
consumes:
- application/json
parameters:
- in: body
name: body
schema:
type: object
required:
- StackName
- StackStatus
properties:
StackName:
type: string
StackStatus:
type: string
responses:
200:
description: Stack record updated
400:
description: Error updating stack record
"""
warning(request)
record = request.get_json(force=True)
warning(record)
record = request.get_json()
new_records = []
records = get_json('data/list-stacks.json')
for r in records:
if r['StackName'] == record['StackName']:
r = record
new_records.append(r)
put_json('data/list-stacks.json', new_records)
return jsonify(record)
@app.route('/stacks/{StackName}', methods=['DELETE'])
def delete_record(StackName):
"""
Delete a stack record
---
parameters:
- in: body
name: body
schema:
type: object
required:
- StackName
properties:
StackName:
type: string
responses:
200:
description: Stack record deleted
400:
description: Error deleting stack record
"""
record = request.get_json()
new_records = []
records = get_json('data/list-stacks.json')
for r in records:
if r['StackName'] == record['StackName']:
continue
new_records.append(r)
put_json('data/list-stacks.json', new_records)
return jsonify(record)
if __name__ == '__main__':
app.run(debug=True)