Replace 'aid's with 'uid's

Blueprint: send-anon-usage

Change-Id: I5da1747dac391d3e4fc818b86d7e1d2b1ea19df1
This commit is contained in:
Aleksey Kasatkin 2014-10-22 11:31:56 +03:00
parent d7d2bd7105
commit 8a52420d3b
9 changed files with 46 additions and 43 deletions

View File

@ -33,21 +33,21 @@ def upgrade():
op.create_table(
'action_logs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('node_aid', sa.String(), nullable=False),
sa.Column('master_node_uid', sa.String(), nullable=False),
sa.Column('external_id', sa.Integer(), nullable=False),
sa.Column('body', sa.Text(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('node_aid', 'external_id')
sa.UniqueConstraint('master_node_uid', 'external_id')
)
op.create_table(
'installation_structs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('aid', sa.String(), nullable=False),
sa.Column('master_node_uid', sa.String(), nullable=False),
sa.Column('struct', sa.Text(), nullable=False),
sa.Column('creation_date', sa.DateTime(), nullable=True),
sa.Column('modification_date', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('aid')
sa.UniqueConstraint('master_node_uid')
)
### end Alembic commands ###

View File

@ -18,11 +18,11 @@ from collector.api.app import db
class ActionLog(db.Model):
__tablename__ = 'action_logs'
__table_args__ = (
db.UniqueConstraint('node_aid', 'external_id'),
db.UniqueConstraint('master_node_uid', 'external_id'),
)
id = db.Column(db.Integer, primary_key=True)
node_aid = db.Column(db.String, nullable=False)
master_node_uid = db.Column(db.String, nullable=False)
external_id = db.Column(db.Integer, nullable=False)
body = db.Column(db.Text, nullable=False)
@ -31,7 +31,7 @@ class InstallationStruct(db.Model):
__tablename__ = 'installation_structs'
id = db.Column(db.Integer, primary_key=True)
aid = db.Column(db.String, nullable=False, unique=True)
master_node_uid = db.Column(db.String, nullable=False, unique=True)
struct = db.Column(db.Text, nullable=False)
creation_date = db.Column(db.DateTime)
modification_date = db.Column(db.DateTime)

View File

@ -61,7 +61,7 @@ def _save_action_logs(action_logs):
db.session.execute(ActionLog.__table__.insert(), action_logs)
for action_log in action_logs:
result.append({
'node_aid': action_log['node_aid'],
'master_node_uid': action_log['master_node_uid'],
'external_id': action_log['external_id'],
'status': consts.ACTION_LOG_STATUSES.added
})
@ -75,7 +75,7 @@ def _extract_objects_info(existed_objects):
result = []
for obj in existed_objects:
result.append({
'node_aid': obj.node_aid,
'master_node_uid': obj.master_node_uid,
'external_id': obj.external_id,
'status': consts.ACTION_LOG_STATUSES.existed
})
@ -86,7 +86,7 @@ def _handle_chunk_processing_error(chunk):
result = []
for action_log in chunk:
result.append({
'node_aid': action_log['node_aid'],
'master_node_uid': action_log['master_node_uid'],
'external_id': action_log['external_id'],
'status': consts.ACTION_LOG_STATUSES.failed
})
@ -95,17 +95,18 @@ def _handle_chunk_processing_error(chunk):
def _separate_action_logs(action_logs):
existed_objs = []
action_logs_idx = util.build_index(action_logs, 'node_aid', 'external_id')
action_logs_idx = \
util.build_index(action_logs, 'master_node_uid', 'external_id')
clauses = []
for aid, ext_id in six.iterkeys(action_logs_idx):
for master_node_uid, ext_id in six.iterkeys(action_logs_idx):
clauses.append(and_(
ActionLog.node_aid == aid,
ActionLog.master_node_uid == master_node_uid,
ActionLog.external_id == ext_id
))
found_objs = db.session.query(ActionLog).filter(or_(*clauses)).all()
for existed in found_objs:
existed_objs.append(existed)
idx = (existed.node_aid, existed.external_id)
idx = (existed.master_node_uid, existed.external_id)
action_logs_idx.pop(idx)
return existed_objs, list(six.itervalues(action_logs_idx))

View File

@ -39,12 +39,12 @@ def post():
"Handling installation_struct post request: {}".format(request.json)
)
struct = request.json['installation_struct']
aid = struct['aid']
master_node_uid = struct['master_node_uid']
obj = db.session.query(InstallationStruct).filter(
InstallationStruct.aid == aid).first()
InstallationStruct.master_node_uid == master_node_uid).first()
if obj is None:
app.logger.debug("Saving new struct")
obj = InstallationStruct(aid=aid)
obj = InstallationStruct(master_node_uid=master_node_uid)
obj.creation_date = datetime.utcnow()
else:
app.logger.debug("Updating struct {}".format(obj.id))

View File

@ -11,7 +11,7 @@
"items": {
"type": "object",
"properties": {
"node_aid": {"type": "string"},
"master_node_uid": {"type": "string"},
"external_id": {"type": "integer"},
"body": {
"type": "object",
@ -30,7 +30,7 @@
}
}
},
"required": ["node_aid", "external_id", "body"]
"required": ["master_node_uid", "external_id", "body"]
}
}
},
@ -50,11 +50,11 @@
"items": {
"type": "object",
"properties": {
"node_aid": {"type": "string"},
"master_node_uid": {"type": "string"},
"external_id": {"type": "integer"},
"status": {"enum": ["added", "existed", "failed"]}
},
"required": ["node_aid", "external_id", "status"],
"required": ["master_node_uid", "external_id", "status"],
"additionalProperties": false
}
}

