Example #1
0
    def create(self, validated_data):
        """Create a source."""
        name = validated_data.get('name')
        check_for_existing_name(Source.objects, name,
                                _(messages.SOURCE_NAME_ALREADY_EXISTS % name))

        if 'source_type' not in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]}
            raise ValidationError(error)
        source_type = validated_data.get('source_type')
        credentials = validated_data.pop('credentials')
        hosts_list = validated_data.pop('hosts', None)
        port = None
        if 'port' in validated_data:
            port = validated_data['port']

        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
            if port is None:
                validated_data['port'] = 22
        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        source = Source.objects.create(**validated_data)

        if options:
            if source_type == Source.SATELLITE_SOURCE_TYPE:
                if options.get('ssl_cert_verify') is None:
                    options['ssl_cert_verify'] = True
            if (source_type == Source.VCENTER_SOURCE_TYPE
                    and options.get('ssl_cert_verify') is None):
                options['ssl_cert_verify'] = True
            if source_type == Source.NETWORK_SOURCE_TYPE and \
                    bool(options):
                invalid_options = ', '.join([key for key in options.keys()])
                error = {
                    'options': [
                        _(messages.NET_SSL_OPTIONS_NOT_ALLOWED %
                          invalid_options)
                    ]
                }
                raise ValidationError(error)

            options = SourceOptions.objects.create(**options)
            options.save()
            source.options = options
        elif not options and source_type == Source.SATELLITE_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options
        elif not options and source_type == Source.VCENTER_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options

        source.hosts = json.dumps(hosts_list)

        for credential in credentials:
            source.credentials.add(credential)

        source.save()
        return source
Example #2
0
    def create(self, validated_data):
        """Create a source."""
        name = validated_data.get('name')
        check_for_existing_name(Source.objects, name,
                                _(messages.SOURCE_NAME_ALREADY_EXISTS % name))

        if 'source_type' not in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]}
            raise ValidationError(error)
        source_type = validated_data.get('source_type')
        credentials = validated_data.pop('credentials')
        hosts_list = validated_data.pop('hosts', None)
        exclude_hosts_list = validated_data.pop('exclude_hosts', None)
        port = None
        if 'port' in validated_data:
            port = validated_data['port']

        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
            if port is None:
                validated_data['port'] = 22

        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.VC_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.SAT_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        source = Source.objects.create(**validated_data)

        if options:
            SourceSerializer.validate_opts(options, source_type)
            options = SourceOptions.objects.create(**options)
            options.save()
            source.options = options
        elif not options and source_type == Source.SATELLITE_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options
        elif not options and source_type == Source.VCENTER_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options

        source.hosts = json.dumps(hosts_list)
        if exclude_hosts_list:
            source.exclude_hosts = json.dumps(exclude_hosts_list)

        for credential in credentials:
            source.credentials.add(credential)

        source.save()
        return source
Example #3
0
    def create(self, validated_data):
        """Create a source."""
        SourceSerializer.check_for_existing_name(
            name=validated_data.get('name'))

        if 'source_type' not in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]}
            raise ValidationError(error)
        source_type = validated_data.get('source_type')
        credentials = validated_data.pop('credentials')
        hosts_list = validated_data.pop('hosts', None)
        port = None
        if 'port' in validated_data:
            port = validated_data['port']

        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
            if port is None:
                validated_data['port'] = 22
        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        source = Source.objects.create(**validated_data)

        if options:
            if source_type == Source.SATELLITE_SOURCE_TYPE:
                if not options.get('satellite_version'):
                    options['satellite_version'] = \
                        SourceOptions.SATELLITE_VERSION_62
                if options.get('ssl_cert_verify') is None:
                    options['ssl_cert_verify'] = True
            if (source_type == Source.VCENTER_SOURCE_TYPE
                    and options.get('ssl_cert_verify') is None):
                options['ssl_cert_verify'] = True

            options = SourceOptions.objects.create(**options)
            options.save()
            source.options = options
        elif not options and source_type == Source.SATELLITE_SOURCE_TYPE:
            options = SourceOptions()
            options.satellite_version = SourceOptions.SATELLITE_VERSION_62
            options.ssl_cert_verify = True
            options.save()
            source.options = options
        elif not options and source_type == Source.VCENTER_SOURCE_TYPE:
            options = SourceOptions()
            options.satellite_version = None
            options.ssl_cert_verify = True
            options.save()
            source.options = options

        source.hosts = json.dumps(hosts_list)

        for credential in credentials:
            source.credentials.add(credential)

        source.save()
        return source