package com.emc.ecs.serviceBroker.service;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.cloudfoundry.community.servicebroker.exception.ServiceBrokerException;
import org.cloudfoundry.community.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.cloudfoundry.community.servicebroker.model.ServiceInstance;
import org.cloudfoundry.community.servicebroker.model.ServiceInstanceBinding;
import org.cloudfoundry.community.servicebroker.service.ServiceInstanceBindingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emc.ecs.serviceBroker.EcsManagementClientException;
import com.emc.ecs.serviceBroker.EcsManagementResourceNotFoundException;
import com.emc.ecs.serviceBroker.EcsService;
import com.emc.ecs.serviceBroker.model.UserSecretKey;
import com.emc.ecs.serviceBroker.repository.ServiceInstanceBindingRepository;

@Service
public class BucketServiceInstanceBindingService implements ServiceInstanceBindingService {

	private EcsService ecs;
	private ServiceInstanceBindingRepository repository;
	
	@Autowired
	public BucketServiceInstanceBindingService(EcsService ecs) throws EcsManagementClientException, EcsManagementResourceNotFoundException, URISyntaxException {
		this.ecs = ecs;
		this.repository = new ServiceInstanceBindingRepository(ecs);
	}

	@Override
	public ServiceInstanceBinding createServiceInstanceBinding(String bindingId, ServiceInstance serviceInstance, String serviceId,
			String planId, String appGuid) throws ServiceInstanceBindingExistsException, ServiceBrokerException {
		UserSecretKey userSecret;
		ServiceInstanceBinding binding = new ServiceInstanceBinding(bindingId, serviceId, null, null, appGuid);
		Map<String,Object> credentials = new HashMap<String,Object>();
		credentials.put("accessKey", bindingId);
		credentials.put("bucket", serviceId);
		try {
			if (ecs.userExists(bindingId)) throw new ServiceInstanceBindingExistsException(binding);
			userSecret = ecs.createUser(bindingId);
			ecs.addUserToBucket(serviceId, bindingId);
			repository.save(binding);
			credentials.put("secretKey", userSecret.getSecretKey());
			credentials.put("endpoint", ecs.getObjectEndpoint());
		} catch (Exception e) {
			throw new ServiceBrokerException(e.getMessage());
		}
		binding = new ServiceInstanceBinding(bindingId, serviceId, credentials, null, appGuid);
		return binding;
	}

	@Override
	public ServiceInstanceBinding deleteServiceInstanceBinding(String bindingId, ServiceInstance instance, String serviceId,
			String planId) throws ServiceBrokerException {
		try {
			ServiceInstanceBinding binding = repository.find(bindingId);
			ecs.deleteUser(bindingId);			
			repository.delete(bindingId);
			return binding;
		} catch (Exception e) {
			throw new ServiceBrokerException(e.getMessage());
		}
	}
}

@@ -1673,16 +1673,63 @@
 Secret;%0A
+%09%09String instanceId = serviceInstance.getId();%0A
 %09%09Servic
@@ -1789,29 +1789,30 @@
 (bindingId, 
-servi
+instan
 ceId, null, 
@@ -1963,21 +1963,22 @@
 ucket%22, 
-servi
+instan
 ceId);%0A%09
@@ -2142,21 +2142,22 @@
 oBucket(
-servi
+instan
 ceId, bi
@@ -2445,21 +2445,22 @@
 dingId, 
-servi
+instan
 ceId, cr
@@ -2792,19 +2792,66 @@
 dingId);
+%0A
 %09%09%09
+// TODO Delete User ACLs as a cleanup exercise
 %0A%09%09%09repo

Fix instanceId for serviceId mixup