Esempio n. 1
0
def listen_for_allocation_source_name_changed(
    sender, instance, created, **kwargs
):
    """
        This listener expects:
               EventType - 'allocation_source_name_changed'
               EventPayload - {
                   "source_id": "32712",
                   "name": "TestAllocationSource2",
               }

               The method should result in name of allocation source being changed

    """
    event = instance
    if event.name != 'allocation_source_name_changed':
        return None
    logger.info('Allocation Source name changed event: %s', event.__dict__)
    payload = event.payload
    allocation_source_id = payload['source_id']
    new_name = payload['name']
    ##CHANGED
    # allocation_source = AllocationSource.objects.filter(source_id=allocation_source_id)
    try:
        allocation_source = get_allocation_source_object(allocation_source_id)
        allocation_source.name = new_name
        allocation_source.save()

    except Exception as e:
        raise Exception(
            'Allocation Source %s name could not be changed because of the following error %s'
            % (allocation_source.name, e)
        )
    return
def allocation_source_removal_is(context, allocation_source_is_removed):
    result = True if context.response.status_code == 200 else False
    allocation_source = get_allocation_source_object(context.source_id)
    allocation_source_end_date = allocation_source.end_date
    assert (
        result == _str2bool(allocation_source_is_removed.lower())
        and allocation_source_end_date is not None
    )
Esempio n. 3
0
    def _for_validate_compute_allowed(self, compute_allowed, source_id):
        # raise Exception('Error with Compute Allowed')

        # Compute Allowed always >= 1
        if compute_allowed < 0:
            raise Exception('Compute allowed cannot be less than 0')

        # Compute Allowed is less than compute used
        if source_id:
            allocation_source = get_allocation_source_object(source_id)
            if compute_allowed < allocation_source.compute_used:
                raise Exception('Compute allowed cannot be less than compute used')

        return True
Esempio n. 4
0
    def _for_validate_compute_allowed(self, compute_allowed, source_id):
        # raise Exception('Error with Compute Allowed')

        # Compute Allowed always >= 1
        if compute_allowed < 0:
            raise Exception('Compute allowed cannot be less than 0')

        # Compute Allowed is less than compute used
        if source_id:
            allocation_source = get_allocation_source_object(source_id)
            if compute_allowed < allocation_source.compute_used:
                raise Exception(
                    'Compute allowed cannot be less than compute used')

        return True
Esempio n. 5
0
    def _create_allocation_source(self, request_data):

        payload = {}
        payload['uuid'] = str(uuid.uuid4())
        payload['allocation_source_name'] = request_data.get('name')
        payload['compute_allowed'] = request_data.get('compute_allowed')
        payload['renewal_strategy'] = request_data.get('renewal_strategy')

        creation_event = EventTable(
            name='allocation_source_created_or_renewed',
            entity_id=payload['allocation_source_name'],
            payload=payload)

        creation_event.save()

        return get_allocation_source_object(payload['uuid'])
Esempio n. 6
0
    def _create_allocation_source(self, request_data):

        payload = {}
        payload['uuid'] = str(uuid.uuid4())
        payload['allocation_source_name'] = request_data.get('name')
        payload['compute_allowed'] = request_data.get('compute_allowed')
        payload['renewal_strategy'] = request_data.get('renewal_strategy')

        creation_event = EventTable(
            name='allocation_source_created_or_renewed',
            entity_id=payload['allocation_source_name'],
            payload=payload)

        creation_event.save()

        return get_allocation_source_object(payload['uuid'])
Esempio n. 7
0
def allocation_source_with_compute_allowed(context):
    for row in context.table:
        response = context.client.post('/api/v2/allocation_sources',
                                       {"renewal_strategy": 'default',
                                        "name": "TestAllocationSource",
                                        "compute_allowed": int(row["compute_allowed"])})

        context.old_compute_allowed = row['compute_allowed']
        context.source_id = response.data['uuid']

        # set compute used for AllocationSourceSnapshot if compute_used > 0
        if int(row['compute_used']) > 0 and response.status_code == 201:
            allocation_source = get_allocation_source_object(context.source_id)
            snapshot = AllocationSourceSnapshot.objects.get(
                allocation_source=allocation_source)
            snapshot.compute_used = int(row['compute_used'])
            snapshot.save()
