only save numberCores to OS metadata if it changes
diff --git a/plstackapi/core/models/plcorebase.py b/plstackapi/core/models/plcorebase.py
index 52aa0f7..eac3114 100644
--- a/plstackapi/core/models/plcorebase.py
+++ b/plstackapi/core/models/plcorebase.py
@@ -1,5 +1,6 @@
import os
from django.db import models
+from django.forms.models import model_to_dict
class PlCoreBase(models.Model):
@@ -10,5 +11,36 @@
abstract = True
app_label = "core"
+ def __init__(self, *args, **kwargs):
+ super(PlCoreBase, self).__init__(*args, **kwargs)
+ self.__initial = self._dict
+
+ @property
+ def diff(self):
+ d1 = self.__initial
+ d2 = self._dict
+ diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
+ return dict(diffs)
+
+ @property
+ def has_changed(self):
+ return bool(self.diff)
+
+ @property
+ def changed_fields(self):
+ return self.diff.keys()
+
+ def get_field_diff(self, field_name):
+ return self.diff.get(field_name, None)
+
+ def save(self, *args, **kwargs):
+ super(PlCoreBase, self).save(*args, **kwargs)
+ self.__initial = self._dict
+
+ @property
+ def _dict(self):
+ return model_to_dict(self, fields=[field.name for field in
+ self._meta.fields])
+
diff --git a/plstackapi/openstack/manager.py b/plstackapi/openstack/manager.py
index 171946e..f991676 100644
--- a/plstackapi/openstack/manager.py
+++ b/plstackapi/openstack/manager.py
@@ -215,7 +215,7 @@
sliver.instance_id = instance.id
sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
- if sliver.instance_id:
+ if sliver.instance_id and ("numberCores" in sliver.changed_fields):
self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)})
@require_enabled