Skip to content

Commit

Permalink
Merge 569dc71 into e265233
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Weismüller committed Aug 6, 2018
2 parents e265233 + 569dc71 commit a8faa1f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v2.0.2
======

- return proper HTTP status codes, when deleting postgraas instances

v2.0.1
======

Expand Down
2 changes: 2 additions & 0 deletions postgraas_server/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

def create_app(config):
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = get_meta_db_config_path(config)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['BUNDLE_ERRORS'] = True
app_config = get_application_config(config)

for key in app_config:
Expand Down
11 changes: 6 additions & 5 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ def get(self, id):

def delete(self, id):
parser = reqparse.RequestParser()
parser.add_argument('db_pwd', required=True, type=str, help='pass of the db user')
parser.add_argument('db_pwd', required=True, type=str, help='pass of the db user is needed to delete instance.')
args = parser.parse_args()

entity = DBInstance.query.get(id)
if not entity:
return {'status': 'failed', 'msg': 'Postgraas instance {} does not exist'.format(id)}
return {'status': 'failed', 'msg': 'Postgraas instance {} does not exist'.format(id)}, 404

try:
with psycopg2.connect(
Expand All @@ -88,10 +88,11 @@ def delete(self, id):
):
pass
except Exception as ex:
return_code = 401 if 'authentication failed' in str(ex) else 500
return {
'status': 'failed',
'msg': 'Could not connect to postgres instance: {}'.format(str(ex))
}
}, return_code

if not current_app.postgraas_backend.exists(entity):
logger.warning(
Expand All @@ -108,7 +109,7 @@ def delete(self, id):
current_app.postgraas_backend.delete(entity)
except PostgraasApiException as e:
logger.warning("error deleting container {}: {}".format(entity.container_id, str(e)))
return {'status': 'failed', 'msg': str(e)}
return {'status': 'failed', 'msg': str(e)}, 500
db.session.delete(entity)
db.session.commit()
return {'status': 'success', 'msg': 'deleted postgraas instance'}
Expand Down Expand Up @@ -172,7 +173,7 @@ def post(self):
try:
db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)
except PostgraasApiException as e:
return {'msg': str(e)}
return {'msg': str(e)}, 500

if '@' not in args['db_username']:
try:
Expand Down
4 changes: 2 additions & 2 deletions postgraas_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def wait_for_postgres(dbname, user, password, host, port):
try:
psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
return
except psycopg2.OperationalError:
print(i, " ..waiting for db")
except psycopg2.OperationalError as error:
print(i, " ..waiting for db, error: {}".format(error))
time.sleep(1)
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_delete_postgres_instance_api(self):
headers=headers
)
deleted_db = json.loads(delete_result.get_data(as_text=True))

assert delete_result.status_code == 401
assert deleted_db["status"] == 'failed'
assert 'password authentication failed' in deleted_db[
'msg'
Expand Down Expand Up @@ -323,6 +323,7 @@ def test_delete_notfound(self):
}),
headers=headers
)
assert res.status_code == 404
res = json.loads(res.get_data(as_text=True))
assert res['status'] == 'failed'
assert "123456789" in res['msg'], 'unexpected error message'
Expand Down

0 comments on commit a8faa1f

Please sign in to comment.