package com.emc.ecs.serviceBroker.config;

import java.net.URL;

import org.cloudfoundry.community.servicebroker.model.BrokerApiVersion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.emc.ecs.managementClient.Connection;
import com.emc.ecs.serviceBroker.repository.EcsRepositoryCredentials;

@Configuration
@ComponentScan(basePackages = "com.emc.ecs.serviceBroker")
public class BrokerConfig {
	
	// TODO support multiple Cloud Foundry instances per http://docs.cloudfoundry.org/services/supporting-multiple-cf-instances.html
	// TODO support syslog drain URL
	
	@Value("${endpoint}")
	private String endpoint;
	
	@Value("${namespace}")
	private String namespace;
	
	@Value("${replicationGroup}")
	private String replicationGroup;
	
	@Value("${repositoryUser:user}")
	private String repositoryUser;
	
	@Value("${managementPort:4443}")
	private String managementPort;
	
	@Value("${username:root}")
	private String username;
	
	@Value("${password:ChangeMe}")
	private String password;
	
	@Value("${repositoryBucket:repository}")
	private String repositoryBucket;

	@Value("${repositoryEndpoint}")
	private String repositoryEndpoint;
	
	@Value("${prefix:ecs-cf-broker-}")
	private String prefix;
	
	@Value("${brokerApiVersion:2.8}")
	private String brokerApiVersion;

	@Bean
	public Connection ecsConnection() {
		URL certificate = getClass().getClassLoader().getResource("localhost.pem");
		return new Connection("https://" + endpoint + ":" + managementPort, username, password, certificate);
	}
	
	@Bean
	public BrokerApiVersion brokerApiVersion() {
		return new BrokerApiVersion(brokerApiVersion);
	}
	
	@Bean
	public EcsRepositoryCredentials getRepositoryCredentials() {
		EcsRepositoryCredentials creds = new EcsRepositoryCredentials(
				repositoryBucket, repositoryUser, namespace, replicationGroup,
				prefix);
		if (repositoryEndpoint != null) creds.setEndpoint(repositoryEndpoint);
		return creds;
	}
}
@@ -189,16 +189,92 @@
 .Value;%0A
+import org.springframework.boot.context.properties.ConfigurationProperties;%0A
 import o
@@ -840,17 +840,41 @@
 alue(%22$%7B
-e
+broker-config.managementE
 ndpoint%7D
@@ -892,17 +892,27 @@
  String 
-e
+managementE
 ndpoint;
@@ -925,16 +925,30 @@
 alue(%22$%7B
+broker-config.
 namespac
@@ -992,16 +992,30 @@
 alue(%22$%7B
+broker-config.
 replicat
@@ -1065,32 +1065,46 @@
 p;%0A%09%0A%09@Value(%22$%7B
+broker-config.
 repositoryUser:u
@@ -1159,76 +1159,22 @@
 (%22$%7B
-managementPort:4443%7D%22)%0A%09private String managementPort;%0A%09%0A%09@Value(%22$%7B
+broker-config.
 user
@@ -1225,16 +1225,30 @@
 alue(%22$%7B
+broker-config.
 password
@@ -1291,32 +1291,46 @@
 d;%0A%09%0A%09@Value(%22$%7B
+broker-config.
 repositoryBucket
@@ -1390,16 +1390,30 @@
 alue(%22$%7B
+broker-config.
 reposito
@@ -1475,16 +1475,30 @@
 alue(%22$%7B
+broker-config.
 prefix:e
@@ -1553,25 +1553,33 @@
 ue(%22$%7Bbroker
-A
+-config.a
 piVersion:2.
@@ -1768,38 +1768,8 @@
 ion(
-%22https://%22 + endpoint + %22:%22 + 
 mana
@@ -1778,11 +1778,15 @@
 ment
-Por
+Endpoin
 t, u

refactor config vars & names