def set_fields(self, **kwargs): """ Universal integer setter for all fields. Values should be non-negative integers. Throws a RuntimeError if you try to set a non-existent field. :param kwargs: :return: self to support call chaining """ for k, v in kwargs.items(): try: # will toss an exception if field is not defined self.__getattribute__(k) if k == Constants.PROP_CAPACITIES or k == Constants.PROP_ALLOCATED_CAPACITIES: c = Capacities() v = c.from_json(json_string=v) elif k == Constants.PROP_LABELS or k == Constants.PROP_ALLOCATED_LABELS: l = Labels() v = l.from_json(json_string=v) elif k == Constants.PROP_CAPACITY_HINTS: ch = CapacityHints() v = ch.from_json(json_string=v) self.__setattr__(k, v) except AttributeError: raise RuntimeError( f"Unable to set field {k} of reservation, no such field available" ) return self
def testCapacityAssignment(self): c = Capacities(cpu=1, core=2) self.assertEqual(c.cpu, 1) self.assertEqual(c.core, 2) s = '{"core": 32, "disk": 3000, "ram": 384, "unit": 1}' c1 = Capacities.from_json(s) self.assertEqual(c1.unit, 1) self.assertEqual(c1.ram, 384) # because we sort the dict self.assertEqual(c1.to_json(), s) self.assertEqual(c1.core, 32)