Skip to content
This repository was archived by the owner on Jun 30, 2018. It is now read-only.

Commit a81c055

Browse files
Marx314fguillot
authored andcommitted
Modify api to support edition of closed entity
1 parent 47ab456 commit a81c055

24 files changed

+363
-118
lines changed

Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ ADD README.md /opt/almanach/src/
77
ADD requirements.txt /opt/almanach/src/
88
ADD LICENSE /opt/almanach/src/
99
ADD almanach/resources/config/almanach.cfg /etc/almanach.cfg
10+
COPY docker-entrypoint.sh /opt/almanach/entrypoint.sh
1011

11-
WORKDIR /opt/almanach
12-
13-
RUN cd src && \
12+
RUN cd /opt/almanach/src && \
1413
pip install -r requirements.txt && \
15-
PBR_VERSION=2.0.dev0 python setup.py install
14+
PBR_VERSION=2.0.dev0 python setup.py install && \
15+
chmod +x /opt/almanach/entrypoint.sh
16+
17+
VOLUME /opt/almanach
1618

1719
USER nobody
20+
21+
ENTRYPOINT ["/opt/almanach/entrypoint.sh"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Running Almanach with Docker
6868
The actual Docker configuration assume that you already have RabbitMQ (mandatory for Openstack) and MongoDB configured for Almanach.
6969

7070
```bash
71-
export RABBITMQ_URL="amqp://openstack:openstack@my-hostname:5672/"
72-
export MONGODB_URL="mongodb://almanach:almanach@my-hostname:27017/almanach"
71+
export RABBITMQ_URL="amqp://guest:guest@messaging:5672/"
72+
export MONGODB_URL="mongodb://almanach:almanach@database:27017/almanach"
7373

7474
docker-compose build
7575
docker-compose up

almanach/adapters/api_route_v1.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414

1515
import logging
1616
import json
17-
import jsonpickle
18-
1917
from datetime import datetime
2018
from functools import wraps
21-
from almanach.common.validation_exception import InvalidAttributeException
19+
20+
import jsonpickle
21+
2222
from flask import Blueprint, Response, request
23+
2324
from werkzeug.wrappers import BaseResponse
2425

26+
from almanach.common.exceptions.almanach_entity_not_found_exception import AlmanachEntityNotFoundException
27+
from almanach.common.exceptions.multiple_entities_matching_query import MultipleEntitiesMatchingQuery
28+
from almanach.common.exceptions.validation_exception import InvalidAttributeException
2529
from almanach import config
26-
from almanach.common.date_format_exception import DateFormatException
30+
from almanach.common.exceptions.date_format_exception import DateFormatException
2731

2832
api = Blueprint("api", __name__)
2933
controller = None
@@ -53,9 +57,18 @@ def decorator(*args, **kwargs):
5357
except InvalidAttributeException as e:
5458
logging.warning(e.get_error_message())
5559
return encode({"error": e.get_error_message()}), 400, {"Content-Type": "application/json"}
60+
except MultipleEntitiesMatchingQuery as e:
61+
logging.warning(e.message)
62+
return encode({"error": "Multiple entities found while updating closed"}), 400, {
63+
"Content-Type": "application/json"}
64+
except AlmanachEntityNotFoundException as e:
65+
logging.warning(e.message)
66+
return encode({"error": "Entity not found for updating closed"}), 400, {"Content-Type": "application/json"}
67+
5668
except Exception as e:
5769
logging.exception(e)
5870
return Response(encode({"error": e.message}), 500, {"Content-Type": "application/json"})
71+
5972
return decorator
6073

6174

@@ -256,7 +269,12 @@ def list_entity(project_id):
256269
def update_instance_entity(instance_id):
257270
data = json.loads(request.data)
258271
logging.info("Updating instance entity with id %s with data %s", instance_id, data)
259-
return controller.update_active_instance_entity(instance_id=instance_id, **data)
272+
if 'start' in request.args:
273+
start, end = get_period()
274+
result = controller.update_inactive_entity(instance_id=instance_id, start=start, end=end, **data)
275+
else:
276+
result = controller.update_active_instance_entity(instance_id=instance_id, **data)
277+
return result
260278

261279

262280
@api.route("/volume_types", methods=["GET"])

almanach/adapters/database_adapter.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
# limitations under the License.
1414

1515
import logging
16-
import pymongo
1716

17+
import pymongo
1818
from pymongo.errors import ConfigurationError
19+
from pymongomodem.utils import decode_output, encode_input
20+
1921
from almanach import config
20-
from almanach.common.almanach_exception import AlmanachException
21-
from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException
22+
from almanach.common.exceptions.almanach_exception import AlmanachException
23+
from almanach.common.exceptions.volume_type_not_found_exception import VolumeTypeNotFoundException
2224
from almanach.core.model import build_entity_from_dict, VolumeType
23-
from pymongomodem.utils import decode_output, encode_input
2425

2526

2627
def database(function):
@@ -55,7 +56,6 @@ def ensureindex(db):
5556

5657

5758
class DatabaseAdapter(object):
58-
5959
def __init__(self):
6060
self.db = None
6161

@@ -90,6 +90,22 @@ def list_entities(self, project_id, start, end, entity_type=None):
9090
entities = self._get_entities_from_db(args)
9191
return [build_entity_from_dict(entity) for entity in entities]
9292

93+
@database
94+
def list_entities_by_id(self, entity_id, start, end):
95+
entities = self.db.entity.find({"entity_id": entity_id,
96+
"start": {"$gte": start},
97+
"$and": [
98+
{"end": {"$ne": None}},
99+
{"end": {"$lte": end}}
100+
]
101+
}, {"_id": 0})
102+
return [build_entity_from_dict(entity) for entity in entities]
103+
104+
@database
105+
def update_closed_entity(self, entity, data):
106+
self.db.entity.update({"entity_id": entity.entity_id, "start": entity.start, "end": entity.end},
107+
{"$set": data})
108+
93109
@database
94110
def insert_entity(self, entity):
95111
self._insert_entity(entity.as_dict())

almanach/common/almanach_entity_not_found_exception.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

almanach/common/exceptions/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Internap.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from almanach.common.exceptions.almanach_exception import AlmanachException
15+
16+
17+
class AlmanachEntityNotFoundException(AlmanachException):
18+
pass
File renamed without changes.

almanach/common/date_format_exception.py renamed to almanach/common/exceptions/date_format_exception.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from almanach.common.exceptions.almanach_exception import AlmanachException
1415

1516

16-
class DateFormatException(Exception):
17+
class DateFormatException(AlmanachException):
1718

1819
def __init__(self, message=None):
1920
if not message:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Internap.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from almanach.common.exceptions.almanach_exception import AlmanachException
15+
16+
17+
class MultipleEntitiesMatchingQuery(AlmanachException):
18+
pass

0 commit comments

Comments
 (0)