def record_rhic_usage(self, rhic, inst_index, usage_datetime, splice_server): """ Record one record of a RHIC usage. """ pu = ProductUsage( consumer=rhic.uuid, splice_server=splice_server, instance_identifier=rhic.instance_identifiers[inst_index], allowed_product_info=rhic.engineering_ids, facts=rhic.instance_facts[inst_index], date=usage_datetime) pu.save()
def create_product_usage_data(self, addr, num, save=True): retval = [] for index in range(0, num): pu = ProductUsage() pu.splice_server = addr pu.consumer = "consumer_uuid" pu.date = datetime.now(tzutc()) - timedelta(hours=num-index) pu.instance_identifier = "instance_identifier" pu.allowed_product_info = ["1"] pu.unallowed_product_info = ["0"] pu.facts = {"tbd":"values"} if save: pu.save() retval.append(pu) return retval
def record_usage(self, identity, consumer_identifier, facts, allowed_products, unallowed_products): """ @param identity consumer's identity @type identity: str @param consumer_identifier means of uniquely identifying different instances with same consumer identity an example could be a mac address @type consumer_identifier: str @param facts system facts @type facts: {} @param allowed_products: list of product ids that are installed and entitled for usage by consumer @type allowed_products: [str] @param unallowed_products: list of product ids that are installed but _not_ entitled for usage by consumer @type unallowed_products: [str] """ try: sanitized_facts = utils.sanitize_dict_for_mongo(facts) _LOG.info("Record usage for '%s' with " "allowed_products '%s', " "unallowed_products '%s' " "on instance with identifier '%s' and facts <%s>" %\ (identity, allowed_products, unallowed_products, consumer_identifier, sanitized_facts)) consumer_uuid_str = str(identity.uuid) prod_usage = ProductUsage( consumer=consumer_uuid_str, splice_server=self.get_this_server().uuid, instance_identifier=consumer_identifier, allowed_product_info=allowed_products, unallowed_product_info=unallowed_products, facts=sanitized_facts, date=datetime.now()) prod_usage.save() except Exception, e: _LOG.exception(e)
def test_tracker_for_product_usage_prevents_duplicate_server_entries(self): self.assertEqual(len(ProductUsage.objects()), 0) pu = ProductUsage() pu.consumer = "consumer_uuid" pu.splice_server = "splice server uuid" pu.date = datetime.now(tzutc()) pu.instance_identifier = "mac addr" pu.allowed_product_info = ["1"] pu.unallowed_product_info = [] pu.facts = {"key":"value"} self.assertEqual(pu.tracker, []) pu.tracker.append("a.example.com") pu.tracker.append("a.example.com") pu.tracker.append("b.example.com") self.assertEqual(len(pu.tracker), 3) pu.save() self.assertEqual(len(pu.tracker), 2) found = ProductUsage.objects() self.assertEqual(len(found), 1) self.assertEqual(len(found[0].tracker), 2) self.assertIn("a.example.com", found[0].tracker) self.assertIn("b.example.com", found[0].tracker)