View File

@ -9,7 +9,7 @@
"installation_struct": {
"type": "object",
"properties": {
"aid": {"type": "string"},
"master_node_uid": {"type": "string"},
"clusters_num": {"type": "integer"},
"allocated_nodes_num": {"type": "integer"},
"unallocated_nodes_num": {"type": "integer"},
@ -68,7 +68,7 @@
}
}
},
"required": ["aid", "clusters_num", "allocated_nodes_num",
"required": ["master_node_uid", "clusters_num", "allocated_nodes_num",
"unallocated_nodes_num", "clusters"]
}
},

View File

@ -23,8 +23,10 @@ from collector.api.db.model import ActionLog
class TestModelActionLog(DbTest):
def test_unique_constraints(self):
db.session.add(ActionLog(node_aid='aid', external_id=1))
db.session.add(ActionLog(node_aid='aid', external_id=1))
db.session.add(
ActionLog(master_node_uid='master_node_uid', external_id=1))
db.session.add(
ActionLog(master_node_uid='master_node_uid', external_id=1))
self.assertRaises(IntegrityError, db.session.flush)
def test_non_nullable_fields(self):
@ -32,6 +34,6 @@ class TestModelActionLog(DbTest):
self.assertRaises(IntegrityError, db.session.flush)
db.session.rollback()
db.session.add(ActionLog(node_aid='aid'))
db.session.add(ActionLog(master_node_uid='master_node_uid'))
self.assertRaises(IntegrityError, db.session.flush)
db.session.rollback()

View File

