/*
 * Copyright 2018 Google, 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.halyard.config.validate.v1.canary;

import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.halyard.config.model.v1.canary.AbstractCanaryAccount;
import com.netflix.spinnaker.halyard.config.model.v1.canary.AbstractCanaryServiceIntegration;
import com.netflix.spinnaker.halyard.config.model.v1.canary.Canary;
import com.netflix.spinnaker.halyard.config.model.v1.canary.aws.AwsCanaryServiceIntegration;
import com.netflix.spinnaker.halyard.config.model.v1.canary.google.GoogleCanaryServiceIntegration;
import com.netflix.spinnaker.halyard.config.model.v1.node.Validator;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder;
import com.netflix.spinnaker.halyard.config.validate.v1.canary.aws.AwsCanaryValidator;
import com.netflix.spinnaker.halyard.config.validate.v1.canary.google.GoogleCanaryValidator;
import com.netflix.spinnaker.halyard.core.problem.v1.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class CanaryValidator extends Validator<Canary> {

  @Autowired
  private String halyardVersion;

  @Autowired
  private Registry registry;

  @Override
  public void validate(ConfigProblemSetBuilder p, Canary n) {
    Set<String> accounts = new HashSet<>();

    for (AbstractCanaryServiceIntegration svc : n.getServiceIntegrations()) {
      for (AbstractCanaryAccount account : (List<AbstractCanaryAccount>)svc.getAccounts()) {
        if (accounts.contains(account.getName())) {
          p.addProblem(Problem.Severity.FATAL, "Canary account \"" + account.getName() + "\" appears more than once")
            .setRemediation("Change the name of the account in " + svc.getNodeName() + " service integration");
        } else {
          accounts.add(account.getName());
        }
      }
    }

    for (AbstractCanaryServiceIntegration s : n.getServiceIntegrations()) {
      if (s instanceof GoogleCanaryServiceIntegration) {
        new GoogleCanaryValidator().setHalyardVersion(halyardVersion).setRegistry(registry).validate(p, (GoogleCanaryServiceIntegration)s);
      } else if (s instanceof AwsCanaryServiceIntegration) {
        new AwsCanaryValidator().validate(p, (AwsCanaryServiceIntegration)s);
      }
    }
  }
}

@@ -2325,16 +2325,17 @@
 han once
+.
 %22)%0A     
@@ -2526,16 +2526,80 @@
 %0A    %7D%0A%0A
+    boolean configurationAndObjectStoresAreConfigured = false;%0A%0A
     for 
@@ -2735,276 +2735,1247 @@
     
-new GoogleCanaryValidator().setHalyardVersion(halyardVersion).setRegistry(registry).validate(p, (GoogleCanaryServiceIntegration)s);%0A      %7D else if (s instanceof AwsCanaryServiceIntegration) %7B%0A        new AwsCanaryValidator().validate(p, (AwsCanaryServiceIntegration)s
+GoogleCanaryServiceIntegration googleCanaryServiceIntegration = (GoogleCanaryServiceIntegration)s;%0A%0A        new GoogleCanaryValidator().setHalyardVersion(halyardVersion).setRegistry(registry).validate(p, googleCanaryServiceIntegration);%0A%0A        if (!configurationAndObjectStoresAreConfigured) %7B%0A          configurationAndObjectStoresAreConfigured = googleCanaryServiceIntegration.isEnabled() && googleCanaryServiceIntegration.isGcsEnabled();%0A        %7D%0A      %7D else if (s instanceof AwsCanaryServiceIntegration) %7B%0A        AwsCanaryServiceIntegration awsCanaryServiceIntegration = (AwsCanaryServiceIntegration)s;%0A%0A        new AwsCanaryValidator().validate(p, awsCanaryServiceIntegration);%0A%0A        if (!configurationAndObjectStoresAreConfigured) %7B%0A          configurationAndObjectStoresAreConfigured = awsCanaryServiceIntegration.isEnabled() && awsCanaryServiceIntegration.isS3Enabled();%0A        %7D%0A      %7D%0A    %7D%0A%0A    if (n.isEnabled()) %7B%0A      if (!configurationAndObjectStoresAreConfigured) %7B%0A        p.addProblem(Problem.Severity.WARNING, %22There is no account of type CONFIGURATION_STORE and OBJECT_STORE configured.%22)%0A          .setRemediation(%22Enable GCS or S3 and ensure that the relevant Google or AWS canary account is also enabled.%22
 );%0A 

Add validation so warning is issued if no CONFIGURATION/OBJECT_STORE is configured. (#979)