package com.suse.salt.netapi.examples;

import com.suse.salt.netapi.AuthModule;
import com.suse.salt.netapi.calls.WheelResult;
import com.suse.salt.netapi.calls.modules.Grains;
import com.suse.salt.netapi.calls.modules.Test;
import com.suse.salt.netapi.calls.wheel.Key;
import com.suse.salt.netapi.client.SaltClient;
import com.suse.salt.netapi.datatypes.target.Glob;
import com.suse.salt.netapi.datatypes.target.MinionList;
import com.suse.salt.netapi.datatypes.target.Target;
import com.suse.salt.netapi.exception.SaltException;
import com.suse.salt.netapi.results.Result;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Example code calling salt modules using the generic interface.
 */
public class Calls {

    private static final String SALT_API_URL = "http://localhost:8000";
    private static final String USER = "saltdev";
    private static final String PASSWORD = "saltdev";

    public static void main(String[] args) throws SaltException {
        // Init the client
        SaltClient client = new SaltClient(URI.create(SALT_API_URL));

        // Ping all minions using a glob matcher
        Target<String> globTarget = new Glob("*");
        Map<String, Result<Boolean>> results = Test.ping().callSync(
                client, globTarget, USER, PASSWORD, AuthModule.AUTO);

        System.out.println("--> Ping results:\n");
        results.forEach((minion, result) -> System.out.println(minion + " -> " + result));

        // Get the grains from a list of minions
        Target<List<String>> minionList = new MinionList("minion1", "minion2");
        Map<String, Result<Map<String, Object>>> grainResults =
                Grains.items(false).callSync(
                client, minionList, USER, PASSWORD, AuthModule.AUTO);

        grainResults.forEach((minion, grains) -> {
            System.out.println("\n--> Listing grains for '" + minion + "':\n");
            String message = grains.fold(
                    Object::toString,
                    m -> m.entrySet().stream()
                    .map(e -> e.getKey() + ": " + e.getValue())
                    .collect(Collectors.joining("\n"))
            );
        });

        // Call a wheel function: list accepted and pending minion keys
        WheelResult<Key.Names> keyResults = Key.listAll().callSync(
                client, USER, PASSWORD, AuthModule.AUTO);
        Key.Names keys = keyResults.getData().getResult();

        System.out.println("\n--> Accepted minion keys:\n");
        keys.getMinions().forEach(System.out::println);
        System.out.println("\n--> Pending minion keys:\n");
        keys.getUnacceptedMinions().forEach(System.out::println);
    }
}

@@ -1684,32 +1684,16 @@
 esults =
-%0A               
  Grains.
@@ -1704,26 +1704,16 @@
 s(false)
-.callSync(
 %0A       
@@ -1713,32 +1713,42 @@
                 
+.callSync(
 client, minionLi
@@ -1940,15 +1940,20 @@
 ing 
-message
+grainsOutput
  = g
@@ -1988,24 +1988,43 @@
     
-Object::
+error -%3E %22Error: %22 + error.
 toString
 ,%0A  
@@ -2019,16 +2019,18 @@
 toString
+()
 ,%0A      
@@ -2047,14 +2047,30 @@
     
-m -%3E m
+grainsMap -%3E grainsMap
 .ent
@@ -2086,16 +2086,24 @@
 tream()%0A
+        
         
@@ -2178,16 +2178,24 @@
         
+     
+   
 .collect
@@ -2236,16 +2236,62 @@
      );%0A
+            System.out.println(grainsOutput);%0A
         

Reformat the examples in Calls.java