From 834765a2d762068fba0e081d1b1b71227c6aa376 Mon Sep 17 00:00:00 2001 From: Lingxian Kong Date: Wed, 6 Jul 2016 10:42:16 +1200 Subject: [PATCH] Dn not update some resource properties for each period Change-Id: I2f27dd11c516d34d8c914191f72034c91fe7d517 Closes-Bug: #1599308 --- distil/collector/base.py | 29 ++++++++++++++--------------- distil/db/sqlalchemy/api.py | 7 ++++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/distil/collector/base.py b/distil/collector/base.py index 7333945..ed6a8ce 100644 --- a/distil/collector/base.py +++ b/distil/collector/base.py @@ -138,7 +138,7 @@ class BaseCollector(object): return os_distro - def _get_resource_info(self, resource_id, resource_type, entry, + def _get_resource_info(self, project_id, resource_id, resource_type, entry, defined_meta): resource_info = {'type': resource_type} @@ -156,13 +156,15 @@ class BaseCollector(object): # Or value isn't present. pass - if resource_type == 'Virtual Machine': - resource_info['os_distro'] = self._get_os_distro(entry) - if resource_type == 'Object Storage Container': - # NOTE(flwang): It's safe to get container name by /, since - # Swift doesn't allow container name with /. - idx = resource_id.index('/') + 1 - resource_info['name'] = resource_id[idx:] + # If the resource is already created, don't update properties below. + if not db_api.resource_get_by_ids(project_id, [resource_id]): + if resource_type == 'Virtual Machine': + resource_info['os_distro'] = self._get_os_distro(entry) + if resource_type == 'Object Storage Container': + # NOTE(flwang): It's safe to get container name by /, since + # Swift doesn't allow container name with /. + idx = resource_id.index('/') + 1 + resource_info['name'] = resource_id[idx:] return resource_info @@ -181,19 +183,16 @@ class BaseCollector(object): if transformed: res_id = mapping.get('res_id_template', '%s') % res_id res_info = self._get_resource_info( + project_id, res_id, mapping['type'], entries[-1], mapping['metadata'] ) - new_res = { - 'tenant_id': project_id, - 'info': res_info - } - res = resources.setdefault(res_id, new_res) - res.update({'info': res_info}) - LOG.debug('resource: %s', res) + res = resources.setdefault(res_id, res_info) + res.update(res_info) + LOG.debug('resource info: %s', res) for service, volume in transformed.items(): entry = { diff --git a/distil/db/sqlalchemy/api.py b/distil/db/sqlalchemy/api.py index cd0a57e..6c82fa3 100644 --- a/distil/db/sqlalchemy/api.py +++ b/distil/db/sqlalchemy/api.py @@ -221,14 +221,15 @@ def usages_add(project_id, resources, usage_entries, last_collect): try: with session.begin(subtransactions=True): - for (id, res) in six.iteritems(resources): + for (id, res_info) in six.iteritems(resources): res_db = _get_resource(session, project_id, id) if res_db: - res_db.info = json.dumps(res['info']) + orig_info = json.loads(res_db.info) + res_db.info = json.dumps(orig_info.update(res_info)) else: resource_ref = Resource( id=id, - info=json.dumps(res['info']), + info=json.dumps(res_info), tenant_id=project_id, created=timestamp )