
from oslo_utils import timeutils


def transform(metrics, tenant_id, region):
    transformed_metric = {'metric': {},
                          'meta': {'tenantId': tenant_id, 'region': region},
                          'creation_time': timeutils.utcnow_ts()}

    if isinstance(metrics, list):
        transformed_metrics = []
        for metric in metrics:
            transformed_metric['metric'] = metric
            transformed_metrics.append(transformed_metric)
        return transformed_metrics
    else:
        transformed_metric['metric'] = metrics
        return transformed_metric

import copy

from oslo_utils import timeutils


def transform(metrics, tenant_id, region):
    transformed_metric = {'metric': {},
                          'meta': {'tenantId': tenant_id, 'region': region},
                          'creation_time': timeutils.utcnow_ts()}

    if isinstance(metrics, list):
        transformed_metrics = []
        for metric in metrics:
            transformed_metric['metric'] = metric
            transformed_metrics.append(copy.deepcopy(transformed_metric))
        return transformed_metrics
    else:
        transformed_metric['metric'] = metrics
        return transformed_metric

Correct the 'reference' format transform method
Correct the 'reference' format transform method

Currently, the 'reference' format transform method will traversal the
metrics list and reconstruct every item of the list to add tenant_id and
region info, but new transformed metrics list will use the reference
of the local dict variable "transformed_metric", that will lead that all
the items of the transformed metrics list be the same value.

Change-Id: Id7f4e18ca3ae0fa93cdafe0d63b7e90c96ce4b28
Closes-bug: #1439055

+ 
+ import copy
  
  from oslo_utils import timeutils
  
  
  def transform(metrics, tenant_id, region):
      transformed_metric = {'metric': {},
                            'meta': {'tenantId': tenant_id, 'region': region},
                            'creation_time': timeutils.utcnow_ts()}
  
      if isinstance(metrics, list):
          transformed_metrics = []
          for metric in metrics:
              transformed_metric['metric'] = metric
-             transformed_metrics.append(transformed_metric)
+             transformed_metrics.append(copy.deepcopy(transformed_metric))
          return transformed_metrics
      else:
          transformed_metric['metric'] = metrics
          return transformed_metric
+ 
Correct the 'reference' format transform method
## Code Before:

from oslo_utils import timeutils


def transform(metrics, tenant_id, region):
    transformed_metric = {'metric': {},
                          'meta': {'tenantId': tenant_id, 'region': region},
                          'creation_time': timeutils.utcnow_ts()}

    if isinstance(metrics, list):
        transformed_metrics = []
        for metric in metrics:
            transformed_metric['metric'] = metric
            transformed_metrics.append(transformed_metric)
        return transformed_metrics
    else:
        transformed_metric['metric'] = metrics
        return transformed_metric
## Instruction:
Correct the 'reference' format transform method
## Code After:

import copy

from oslo_utils import timeutils


def transform(metrics, tenant_id, region):
    transformed_metric = {'metric': {},
                          'meta': {'tenantId': tenant_id, 'region': region},
                          'creation_time': timeutils.utcnow_ts()}

    if isinstance(metrics, list):
        transformed_metrics = []
        for metric in metrics:
            transformed_metric['metric'] = metric
            transformed_metrics.append(copy.deepcopy(transformed_metric))
        return transformed_metrics
    else:
        transformed_metric['metric'] = metrics
        return transformed_metric
