All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.kube.xscript.api.KubeUpdate Maven / Gradle / Ivy

package com.alogic.kube.xscript.api;

import com.alogic.kube.KubeApiClients;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import okhttp3.Call;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Post操作
 *
 * @since 1.6.16.14
 */
public abstract class KubeUpdate extends Segment {
    protected String pid = KubeUtil.DFT_CONTEXT_ID;
    protected String $result = KubeUtil.DFT_RESULT;
    protected String $path = null;
    protected String $kubeClientId = KubeApiClients.DEFAULT;

    public KubeUpdate(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        pid = PropertiesConstants.getString(p, "pid", pid,true);
        $result = PropertiesConstants.getRaw(p,"id",$result);
        $path = PropertiesConstants.getRaw(p,"path",$path);
        $kubeClientId = PropertiesConstants.getRaw(p,"kubeId",$kubeClientId);
    }

    public String getApiPath(Properties ctx){
        return PropertiesConstants.transform(ctx,$path,null);
    }

    public String getResultId(Properties ctx){
        return PropertiesConstants.transform(ctx,$result,KubeUtil.DFT_RESULT);
    }

    public String getCodeId(String resultId){
        return String.format("%s.code",resultId);
    }

    public String getReasonId(String resultId){
        return String.format("%s.reason",resultId);
    }

    protected void handleResult(Properties ctx, Response response){
        String resultId = getResultId(ctx);
        if (StringUtils.isNotEmpty(resultId)){
            PropertiesConstants.setBoolean(ctx,resultId,response.isSuccessful());
            PropertiesConstants.setInt(ctx,getCodeId(resultId),response.code());
            PropertiesConstants.setString(ctx,getReasonId(resultId),response.message());
        }
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        ApiClient client = ctx.getObject(pid);
        if (client == null){
            String clientId = PropertiesConstants.transform(ctx,$kubeClientId, KubeApiClients.DEFAULT);
            log("Api client is null,api client [{}] is using.",clientId);
            try {
                client = KubeApiClients.getClient(clientId,false);
            }catch (Exception ex){
                log("Can not get default api client.");
            }
        }
        if (client != null) {
            onExecute(client, root, current, ctx, watcher);
        }
    }

    protected void onExecute(ApiClient client, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String path = getApiPath(ctx);
        if (StringUtils.isNotEmpty(path)) {
            Response response = null;
            try {
                Object body = createBody(client,root,current,ctx,watcher);

                response = buildCall(client, getHttpMethod(), path, ctx, body).execute();

                handleResult(ctx,response);

                if (response.isSuccessful()) {
                    client.handleResponse(response, null);
                }
            }catch (IOException ex){
                log(String.format("Kube api call failed:%s",ex.getMessage()));
            }catch (ApiException e) {
                log(String.format("Kube api call failed:%d-%s",e.getCode(),e.getMessage()));
            }finally {
                if (response != null){
                    response.close();
                }
            }
        }else{
            log("Kube path is null,ignored.");
        }
    }

    protected Call buildCall(ApiClient client, String method, String path, Properties ctx, Object body) throws ApiException{
        if (body == null){
            return KubeUtil.updateCall(client,method,path,ctx);
        }else {
            return KubeUtil.updateCall(client, method, path, ctx, body);
        }
    }

    protected void onSuperExecute(ApiClient client,XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher){
        super.onExecute(root,current,ctx,watcher);
    }

    abstract protected String getHttpMethod();
    abstract protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher);

    /**
     * Post json对象
     */
    public static class PostByJson extends KubeUpdate {
        public PostByJson(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            Map jsonData = new HashMap();
            XsObject newDoc = new JsonObject("root",jsonData);
            onSuperExecute(client,newDoc,newDoc,ctx,watcher);
            return jsonData;
        }

        @Override
        protected String getHttpMethod(){
            return KubeUtil.METHOD_POST;
        }
    }

    /**
     * Post文本
     */
    public static class PostByText extends KubeUpdate {
        protected String $content = null;
        protected boolean ref = false;
        public PostByText(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
            $content = PropertiesConstants.getRaw(p,"content",$content);
            ref = PropertiesConstants.getBoolean(p,"ref",ref,true);
        }

        @Override
        protected String getHttpMethod() {
            return KubeUtil.METHOD_POST;
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            String content = PropertiesConstants.transform(ctx,$content,null);
            return ref?PropertiesConstants.getString(ctx,content,null):content;
        }
    }

    /**
     * Put json对象
     */
    public static class PutByJson extends KubeUpdate {
        public PutByJson(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            Map jsonData = new HashMap();
            XsObject newDoc = new JsonObject("root",jsonData);
            onSuperExecute(client,newDoc,newDoc,ctx,watcher);
            return jsonData;
        }

        @Override
        protected String getHttpMethod(){
            return KubeUtil.METHOD_PUT;
        }
    }

    /**
     * Post文本
     */
    public static class PutByText extends KubeUpdate {
        protected String $content = null;
        protected boolean ref = false;
        public PutByText(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
            $content = PropertiesConstants.getRaw(p,"content",$content);
            ref = PropertiesConstants.getBoolean(p,"ref",ref,true);
        }

        @Override
        protected String getHttpMethod() {
            return KubeUtil.METHOD_PUT;
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            String content = PropertiesConstants.transform(ctx,$content,null);
            return ref?PropertiesConstants.getString(ctx,content,null):content;
        }
    }

    /**
     * Put json对象
     */
    public static class PatchByJson extends KubeUpdate {
        public PatchByJson(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            Map jsonData = new HashMap();
            XsObject newDoc = new JsonObject("root",jsonData);
            onSuperExecute(client,newDoc,newDoc,ctx,watcher);
            return jsonData;
        }

        @Override
        protected String getHttpMethod(){
            return KubeUtil.METHOD_PATCH;
        }

        @Override
        protected Call buildCall(ApiClient client, String method, String path, Properties ctx, Object body) throws ApiException{
            return KubeUtil.patchCall(client,method,path,ctx,body);
        }
    }

    /**
     * Post文本
     */
    public static class PatchByText extends KubeUpdate {
        protected String $content = null;
        protected boolean ref = false;
        public PatchByText(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
            $content = PropertiesConstants.getRaw(p,"content",$content);
            ref = PropertiesConstants.getBoolean(p,"ref",ref,true);
        }

        @Override
        protected String getHttpMethod() {
            return KubeUtil.METHOD_PATCH;
        }

        @Override
        protected Object createBody(ApiClient client,XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
            String content = PropertiesConstants.transform(ctx,$content,null);
            return ref?PropertiesConstants.getString(ctx,content,null):content;
        }

        @Override
        protected Call buildCall(ApiClient client, String method, String path, Properties ctx, Object body) throws ApiException{
            return KubeUtil.patchCall(client,method,path,ctx,body);
        }
    }

    /**
     * Delete object
     */
    public static class Delete extends KubeUpdate {

        public Delete(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);
        }

        @Override
        protected String getHttpMethod() {
            return KubeUtil.METHOD_DELETE;
        }

        @Override
        protected Object createBody(ApiClient client, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            return null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy