/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.cloud.skipper.server.deployer;

import java.util.Map;
import java.util.TreeMap;

import org.springframework.cloud.deployer.resource.support.DelegatingResourceLoader;
import org.springframework.cloud.deployer.spi.app.AppDeployer;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.skipper.server.domain.SpringBootAppKind;
import org.springframework.cloud.skipper.server.domain.SpringBootAppSpec;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

/**
 * Utilities for managing AppDeploymentRequests
 * @author Mark Pollack
 */
public class AppDeploymentRequestFactory {

	private final DelegatingResourceLoader delegatingResourceLoader;

	public AppDeploymentRequestFactory(DelegatingResourceLoader delegatingResourceLoader) {
		this.delegatingResourceLoader = delegatingResourceLoader;
	}

	public AppDeploymentRequest createAppDeploymentRequest(SpringBootAppKind springBootAppKind, String releaseName,
			String version) {
		SpringBootAppSpec spec = springBootAppKind.getSpec();
		Map<String, String> applicationProperties = new TreeMap<>();
		if (spec.getApplicationProperties() != null) {
			applicationProperties.putAll(spec.getApplicationProperties());
		}
		AppDefinition appDefinition = new AppDefinition(springBootAppKind.getApplicationName(), applicationProperties);

		Assert.hasText(spec.getResource(), "Package template must define a resource uri");
		Resource resource = delegatingResourceLoader.getResource(spec.getResource());

		Map<String, String> deploymentProperties = new TreeMap<>();
		if (spec.getDeploymentProperties() != null) {
			deploymentProperties.putAll(spec.getDeploymentProperties());
		}
		Map<String, String> metadata = springBootAppKind.getMetadata();
		if (metadata.containsKey("count")) {
			deploymentProperties.put(AppDeployer.COUNT_PROPERTY_KEY, String.valueOf(metadata.get("count")));
		}

		// TODO fix setting of group property with new version, will mess up consumer groups.
		deploymentProperties.put(AppDeployer.GROUP_PROPERTY_KEY, releaseName + "-v" + version);

		AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(appDefinition, resource,
				deploymentProperties);
		return appDeploymentRequest;
	}

}

@@ -1250,21 +1250,15 @@
 %0A * 
-Utilities f
+Fact
 or
+y
  man
@@ -1263,16 +1263,23 @@
 anaging 
+%7B@link 
 AppDeplo
@@ -1290,17 +1290,22 @@
 tRequest
-s
+%7Ds.%0A *
 %0A * @aut
@@ -1321,16 +1321,45 @@
 Pollack%0A
+ * @author Janne Valkealahti%0A
  */%0Apubl
@@ -1470,159 +1470,614 @@
 ;%0A%0A%09
-public AppDeploymentRequestFactory(DelegatingResourceLoader delegatingResourceLoader) %7B%0A%09%09this.delegatingResourceLoader = delegatingResourceLoader;%0A%09%7D%0A
+/**%0A%09 * Instantiates a new %7B@code AppDeploymentRequestFactory%7D.%0A%09 *%0A%09 * @param delegatingResourceLoader the delegating resource loader%0A%09 */%0A%09public AppDeploymentRequestFactory(DelegatingResourceLoader delegatingResourceLoader) %7B%0A%09%09Assert.notNull(delegatingResourceLoader, %22'delegatingResourceLoader' must be set%22);%0A%09%09this.delegatingResourceLoader = delegatingResourceLoader;%0A%09%7D%0A%0A%09/**%0A%09 * Creates an %7B@link AppDeploymentRequest%7D.%0A%09 *%0A%09 * @param springBootAppKind the boot app kind%0A%09 * @param releaseName the release name%0A%09 * @param version the release version%0A%09 * @return a created AppDeploymentRequest%0A%09 */
 %0A%09pu
@@ -2445,16 +2445,212 @@
 ));%0A%09%09%7D%0A
+%09%09// we need to keep group name same for consumer groups not getting broken, but%0A%09%09// app name needs to differentiate as otherwise it may result same deployment id and%0A%09%09// failure on a deployer.%0A
 %09%09AppDef
@@ -2729,16 +2729,33 @@
 onName()
+ + %22-v%22 + version
 , applic
@@ -3331,96 +3331,8 @@
 %09%7D%0A%0A
-%09%09// TODO fix setting of group property with new version, will mess up consumer groups.%0A
 %09%09de
@@ -3397,33 +3397,16 @@
 easeName
- + %22-v%22 + version
 );%0A%0A%09%09Ap

Fix assignment of groupname when deploying