Skip to content

Commit

Permalink
implement Match module functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidd committed Sep 22, 2015
1 parent 9283ad1 commit 01a14f0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/suse/saltstack/netapi/calls/modules/Match.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> compound(String tgt, Optional<String> minionId) {
LinkedHashMap<String, Object> 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<Boolean>(){});
}

public static LocalCall<Boolean> compound(String tgt) {
return compound(tgt, Optional.empty());
}


public static LocalCall<Boolean> glob(String tgt, Optional<String> minionId) {
LinkedHashMap<String, Object> 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<Boolean>(){});
}

public static LocalCall<Boolean> glob(String tgt) {
return glob(tgt, Optional.empty());
}

public static LocalCall<Boolean> grain(String tgt, Optional<String> delimiter) {
LinkedHashMap<String, Object> 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<Boolean>(){});
}

public static LocalCall<Boolean> list(List<String> tgt, Optional<String> minionId) {
LinkedHashMap<String, Object> 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<Boolean>(){});
}

public static LocalCall<Boolean> list(String... tgt) {
return list(Arrays.asList(tgt), Optional.empty());
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/suse/saltstack/netapi/utils/ClientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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();
}
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/suse/saltstack/netapi/utils/ClientUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -64,4 +67,16 @@ public void testStreamToString() {
assertEquals("Result doesn't match test string", TEST_STRING, result);
}


@Test
public void testJoin() {
List<String> strings = Arrays.asList("Hello", "World", "123");
List<String> single = Arrays.asList("single");
List<String> empty = new ArrayList<>();

assertEquals("", ClientUtils.join(empty, ","));
assertEquals("single", ClientUtils.join(single, ","));
assertEquals("Hello,World,123", ClientUtils.join(strings, ","));
}

}

0 comments on commit 01a14f0

Please sign in to comment.