/**
 * Copyright (C) 2015 Red Hat, 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 io.fabric8.istio.api.examples.v1beta1;

import io.fabric8.istio.api.networking.v1beta1.Destination;
import io.fabric8.istio.api.networking.v1beta1.HTTPMatchRequestBuilder;
import io.fabric8.istio.api.networking.v1beta1.HTTPRewriteBuilder;
import io.fabric8.istio.api.networking.v1beta1.HTTPRouteBuilder;
import io.fabric8.istio.api.networking.v1beta1.HTTPRouteDestinationBuilder;
import io.fabric8.istio.api.networking.v1beta1.StringMatch;
import io.fabric8.istio.api.networking.v1beta1.StringMatchPrefix;
import io.fabric8.istio.api.networking.v1beta1.VirtualServiceBuilder;
import io.fabric8.istio.api.networking.v1beta1.VirtualServiceList;
import io.fabric8.istio.client.IstioClient;
import io.fabric8.kubernetes.client.KubernetesClientException;

public class VirtualServiceExample {
  private static final String NAMESPACE = "test";

  public static void main(String[] args) {
    try {
      IstioClient client = ClientFactory.newClient(args);
      createResource(client);
      System.exit(0);
    } catch (KubernetesClientException ex) {
      System.err.println("Failed with " + ex.getMessage());
      System.exit(1);
    }
  }

  public static void createResource(IstioClient client) {
    System.out.println("Creating a virtual service");
    // Example from: https://istio.io/latest/docs/reference/config/networking/virtual-service/
    client.v1beta1().virtualServices().inNamespace(NAMESPACE).create(new VirtualServiceBuilder()
      .withNewMetadata()
      .withName("reviews-route")
      .endMetadata()
      .withNewSpec()
      .withHosts("reviews-v2-routes")
      .withHttp(
        new HTTPRouteBuilder().withName("reviews-v2-routes")
          .withMatch(
            new HTTPMatchRequestBuilder().withUri(new StringMatch(new StringMatchPrefix("/wpcatalog"))).build(),
            new HTTPMatchRequestBuilder().withUri(new StringMatch(new StringMatchPrefix("/consumercatalog"))).build()
          )
          .withRewrite(new HTTPRewriteBuilder().withUri("/newcatalog").build())
          .withRoute(
            new HTTPRouteDestinationBuilder().withDestination(new Destination("reviews.prod.svc.cluster.local", null, "v2"))
              .build())
          .build()
      )
      .endSpec()
      .build());

    System.out.println("Listing Virtual Service Instances:");
    VirtualServiceList list = client.v1beta1().virtualServices().inNamespace(NAMESPACE).list();
    list.getItems().forEach(b -> System.out.println(b.getMetadata().getName()));
    System.out.println("Done");
  }
}

@@ -651,474 +651,8 @@
 1;%0A%0A
-import io.fabric8.istio.api.networking.v1beta1.Destination;%0Aimport io.fabric8.istio.api.networking.v1beta1.HTTPMatchRequestBuilder;%0Aimport io.fabric8.istio.api.networking.v1beta1.HTTPRewriteBuilder;%0Aimport io.fabric8.istio.api.networking.v1beta1.HTTPRouteBuilder;%0Aimport io.fabric8.istio.api.networking.v1beta1.HTTPRouteDestinationBuilder;%0Aimport io.fabric8.istio.api.networking.v1beta1.StringMatch;%0Aimport io.fabric8.istio.api.networking.v1beta1.StringMatchPrefix;%0A
 impo
@@ -717,16 +717,16 @@
 uilder;%0A
+
 import i
@@ -929,16 +929,17 @@
 ample %7B%0A
+%0A
   privat
@@ -1489,16 +1489,81 @@
 ervice/%0A
+    final String reviewsHost = %22reviews.prod.svc.cluster.local%22;%0A
     clie
@@ -1761,56 +1761,56 @@
     
-.with
+  .addTo
 Hosts(
-%22
 reviews
--v2-routes%22)%0A      .with
+Host)%0A        .addNew
 Http(
+)
 %0A   
@@ -1818,30 +1818,10 @@
     
-new HTTPRouteBuilder()
+  
 .wit
@@ -1862,89 +1862,42 @@
    .
-withMatch(%0A            new HTTPMatchRequestBuilder
+addNewMatch
 ().with
+New
 Uri(
-new StringMatch(n
+).withN
 ew
- 
 Stri
@@ -1909,16 +1909,20 @@
 chPrefix
+Type
 (%22/wpcat
@@ -1931,19 +1931,28 @@
 og%22)
-)).build
+.endUri().endMatch
 ()
-,
 %0A   
@@ -1962,68 +1962,43 @@
     
-  new HTTPMatchRequestBuilder
+.addNewMatch
 ().with
+New
 Uri(
-new StringMatch(n
+).withN
 ew
- 
 Stri
@@ -2010,16 +2010,20 @@
 chPrefix
+Type
 (%22/consu
@@ -2038,76 +2038,54 @@
 og%22)
-)).build()%0A          )%0A          .withRewrite(new HTTPRewriteBuilder
+.endUri().endMatch()%0A          .withNewRewrite
 ().w
@@ -2106,23 +2106,27 @@
 talog%22).
-build()
+endRewrite(
 )%0A      
@@ -2134,18 +2134,21 @@
    .
-with
+addNew
 Route(
+)
 %0A   
@@ -2160,143 +2160,303 @@
     
-new HTTPRouteDestinationBuilder().withDestination(new Destination(%22reviews.prod.svc.cluster.local%22, null, %22v2%22))%0A              .build()
+.withNewDestination().withHost(reviewsHost).withSubset(%22v2%22).endDestination()%0A          .endRoute()%0A        .endHttp()%0A        .addNewHttp()%0A          .withName(%22reviews-v2-routes%22)%0A          .addNewRoute()%0A            .withNewDestination().withHost(reviewsHost).withSubset(%22v1%22).endDestination(
 )%0A  
@@ -2464,21 +2464,24 @@
        .
-build
+endRoute
 ()%0A     
@@ -2481,16 +2481,27 @@
 )%0A      
+  .endHttp(
 )%0A      

use nested builders