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 create_product_usage(self, consumer, splice_server, instance_identifier, date=None,
                             allowed_product_info=None, unallowed_product_info=None, facts=None):
        if not date:
            date = datetime.now(tzutc())
        if not allowed_product_info:
            allowed_product_info = ["1", "2", "3"]
        if not unallowed_product_info:
            unallowed_product_info = []
        if not facts:
            facts = {"tbd": "values"}

        pu = ProductUsage()
        pu.consumer = consumer
        pu.splice_server = splice_server
        pu.date = date
        pu.instance_identifier = instance_identifier
        pu.allowed_product_info = allowed_product_info
        pu.unallowed_product_info = unallowed_product_info
        pu.facts = facts
        return pu
 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)
    return status, data


if __name__ == "__main__":
    from datetime import datetime
    from dateutil.tz import tzutc
    from splice.common.models import ProductUsage

    # Create a dummy product usage object
    pu = ProductUsage()
    pu.consumer = "test_consumer"
    pu.splice_server = "test_splice_server"
    pu.instance_identifier = "test_instance_identifier"
    pu.allowed_product_info = ["1", "2", "3", "4"]
    pu.unallowed_product_info = ["100"]
    pu.facts = {"tbd": "values"}
    pu.date = datetime.now(tzutc())

    config.init(settings.SPLICE_CONFIG_FILE)
    cfg = config.get_reporting_config_info()
    if cfg["servers"]:
        remote_server = cfg["servers"][0]
    else:
        remote_server = ("127.0.0.1", "443", "/splice/api/v1/productusage/")
    host = remote_server[0]
    port = remote_server[1]
    url = remote_server[2]

    status, data = upload_product_usage_data(host, port, url, [pu])
    print "---\n\n"
    print "Response:\n Status Code: %s\n%s" % (status, data)