@ -38,10 +38,10 @@ class TestActionLogs(DbTest):
self.check_response_ok(resp, code=201)
def test_post(self):
node_aid = 'x'
master_node_uid = 'x'
expected_logs = [
{
'node_aid': node_aid,
'master_node_uid': master_node_uid,
'external_id': i,
'body': {
"id": i,
@ -71,7 +71,7 @@ class TestActionLogs(DbTest):
)
actual_logs = db.session.query(ActionLog).filter(
ActionLog.node_aid == node_aid).all()
ActionLog.master_node_uid == master_node_uid).all()
self.assertEquals(len(expected_logs), len(actual_logs))
self.assertListEqual(
sorted([l['external_id'] for l in expected_logs]),
@ -79,10 +79,10 @@ class TestActionLogs(DbTest):
)
def test_post_duplication(self):
node_aid = 'x'
master_node_uid = 'x'
action_logs = [
{
'node_aid': node_aid,
'master_node_uid': master_node_uid,
'external_id': i,
'body': {
"id": i,
@ -105,7 +105,7 @@ class TestActionLogs(DbTest):
)
self.check_response_ok(resp, code=201)
count_actual = db.session.query(ActionLog).filter(
ActionLog.node_aid == node_aid).count()
ActionLog.master_node_uid == master_node_uid).count()
resp_data = json.loads(resp.data)
for d in resp_data['action_logs']:
self.assertEquals(
@ -117,7 +117,7 @@ class TestActionLogs(DbTest):
# Checking duplications is not added
new_action_logs = [
{
'node_aid': node_aid,
'master_node_uid': master_node_uid,
'external_id': i,
'body': {
"id": i,
@ -140,7 +140,7 @@ class TestActionLogs(DbTest):
)
self.check_response_ok(resp, code=201)
count_actual = db.session.query(ActionLog).filter(
ActionLog.node_aid == node_aid).count()
ActionLog.master_node_uid == master_node_uid).count()
self.assertEquals(
len(new_action_logs),
count_actual
@ -158,7 +158,7 @@ class TestActionLogs(DbTest):
self.assertEquals(len(new_action_logs) - len(action_logs), len(added))
def test_validation_error(self):
expected_logs = [{'node_aid': 'x', 'external_id': None}]
expected_logs = [{'master_node_uid': 'x', 'external_id': None}]
resp = self.post(
'/api/v1/action_logs/',
{'action_logs': expected_logs}

View File

@ -34,7 +34,7 @@ class TestInstallationStruct(DbTest):
def test_validation_error(self):
wrong_data_sets = [
{'installation_struct': {'aid': 'x'}},
{'installation_struct': {'master_node_uid': 'x'}},
None,
{}
]
@ -46,9 +46,9 @@ class TestInstallationStruct(DbTest):
self.check_response_error(resp, code=400)
def test_post(self):
aid = 'x'
master_node_uid = 'x'
struct = {
'aid': aid,
'master_node_uid': master_node_uid,
'fuel_release': {
'release': 'r',
'ostf_sha': 'o_sha',
@ -98,15 +98,15 @@ class TestInstallationStruct(DbTest):
)
self.check_response_ok(resp, code=201)
obj = db.session.query(InstallationStruct).filter(
InstallationStruct.aid == aid).one()
InstallationStruct.master_node_uid == master_node_uid).one()
self.assertEquals(json.dumps(struct), obj.struct)
self.assertIsNotNone(obj.creation_date)
self.assertIsNone(obj.modification_date)
def test_post_update(self):
aid = 'xx'
master_node_uid = 'xx'
struct = {
'aid': aid,
'master_node_uid': master_node_uid,
'allocated_nodes_num': 0,
'unallocated_nodes_num': 0,
'clusters_num': 0,
@ -118,7 +118,7 @@ class TestInstallationStruct(DbTest):
)
self.check_response_ok(resp, code=201)
obj_new = db.session.query(InstallationStruct).filter(
InstallationStruct.aid == aid).one()
InstallationStruct.master_node_uid == master_node_uid).one()
self.assertEquals(json.dumps(struct), obj_new.struct)
self.assertIsNotNone(obj_new.creation_date)
self.assertIsNone(obj_new.modification_date)
@ -130,7 +130,7 @@ class TestInstallationStruct(DbTest):
)
self.check_response_ok(resp, code=201)
obj_upd = db.session.query(InstallationStruct).filter(
InstallationStruct.aid == aid).one()
InstallationStruct.master_node_uid == master_node_uid).one()
self.assertEquals(json.dumps(struct), obj_upd.struct)
self.assertIsNotNone(obj_upd.creation_date)
self.assertIsNotNone(obj_upd.modification_date)