<commit_before>package org.ovirt.engine.api.restapi.resource;

import static org.ovirt.engine.api.restapi.resource.BackendStorageDomainResource.getLinksToExclude;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.api.common.util.StatusUtils;
import org.ovirt.engine.api.model.LogicalUnit;
import org.ovirt.engine.api.model.Storage;
import org.ovirt.engine.api.model.StorageDomain;
import org.ovirt.engine.api.model.StorageDomainStatus;
import org.ovirt.engine.api.model.StorageDomains;
import org.ovirt.engine.api.model.VolumeGroup;
import org.ovirt.engine.api.resource.StorageDomainResource;
import org.ovirt.engine.api.resource.StorageDomainsResource;
import org.ovirt.engine.api.restapi.types.StorageDomainMapper;
import org.ovirt.engine.api.restapi.util.StorageDomainHelper;
import org.ovirt.engine.core.common.action.AddSANStorageDomainParameters;
import org.ovirt.engine.core.common.action.StorageDomainManagementParameter;
import org.ovirt.engine.core.common.action.StorageServerConnectionParametersBase;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.businessentities.StorageDomainSharedStatus;
import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
import org.ovirt.engine.core.common.businessentities.StorageDomainType;
import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VdsStatic;
import org.ovirt.engine.core.common.businessentities.storage.LUNs;
import org.ovirt.engine.core.common.businessentities.storage.StorageType;
import org.ovirt.engine.core.common.interfaces.SearchType;
import org.ovirt.engine.core.common.queries.GetDeviceListQueryParameters;
import org.ovirt.engine.core.common.queries.GetExistingStorageDomainListParameters;
import org.ovirt.engine.core.common.queries.GetLunsByVgIdParameters;
import org.ovirt.engine.core.common.queries.GetUnregisteredBlockStorageDomainsParameters;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.NameQueryParameters;
import org.ovirt.engine.core.common.queries.StorageServerConnectionQueryParametersBase;
import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
import org.ovirt.engine.core.common.queries.VdcQueryType;
import org.ovirt.engine.core.common.utils.Pair;
import org.ovirt.engine.core.compat.Guid;

