Skip to content

Commit

Permalink
fix status code on name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Klein committed May 7, 2018
1 parent 5885998 commit 277cc41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
35 changes: 20 additions & 15 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class DBInstance(db.Model):
container_id = db.Column(db.String(100))

def __init__(
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
):
self.postgraas_instance_name = postgraas_instance_name
self.creation_timestamp = datetime.datetime.now()
Expand Down Expand Up @@ -134,10 +134,12 @@ def post(self):

if DBInstance.query.filter_by(postgraas_instance_name=args['postgraas_instance_name']
).first():
return {
'msg':
"postgraas_instance_name already exists {}".format(args['postgraas_instance_name'])
}
abort(
409,
msg="postgraas_instance_name already exists {}".format(
args['postgraas_instance_name']
)
)

db_credentials = {
"db_name": args['db_name'],
Expand All @@ -156,9 +158,12 @@ def post(self):
port=db_credentials['port']
)
if current_app.postgraas_backend.exists(db_entry):
abort(409,
description="database or user already exists {}, {}".format(args['db_name'], args['db_username'])
)
abort(
409,
description="database or user already exists {}, {}".format(
args['db_name'], args['db_username']
)
)

try:
db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)
Expand Down
1 change: 1 addition & 0 deletions tests/test_integration/test_postgras_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def test_create_postgres_instance_name_exists(self):
second = self.app_client.post(
'/api/v2/postgraas_instances', headers=headers, data=json.dumps(db_credentials)
)
assert second.status_code == 409 # Conflict
assert second.get_data(as_text=True) == json.dumps(
{
"msg": "postgraas_instance_name already exists tests_postgraas_my_postgraas_twice"
Expand Down

0 comments on commit 277cc41

Please sign in to comment.