Esempio n. 8
0
def step_impl(context):
    for row in context.table:
        response = context.client.post('/api/v2/allocation_sources',
                                       {"renewal_strategy": 'default',
                                        "name": "TestAllocationSource",
                                        "compute_allowed": int(row["compute_allowed"])})

        context.old_compute_allowed = row['compute_allowed']
        context.source_id = response.data['uuid']

        # set compute used for AllocationSourceSnapshot if compute_used > 0
        if int(row['compute_used']) > 0 and response.status_code == 201:
            allocation_source = get_allocation_source_object(context.source_id)
            snapshot = AllocationSourceSnapshot.objects.get(
                allocation_source=allocation_source)
            snapshot.compute_used = int(row['compute_used'])
            snapshot.save()
Esempio n. 9
0
def allocation_source_removal_is(context, allocation_source_is_removed):
    result = True if context.response.status_code == 200 else False
    allocation_source = get_allocation_source_object(context.source_id)
    allocation_source_end_date = allocation_source.end_date
    assert (result == _str2bool(allocation_source_is_removed.lower())
            and allocation_source_end_date is not None)
Esempio n. 10
0
 def _for_validate_allocation_source(self, source_id):
     get_allocation_source_object(source_id)
     return True
Esempio n. 11
0
 def _for_validate_allocation_source(self, source_id):
     get_allocation_source_object(source_id)
     return True
Esempio n. 12
0
def listen_for_allocation_source_created_or_renewed(
    sender, instance, created, **kwargs
):
    """
       This listener expects:
       EventType - 'allocation_source_created_or_renewed'
       entity_id - "TG-AG100345" # Allocation Source Name

       # CyVerse Payload

       EventPayload - {
           "uuid" : "16fd2706-8baf-433b-82eb-8c7fada847da"
           "allocation_source_name": "TG-AG100345",
           "compute_allowed":1000,
           "renewal_strategy":"default"
       }

       # Jetstream Payload

       EventPayload - {
           "allocation_source_name": "TG-AG100345",
           "compute_allowed":1000,
       }

       The method should result in renewal of allocation source
    """
    event = instance
    if event.name != 'allocation_source_created_or_renewed':
        return None
    logger.info(
        "Allocation Source created or renewed event: %s" % event.__dict__
    )
    payload = event.payload
    allocation_source_name = payload['allocation_source_name']
    compute_allowed = payload['compute_allowed']

    if 'renewal_strategy' in payload:
        object_updated, created = AllocationSource.objects.update_or_create(
            uuid=uuid.UUID(payload['uuid']),
            name=allocation_source_name,
            defaults={
                'compute_allowed': compute_allowed,
                'renewal_strategy': payload['renewal_strategy']
            }
        )

        allocation_source = get_allocation_source_object(payload['uuid'])
        assert isinstance(allocation_source, AllocationSource)

        AllocationSourceSnapshot.objects.update_or_create(
            allocation_source=allocation_source,
            defaults={
                "compute_allowed": compute_allowed,
                "global_burn_rate": 0,
                "compute_used": 0.0,
                "updated": event.timestamp
            }
        )

        for user_allocation_snapshot in allocation_source.user_allocation_snapshots.all(
        ):
            user_allocation_snapshot.compute_used = 0.0
            user_allocation_snapshot.save()
    else:
        # Jetstream
        object_updated, created = AllocationSource.objects.update_or_create(
            name=allocation_source_name,
            defaults={'compute_allowed': compute_allowed}
        )

        allocation_source = AllocationSource.objects.get(
            name=allocation_source_name
        )

        AllocationSourceSnapshot.objects.update_or_create(
            allocation_source=allocation_source,
            defaults={
                "compute_allowed": compute_allowed,
                "global_burn_rate": 0,
                "compute_used": 0.0,
                "updated": event.timestamp
            }
        )

    logger.info(
        'object_updated: %s, created: %s' % (
            object_updated,
            created,
        )
    )