public void TestMetastoreConfigsForPaaS()
        {
            //Hive metastore
            var hiveMetastore = new Metastore("server1", "hivedb", "user1", "dummy");
            var config = ClusterOperations.GetMetastoreConfig(hiveMetastore, OSType.Windows, "hive");
            Assert.Equal(config.Count, 1);
            var hiveConfig = config["hive-site"];
            Assert.Equal(hiveConfig.Count, 4);
            Assert.Equal(hiveConfig["javax.jdo.option.ConnectionURL"],
                "jdbc:sqlserver://server1.database.windows.net;database=hivedb;encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300;sendStringParametersAsUnicode=true;prepareSQL=0");
            Assert.Equal(hiveConfig["javax.jdo.option.ConnectionUserName"], "user1");
            Assert.Equal(hiveConfig["javax.jdo.option.ConnectionPassword"], "dummy");
            Assert.Equal(hiveConfig["javax.jdo.option.ConnectionDriverName"],
                "com.microsoft.sqlserver.jdbc.SQLServerDriver");

            //Oozie metastore
            var oozieMetastore = new Metastore("server2", "ooziedb", "user2", "dummy2");
            config = ClusterOperations.GetMetastoreConfig(oozieMetastore, OSType.Windows, "oozie");
            Assert.Equal(config.Count, 1);
            var oozieConfig = config["oozie-site"];
            Assert.Equal(oozieConfig.Count, 4);
            Assert.Equal(oozieConfig["oozie.service.JPAService.jdbc.url"],
                "jdbc:sqlserver://server2.database.windows.net;database=ooziedb;encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300;sendStringParametersAsUnicode=true;prepareSQL=0");
            Assert.Equal(oozieConfig["oozie.service.JPAService.jdbc.username"], "user2");
            Assert.Equal(oozieConfig["oozie.service.JPAService.jdbc.password"], "dummy2");
            Assert.Equal(oozieConfig["oozie.service.JPAService.jdbc.driver"], "com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        public void TestMetastoreConfigsForIaaS()
        {
            //Hive metastore
            var hiveMetastore = new Metastore("server1", "hivedb", "user1", "dummy");
            var config = ClusterOperations.GetMetastoreConfig(hiveMetastore, OSType.Linux, "hive");
            Assert.Equal(config.Count, 2);
            var hiveSite = config["hive-site"];
            Assert.Equal(hiveSite.Count, 4);
            Assert.Equal(hiveSite["javax.jdo.option.ConnectionURL"],
                "jdbc:sqlserver://server1.database.windows.net;database=hivedb;encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300;sendStringParametersAsUnicode=true;prepareSQL=0");
            Assert.Equal(hiveSite["javax.jdo.option.ConnectionUserName"], "user1");
            Assert.Equal(hiveSite["javax.jdo.option.ConnectionPassword"], "dummy");
            Assert.Equal(hiveSite["javax.jdo.option.ConnectionDriverName"],
                "com.microsoft.sqlserver.jdbc.SQLServerDriver");
            var hiveEnv = config["hive-env"];
            Assert.Equal(hiveEnv.Count, 6);
            Assert.Equal(hiveEnv["hive_database"], "Existing MSSQL Server database with SQL authentication");
            Assert.Equal(hiveEnv["hive_database_name"], "hivedb");
            Assert.Equal(hiveEnv["hive_database_type"], "mssql");
            Assert.Equal(hiveEnv["hive_existing_mssql_server_database"], "hivedb");
            Assert.Equal(hiveEnv["hive_existing_mssql_server_host"], "server1.database.windows.net");
            Assert.Equal(hiveEnv["hive_hostname"], "server1.database.windows.net");

            //Oozie metastore
            var oozieMetastore = new Metastore("server2", "ooziedb", "user2", "dummy2");
            config = ClusterOperations.GetMetastoreConfig(oozieMetastore, OSType.Linux, "oozie");
            Assert.Equal(config.Count, 2);
            var oozieSite = config["oozie-site"];
            Assert.Equal(oozieSite.Count, 5);
            Assert.Equal(oozieSite["oozie.service.JPAService.jdbc.url"],
                "jdbc:sqlserver://server2.database.windows.net;database=ooziedb;encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300;sendStringParametersAsUnicode=true;prepareSQL=0");
            Assert.Equal(oozieSite["oozie.service.JPAService.jdbc.username"], "user2");
            Assert.Equal(oozieSite["oozie.service.JPAService.jdbc.password"], "dummy2");
            Assert.Equal(oozieSite["oozie.service.JPAService.jdbc.driver"], "com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Assert.Equal(oozieSite["oozie.db.schema.name"], "oozie");
            var oozieEnv = config["oozie-env"];
            Assert.Equal(oozieEnv.Count, 6);
            Assert.Equal(oozieEnv["oozie_database"], "Existing MSSQL Server database with SQL authentication");
            Assert.Equal(oozieEnv["oozie_database_name"], "ooziedb");
            Assert.Equal(oozieEnv["oozie_database_type"], "mssql");
            Assert.Equal(oozieEnv["oozie_existing_mssql_server_database"], "ooziedb");
            Assert.Equal(oozieEnv["oozie_existing_mssql_server_host"], "server2.database.windows.net");
            Assert.Equal(oozieEnv["oozie_hostname"], "server2.database.windows.net");
        }
        internal static Dictionary<string, Dictionary<string, string>> GetMetastoreConfig(Metastore metastore,
            OSType osType, string metastoreType)
        {
            var server = "";
            if (metastore.Server != null)
            {
                server = metastore.Server;
            }

            var index = server.LastIndexOf(".database.windows.net", StringComparison.OrdinalIgnoreCase);
            if (index > 0)
            {
                server = server.Substring(0, index);
            }

            var connectionUrl =
                string.Format(
                    "jdbc:sqlserver://{0}.database.windows.net;database={1};encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300;sendStringParametersAsUnicode=true;prepareSQL=0",
                    server, metastore.Database);
            var configurations = new Dictionary<string, Dictionary<string, string>>();
            if (metastoreType.Equals("hive", StringComparison.OrdinalIgnoreCase))
            {
                configurations.Add(ConfigurationKey.HiveSite, new Dictionary<string, string>
                {
                    {"javax.jdo.option.ConnectionURL", connectionUrl},
                    {"javax.jdo.option.ConnectionUserName", metastore.User},
                    {"javax.jdo.option.ConnectionPassword", metastore.Password},
                    {"javax.jdo.option.ConnectionDriverName", "com.microsoft.sqlserver.jdbc.SQLServerDriver"}
                });

                if (osType == OSType.Windows)
                {
                    return configurations;
                }

                configurations.Add(ConfigurationKey.HiveEnv, new Dictionary<string, string>
                    {
                        {"hive_database", "Existing MSSQL Server database with SQL authentication"},
                        {"hive_database_name", metastore.Database},
                        {"hive_database_type", "mssql"},
                        {"hive_existing_mssql_server_database", metastore.Database},
                        {"hive_existing_mssql_server_host", string.Format("{0}.database.windows.net", server)},
                        {"hive_hostname", string.Format("{0}.database.windows.net", server)}
                    });
            }
            else
            {
                configurations.Add(ConfigurationKey.OozieSite, new Dictionary<string, string>
                {
                    {"oozie.service.JPAService.jdbc.url", connectionUrl},
                    {"oozie.service.JPAService.jdbc.username", metastore.User},
                    {"oozie.service.JPAService.jdbc.password", metastore.Password},
                    {"oozie.service.JPAService.jdbc.driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"}
                });

                if (osType == OSType.Windows)
                {
                    return configurations;
                }

                configurations[ConfigurationKey.OozieSite].Add("oozie.db.schema.name", "oozie");
                configurations.Add(ConfigurationKey.OozieEnv, new Dictionary<string, string>
                {
                    {"oozie_database", "Existing MSSQL Server database with SQL authentication"},
                    {"oozie_database_name", metastore.Database},
                    {"oozie_database_type", "mssql"},
                    {"oozie_existing_mssql_server_database", metastore.Database},
                    {"oozie_existing_mssql_server_host", string.Format("{0}.database.windows.net", server)},
                    {"oozie_hostname", string.Format("{0}.database.windows.net", server)}
                });
            }
            return configurations;
        }
        private static Dictionary<string, Dictionary<string, string>> GetMetastoreConfigPaas(Metastore metastore,
            string metastoreType)
        {
            var connectionUrl =
                string.Format(
                    "jdbc:sqlserver://{0}.database.windows.net;database={1};encrypt=true;trustServerCertificate=true;create=false;loginTimeout=300",
                    metastore.Server, metastore.Database);
            var username = string.Format("{0}@{1}", metastore.User, metastore.Server);
            var config = new Dictionary<string, string>
            {
                {"javax.jdo.option.ConnectionURL", connectionUrl},
                {"javax.jdo.option.ConnectionUserName", username},
                {"javax.jdo.option.ConnectionPassword", metastore.Password}
            };
            var configKey = "";
            if (metastoreType.Equals("hive", StringComparison.OrdinalIgnoreCase))
            {
                configKey = ConfigurationKey.HiveSite;
            }
            else if (metastoreType.Equals("oozie", StringComparison.OrdinalIgnoreCase))
            {
                configKey = ConfigurationKey.OozieSite;
            }

            return new Dictionary<string, Dictionary<string, string>> { { configKey, config } };
        }
 private static Dictionary<string, Dictionary<string, string>> GetMetastoreConfig(Metastore metastore, OSType osType, string metastoreType)
 {
     if (osType == OSType.Windows)
     {
         return GetMetastoreConfigPaas(metastore, metastoreType);
     }
     else
     {
         return GetMetastoreConfigIaas(metastore, metastoreType);
     }
 }