class DistroXV1RequestToStackV4RequestConverter {

    public StackV4Request convertAsTemplate(DistroXV1Request source) {
        StackV4Request request = new StackV4Request();
        DetailedEnvironmentResponse environment = null;
        if (source.getEnvironmentName() != null) {
            environment = environmentClientService.getByName(source.getEnvironmentName());
            if (environment != null) {
                if (environment.getEnvironmentStatus() != EnvironmentStatus.AVAILABLE) {
                    throw new BadRequestException(String.format("Environment state is %s instead of AVAILABLE", environment.getEnvironmentStatus()));
                }
                request.setCloudPlatform(getCloudPlatform(environment));
                request.setEnvironmentCrn(environment.getCrn());
            }
        }
        SdxClusterResponse sdxClusterResponse = getSdxClusterResponse(environment);
        request.setName(source.getName());
        request.setType(StackType.WORKLOAD);
        request.setImage(getIfNotNull(source.getImage(), imageConverter::convert));
        request.setCluster(getIfNotNull(source, clusterConverter::convert));
        DetailedEnvironmentResponse environmentRef = environment;
        request.setInstanceGroups(getIfNotNull(source.getInstanceGroups(), instanceGroups -> instanceGroupConverter.convertTo(instanceGroups, environmentRef)));
        if (environment != null) {
            request.setNetwork(getNetwork(source.getNetwork(), environment));
        }
        request.setAws(getIfNotNull(source.getAws(), stackParameterConverter::convert));
        request.setAzure(getIfNotNull(source.getAzure(), stackParameterConverter::convert));
        request.setGcp(getIfNotNull(source.getGcp(), stackParameterConverter::convert));
        request.setYarn(getYarnProperties(source, environment));
        request.setInputs(source.getInputs());
        request.setTags(getIfNotNull(source.getTags(), this::getTags));
        request.setSharedService(getIfNotNull(sdxClusterResponse, sdx -> sdxConverter.getSharedServiceV4Request(sdx)));
        request.setTimeToLive(source.getTimeToLive());
        request.setTelemetry(getTelemetryRequest(source, environment, sdxClusterResponse));
        request.setExternalDatabase(getIfNotNull(source.getExternalDatabase(), databaseRequestConverter::convert));
        return request;
    }

}