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

cn.zenliu.java.consul.Endpoints Maven / Gradle / Ivy

There is a newer version: 0.0.2
Show newest version
/*
 * Source of consul_client
 * Copyright (C) 2023.  Zen.Liu
 *
 * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0"
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Class Path Exception
 * Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.
 *  As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.
 */

package cn.zenliu.java.consul;

import cn.zenliu.java.consul.trasport.Response;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Zen.Liu
 * @since 2023-08-20
 */
public interface Endpoints {

    interface Acl> extends Context, Values.Acl {
        default Response> create(CreateAcl createAcl) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "acl", "create")
                    .query(parameter())

                    .put(IDOnly.class, (IDOnly) null)
                    .send(createAcl)
                    .response()
                    .map(Info::id)
                    ;
        }

        default Response> update(UpdateAcl updateAcl) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "acl", "update")
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(updateAcl)
                    .response()
                    .map(Info::parse)
                    ;
        }

        default Response> destroy(String id) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "acl", "destroy", id)
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response> info(String id) {

            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "acl", "info", id)
                    .query(parameter())

                    .put(ACL.LIST, ACL.EMPTY_LIST)
                    .send(null)
                    .response()
                    .map(Info::one);

        }

        default Response> clone(String id) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "acl", "clone", id)
                    .query(parameter())

                    .put(IDOnly.class, (IDOnly) null)
                    .send(null)
                    .response()
                    .map(Info::id);

        }

        default Response>> list() {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "acl", "list")
                    .query(parameter())

                    .put(ACL.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


    }

    interface Agent> extends Values.Agent, Context {

        default Response>> checks() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "checks")
                    .query(parameter())

                    .get(Check.MAP, (Map) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response>> services() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "services")
                    .query(parameter())
                    .get(Service.MAP, (Map) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response>> members() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "members")
                    .query(parameter())
                    .get(Member.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);
        }


        default Response> self() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "self")
                    .query(parameter())

                    .get(Self.class, (Self) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response> maintenance(boolean maintenanceEnabled, @Nullable String reason) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "maintenance")
                    .query(parameter())

                    .query("enable", Boolean.toString(maintenanceEnabled))
                    .query(reason != null, "reason", Parameter.encode(reason))

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);
        }

        default Response> join(String address, boolean wan) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "join", Parameter.encode(address))
                    .query(parameter())

                    .query(wan, "wan", "1")

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);
        }

        default Response> forceLeave(String node) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "force-leave", Parameter.encode(node))
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response> checkRegister(CreateCheck createCheck) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "check", "register")
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(createCheck)
                    .response()
                    .map(Info::parse);

        }


        default Response> checkDeregister(String checkId) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "check", "deregister", checkId)
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response> checkPass(String checkId, @Nullable String note) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "check", "pass", checkId)
                    .query(parameter())

                    .query(note != null, "note", Parameter.encode(note))

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response> checkWarn(String checkId, @Nullable String note) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "check", "warn", checkId)
                    .query(parameter())

                    .query(note != null, "note", Parameter.encode(note))

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


        default Response> checkFail(String checkId, @Nullable String note) {
            return requester()
                    .header(TOKEN, token())

                    .path(VERSION, "agent", "check", "fail", checkId)
                    .query(parameter())

                    .query(note != null, "note", Parameter.encode(note))

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);
        }


        default Response> serviceRegister(CreateService createService) {
            return requester()
                    .header(TOKEN, token())

                    .path(VERSION, "agent", "service", "register")
                    .query(parameter())

                    .put(Void.class, (Void) null)

                    .send(createService)
                    .response()
                    .map(Info::parse);

        }


        default Response> serviceDeregister(String serviceId) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "service", "deregister", serviceId)
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response> serviceMaintenance(String serviceId, boolean maintenanceEnabled, @Nullable String reason) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "service", "maintenance", serviceId)
                    .query(parameter())

                    .query("enable", Boolean.toString(maintenanceEnabled))
                    .query(reason != null, "reason", Parameter.encode(reason))


                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response> reload() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "agent", "reload")
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }
        //endregion


    }

    interface Catalog> extends Values.Catalog, Context {


        //region Function
        default Response> register(Registration registration) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "register")
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(registration)
                    .response()
                    .map(Info::parse);

        }

        default Response> deregister(Deregistration deregistration) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "deregister")
                    .query(parameter())

                    .put(Void.class, (Void) null)
                    .send(deregistration)
                    .response()
                    .map(Info::parse);


        }


        default Response>> datacenters() {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "datacenters")
                    .query(parameter())

                    .get(STRING_LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response> node(String name) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "node", Parameter.encode(name))
                    .query(parameter())


                    .get(Node.class, (Node) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response>> nodes(@Nullable Values.NodeParameter query) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "nodes")
                    .query(parameter())

                    .query(query)

                    .get(Values.Node.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response>> service(String serviceName, @Nullable Values.ServiceParameter query) {
            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "service", Parameter.encode(serviceName))
                    .query(parameter())

                    .query(query)

                    .get(Service.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response>>> services(@Nullable Values.ServiceParameter query) {

            return requester()
                    .header(TOKEN, token())
                    .path(VERSION, "catalog", "services")
                    .query(parameter())

                    .query(query)

                    .get(MAP_STRING_LIST, (Map>) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }
        //endregion


    }

    interface Coordinate> extends Values.Coordinate, Context {


        default Response>> datacenters() {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "coordinate", "datacenters")
                    .query(parameter())


                    .get(Datacenter.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response>> nodes() {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "coordinate", "nodes")
                    .query(parameter())


                    .get(Node.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


    }

    interface Events> extends Values.Events, Context {


        default Response>> list(@Nullable EventServiceParameter query) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "event", "list")
                    .query(parameter())

                    .query(query)

                    .get(Event.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response> fire(String event, String payload, @Nullable EventServiceParameter query) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "event", "fire", Parameter.encode(event))
                    .query(parameter())

                    .query(query)

                    .put(Event.class, (Event) null)
                    .send(payload)
                    .response()
                    .map(Info::parse);

        }


    }

    interface Health> extends Values.Health, Context {


        default Response>> checksForNode(String nodeName) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "health", "node", Parameter.encode(nodeName))
                    .query(parameter())


                    .get(Check.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response>> checksForService(String serviceName, @Nullable Values.NodeParameter query) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "health", "checks", Parameter.encode(serviceName))
                    .query(parameter())

                    .query(query)

                    .get(Check.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response>> services(String serviceName, @Nullable Values.ServiceParameter query) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "health", "service", Parameter.encode(serviceName))
                    .query(parameter())

                    .query(query)

                    .get(Service.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


        default Response>> checksState(@Nullable Check.Status status) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "health", "state", status == null ? "any" : status.name().toLowerCase())
                    .query(parameter())


                    .get(Check.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


    }

    interface Query> extends Values.Query, Context {


        default Response> execute(String uuid) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "query", uuid, "execute")
                    .query(parameter())


                    .get(QueryExecution.class, (QueryExecution) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


    }

    interface Sessions> extends Values.Sessions, Context {


        default Response> create(CreateSession create) {

            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "create")
                    .query(parameter())


                    .put(IDOnly.class, (IDOnly) null)
                    .send(create)
                    .response()
                    .map(Info::id);

        }

        default Response> destroy(String session) {

            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "destroy", Parameter.encode(session))
                    .query(parameter())


                    .put(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response> info(String session) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "info", Parameter.encode(session))
                    .query(parameter())


                    .get(Session.LIST, Session.EMPTY_LIST)// (List) null)
                    .send(null)
                    .response()
                    .map(Info::one);

        }

        default Response>> node(String node) {

            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "node", Parameter.encode(node))
                    .query(parameter())


                    .get(Session.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response>> list() {

            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "list")
                    .query(parameter())


                    .get(Session.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response> renew(String id) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "session", "renew", id)
                    .query(parameter())


                    .put(Session.LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::one);


        }


    }

    interface Status> extends Values.Status, Context {

        default Response> leader() {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "status", "leader")
                    .query(parameter())


                    .get(String.class, (String) null)
                    .send(null)
                    .response()
                    .map(Info::parse);
        }

        default Response>> peers() {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "status", "peers")
                    .query(parameter())


                    .get(STRING_LIST, (List) null)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


    }

    interface Store> extends Values.Store, Context {


        //region function
        static String[] buildKeys(CharSequence key, CharSequence... segments) {
            var s = new String[segments.length + 3];
            s[0] = VERSION;
            s[1] = "kv";
            s[2] = Parameter.encode(key.toString());
            for (int i = 0; i < segments.length; i++) {
                s[i + 3] = Parameter.encode(segments[i].toString());
            }
            return s;
        }

        default Response> text(CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())


                    .get(Text.LIST, Text.EMPTY_LIST)
                    .send(null)
                    .response()
                    .map(Info::one);

        }

        default Response>> textAll(CharSequence key, @Nullable CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query("recurse")

                    .get(Text.LIST, Text.EMPTY_LIST)
                    .send(null)
                    .response()
                    .map(Info::parse);


        }

        default Response> binary(CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .get(Binary.LIST, Binary.EMPTY_LIST)
                    .send(null)
                    .response()
                    .map(Info::one);

        }

        default Response>> binaryAll(CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query("recurse")

                    .get(Binary.LIST, Binary.EMPTY_LIST)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }


        default Response>> keys(@Nullable String separator, CharSequence keys) {
            return requester()

                    .header(TOKEN, token())
                    .path(VERSION, "kv", keys)
                    .query(parameter())

                    .query("keys")
                    .query(separator != null, "separator", separator)

                    .get(STRING_LIST, Collections.emptyList())
                    .send(null)
                    .response()
                    .map(Info::parse);


        }


        default Response> set(@Nullable String value, @Nullable PutParameter parameter, CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query(parameter)


                    .put(Boolean.class, (Boolean) null)
                    .send(value)
                    .response()
                    .map(Info::parse);


        }

        default Response> set(byte @Nullable [] value, @Nullable PutParameter parameter, CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query(parameter)


                    .put(Boolean.class, (Boolean) null)
                    .send(value)
                    .response()
                    .map(Info::parse);

        }

        default Response> delete(@Nullable PutParameter parameter, CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query(parameter)


                    .delete(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }

        default Response> deleteAll(@Nullable PutParameter parameter, CharSequence key, CharSequence... segments) {
            return requester()

                    .header(TOKEN, token())
                    .path(buildKeys(key, segments))
                    .query(parameter())

                    .query(parameter)
                    .query("recurse")

                    .delete(Void.class, (Void) null)
                    .send(null)
                    .response()
                    .map(Info::parse);

        }
        //endregion


    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy