/*
Copyright 2020 The Kubernetes Authors.
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.kubernetes.client.extended.kubectl;

import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.custom.V1Patch;
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1ReplicaSet;
import io.kubernetes.client.util.PatchUtils;

public class KubectlScale<ApiType extends KubernetesObject>
    extends Kubectl.ResourceBuilder<ApiType, KubectlScale<ApiType>>
    implements Kubectl.Executable<ApiType> {
  private int replicas;

  KubectlScale(Class<ApiType> apiTypeClass) {
    super(apiTypeClass);
    this.replicas = -1;
  }

  public KubectlScale<ApiType> replicas(int replicas) {
    this.replicas = replicas;
    return this;
  }

  @Override
  public ApiType execute() throws KubectlException {
    validate();

    String jsonPatchStr =
        String.format("[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":%d}]", replicas);

    AppsV1Api api = new AppsV1Api(this.apiClient);
    try {
      if (apiTypeClass.equals(V1Deployment.class)) {
        return PatchUtils.patch(
            apiTypeClass,
            () ->
                api.patchNamespacedDeploymentCall(
                    name,
                    namespace,
                    new V1Patch(jsonPatchStr),
                    null,
                    null,
                    null, // field-manager is optional
                    null,
                    null),
            V1Patch.PATCH_FORMAT_JSON_PATCH,
            this.apiClient);
      } else if (apiTypeClass.equals(V1ReplicaSet.class)) {
        return PatchUtils.patch(
            apiTypeClass,
            () ->
                api.patchNamespacedReplicaSetCall(
                    name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null),
            V1Patch.PATCH_FORMAT_JSON_PATCH,
            this.apiClient);
      } else {
        throw new KubectlException("Unsupported class for scale: " + apiTypeClass);
      }
    } catch (ApiException ex) {
      throw new KubectlException(ex);
    }
  }

  private void validate() throws KubectlException {
    StringBuilder msg = new StringBuilder();
    if (name == null) {
      msg.append("Missing name, ");
    }
    if (namespace == null) {
      msg.append("Missing namespace, ");
    }
    if (replicas < 0) {
      msg.append("Invalid replicas");
    }
    if (msg.length() > 0) {
      throw new KubectlException(msg.toString());
    }
  }
}

@@ -990,16 +990,74 @@
 icaSet;%0A
+import io.kubernetes.client.openapi.models.V1StatefulSet;%0A
 import i
@@ -2638,32 +2638,391 @@
 his.apiClient);%0A
+      %7D else if (apiTypeClass.equals(V1StatefulSet.class)) %7B%0A        return PatchUtils.patch(%0A            apiTypeClass,%0A            () -%3E%0A                api.patchNamespacedStatefulSetCall(%0A                    name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null),%0A            V1Patch.PATCH_FORMAT_JSON_PATCH,%0A            this.apiClient);%0A
       %7D else %7B%0A 

fix : let scale support StatefulSet