예제 #1
0
 def process_request(self, request):
     # type: (UsageFile) -> None
     if request.contract.id == 'CRD-99082-45842-69181':
         usage_file = UsageFile(
             name='sdk test',
             product=Product(id=request.product.id),
             contract=Contract(id=request.contract.id)
         )
         usages = [UsageRecord(
             item_search_criteria='item.mpn',
             item_search_value='SKUA',
             quantity=1,
             start_time_utc=(date.today() - timedelta(1)).strftime('%Y-%m-%d'),
             end_time_utc=time.strftime('%Y-%m-%d %H:%M:%S'),
             asset_search_criteria='parameter.param_b',
             asset_search_value='tenant2'
         )]
         self.submit_usage(usage_file, usages)
     elif request.contract.id == 'CRD-99082-45842-69182':
         pass
     elif request.contract.id == 'CRD-99082-45842-69183':
         pass
     elif request.contract.id == 'CRD-99082-45842-69184':
         usage_file = UsageFile(
             product=Product(id=request.product.id),
             contract=Contract(id=request.contract.id)
         )
         usages = [UsageRecord(
             record_id='123',
             item_search_criteria='item.mpn',
             item_search_value='SKUA',
             quantity=1,
             start_time_utc=(date.today() - timedelta(1)).strftime('%Y-%m-%d'),
             end_time_utc=time.strftime('%Y-%m-%d %H:%M:%S'),
             asset_search_criteria='parameter.param_b',
             asset_search_value='tenant2'
         )]
         self.submit_usage(usage_file, usages)
     elif request.contract.id == 'CRD-99082-45842-69185':
         pass
     elif request.contract.id == 'CRD-99082-45842-69186':
         pass
     elif request.contract.id == 'CRD-99082-45842-69187':
         pass
     else:
         raise UserWarning('Invalid test')
예제 #2
0
    def process_request(self, request):
        # type: (UsageListing) -> None

        # Detect specific provider contract
        if request.contract.id == 'CRD-41560-05399-123':
            # Can also be seen from request.provider.id and parametrized further
            # via marketplace available at request.marketplace.id
            usage_file = UsageFile(
                name='sdk test',
                product=Product(id=request.product.id),
                contract=Contract(id=request.contract.id)
            )

            today = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
            yesterday = today - timedelta(days=1)

            usages = [
                UsageRecord(
                    usage_record_id='unique record value',

                    item_search_criteria='item.mpn',
                    # Possible values are item.mpn or item.local_id.

                    item_search_value='SKUA',
                    # Value defined as MPN on vendor portal.

                    quantity=1,
                    # Quantity to be reported.

                    start_time_utc=yesterday.strftime('%Y-%m-%d %H:%M:%S'),
                    # From when to report.

                    end_time_utc=today.strftime('%Y-%m-%d %H:%M:%S'),
                    # Till when to report.

                    asset_search_criteria='parameter.param_b',
                    # How to find the asset on Connect.  Typical use case is to use a parameter
                    # provided by vendor, in this case called param_b.  Additionally, asset.id
                    # can be used in case you want to use Connect identifiers.

                    asset_search_value='tenant2'
                )
            ]

            self.submit_usage(usage_file, usages)
        else:
            # Something different could be done here
            pass
예제 #3
0
    def create_record(self, project, start_time, end_time, mpn, value):
        """Create UsageRecord object"""

        self.logger.info("add '%s' value %s", mpn, value)
        return UsageRecord(
            # should we store this object somewhere?
            usage_record_id=self._format_usage_record_id(
                project, end_time, mpn),
            item_search_criteria='item.mpn',
            # CPU_consumption, Storage_consumption, RAM_consumption
            item_search_value=mpn,
            quantity=value,
            start_time_utc=start_time.strftime('%Y-%m-%d %H:%M:%S'),
            end_time_utc=end_time.strftime('%Y-%m-%d %H:%M:%S'),
            asset_search_criteria='parameter.project_id',
            asset_search_value=project.id,
        )
예제 #4
0
 def make_object(self, data):
     from connect.models import UsageRecord
     return UsageRecord(**data)