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

Commit c535514

Browse files
author
Frederic Guillot
committed
Replace legacy API endpoint
1 parent 2ea3c25 commit c535514

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

almanach/adapters/api_route_v1.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,13 @@ def list_entity(project_id):
246246
return controller.list_entities(project_id, start, end)
247247

248248

249-
# Temporary for AgileV1 migration
250-
@api.route("/instance/<instance_id>/create_date/<create_date>", methods=["PUT"])
249+
@api.route("/entity/instance/<instance_id>", methods=["PUT"])
251250
@authenticated
252251
@to_json
253-
def update_instance_create_date(instance_id, create_date):
254-
logging.info("Update create date for instance %s to %s", instance_id, create_date)
255-
return controller.update_instance_create_date(instance_id, create_date)
252+
def update_instance_entity(instance_id):
253+
data = json.loads(request.data)
254+
logging.info("Updating instance entity with id %s with data %s", instance_id, data)
255+
return controller.update_active_instance_entity(instance_id=instance_id, **data)
256256

257257

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

almanach/core/controller.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import logging
1616
import pytz
1717

18-
from datetime import datetime
1918
from datetime import timedelta
2019
from dateutil import parser as date_parser
2120
from pkg_resources import get_distribution
@@ -103,15 +102,16 @@ def rebuild_instance(self, instance_id, distro, version, os_type, rebuild_date):
103102
instance.last_event = rebuild_date
104103
self.database_adapter.insert_entity(instance)
105104

106-
def update_instance_create_date(self, instance_id, create_date):
107-
logging.info("instance %s create date updated for %s" % (instance_id, create_date))
105+
def update_active_instance_entity(self, instance_id, start_date):
108106
try:
109107
instance = self.database_adapter.get_active_entity(instance_id)
110-
instance.start = datetime.strptime(create_date[0:19], "%Y-%m-%d %H:%M:%S")
108+
instance.start = self._validate_and_parse_date(start_date)
109+
110+
logging.info("Updating entity for instance '{0}' with a new start_date={1}".format(instance_id, start_date))
111111
self.database_adapter.update_active_entity(instance)
112-
return True
112+
return instance
113113
except KeyError as e:
114-
logging.error("Trying to update an instance with id '%s' not in the database yet." % instance_id)
114+
logging.error("Instance '{0}' is not in the database yet.".format(instance_id))
115115
raise e
116116

117117
def create_volume(self, volume_id, project_id, start, volume_type, size, volume_name, attached_to=None):

tests/api_test.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,29 @@ def test_instances_with_authentication(self):
7272
assert_that(result[0], has_key('entity_id'))
7373
assert_that(result[0]['entity_id'], equal_to('123'))
7474

75-
def test_update_create_date_instance(self):
75+
def test_update_instance_entity_with_a_new_start_date(self):
76+
data = {
77+
"start_date": "2014-01-01 00:00:00.0000",
78+
}
79+
7680
self.having_config('api_auth_token', 'some token value')
7781

78-
self.controller.should_receive('update_instance_create_date')\
79-
.with_args("INSTANCE_ID", "2014-01-01 00:00:00.0000")\
80-
.and_return(True)
82+
self.controller.should_receive('update_active_instance_entity')\
83+
.with_args(
84+
instance_id="INSTANCE_ID",
85+
start_date=data["start_date"],
86+
).and_return(a(instance().with_id('INSTANCE_ID')))
8187

8288
code, result = self.api_update(
83-
'/instance/INSTANCE_ID/create_date/2014-01-01 00:00:00.0000',
84-
headers={'X-Auth-Token': 'some token value'}
89+
'/entity/instance/INSTANCE_ID',
90+
headers={'X-Auth-Token': 'some token value'},
91+
data=data,
8592
)
8693

8794
assert_that(code, equal_to(200))
88-
assert_that(result, equal_to(True))
95+
assert_that(result, has_key('entity_id'))
96+
assert_that(result, has_key('start'))
97+
assert_that(result, has_key('end'))
8998

9099
def test_instances_with_wrong_authentication(self):
91100
self.having_config('api_auth_token', 'some token value')

tests/core/test_controller.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ def test_resize_instance(self):
9191

9292
self.controller.resize_instance(fake_instance.entity_id, "newly_flavor", dates_str)
9393

94-
def test_instance_create_date_updated(self):
94+
def test_update_active_instance_entity_with_a_new_start_date(self):
9595
fake_instance1 = a(instance())
9696
fake_instance2 = fake_instance1
97-
fake_instance2.start = "2015-10-05 12:04:00.0000Z"
97+
fake_instance2.start = "2015-10-21T16:25:00.000000Z"
9898

9999
(flexmock(self.database_adapter)
100100
.should_receive("get_active_entity")
@@ -107,7 +107,10 @@ def test_instance_create_date_updated(self):
107107
.with_args(fake_instance2)
108108
.once())
109109

110-
self.controller.update_instance_create_date(fake_instance1.entity_id, "2015-10-05 12:04:00.0000Z")
110+
self.controller.update_active_instance_entity(
111+
instance_id=fake_instance1.entity_id,
112+
start_date="2015-10-21T16:25:00.000000Z",
113+
)
111114

112115
def test_instance_created_but_its_an_old_event(self):
113116
fake_instance = a(instance()

0 commit comments

Comments
 (0)