<commit_before>package com.emc.ecs.cloudfoundry.broker.service;

import com.emc.ecs.cloudfoundry.broker.EcsManagementClientException;
import com.emc.ecs.cloudfoundry.broker.EcsManagementResourceNotFoundException;
import com.emc.ecs.cloudfoundry.broker.config.CatalogConfig;
import com.emc.ecs.cloudfoundry.broker.model.PlanProxy;
import com.emc.ecs.cloudfoundry.broker.model.ServiceDefinitionProxy;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceBinding;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceBindingRepository;
import com.emc.ecs.management.sdk.model.UserSecretKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.springframework.cloud.servicebroker.model.*;
import org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService;
import org.springframework.stereotype.Service;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class EcsServiceInstanceBindingService
        implements ServiceInstanceBindingService {
    private static final String NO_SERVICE_MATCHING_TYPE = "No service matching type: ";
    private static final String SERVICE_TYPE = "service-type";
    private static final String NAMESPACE = "namespace";
    private static final String BUCKET = "bucket";
    private static final String VOLUME_DRIVER = "nfsv3driver";
    private static final String DEFAULT_MOUNT = "/var/vcap/data";
    public static final String MOUNT = "mount";

    static final Logger LOG = LoggerFactory.getLogger(EcsServiceInstanceBindingService.class);

    @Autowired
    private EcsService ecs;

    @Autowired
    private CatalogConfig catalog;

    @Autowired
    private ServiceInstanceBindingRepository repository;

    public EcsServiceInstanceBindingService()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        super();
    }

    @Override
    public CreateServiceInstanceBindingResponse createServiceInstanceBinding(
            CreateServiceInstanceBindingRequest request)
            throws ServiceInstanceBindingExistsException,
            ServiceBrokerException {
        LOG.error("Creating service instance binding response");
        String instanceId = request.getServiceInstanceId();
        String bindingId = request.getBindingId();
        String serviceDefinitionId = request.getServiceDefinitionId();
        ServiceInstanceBinding binding = new ServiceInstanceBinding(request);
        Map<String, Object> credentials = new HashMap<>();
        Map<String, Object> parameters = request.getParameters();
        credentials.put("accessKey", ecs.prefix(bindingId));
        try {
            if (ecs.userExists(bindingId))
                throw new ServiceInstanceBindingExistsException(instanceId,
                        bindingId);
            binding.setBindingId(bindingId);

            ServiceDefinitionProxy service = ecs
                    .lookupServiceDefinition(serviceDefinitionId);
            PlanProxy plan = service.findPlan(request.getPlanId());

            String serviceType = (String) service.getServiceSettings()
                    .get(SERVICE_TYPE);

            UserSecretKey userSecret;
            CreateServiceInstanceAppBindingResponse resp =  new CreateServiceInstanceAppBindingResponse();
            String s3Url;
            String endpoint;

            if (NAMESPACE.equals(serviceType)) {
                userSecret = ecs.createUser(bindingId, instanceId);
                LOG.info("Created user with binding id: " + bindingId + ", instanceId: " + instanceId);
                endpoint = ecs.getNamespaceURL(ecs.prefix(instanceId), service, plan,
                        Optional.ofNullable(parameters));
                String userInfo = bindingId + ":" + userSecret.getSecretKey();
                URL baseUrl = new URL(endpoint);
                s3Url = new StringBuilder(baseUrl.getProtocol())
                        .append("://")
                        .append(ecs.prefix(userInfo))
                        .append("@")
                        .append(baseUrl.getHost())
                        .append(":")
                        .append(baseUrl.getPort())
                        .toString();
            } else if (BUCKET.equals(serviceType)) {
                endpoint = ecs.getObjectEndpoint();
                URL baseUrl = new URL(endpoint);
                userSecret = ecs.createUser(bindingId);
                LOG.info("Created user with binding id: " + bindingId);
                List<String> permissions = null;
                Boolean hasMounts = ecs.getBucketFileEnabled(instanceId);
                String export = "";
                if (parameters != null) {
                    permissions = (List<String>) parameters.get("permissions");
                    export = (String) parameters.get("export");
                }
                if (permissions != null) {
                    ecs.addUserToBucket(instanceId, bindingId, permissions);
                    LOG.info("Added user to bucket");
                } else {
                    ecs.addUserToBucket(instanceId, bindingId);
                    LOG.info("Added user to bucket");
                }
                if (hasMounts) {
                    int unixUid = (int)(2000 + System.currentTimeMillis() % 8000);
                    while (true) {
                        try {
                            ecs.createUserMap(bindingId, unixUid);
                            LOG.info("Created user map binding id: " + bindingId + ", unixUid: " + unixUid);
                            break;
                        } catch (EcsManagementClientException e) {
                            if (e.getMessage().contains("Bad request body (1013)")) {
                                unixUid++;
                            } else {
                                throw e;
                            }
                        }
                    }

                    String host = ecs.getNfsMountHost();
                    if (host == null || host.isEmpty()) {
                        host = baseUrl.getHost();
                    }
                    String volumeGUID = UUID.randomUUID().toString();
                    String absoluteExportPath = ecs.addExportToBucket(instanceId, export);
                    Map<String, Object> opts = new HashMap<>();
                    String nfsUrl = new StringBuilder("nfs://")
                                .append(host)
                                .append(absoluteExportPath)
                                .toString();
                    opts.put("source", nfsUrl);<commit_msg>additional logging around add-export API calls<commit_after>package com.emc.ecs.cloudfoundry.broker.service;

import com.emc.ecs.cloudfoundry.broker.EcsManagementClientException;
import com.emc.ecs.cloudfoundry.broker.EcsManagementResourceNotFoundException;
import com.emc.ecs.cloudfoundry.broker.config.CatalogConfig;
import com.emc.ecs.cloudfoundry.broker.model.PlanProxy;
import com.emc.ecs.cloudfoundry.broker.model.ServiceDefinitionProxy;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceBinding;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceBindingRepository;
import com.emc.ecs.management.sdk.model.UserSecretKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.springframework.cloud.servicebroker.model.*;
import org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService;
import org.springframework.stereotype.Service;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class EcsServiceInstanceBindingService
        implements ServiceInstanceBindingService {
    private static final String NO_SERVICE_MATCHING_TYPE = "No service matching type: ";
    private static final String SERVICE_TYPE = "service-type";
    private static final String NAMESPACE = "namespace";
    private static final String BUCKET = "bucket";
    private static final String VOLUME_DRIVER = "nfsv3driver";
    private static final String DEFAULT_MOUNT = "/var/vcap/data";
    public static final String MOUNT = "mount";

    static final Logger LOG = LoggerFactory.getLogger(EcsServiceInstanceBindingService.class);

    @Autowired
    private EcsService ecs;

    @Autowired
    private CatalogConfig catalog;

    @Autowired
    private ServiceInstanceBindingRepository repository;

    public EcsServiceInstanceBindingService()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        super();
    }

    @Override
    public CreateServiceInstanceBindingResponse createServiceInstanceBinding(
            CreateServiceInstanceBindingRequest request)
            throws ServiceInstanceBindingExistsException,
            ServiceBrokerException {
        LOG.error("Creating service instance binding response");
        String instanceId = request.getServiceInstanceId();
        String bindingId = request.getBindingId();
        String serviceDefinitionId = request.getServiceDefinitionId();
        ServiceInstanceBinding binding = new ServiceInstanceBinding(request);
        Map<String, Object> credentials = new HashMap<>();
        Map<String, Object> parameters = request.getParameters();
        credentials.put("accessKey", ecs.prefix(bindingId));
        try {
            if (ecs.userExists(bindingId))
                throw new ServiceInstanceBindingExistsException(instanceId,
                        bindingId);
            binding.setBindingId(bindingId);

            ServiceDefinitionProxy service = ecs
                    .lookupServiceDefinition(serviceDefinitionId);
            PlanProxy plan = service.findPlan(request.getPlanId());

            String serviceType = (String) service.getServiceSettings()
                    .get(SERVICE_TYPE);

            UserSecretKey userSecret;
            CreateServiceInstanceAppBindingResponse resp =  new CreateServiceInstanceAppBindingResponse();
            String s3Url;
            String endpoint;

            if (NAMESPACE.equals(serviceType)) {
                userSecret = ecs.createUser(bindingId, instanceId);
                LOG.info("Created user with binding id: " + bindingId + ", instanceId: " + instanceId);
                endpoint = ecs.getNamespaceURL(ecs.prefix(instanceId), service, plan,
                        Optional.ofNullable(parameters));
                String userInfo = bindingId + ":" + userSecret.getSecretKey();
                URL baseUrl = new URL(endpoint);
                s3Url = new StringBuilder(baseUrl.getProtocol())
                        .append("://")
                        .append(ecs.prefix(userInfo))
                        .append("@")
                        .append(baseUrl.getHost())
                        .append(":")
                        .append(baseUrl.getPort())
                        .toString();
            } else if (BUCKET.equals(serviceType)) {
                endpoint = ecs.getObjectEndpoint();
                URL baseUrl = new URL(endpoint);
                userSecret = ecs.createUser(bindingId);
                LOG.info("Created user with binding id: " + bindingId);
                List<String> permissions = null;
                Boolean hasMounts = ecs.getBucketFileEnabled(instanceId);
                String export = "";
                if (parameters != null) {
                    permissions = (List<String>) parameters.get("permissions");
                    export = (String) parameters.get("export");
                }
                if (permissions != null) {
                    ecs.addUserToBucket(instanceId, bindingId, permissions);
                    LOG.info("Added user to bucket");
                } else {
                    ecs.addUserToBucket(instanceId, bindingId);
                    LOG.info("Added user to bucket");
                }
                if (hasMounts) {
                    int unixUid = (int)(2000 + System.currentTimeMillis() % 8000);
                    while (true) {
                        try {
                            ecs.createUserMap(bindingId, unixUid);
                            LOG.info("Created user map binding id: " + bindingId + ", unixUid: " + unixUid);
                            break;
                        } catch (EcsManagementClientException e) {
                            if (e.getMessage().contains("Bad request body (1013)")) {
                                unixUid++;
                            } else {
                                throw e;
                            }
                        }
                    }

                    String host = ecs.getNfsMountHost();
                    if (host == null || host.isEmpty()) {
                        host = baseUrl.getHost();
                    }
                    String volumeGUID = UUID.randomUUID().toString();
                    LOG.info("Adding export:  " + export + " to bucket: " + instanceId);
                    String absoluteExportPath = ecs.addExportToBucket(instanceId, export);
                    LOG.info("export added.");
                    Map<String, Object> opts = new HashMap<>();
                    String nfsUrl = new StringBuilder("nfs://")
                                .append(host)
                                .append(absoluteExportPath)
                                .toString();
                    opts.put("source", nfsUrl);
{'fraction_non_alphanumeric': 0.08820418811252909, 'fraction_numerical': 0.002397236127758584, 'mean_word_length': 2.040514469453376, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5532439', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3337, 'n_words': 690}