class ProviderMachineSerializer(serializers.HyperlinkedModelSerializer):
    uuid = serializers.ReadOnlyField(source='instance_source.identifier')
    image = ImageSummarySerializer(source='application')
    provider = ProviderSummarySerializer(source='instance_source.provider')
    created_by = UserSummarySerializer(source='instance_source.created_by')
    start_date = serializers.DateTimeField(source='instance_source.start_date')
    end_date = serializers.DateTimeField(source='instance_source.end_date')

    class Meta:
        model = ProviderMachine
        view_name = 'api:v2:providermachine-detail'
        fields = ('id', 'uuid', 'url', 'image', 'provider', 'created_by',
                  'start_date', 'end_date')
Example #2
0
class IdentityMembershipSerializer(serializers.HyperlinkedModelSerializer):
    quota = QuotaSummarySerializer()
    allocation = AllocationSummarySerializer()
    identity = IdentitySummarySerializer()
    user = UserSummarySerializer(source='identity.created_by')
    provider = ProviderSummarySerializer(source='identity.provider')
    url = serializers.HyperlinkedIdentityField(
        view_name='api:v2:identitymembership-detail', )

    class Meta:
        model = IdentityMembership
        fields = ('id', 'url', 'quota', 'allocation', 'end_date', 'provider',
                  'identity', 'user')
Example #3
0
class VolumeSerializer(serializers.HyperlinkedModelSerializer):
    provider = ProviderSummarySerializer(source='instance_source.provider')
    identity = IdentitySummarySerializer(
        source='instance_source.created_by_identity')
    user = UserSummarySerializer(source='instance_source.created_by')
    start_date = serializers.DateTimeField(source='instance_source.start_date')
    end_date = serializers.DateTimeField(source='instance_source.end_date')
    projects = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    uuid = serializers.CharField(source='instance_source.identifier')

    class Meta:
        model = Volume
        view_name = 'api_v2:volume-detail'
        fields = ('id', 'uuid', 'url', 'name', 'size', 'user', 'provider',
                  'identity', 'projects', 'start_date', 'end_date')
Example #4
0
class IdentitySerializer(serializers.HyperlinkedModelSerializer):
    quota = QuotaSummarySerializer(source='get_quota')
    allocation = AllocationSummarySerializer(source='get_allocation')
    usage = serializers.SerializerMethodField()
    user = UserSummarySerializer(source='created_by')
    provider = ProviderSummarySerializer()
    url = UUIDHyperlinkedIdentityField(view_name='api:v2:identity-detail', )

    def get_usage(self, identity):
        return identity.get_allocation_usage()

    class Meta:
        model = Identity
        fields = ('id', 'uuid', 'url', 'quota', 'allocation', 'usage',
                  'provider', 'user')
Example #5
0
class IdentityMembershipSerializer(serializers.HyperlinkedModelSerializer):
    quota = QuotaSummarySerializer()
    identity_key = serializers.SerializerMethodField()
    identity = IdentitySummarySerializer()
    user = UserSummarySerializer(source='identity.created_by')
    provider = ProviderSummarySerializer(source='identity.provider')
    url = serializers.HyperlinkedIdentityField(
        view_name='api:v2:identitymembership-detail', )

    def get_identity_key(self, identity_membership):
        return identity_membership.identity.get_key()

    class Meta:
        model = IdentityMembership
        fields = ('id', 'url', 'quota', 'end_date', 'provider', 'identity',
                  'identity_key', 'user')
Example #6
0
class InstanceSerializer(serializers.HyperlinkedModelSerializer):
    identity = IdentitySummarySerializer(source='created_by_identity')
    user = UserSummarySerializer(source='created_by')
    provider = ProviderSummarySerializer(source='created_by_identity.provider')
    status = serializers.CharField(source='esh_status', read_only=True)
    projects = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    size = serializers.SerializerMethodField()
    image = serializers.SerializerMethodField()
    uuid = serializers.CharField(source='provider_alias')

    def get_size(self, obj):
        size_alias = obj.esh_size()
        provider_id = obj.created_by_identity.provider_id
        size = Size.objects.get(alias=size_alias, provider=provider_id)
        serializer = SizeSummarySerializer(size, context=self.context)
        return serializer.data

    def get_image(self, obj):
        image_uuid = obj.application_uuid()
        image = Image.objects.get(uuid=image_uuid)
        serializer = ImageSummarySerializer(image, context=self.context)
        return serializer.data

    class Meta:
        model = Instance
        view_name = 'api:v2:instance-detail'
        fields = (
            'id',
            'uuid',
            'url',
            'name',
            'status',
            'size',
            'ip_address',
            'shell',
            'vnc',
            'identity',
            'user',
            'provider',
            'image',
            'projects',
            'start_date',
            'end_date'
        )
class ResourceRequestSerializer(serializers.HyperlinkedModelSerializer):
    uuid = serializers.CharField(read_only=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:resourcerequest-detail', )
    created_by = UserRelatedField(read_only=True)
    user = UserSummarySerializer(source='membership.identity.created_by',
                                 read_only=True)
    identity = IdentityRelatedField(source='membership.identity',
                                    queryset=Identity.objects.none())
    provider = ProviderSummarySerializer(source='membership.identity.provider',
                                         read_only=True)
    status = StatusTypeRelatedField(queryset=StatusType.objects.none(),
                                    allow_null=True,
                                    required=False)
    quota = QuotaRelatedField(queryset=Quota.objects.all(),
                              allow_null=True,
                              required=False)

    allocation = AllocationRelatedField(queryset=Allocation.objects.all(),
                                        allow_null=True,
                                        required=False)

    # TODO should be refactored to not use SerializerMethodField()
    current_quota = serializers.SerializerMethodField()
    current_allocation = serializers.SerializerMethodField()

    def get_current_quota(self, request):
        user_membership = IdentityMembership.objects.get(
            id=request.membership_id)
        return user_membership.quota.id if user_membership.quota else None

    def get_current_allocation(self, request):
        user_membership = IdentityMembership.objects.get(
            id=request.membership_id)
        return user_membership.allocation.id if user_membership.allocation else None

    class Meta:
        model = ResourceRequest
        fields = ('id', 'uuid', 'url', 'request', 'description', 'status',
                  'created_by', 'user', 'identity', 'provider',
                  'admin_message', 'quota', 'allocation', 'current_quota',
                  'current_allocation')
class UserResourceRequestSerializer(serializers.HyperlinkedModelSerializer):
    quota = QuotaRelatedField(read_only=True)
    allocation = AllocationRelatedField(read_only=True)
    status = StatusTypeRelatedField(read_only=True)
    admin_message = serializers.CharField(read_only=True)
    uuid = serializers.CharField(read_only=True)
    created_by = UserRelatedField(read_only=True)
    user = UserSummarySerializer(source='membership.identity.created_by',
                                 read_only=True)
    identity = IdentityRelatedField(source='membership.identity',
                                    queryset=Identity.objects.none())
    provider = ProviderSummarySerializer(source='membership.identity.provider',
                                         read_only=True)

    class Meta:
        model = ResourceRequest
        view_name = 'api:v2:resourcerequest-detail'
        fields = ('id', 'uuid', 'url', 'request', 'description', 'status',
                  'created_by', 'user', 'identity', 'provider',
                  'admin_message', 'quota', 'allocation')
Example #9
0
class ProviderMachineSerializer(serializers.HyperlinkedModelSerializer):
    # NOTE: these fields could be generalized for reuse in VolumeSerializer
    uuid = serializers.ReadOnlyField(source='instance_source.identifier')
    provider = ProviderSummarySerializer(source='instance_source.provider')
    version = ImageVersionSummarySerializer(source='application_version')
    image = ImageSummarySerializer(source='application')
    created_by = UserSummarySerializer(source='instance_source.created_by')
    size_gb = serializers.ReadOnlyField(source='instance_source.size_gb')
    start_date = serializers.DateTimeField(source='instance_source.start_date')
    end_date = serializers.DateTimeField(
        source='instance_source.end_date', allow_null=True
    )
    licenses = LicenseSummarySerializer(many=True, read_only=True)    # NEW
    members = serializers.SlugRelatedField(
        slug_field='name', read_only=True, many=True
    )    # NEW
    # NOTE: this is still using ID instead of UUID -- due to abstract classes
    # and use of getattr in L271 of rest_framework/relations.py, this is a
    # 'kink' that has not been worked out yet.
    url = InstanceSourceHyperlinkedIdentityField(
        view_name='api:v2:providermachine-detail',
    )

    class Meta:
        model = ProviderMachine
        fields = (
            'id', 'uuid', 'url', 'provider', 'image', 'licenses', 'members',
            'version', 'size_gb', 'created_by', 'start_date', 'end_date'
        )

    def update(self, instance, validated_data):
        if 'instance_source' in validated_data:
            source = instance.instance_source
            source_data = validated_data.pop('instance_source')
            for key, val in source_data.items():
                setattr(source, key, val)
            source.save()
        for (key, val) in validated_data.items():
            setattr(instance, key, val)
        instance.save()
        return instance
Example #10
0
class UserResourceRequestSerializer(serializers.HyperlinkedModelSerializer):
    def validate_status(self, value):
        if str(value) not in ["pending", "closed"]:
            raise serializers.ValidationError("Users can only open and close requests.")
        return value

    quota = QuotaRelatedField(read_only=True)
    allocation = AllocationRelatedField(read_only=True)
    status = StatusTypeRelatedField(queryset = StatusType.objects.all(), default=StatusType.objects.get(name="pending"))
    admin_message = serializers.CharField(read_only=True)
    uuid = serializers.CharField(read_only=True)
    created_by = UserRelatedField(read_only=True)
    user = UserSummarySerializer(
        source='membership.identity.created_by',
        read_only=True)
    identity = IdentityRelatedField(source='membership.identity',
                                    queryset=Identity.objects.none())
    provider = ProviderSummarySerializer(
        source='membership.identity.provider',
        read_only=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:resourcerequest-detail',
    )
    class Meta:
        model = ResourceRequest
        fields = (
            'id',
            'uuid',
            'url',
            'request',
            'description',
            'status',
            'created_by',
            'user',
            'identity',
            'provider',
            'admin_message',
            'quota',
            'allocation'
        )
Example #11
0
class InstanceStatusHistorySerializer(serializers.HyperlinkedModelSerializer):
    instance = ModelRelatedField(
        queryset=Instance.objects.all(),
        serializer_class=InstanceSuperSummarySerializer,
        style={'base_template': 'input.html'})
    size = SizeRelatedField(queryset=Size.objects.none())
    provider = ProviderSummarySerializer(
        source='instance.get_provider')
    image = ImageSummarySerializer(
        source='instance.provider_machine.application_version.application')
    total_hours = serializers.SerializerMethodField()
    status = serializers.SlugRelatedField(slug_field='name', read_only=True)
    activity = serializers.CharField(max_length=36, allow_blank=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:instancestatushistory-detail',
    )

    def get_total_hours(self, instance_history):
        instance = instance_history.instance
        if not instance.allocation_source\
                or not instance.allocation_source.snapshot:
            return -1
        return instance.allocation_source.snapshot.compute_used

    class Meta:
        model = InstanceStatusHistory
        fields = (
            'id',
            'uuid',
            'url',
            'instance',
            'status',
            'activity',
            'size',
            'total_hours',
            'provider',
            'image',
            'start_date',
            'end_date',
        )
Example #12
0
class IdentitySerializer(serializers.HyperlinkedModelSerializer):
    quota = ModelRelatedField(
        queryset=Quota.objects.all(),
        serializer_class=QuotaSummarySerializer,
        lookup_field='id'
    )
    usage = serializers.SerializerMethodField()
    credentials = CredentialSummarySerializer(
        many=True, source='credential_set'
    )
    key = serializers.SerializerMethodField()
    is_leader = serializers.SerializerMethodField()
    user = UserSummarySerializer(source='created_by')
    members = GroupSummarySerializer(
        source='get_membership', many=True, read_only=True
    )
    provider = ProviderSummarySerializer()
    url = UUIDHyperlinkedIdentityField(view_name='api:v2:identity-detail', )

    def update(self, core_identity, validated_data):
        quota = validated_data.get('quota')
        data = {'quota': quota.id, 'identity': core_identity.id}
        event_serializer = QuotaAssignedSerializer(data=data)
        if not event_serializer.is_valid():
            raise serializers.ValidationError(
                "Validation of EventSerializer failed with: %s" %
                event_serializer.errors
            )
        try:
            event_serializer.save()
        except Exception as exc:
            logger.exception("Unexpected error occurred during Event save")
            raise serializers.ValidationError(
                "Unexpected error occurred during Event save: %s" % exc
            )
        # Synchronous call to EventTable -> Set Quota for Identity's CloudProvider -> Save the Quota to Identity
        identity = Identity.objects.get(uuid=core_identity.uuid)
        return identity

    def get_usage(self, identity):
        return -1

    def get_is_leader(self, identity):
        """
        Returns true/false if the user requesting the object is the leader.
        """
        user = None
        if self.context:
            if 'request' in self.context:
                user = self.context['request'].user
            elif 'user' in self.context:
                user = self.context['user']
        if user == identity.created_by:
            return True
        return Identity.shared_with_user(
            user, is_leader=True
        ).filter(id=identity.id).exists()

    def get_key(self, identity):
        return identity.get_key()

    class Meta:
        model = Identity
        fields = (
            'id', 'uuid', 'url', 'key', 'quota', 'credentials', 'usage',
            'is_leader', 'provider', 'members', 'user'
        )
Example #13
0
class InstanceSerializer(serializers.HyperlinkedModelSerializer):
    identity = IdentitySummarySerializer(source='created_by_identity')
    user = UserSummarySerializer(source='created_by')
    provider = ProviderSummarySerializer(source='created_by_identity.provider')
    status = serializers.CharField(source='api_status', read_only=True)
    activity = serializers.CharField(source='api_activity', read_only=True)
    projects = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    scripts = ModelRelatedField(many=True,
                                required=False,
                                queryset=BootScript.objects.all(),
                                serializer_class=BootScriptSummarySerializer,
                                style={'base_template': 'input.html'})
    size = serializers.SerializerMethodField()
    image = serializers.SerializerMethodField()
    ip_address = serializers.SerializerMethodField()
    usage = serializers.SerializerMethodField()
    version = serializers.SerializerMethodField()
    allocation_source = serializers.SerializerMethodField()
    uuid = serializers.CharField(source='provider_alias')
    url = UUIDHyperlinkedIdentityField(view_name='api:v2:instance-detail',
                                       lookup_field='uuid',
                                       uuid_field='provider_alias')

    def get_allocation_source(self, instance):
        snapshot = InstanceAllocationSourceSnapshot.objects.filter(
            instance=instance).first()
        if not snapshot:
            return None
        serializer = AllocationSourceSerializer(snapshot.allocation_source,
                                                context=self.context)
        return serializer.data

    def get_usage(self, instance):
        return instance.get_total_hours()

    def get_size(self, obj):
        size = obj.get_size()
        serializer = SizeSummarySerializer(size, context=self.context)
        return serializer.data

    def get_image(self, obj):
        if obj.source.is_volume():
            return {}
        image_uuid = obj.application_uuid()
        image = Image.objects.get(uuid=image_uuid)
        serializer = ImageSummarySerializer(image, context=self.context)
        return serializer.data

    def get_ip_address(self, obj):
        status = obj.esh_status()
        if status in ["suspended", "shutoff", "shelved"]:
            return "0.0.0.0"
        return obj.ip_address

    def get_version(self, obj):
        if obj.source.is_volume():
            return {}
        version = obj.source.providermachine.application_version
        serializer = ImageVersionSummarySerializer(version,
                                                   context=self.context)
        return serializer.data

    class Meta:
        model = Instance
        fields = (
            'id',
            'uuid',
            'url',
            'name',
            'status',
            'activity',
            'size',
            'ip_address',
            'shell',
            'vnc',
            'web_desktop',
            'identity',
            'user',
            'provider',
            'image',
            'version',  # NOTE:Should replace image?
            'usage',
            'scripts',
            'projects',
            'start_date',
            'end_date',
            'allocation_source',
        )
Example #14
0
 def to_representation(self, value):
     provider = Provider.objects.get(id=value.id)
     serializer = ProviderSummarySerializer(provider, context=self.context)
     return serializer.data