/*
 * Copyright 2017 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.gate.services;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.spinnaker.gate.services.commands.HystrixFactory;
import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector;
import com.netflix.spinnaker.gate.services.internal.IgorService;
import groovy.transform.CompileStatic;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import retrofit.client.Response;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@CompileStatic
@Component
public class ArtifactService {
  private static final String GROUP = "artifacts";

  private ClouddriverServiceSelector clouddriverServiceSelector;
  private IgorService igorService;

  @Autowired
  public ArtifactService(ClouddriverServiceSelector clouddriverServiceSelector, IgorService igorService) {
    this.clouddriverServiceSelector = clouddriverServiceSelector;
    this.igorService = igorService;
  }

  private static HystrixCommand<List<Map>> mapListCommand(String type, Callable<List<Map>> work) {
    return HystrixFactory.newListCommand(GROUP, type, work);
  }

  private static HystrixCommand<List<String>> stringListCommand(String type, Callable<List<String>> work) {
    return HystrixFactory.newListCommand(GROUP, type, work);
  }

  private static HystrixCommand<Void> voidCommand(String type, Callable<Void> work) {
    return HystrixFactory.newVoidCommand(GROUP, type, work);
  }

  public List<Map> getArtifactCredentials(String selectorKey) {
    return mapListCommand("artifactCredentials",
      clouddriverServiceSelector.select(selectorKey)::getArtifactCredentials)
      .execute();
  }

  public List<String> getArtifactNames(String selectorKey, String accountName, String type) {
    return stringListCommand("artifactNames",
      () -> clouddriverServiceSelector.select(selectorKey).getArtifactNames(accountName, type))
      .execute();
  }

  public List<String> getArtifactVersions(String selectorKey, String accountName, String type, String artifactName) {
    return stringListCommand("artifactVersions",
      () -> clouddriverServiceSelector.select(selectorKey).getArtifactVersions(accountName, type, artifactName))
      .execute();
  }

  public Void getArtifactContents(String selectorKey, Map<String, String> artifact, OutputStream outputStream) {
    return voidCommand("artifactContents", () -> {
      Response contentResponse = clouddriverServiceSelector.select(selectorKey).getArtifactContent(artifact);
      IOUtils.copy(contentResponse.getBody().in(), outputStream);
      return null;
    }).execute();
  }

  public List<String> getVersionsOfArtifactForProvider(String provider, String packageName) {
    return stringListCommand("artifactVersionsByProvider",
      () -> igorService.getArtifactVersions(provider, packageName))
      .execute();
  }
}

@@ -1188,16 +1188,43 @@
 il.Map;%0A
+import java.util.Optional;%0A
 import j
@@ -1438,16 +1438,25 @@
 private 
+Optional%3C
 IgorServ
@@ -1450,32 +1450,33 @@
 onal%3CIgorService
+%3E
  igorService;%0A%0A 
@@ -1474,16 +1474,17 @@
 rvice;%0A%0A
+%0A
   @Autow
@@ -1568,16 +1568,25 @@
 lector, 
+Optional%3C
 IgorServ
@@ -1588,16 +1588,17 @@
 rService
+%3E
  igorSer
@@ -3447,32 +3447,177 @@
  packageName) %7B%0A
+    if (!igorService.isPresent()) %7B%0A      throw new IllegalStateException(%22Cannot fetch artifact versions because Igor is not enabled.%22);%0A    %7D%0A%0A
     return strin
@@ -3683,16 +3683,22 @@
 Service.
+get().
 getArtif

Make igor service optional (#773)