From 65585c0af1753cbdadf490ed56e7be9dee0c6280 Mon Sep 17 00:00:00 2001 From: Kevin Walter Date: Tue, 22 Sep 2015 15:30:07 +0200 Subject: [PATCH] implement Match module functions --- .../saltstack/netapi/calls/modules/Match.java | 61 +++++++++++++++++++ .../saltstack/netapi/utils/ClientUtils.java | 21 +++++++ .../netapi/utils/ClientUtilsTest.java | 15 +++++ 3 files changed, 97 insertions(+) create mode 100644 src/main/java/com/suse/saltstack/netapi/calls/modules/Match.java diff --git a/src/main/java/com/suse/saltstack/netapi/calls/modules/Match.java b/src/main/java/com/suse/saltstack/netapi/calls/modules/Match.java new file mode 100644 index 000000000..0fe78cf4c --- /dev/null +++ b/src/main/java/com/suse/saltstack/netapi/calls/modules/Match.java @@ -0,0 +1,61 @@ +package com.suse.saltstack.netapi.calls.modules; + +import com.google.gson.reflect.TypeToken; +import com.suse.saltstack.netapi.calls.LocalCall; +import com.suse.saltstack.netapi.utils.ClientUtils; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; + +/** + * salt.modules.match + */ +public class Match { + + public static LocalCall compound(String tgt, Optional minionId) { + LinkedHashMap args = new LinkedHashMap<>(); + args.put("tgt", tgt); + minionId.ifPresent(id -> args.put("minion_id", id)); + return new LocalCall<>("match.compound", Optional.empty(), + Optional.of(args), new TypeToken(){}); + } + + public static LocalCall compound(String tgt) { + return compound(tgt, Optional.empty()); + } + + + public static LocalCall glob(String tgt, Optional minionId) { + LinkedHashMap args = new LinkedHashMap<>(); + args.put("tgt", tgt); + minionId.ifPresent(id -> args.put("minion_id", id)); + return new LocalCall<>("match.glob", Optional.empty(), + Optional.of(args), new TypeToken(){}); + } + + public static LocalCall glob(String tgt) { + return glob(tgt, Optional.empty()); + } + + public static LocalCall grain(String tgt, Optional delimiter) { + LinkedHashMap args = new LinkedHashMap<>(); + args.put("tgt", tgt); + delimiter.ifPresent(d -> args.put("delimiter", d)); + return new LocalCall<>("match.grain", Optional.empty(), + Optional.of(args), new TypeToken(){}); + } + + public static LocalCall list(List tgt, Optional minionId) { + LinkedHashMap args = new LinkedHashMap<>(); + args.put("tgt", ClientUtils.join(tgt, ",")); + minionId.ifPresent(id -> args.put("minion_id", id)); + return new LocalCall<>("match.list", Optional.empty(), + Optional.of(args), new TypeToken(){}); + } + + public static LocalCall list(String... tgt) { + return list(Arrays.asList(tgt), Optional.empty()); + } +} diff --git a/src/main/java/com/suse/saltstack/netapi/utils/ClientUtils.java b/src/main/java/com/suse/saltstack/netapi/utils/ClientUtils.java index 3a40f1c8a..90b7aa694 100644 --- a/src/main/java/com/suse/saltstack/netapi/utils/ClientUtils.java +++ b/src/main/java/com/suse/saltstack/netapi/utils/ClientUtils.java @@ -7,6 +7,7 @@ import java.io.InputStream; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.List; import java.util.Scanner; import static com.google.gson.internal.$Gson$Types.newParameterizedTypeWithOwner; @@ -63,4 +64,24 @@ public static ParameterizedType parameterizedType(Type ownerType, Type rawType, Type... typeArguments) { return newParameterizedTypeWithOwner(ownerType, rawType, typeArguments); } + + /** + * Concatenates a list of strings with a delimiter + * + * @param list list of strings + * @param delimiter delimiter string + * @return the concatenated string separated by the delimiter + */ + public static String join(List list, String delimiter) { + if (list.isEmpty()) { + return ""; + } else { + StringBuilder sb = new StringBuilder(list.get(0)); + for (int i = 1; i < list.size(); i++) { + sb.append(delimiter); + sb.append(list.get(i)); + } + return sb.toString(); + } + } } diff --git a/src/test/java/com/suse/saltstack/netapi/utils/ClientUtilsTest.java b/src/test/java/com/suse/saltstack/netapi/utils/ClientUtilsTest.java index a7a0bbcf8..d087da0a0 100644 --- a/src/test/java/com/suse/saltstack/netapi/utils/ClientUtilsTest.java +++ b/src/test/java/com/suse/saltstack/netapi/utils/ClientUtilsTest.java @@ -7,6 +7,9 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Scanner; /** @@ -64,4 +67,16 @@ public void testStreamToString() { assertEquals("Result doesn't match test string", TEST_STRING, result); } + + @Test + public void testJoin() { + List strings = Arrays.asList("Hello", "World", "123"); + List single = Arrays.asList("single"); + List empty = new ArrayList<>(); + + assertEquals("", ClientUtils.join(empty, ",")); + assertEquals("single", ClientUtils.join(single, ",")); + assertEquals("Hello,World,123", ClientUtils.join(strings, ",")); + } + }