Esempio n. 1
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')
    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', '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
Esempio n. 2
0
 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
Esempio n. 3
0
class UserMachineRequestSerializer(serializers.HyperlinkedModelSerializer):

    uuid = serializers.CharField(read_only=True)
    instance = ModelRelatedField(queryset=Instance.objects.all(),
                                 serializer_class=InstanceSummarySerializer,
                                 style={'base_template': 'input.html'})
    status = StatusTypeRelatedField(queryset=StatusType.objects.none(),
                                    allow_null=True,
                                    required=False)
    old_status = serializers.CharField(required=False)
    new_version_tags = serializers.CharField(required=False, allow_blank=True)
    new_version_change_log = serializers.CharField(required=False,
                                                   allow_blank=True)
    new_version_memory_min = serializers.CharField()
    new_version_cpu_min = serializers.CharField()
    new_application_name = serializers.CharField(
        validators=[NoSpecialCharacters('!"#$%&\'*+,/;<=>?@[\\]^`{|}~')])
    new_application_version = ImageVersionSummarySerializer(read_only=True)
    new_application_visibility = serializers.CharField()
    admin_message = serializers.CharField(read_only=True)
    access_list = serializers.CharField(allow_blank=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:machinerequest-detail', )

    #FIXME: tags are missing here.
    # version change log is missing
    #
    class Meta:
        model = MachineRequest
        fields = (
            'id',
            'uuid',
            'url',
            'instance',
            'status',
            'old_status',
            'new_version_tags',
            'new_version_change_log',
            'admin_message',
            'new_application_name',
            'new_application_version',
            'new_application_visibility',
            'new_version_memory_min',
            'new_version_cpu_min',
            'access_list',
        )
Esempio n. 4
0
class ImageVersionSerializer(serializers.HyperlinkedModelSerializer):
    """
    Serializer for ApplicationVersion (aka 'image_version')
    """
    # NOTE: Implicitly included via 'fields'
    # id, application
    parent = ImageVersionSummarySerializer()
    # name, change_log, allow_imaging
    licenses = ModelRelatedField(
        many=True,
        queryset=License.objects.all(),
        serializer_class=LicenseSummarySerializer,
        style={'base_template': 'input.html'}
    )
    scripts = ModelRelatedField(
        source='boot_scripts',
        many=True,
        queryset=BootScript.objects.all(),
        serializer_class=BootScriptSummarySerializer,
        style={'base_template': 'input.html'}
    )
    membership = serializers.SlugRelatedField(
        slug_field='name', read_only=True, many=True
    )
    user = UserSummarySerializer(source='created_by')
    identity = IdentitySummarySerializer(source='created_by_identity')
    machines = serializers.SerializerMethodField('get_machines_for_user')
    image = ModelRelatedField(
        source='application',
        queryset=Image.objects.all(),
        serializer_class=ImageSummarySerializer,
        style={'base_template': 'input.html'}
    )
    start_date = serializers.DateTimeField()
    min_mem = serializers.IntegerField(source='threshold.memory_min')
    min_cpu = serializers.IntegerField(source='threshold.cpu_min')
    end_date = serializers.DateTimeField(allow_null=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:imageversion-detail', uuid_field='id'
    )

    def get_machines_for_user(self, obj):
        """
        Only show version as available on providers the user has access to
        """
        user = self.context['request'].user

        filtered = obj.machines.filter(
            Q(instance_source__provider__active=True)
        )
        if isinstance(user, AnonymousUser):
            filtered = filtered.filter(
                Q(instance_source__provider__public=True)
            )
        elif not user.is_staff:
            filtered = filtered.filter(
                Q(instance_source__provider_id__in=user.provider_ids())
            )
        serializer = ProviderMachineSummarySerializer(
            filtered, context=self.context, many=True
        )
        return serializer.data

    class Meta:
        model = ImageVersion
        fields = (
            'id', 'url', 'parent', 'name', 'change_log', 'image', 'machines',
            'allow_imaging', 'doc_object_id', 'licenses', 'membership',
            'min_mem', 'min_cpu', 'scripts', 'user', 'identity', 'start_date',
            'end_date'
        )

    def update(self, instance, validated_data):
        if 'min_cpu' in self.initial_data or 'min_mem' in self.initial_data:
            self.update_threshold(instance, validated_data)
        return super(ImageVersionSerializer,
                     self).update(instance, validated_data)

    def validate_min_cpu(self, value):
        if value < 0 or value > 16:
            raise serializers.ValidationError(
                "Value of CPU must be between 1 & 16"
            )
        return value

    def validate_min_mem(self, value):
        if value < 0 or value > 32 * 1024:
            raise serializers.ValidationError(
                "Value of mem must be between 1 & 32 GB"
            )
        return value

    def update_threshold(self, instance, validated_data):
        current_threshold = instance.get_threshold()

        try:
            current_mem_min = current_threshold.memory_min
        except:
            current_mem_min = 0

        try:
            current_cpu_min = current_threshold.cpu_min
        except:
            current_cpu_min = 0

        try:
            new_mem_min = validated_data.get('threshold')['memory_min']
        except:
            new_mem_min = current_mem_min

        try:
            new_cpu_min = validated_data.get('threshold')['cpu_min']
        except:
            new_cpu_min = current_cpu_min

        if not current_threshold:
            new_threshold = ApplicationThreshold.objects.create(
                application_version=instance,
                memory_min=new_mem_min,
                cpu_min=new_cpu_min
            )
        else:
            new_threshold = ApplicationThreshold.objects.get(
                application_version=instance
            )
            new_threshold.memory_min = new_mem_min
            new_threshold.cpu_min = new_cpu_min
            new_threshold.save()

        validated_data['threshold'] = new_threshold
Esempio n. 5
0
class MachineRequestSerializer(serializers.HyperlinkedModelSerializer):
    def validate(self, data):

        # set the parent machine
        parent_machine = ProviderMachine.objects.get(
            instance_source__id=data['instance'].source_id,
            instance_source__provider=data['instance'].provider)
        data['parent_machine'] = parent_machine

        # make sure user has access to the new provider
        user = data['new_machine_owner']
        provider = data['new_machine_provider']
        queryset = user.identity_set.filter(provider=provider)
        if not queryset.exists():
            raise exceptions.ValidationError(
                "User %s does not have access to provider %s." %
                (user.username, provider))

        # make sure provider has imaging enabled
        if not provider.public:
            raise exceptions.ValidationError(
                "Provider %s does not allow imaging." % (provider))

        # TODO: make sure user has access to parent machine

        return data

    uuid = serializers.CharField(read_only=True)
    identity = IdentityRelatedField(source='membership.identity',
                                    queryset=Identity.objects.none())
    instance = ModelRelatedField(queryset=Instance.objects.all(),
                                 serializer_class=InstanceSummarySerializer,
                                 style={'base_template': 'input.html'})
    status = StatusTypeRelatedField(queryset=StatusType.objects.none(),
                                    allow_null=True,
                                    required=False)
    admin_message = serializers.CharField(read_only=True)
    parent_machine = ModelRelatedField(
        required=False,
        lookup_field="uuid",
        queryset=ProviderMachine.objects.all(),
        serializer_class=ProviderMachineSummarySerializer,
        style={'base_template': 'input.html'})

    new_application_name = serializers.CharField(
        validators=[NoSpecialCharacters('!"#$%&\'*+,/;<=>?@[\\]^_`{|}~')])
    new_application_description = serializers.CharField()
    old_status = serializers.CharField(required=False)
    new_application_visibility = serializers.CharField()
    access_list = serializers.CharField(allow_blank=True)
    iplant_sys_files = serializers.CharField(allow_blank=True)
    installed_software = serializers.CharField()
    exclude_files = serializers.CharField(allow_blank=True)
    new_version_name = serializers.CharField()
    new_version_change_log = serializers.CharField()
    new_version_tags = serializers.CharField(required=False, allow_blank=True)
    new_version_memory_min = serializers.CharField()
    new_version_cpu_min = serializers.CharField()
    new_version_allow_imaging = serializers.BooleanField()
    new_version_forked = serializers.BooleanField()
    new_version_licenses = ModelRelatedField(
        many=True,
        queryset=License.objects.all(),
        serializer_class=LicenseSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_scripts = ModelRelatedField(
        many=True,
        queryset=BootScript.objects.all(),
        serializer_class=BootScriptSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_membership = ModelRelatedField(
        many=True,
        queryset=Group.objects.all(),
        serializer_class=GroupSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_machine_provider = ModelRelatedField(
        queryset=Provider.objects.all(),
        serializer_class=ProviderSummarySerializer,
        style={'base_template': 'input.html'})
    new_machine_owner = ModelRelatedField(
        queryset=User.objects.all(),
        serializer_class=UserSummarySerializer,
        style={'base_template': 'input.html'})
    start_date = serializers.DateTimeField(read_only=True)
    end_date = serializers.DateTimeField(read_only=True)
    new_machine = ModelRelatedField(
        required=False,
        queryset=ProviderMachine.objects.all(),
        serializer_class=ProviderMachineSummarySerializer,
        style={'base_template': 'input.html'})
    new_application_version = ImageVersionSummarySerializer(read_only=True)
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:machinerequest-detail', )

    class Meta:
        model = MachineRequest
        fields = ('id', 'uuid', 'url', 'instance', 'identity', 'status',
                  'old_status', 'parent_machine', 'admin_message',
                  'new_application_name', 'new_application_description',
                  'new_application_visibility', 'access_list',
                  'iplant_sys_files', 'installed_software', 'exclude_files',
                  'new_version_name', 'new_version_change_log',
                  'new_version_tags', 'new_version_memory_min',
                  'new_version_cpu_min', 'new_version_allow_imaging',
                  'new_version_forked', 'new_version_licenses',
                  'new_version_scripts', 'new_version_membership',
                  'new_machine_provider', 'new_machine_owner', 'start_date',
                  'end_date', 'new_machine', 'new_application_version')
Esempio n. 6
0
 def to_representation(self, value):
     serializer = ImageVersionSummarySerializer(value, context=self.context)
     return serializer.data
Esempio n. 7
0
class MachineRequestSerializer(serializers.HyperlinkedModelSerializer):

    uuid = serializers.CharField(read_only=True)
    identity = IdentityRelatedField(source='membership.identity',
                                    queryset=Identity.objects.none())
    # This is a *STAFF EXCLUSIVE* serializer. These are the values that make it that way:
    admin_message = serializers.CharField(read_only=True)
    parent_machine = ModelRelatedField(
        required=False,
        lookup_field="uuid",
        queryset=ProviderMachine.objects.all(),
        serializer_class=ProviderMachineSummarySerializer,
        style={'base_template': 'input.html'})

    instance = ModelRelatedField(queryset=Instance.objects.all(),
                                 serializer_class=InstanceSummarySerializer,
                                 style={'base_template': 'input.html'})
    status = StatusTypeRelatedField(queryset=StatusType.objects.none(),
                                    allow_null=True,
                                    required=False)
    old_status = serializers.CharField(required=False)

    new_application_visibility = serializers.CharField()
    new_application_version = ImageVersionSummarySerializer(read_only=True)
    new_application_name = serializers.CharField(
        validators=[NoSpecialCharacters('!"#$%&\'*+,/;<=>?@[\\]^_`{|}~')])
    new_application_description = serializers.CharField()
    access_list = serializers.CharField(allow_blank=True)
    system_files = serializers.CharField(allow_blank=True, required=False)
    installed_software = serializers.CharField()
    exclude_files = serializers.CharField(allow_blank=True)
    new_version_name = serializers.CharField()
    new_version_change_log = serializers.CharField(required=False,
                                                   allow_blank=True)
    new_version_tags = serializers.CharField(required=False, allow_blank=True)
    new_version_memory_min = serializers.CharField()
    new_version_cpu_min = serializers.CharField()
    new_version_allow_imaging = serializers.BooleanField()
    new_version_forked = serializers.BooleanField()
    new_version_licenses = ModelRelatedField(
        many=True,
        queryset=License.objects.all(),
        serializer_class=LicenseSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_scripts = ModelRelatedField(
        many=True,
        queryset=BootScript.objects.all(),
        serializer_class=BootScriptSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_membership = ModelRelatedField(
        many=True,
        queryset=Group.objects.all(),
        serializer_class=GroupSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_machine_provider = ModelRelatedField(
        queryset=Provider.objects.all(),
        serializer_class=ProviderSummarySerializer,
        style={'base_template': 'input.html'})
    new_machine_owner = ModelRelatedField(
        queryset=User.objects.all(),
        serializer_class=UserSummarySerializer,
        style={'base_template': 'input.html'})
    start_date = serializers.DateTimeField(read_only=True)
    end_date = serializers.DateTimeField(read_only=True)
    new_machine = ModelRelatedField(
        required=False,
        queryset=ProviderMachine.objects.all(),
        serializer_class=ProviderMachineSummarySerializer,
        style={'base_template': 'input.html'})
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:machinerequest-detail', )

    class Meta:
        model = MachineRequest
        fields = ('id', 'uuid', 'url', 'instance', 'identity', 'status',
                  'old_status', 'parent_machine', 'admin_message',
                  'new_application_name', 'new_application_description',
                  'new_application_visibility', 'access_list', 'system_files',
                  'installed_software', 'exclude_files', 'new_version_name',
                  'new_version_change_log', 'new_version_tags',
                  'new_version_memory_min', 'new_version_cpu_min',
                  'new_version_allow_imaging', 'new_version_forked',
                  'new_version_licenses', 'new_version_scripts',
                  'new_version_membership', 'new_machine_provider',
                  'new_machine_owner', 'start_date', 'end_date', 'new_machine',
                  'new_application_version')
Esempio n. 8
0
class UserMachineRequestSerializer(serializers.HyperlinkedModelSerializer):

    uuid = serializers.CharField(read_only=True)
    admin_message = serializers.CharField(read_only=True)
    instance = ModelRelatedField(queryset=Instance.objects.all(),
                                 serializer_class=InstanceSummarySerializer,
                                 style={'base_template': 'input.html'})
    start_date = serializers.DateTimeField(read_only=True)
    end_date = serializers.DateTimeField(read_only=True)
    status = StatusTypeRelatedField(queryset=StatusType.objects.none(),
                                    allow_null=True,
                                    required=False)
    old_status = serializers.CharField(source='clean_old_status',
                                       required=False)

    new_application_visibility = serializers.CharField()
    new_application_version = ImageVersionSummarySerializer(read_only=True)
    new_application_name = serializers.CharField(
        validators=[NoSpecialCharacters('!"#$%&\'*+,/;<=>?@[\\]^`{|}~')])
    new_application_description = serializers.CharField()
    access_list = serializers.CharField(allow_blank=True)
    system_files = serializers.CharField(allow_blank=True, required=False)
    installed_software = serializers.CharField()
    exclude_files = serializers.CharField(allow_blank=True)
    new_version_name = serializers.CharField()
    new_version_change_log = serializers.CharField(required=False,
                                                   allow_blank=True)
    new_version_tags = serializers.CharField(required=False, allow_blank=True)
    new_version_memory_min = serializers.CharField()
    new_version_cpu_min = serializers.CharField()
    new_version_allow_imaging = serializers.BooleanField()
    new_version_forked = serializers.BooleanField()
    new_version_licenses = ModelRelatedField(
        many=True,
        queryset=License.objects.all(),
        serializer_class=LicenseSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_scripts = ModelRelatedField(
        many=True,
        queryset=BootScript.objects.all(),
        serializer_class=BootScriptSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_version_membership = ModelRelatedField(
        many=True,
        queryset=Group.objects.all(),
        serializer_class=GroupSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_machine_provider = ModelRelatedField(
        queryset=Provider.objects.all(),
        serializer_class=ProviderSummarySerializer,
        style={'base_template': 'input.html'},
        required=False)
    new_machine = ModelRelatedField(
        required=False,
        queryset=ProviderMachine.objects.all(),
        serializer_class=ProviderMachineSummarySerializer,
        style={'base_template': 'input.html'})
    # Absent: new_machine_owner -- determined by User submission
    url = UUIDHyperlinkedIdentityField(
        view_name='api:v2:machinerequest-detail', )

    #FIXME: tags are missing here.
    # version change log is missing
    #
    class Meta:
        model = MachineRequest
        fields = (
            'id',
            'uuid',
            'url',
            'start_date',
            'end_date',
            'admin_message',
            'instance',
            'status',
            'old_status',
            'new_application_visibility',
            'new_application_name',
            'new_application_description',
            'access_list',
            'system_files',
            'installed_software',
            'exclude_files',
            'new_version_name',
            'new_version_change_log',
            'new_version_tags',
            'new_version_memory_min',
            'new_version_cpu_min',
            'new_version_allow_imaging',
            'new_version_forked',
            'new_version_licenses',
            'new_version_scripts',
            'new_version_membership',
            'new_machine_provider',
            'new_application_version',
            'new_machine',
        )