package cz.xtf.manipulation;

import cz.xtf.TestConfiguration;
import cz.xtf.UsageRecorder;
import cz.xtf.openshift.OpenshiftUtil;
import cz.xtf.openshift.builder.ImageStreamBuilder;
import cz.xtf.openshift.imagestream.ImageStreamRequest;
import io.fabric8.openshift.api.model.ImageStream;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Util class responsible for ImageStream manipulation.
 */
@Slf4j
public final class ImageStreamProcessor {

	public static final String IS_NAMESPACE = TestConfiguration.imageStreamNamespace();

	private ImageStreamProcessor() {
		// util class, do not initialize
	}

	/**
	 * @param request the request for Image Stream creation
	 * @see #createImageStream(String, String, List)
	 */
	public static void createImageStream(ImageStreamRequest request) {
		createImageStream(request.getName(), request.getImage(), request.getTags());
	}

	/**
	 * @param name the name of ImageStream to create
	 * @param fromImage the image from which will be the ImageStream created
	 * @param customTags list of custom tag to mark the image
	 * @see #createImageStream(String, String, String...)
	 */
	public static void createImageStream(String name, String fromImage, List<String> customTags) {
		createImageStream(name, fromImage, customTags.toArray(new String[customTags.size()]));
	}

	/**
	 * Creates (removes and recreates) new ImageStream on OpenShift in the {@link #IS_NAMESPACE} namespace.
	 *
	 * @param name the name of ImageStream to create
	 * @param fromImage the image from which will be the ImageStream created
	 * @param customTags list of custom tag to mark the image
	 */
	public static void createImageStream(String name, String fromImage, String... customTags) {
		UsageRecorder.recordImage(name, fromImage);

		final OpenshiftUtil openshift = OpenshiftUtil.getInstance();
		final ImageStreamBuilder isBuilder = new ImageStreamBuilder(name).insecure();
		for (final String tag : customTags) {
			isBuilder.addTag(tag, fromImage);
		}
		final ImageStream is = isBuilder.build();

		log.info("action=create-image-stream status=START name={} image={} tags={}", name, fromImage, customTags);
		openshift.deleteImageStream(is, IS_NAMESPACE);
		openshift.createImageStream(is, IS_NAMESPACE);
		log.info("action=create-image-stream status=FINISH name={} image={} tags={}", name, fromImage, customTags);
	}
}

@@ -113,25 +113,64 @@
 ift.Open
-s
+ShiftUtil;%0Aimport cz.xtf.openshift.OpenS
 hiftUtil
 ;%0Aimport
@@ -153,32 +153,33 @@
 ft.OpenShiftUtil
+s
 ;%0Aimport cz.xtf.
@@ -1810,25 +1810,25 @@
 %09%09final Open
-s
+S
 hiftUtil ope
@@ -1844,17 +1844,17 @@
 Open
-s
+S
 hiftUtil
 .get
@@ -1853,21 +1853,28 @@
 Util
-.getInstance(
+s.admin(IS_NAMESPACE
 );%0A%09
@@ -2215,38 +2215,24 @@
 ageStream(is
-, IS_NAMESPACE
 );%0A%09%09openshi
@@ -2258,22 +2258,8 @@
 m(is
-, IS_NAMESPACE
 );%0A%09

fix recreate of IS in ImageStreamProcessor