Skip to content

Commit

Permalink
Merge pull request #30 from pacman82/fix-status-code-on-name-conflict
Browse files Browse the repository at this point in the history
fix status code on name conflict
  • Loading branch information
matthias-bach-by authored May 8, 2018
2 parents a627811 + 6169388 commit f56813a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ v2.0.1 (UNRLEASED)
==================

- prevent creation of databases with an empty password as those cannot be removed.
- return http status code 409 instead of 200 in case of a name conflict during creation of a new
instance.

v2.0.0
======
Expand Down
35 changes: 20 additions & 15 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,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 @@ -138,10 +138,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 @@ -160,9 +162,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 @@ -342,6 +342,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 f56813a

Please sign in to comment.