public class BackendStorageDomainsResource
        extends AbstractBackendCollectionResource<StorageDomain, org.ovirt.engine.core.common.businessentities.StorageDomain>
        implements StorageDomainsResource {

    static final String[] SUB_COLLECTIONS = { "permissions", "files", "templates", "vms", "disks",
            "storageconnections", "images", "disksnapshots", "diskprofiles" };

    private final EntityIdResolver<Guid> ID_RESOLVER =
            new QueryIdResolver<Guid>(VdcQueryType.GetStorageDomainById, IdQueryParameters.class);

    public BackendStorageDomainsResource() {
        super(StorageDomain.class, org.ovirt.engine.core.common.businessentities.StorageDomain.class, SUB_COLLECTIONS);
    }

    @Override
    public StorageDomains list() {
        if (isFiltered())
            return mapCollection(getBackendCollection(VdcQueryType.GetAllStorageDomains,
                    new VdcQueryParametersBase(), SearchType.StorageDomain));
        else
            return mapCollection(getBackendCollection(SearchType.StorageDomain));
    }

    @Override
    public StorageDomainResource getStorageDomainSubResource(String id) {
        return inject(new BackendStorageDomainResource(id, this));
    }

    private Response addDomain(VdcActionType action,
            StorageDomain model,
            StorageDomainStatic entity,
            Guid hostId,
            StorageServerConnections connection) {
        Response response = null;
        boolean isConnNew = false;
        if (connection.getstorage_type().isFileDomain() && StringUtils.isEmpty(connection.getid())) {
            isConnNew = true;
            connection.setid(addStorageServerConnection(connection, hostId));
        }
        entity.setStorage(connection.getid());
        if (action == VdcActionType.AddNFSStorageDomain || action == VdcActionType.AddPosixFsStorageDomain ||
                action == VdcActionType.AddGlusterFsStorageDomain) {
            org.ovirt.engine.core.common.businessentities.StorageDomain existing =
                    getExistingStorageDomain(hostId,
                            entity.getStorageType(),
                            entity.getStorageDomainType(),
                            connection);
            if (existing != null) {
                StorageDomainMapper.map(model, existing.getStorageStaticData());
                entity = existing.getStorageStaticData();
                action = VdcActionType.AddExistingFileStorageDomain;
            }
        }

        if (action != VdcActionType.AddExistingFileStorageDomain) {
            validateParameters(model, 2, "name");
        }

        try {
            response = performCreate(action, getAddParams(entity, hostId), ID_RESOLVER);
        } catch (WebFaultException e) {
            // cleanup of created connection
            if (isConnNew) {
                removeStorageServerConnection(connection, hostId);
            }
            throw e;
        }
        return response;
    }

    private Response addSAN(StorageDomain model, StorageType storageType, StorageDomainStatic entity, Guid hostId) {
        boolean overrideLuns = model.getStorage().isSetOverrideLuns() ? model.getStorage().isOverrideLuns() : false;

        return performCreate(VdcActionType.AddSANStorageDomain,
                getSanAddParams(entity,
                        hostId,
                        getLunIds(model.getStorage(), storageType, hostId),
                        overrideLuns),
                ID_RESOLVER);<commit_msg>restapi: Avoid having leftovers when adding NFS SD.<commit_after>package org.ovirt.engine.api.restapi.resource;

import static org.ovirt.engine.api.restapi.resource.BackendStorageDomainResource.getLinksToExclude;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.api.common.util.StatusUtils;
import org.ovirt.engine.api.model.LogicalUnit;
import org.ovirt.engine.api.model.Storage;
import org.ovirt.engine.api.model.StorageDomain;
import org.ovirt.engine.api.model.StorageDomainStatus;
import org.ovirt.engine.api.model.StorageDomains;
import org.ovirt.engine.api.model.VolumeGroup;
import org.ovirt.engine.api.resource.StorageDomainResource;
import org.ovirt.engine.api.resource.StorageDomainsResource;
import org.ovirt.engine.api.restapi.types.StorageDomainMapper;
import org.ovirt.engine.api.restapi.util.StorageDomainHelper;
import org.ovirt.engine.core.common.action.AddSANStorageDomainParameters;
import org.ovirt.engine.core.common.action.StorageDomainManagementParameter;
import org.ovirt.engine.core.common.action.StorageServerConnectionParametersBase;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.businessentities.StorageDomainSharedStatus;
import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
import org.ovirt.engine.core.common.businessentities.StorageDomainType;
import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VdsStatic;
import org.ovirt.engine.core.common.businessentities.storage.LUNs;
import org.ovirt.engine.core.common.businessentities.storage.StorageType;
import org.ovirt.engine.core.common.interfaces.SearchType;
import org.ovirt.engine.core.common.queries.GetDeviceListQueryParameters;
import org.ovirt.engine.core.common.queries.GetExistingStorageDomainListParameters;
import org.ovirt.engine.core.common.queries.GetLunsByVgIdParameters;
import org.ovirt.engine.core.common.queries.GetUnregisteredBlockStorageDomainsParameters;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.NameQueryParameters;
import org.ovirt.engine.core.common.queries.StorageServerConnectionQueryParametersBase;
import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
import org.ovirt.engine.core.common.queries.VdcQueryType;
import org.ovirt.engine.core.common.utils.Pair;
import org.ovirt.engine.core.compat.Guid;

public class BackendStorageDomainsResource
        extends AbstractBackendCollectionResource<StorageDomain, org.ovirt.engine.core.common.businessentities.StorageDomain>
        implements StorageDomainsResource {

    static final String[] SUB_COLLECTIONS = { "permissions", "files", "templates", "vms", "disks",
            "storageconnections", "images", "disksnapshots", "diskprofiles" };

    private final EntityIdResolver<Guid> ID_RESOLVER =
            new QueryIdResolver<Guid>(VdcQueryType.GetStorageDomainById, IdQueryParameters.class);

    public BackendStorageDomainsResource() {
        super(StorageDomain.class, org.ovirt.engine.core.common.businessentities.StorageDomain.class, SUB_COLLECTIONS);
    }

    @Override
    public StorageDomains list() {
        if (isFiltered())
            return mapCollection(getBackendCollection(VdcQueryType.GetAllStorageDomains,
                    new VdcQueryParametersBase(), SearchType.StorageDomain));
        else
            return mapCollection(getBackendCollection(SearchType.StorageDomain));
    }

    @Override
    public StorageDomainResource getStorageDomainSubResource(String id) {
        return inject(new BackendStorageDomainResource(id, this));
    }

    private Response addDomain(VdcActionType action,
            StorageDomain model,
            StorageDomainStatic entity,
            Guid hostId,
            StorageServerConnections connection) {
        Response response = null;
        boolean isConnNew = false;
        if (connection.getstorage_type().isFileDomain() && StringUtils.isEmpty(connection.getid())) {
            isConnNew = true;
            connection.setid(addStorageServerConnection(connection, hostId));
        }
        entity.setStorage(connection.getid());
        if (action == VdcActionType.AddNFSStorageDomain || action == VdcActionType.AddPosixFsStorageDomain ||
                action == VdcActionType.AddGlusterFsStorageDomain) {
            org.ovirt.engine.core.common.businessentities.StorageDomain existing =
                    getExistingStorageDomain(hostId,
                            entity.getStorageType(),
                            entity.getStorageDomainType(),
                            connection);
            if (existing != null) {
                StorageDomainMapper.map(model, existing.getStorageStaticData());
                entity = existing.getStorageStaticData();
                action = VdcActionType.AddExistingFileStorageDomain;
            }
        }

        try {
            if (action != VdcActionType.AddExistingFileStorageDomain) {
                validateParameters(model, 2, "name");
            }
            response = performCreate(action, getAddParams(entity, hostId), ID_RESOLVER);
        } catch (WebApplicationException e) {
            // cleanup of created connection
            if (isConnNew) {
                removeStorageServerConnection(connection, hostId);
            }
            throw e;
        }
        return response;
    }

    private Response addSAN(StorageDomain model, StorageType storageType, StorageDomainStatic entity, Guid hostId) {
        boolean overrideLuns = model.getStorage().isSetOverrideLuns() ? model.getStorage().isOverrideLuns() : false;

        return performCreate(VdcActionType.AddSANStorageDomain,
                getSanAddParams(entity,
                        hostId,
                        getLunIds(model.getStorage(), storageType, hostId),
                        overrideLuns),
                ID_RESOLVER);
{'fraction_non_alphanumeric': 0.09282768658526724, 'fraction_numerical': 0.00016172070833670252, 'mean_word_length': 3.567208271787297, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2568293', 'n_tokens_mistral': 3371, 'n_tokens_neox': 3338, 'n_words': 490}