package com.suse.saltstack.netapi.examples;

import com.suse.saltstack.netapi.AuthModule;
import com.suse.saltstack.netapi.calls.WheelResult;
import com.suse.saltstack.netapi.calls.modules.Grains;
import com.suse.saltstack.netapi.calls.modules.Test;
import com.suse.saltstack.netapi.calls.wheel.Key;
import com.suse.saltstack.netapi.client.SaltStackClient;
import com.suse.saltstack.netapi.datatypes.target.Glob;
import com.suse.saltstack.netapi.datatypes.target.MinionList;
import com.suse.saltstack.netapi.datatypes.target.Target;
import com.suse.saltstack.netapi.exception.SaltStackException;

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

/**
 * 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 SaltStackException {
        // Init the client
        SaltStackClient client = new SaltStackClient(URI.create(SALT_API_URL));

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

        System.out.println("--> Ping results:\n");
        results.keySet().forEach(
                key -> System.out.println(key + " -> " + results.get(key)));

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

        for (String minion : grains.keySet()) {
            System.out.println("\n--> Listing grains for '" + minion + "':\n");
            Map<String, Object> minionGrains = grains.get(minion);
            minionGrains.keySet().forEach(
                    key -> System.out.println(key + ": " + minionGrains.get(key)));
        }

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

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

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

@@ -1412,45 +1412,32 @@
 lts.
-keySet().forEach(%0A                key
+forEach((minion, result)
  -%3E 
@@ -1451,27 +1451,30 @@
 out.println(
-key
+minion
  + %22 -%3E %22 + 
@@ -1479,26 +1479,16 @@
 + result
-s.get(key)
 ));%0A%0A   
@@ -1655,24 +1655,30 @@
 ject%3E%3E grain
+Result
 s = client.c
@@ -1781,45 +1781,48 @@
     
-for (String 
+grainResults.forEach((
 minion
- :
+,
  grains
-.keySet())
+) -%3E
  %7B%0A 
@@ -1916,129 +1916,35 @@
     
-Map%3CString, Object%3E minionGrains = grains.get(minion);%0A            minionGrains.keySet().forEach(%0A                    key
+grains.forEach((key, value)
  -%3E 
@@ -1979,29 +1979,13 @@
 %22 + 
-minionGrains.get(key)
+value
 ));%0A
@@ -1993,16 +1993,18 @@
        %7D
+);
 %0A%0A      
@@ -2103,23 +2103,23 @@
 mes%3E key
-s
 Result
+s
  = clien
@@ -2227,15 +2227,15 @@
  key
-s
 Result
+s
 .get
@@ -2308,32 +2308,32 @@
 nion keys:%5Cn%22);%0A
+
         keys.get
@@ -2385,25 +2385,24 @@
 n(minion));%0A
-%0A
         Syst

Compact the code and unify the variable names