/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.teradata.tempto.internal.ssh;

import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.teradata.tempto.configuration.Configuration;
import com.teradata.tempto.initialization.AutoModuleProvider;
import com.teradata.tempto.initialization.SuiteModuleProvider;
import com.teradata.tempto.ssh.SshClient;
import com.teradata.tempto.ssh.SshClientFactory;

import java.util.Optional;

import static com.google.inject.name.Names.named;

@AutoModuleProvider
public class SshClientModuleProvider
        implements SuiteModuleProvider
{
    private static final String SSH_KEY = "ssh";
    private static final String ROLES_KEY = "roles";
    private static final String IDENTITY_KEY = "identity";

    private static final String HOST_KEY = "host";
    private static final String PORT_KEY = "port";
    private static final String USER_KEY = "user";
    private static final String PASSWORD_KEY = "password";

    public Module getModule(Configuration configuration)
    {
        Configuration sshConfiguration = configuration.getSubconfiguration(SSH_KEY);
        Optional<String> identity = sshConfiguration.getString(IDENTITY_KEY);

        Configuration rolesConfiguration = sshConfiguration.getSubconfiguration(ROLES_KEY);
        return new AbstractModule()
        {
            @Override
            protected void configure()
            {
                JschSshClientFactory sshClientFactory = new JschSshClientFactory();
                identity.ifPresent(identityValue -> sshClientFactory.addIdentity(identityValue));
                bind(SshClientFactory.class).toInstance(sshClientFactory);

                for (String role : rolesConfiguration.listKeyPrefixes(1)) {
                    Configuration roleConfiguration = rolesConfiguration.getSubconfiguration(role);
                    String host = roleConfiguration.getStringMandatory(HOST_KEY);
                    int port = roleConfiguration.getIntMandatory(PORT_KEY);
                    String user = roleConfiguration.getStringMandatory(USER_KEY);
                    Optional<String> password = roleConfiguration.getString(PASSWORD_KEY);

                    Key<SshClient> sshClientKey = Key.get(SshClient.class, named(role));
                    bind(sshClientKey).toInstance(sshClientFactory.create(host, port, user, password));
                }
            }
        };
    }
}

@@ -2538,25 +2538,16 @@
 n.getInt
-Mandatory
 (PORT_KE
@@ -2543,24 +2543,35 @@
 Int(PORT_KEY
+).orElse(22
 );%0A         
@@ -2622,25 +2622,16 @@
 etString
-Mandatory
 (USER_KE
@@ -2627,24 +2627,39 @@
 ing(USER_KEY
+).orElse(%22root%22
 );%0A         

Make port and user to be optional for ssh configuration