/*
 * Copyright 2016 Netflix, 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.orca.clouddriver.tasks.image;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.RetryableTask;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.clouddriver.utils.CloudProviderAware;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WaitForUpsertedImageTagsTask implements RetryableTask, CloudProviderAware {
  @Autowired
  List<ImageTagger> imageTaggers;

  @Override
  public TaskResult execute(Stage stage) {
    String cloudProvider = getCloudProvider(stage);

    ImageTagger tagger = imageTaggers.stream()
      .filter(it -> it.getCloudProvider().equalsIgnoreCase(cloudProvider))
      .findFirst()
      .orElseThrow(() -> new IllegalStateException("ImageTagger not found for cloudProvider " + cloudProvider));

    StageData stageData = (StageData) stage.mapTo(StageData.class);
    return new TaskResult(
      tagger.areImagesTagged(stageData.targets, stage) ? ExecutionStatus.SUCCEEDED : ExecutionStatus.RUNNING
    );
  }

  @Override
  public long getBackoffPeriod() {
    return TimeUnit.SECONDS.toMillis(30);
  }

  @Override
  public long getTimeout() {
    return TimeUnit.MINUTES.toMillis(10);
  }

  static class StageData {
    @JsonProperty
    Collection<ImageTagger.Image> targets;
  }
}

@@ -1069,16 +1069,75 @@
 .Stage;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import o
@@ -1389,16 +1389,122 @@
 ggers;%0A%0A
+  @Value(%22$%7Btasks.waitForUpsertedImageTagsTimeout:600%7D%22)%0A  private long waitForUpsertedImageTagsTimeout;%0A%0A
   @Overr
@@ -2233,14 +2233,14 @@
 nit.
-MINUTE
+SECOND
 S.to
@@ -2250,10 +2250,44 @@
 lis(
-10
+this.waitForUpsertedImageTagsTimeout
 );%0A 

Add configurable timeout to wait for upserted image tag task (#1870)