/*
 * Copyright 2019 Pivotal, Inc.
 *
 * 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 com.netflix.spinnaker.kork.configserver.autoconfig;

import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class RemoteConfigSourceConfigured implements Condition {
  @Override
  public boolean matches(ConditionContext context, @NotNull AnnotatedTypeMetadata metadata) {
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beanFactory != null) {
      return
      // beans added via Spring Cloud Config profile activation
      beanFactory.containsBean("defaultEnvironmentRepository")
          || beanFactory.containsBean("vaultEnvironmentRepository")
          || beanFactory.containsBean("jdbcEnvironmentRepository")
          || beanFactory.containsBean("credhubEnvironmentRepository")
          // beans added via composite Spring Cloud Config profile
          || beanFactory.containsBean("git-env-repo0")
          || beanFactory.containsBean("vault-env-repo0")
          || beanFactory.containsBean("jdbc-env-repo0")
          || beanFactory.containsBean("credhub-env-repo0");
    }
    return false;
  }
}

@@ -697,16 +697,61 @@
 otNull;%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -823,16 +823,16 @@
 actory;%0A
-
 import o
@@ -944,16 +944,65 @@
 ontext;%0A
+import org.springframework.core.env.Environment;%0A
 import o
@@ -1119,16 +1119,109 @@
 ition %7B%0A
+  private static final String DEFAULT_REMOTE_REPO_TYPES = %22default,git,vault,jdbc,credhub%22;%0A%0A
   @Overr
@@ -1435,423 +1435,296 @@
     
-return%0A      // beans added via Spring Cloud Config profile activation%0A      beanFactory.containsBean(%22defaultEnvironmentRepository%22)%0A          %7C%7C beanFactory.containsBean(%22vaultEnvironmentRepository%22)%0A          %7C%7C beanFactory.containsBean(%22jdbcEnvironmentRepository%22)%0A          %7C%7C beanFactory.containsBean(%22credhubEnvironmentRepository%22)%0A          // beans added via composite Sp
+Environment environment = context.getEnvironment();%0A      String remoteRepoTypes =%0A          environment.getProperty(%0A              %22spring.cloud.config.remote-repo-types%22, DEFAULT_REMOTE_REPO_TYPES);%0A%0A      for (String remoteRepoType : St
 ring
- Cloud Config profile%0A 
+Utils.split(remoteRepoTypes, ',')) %7B%0A
         
  %7C%7C 
@@ -1711,36 +1711,36 @@
 ',')) %7B%0A        
- %7C%7C 
+if (
 beanFactory.cont
@@ -1752,25 +1752,52 @@
 ean(
-%22git-env-repo0
+remoteRepoType + %22EnvironmentRepository
 %22)%0A
+  
     
@@ -1834,14 +1834,26 @@
 ean(
-%22vault
+remoteRepoType + %22
 -env
@@ -1852,32 +1852,35 @@
  + %22-env-repo0%22)
+) %7B
 %0A          %7C%7C be
@@ -1878,113 +1878,38 @@
     
-%7C%7C beanFactory.containsBean(%22jdbc-env-repo0%22)%0A          %7C%7C beanFactory.containsBean(%22credhub-env-repo0%22);
+return true;%0A        %7D%0A      %7D
 %0A   

Allow native Config Server repos via customization (#413)