示例#1
0
        public void Execute()
        {
            // we are pulling command line arguments therefore we are going directly to the configuration object
            var _configurationFilePath = _configurationRoot.GetValue <string>("configurationFilePath");
            var _environmentFilePath   = _configurationRoot.GetValue <string>("environmentFilePath");

            var postmanEnvironment = new PostmanEnvironment();

            _clientAppRepo.Reset();

            if (!string.IsNullOrEmpty(_configurationFilePath))
            {
                _logger.Debug($"configurationPath = {_configurationFilePath}");

                if (!File.Exists(_configurationFilePath))
                {
                    throw new Exception($"Configuration file {_configurationFilePath} does not exists.");
                }

                _testHarnessConfiguration =
                    JsonConvert.DeserializeObject <TestHarnessConfiguration>(File.ReadAllText(_configurationFilePath));
            }
            else
            {
                _testHarnessConfiguration.Vendors = CreateDefaultVendor();
            }

            foreach (var vendor in _testHarnessConfiguration.Vendors)
            {
                var user = _clientAppRepo.GetUser(vendor.Email) ??
                           _clientAppRepo.CreateUser(
                    new User
                {
                    FullName = vendor.VendorName,
                    Email    = vendor.Email,
                    Vendor   = _clientAppRepo.CreateOrGetVendor(
                        vendor.Email, vendor.VendorName, vendor.NamespacePrefixes)
                });

                foreach (var app in vendor.Applications)
                {
                    var application = _clientAppRepo.CreateApplicationForVendor(
                        user.Vendor.VendorId, app.ApplicationName, app.ClaimSetName);

                    var leaIds = app.ApiClients.SelectMany(s => s.LocalEducationOrganizations).Distinct().ToList();

                    _defaultApplicationCreator.AddLeaIdsToApplication(leaIds, application.ApplicationId);

                    foreach (var client in app.ApiClients)
                    {
                        var key = !string.IsNullOrEmpty(client.Key)
                            ? client.Key
                            : GetGuid();

                        var secret = !string.IsNullOrEmpty(client.Secret)
                            ? client.Secret
                            : GetGuid();

                        var apiClient = _clientAppRepo.CreateApiClient(user.UserId, client.ApiClientName, key, secret);

                        postmanEnvironment.Values.Add(
                            new ValueItem
                        {
                            Enabled = true,
                            Value   = key,
                            Key     = "ApiKey_" + client.ApiClientName
                        });

                        postmanEnvironment.Values.Add(
                            new ValueItem
                        {
                            Enabled = true,
                            Value   = secret,
                            Key     = "ApiSecret_" + client.ApiClientName
                        });

                        _clientAppRepo.AddLeaIdsToApiClient(
                            user.UserId, apiClient.ApiClientId, client.LocalEducationOrganizations,
                            application.ApplicationId);

                        postmanEnvironment.Values.Add(
                            new ValueItem
                        {
                            Enabled = true,
                            Value   = client.LocalEducationOrganizations,
                            Key     = client.ApiClientName + "LeaId"
                        });
                    }

                    if (app.Profiles != null)
                    {
                        _clientAppRepo.AddProfilesToApplication(app.Profiles, application.ApplicationId);
                    }
                }
            }

            CreateEnvironmentFile();

            void CreateEnvironmentFile()
            {
                if (!string.IsNullOrEmpty(_environmentFilePath) && new DirectoryInfo(_environmentFilePath).Exists)
                {
                    postmanEnvironment.Values.Add(
                        new ValueItem
                    {
                        Enabled = true,
                        Value   = _configuration.GetValue <string>("Urls") ?? "http://*****:*****@ed-fi.org",
                    VendorName   = "Test Admin",
                    Applications = new List <Application> {
                        application
                    },
                    NamespacePrefixes = new List <string>
                    {
                        "uri://ed-fi.org",
                        "uri://gbisd.edu",
                        "uri://tpdm.ed-fi.org"
                    }
                };

                return(new List <Vendor> {
                    vendor
                });
            }

            string GetGuid()
            {
                return(Guid.NewGuid().ToString("N").Substring(0, 20));
            }
        }
示例#2
0
        internal static int Main(string[] args)
        {
            var result = ReturnStatus.Success;

            Options options = null;

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(opts => options = opts)
            .WithNotParsed(
                errs =>
            {
                Console.WriteLine("Invalid options were entered.");
                Console.WriteLine(errs.ToString());
                result = ReturnStatus.Error;
            });

            if (result != ReturnStatus.Success)
            {
                return((int)result);
            }

            if (!string.IsNullOrEmpty(options.ConfigurationFilePath))
            {
                if (!File.Exists(options.ConfigurationFilePath))
                {
                    throw new Exception($"Configuration file {options.ConfigurationFilePath} does not exists.");
                }

                _testHarnessConfiguration =
                    JsonConvert.DeserializeObject <TestHarnessConfiguration>(File.ReadAllText(options.ConfigurationFilePath));
            }
            else
            {
                _testHarnessConfiguration.Vendors = CreateDefaultVendor();
            }

            UpdateAdminDatabase();

            StartApi();

            void StartApi()
            {
                string baseAddress = ConfigurationManager.AppSettings["selfHost:baseAddress"] ?? "http://*****:*****@ed-fi.org",
                    VendorName   = "Test Admin",
                    Applications = new List <Application> {
                        applicaton
                    },
                    NamespacePrefixes = new List <string>
                    {
                        "uri://ed-fi.org",
                        "uri://gbisd.edu",
                        "uri://tpdm.ed-fi.org"
                    }
                };

                return(new List <Vendor> {
                    vendor
                });
            }

            void UpdateAdminDatabase()
            {
                using (var container = new WindsorContainer())
                {
                    RegisterComponents();
                    var applicationCreator = container.Resolve <IDefaultApplicationCreator>();
                    var clientAppRepo      = container.Resolve <IClientAppRepo>();
                    var postmanEnvironment = new PostmanEnvironment();

                    clientAppRepo.Reset();

                    foreach (var vendor in _testHarnessConfiguration.Vendors)
                    {
                        var user = clientAppRepo.GetUser(vendor.Email) ??
                                   clientAppRepo.CreateUser(
                            new User
                        {
                            FullName = vendor.VendorName,
                            Email    = vendor.Email,
                            Vendor   = clientAppRepo.CreateOrGetVendor(
                                vendor.Email, vendor.VendorName, vendor.NamespacePrefixes)
                        });

                        foreach (var app in vendor.Applications)
                        {
                            var application = clientAppRepo.CreateApplicationForVendor(
                                user.Vendor.VendorId, app.ApplicationName, app.ClaimSetName);

                            var leaIds = app.ApiClients.SelectMany(s => s.LocalEducationOrganizations).Distinct().ToList();
                            applicationCreator.AddLeaIdsToApplication(leaIds, application.ApplicationId);

                            foreach (var client in app.ApiClients)
                            {
                                var key = !string.IsNullOrEmpty(client.Key)
                                    ? client.Key
                                    : GetGuid();

                                var secret = !string.IsNullOrEmpty(client.Secret)
                                    ? client.Secret
                                    : GetGuid();

                                var apiClient = clientAppRepo.CreateApiClient(user.UserId, client.ApiClientName, key, secret);

                                postmanEnvironment.Values.Add(
                                    new ValueItem
                                {
                                    Enabled = true,
                                    Value   = key,
                                    Key     = "ApiKey_" + client.ApiClientName
                                });

                                postmanEnvironment.Values.Add(
                                    new ValueItem
                                {
                                    Enabled = true,
                                    Value   = secret,
                                    Key     = "ApiSecret_" + client.ApiClientName
                                });

                                clientAppRepo.AddLeaIdsToApiClient(
                                    user.UserId, apiClient.ApiClientId, client.LocalEducationOrganizations,
                                    application.ApplicationId);

                                postmanEnvironment.Values.Add(
                                    new ValueItem
                                {
                                    Enabled = true,
                                    Value   = client.LocalEducationOrganizations,
                                    Key     = client.ApiClientName + "LeaId"
                                });
                            }

                            if (app.Profiles != null)
                            {
                                clientAppRepo.AddProfilesToApplication(app.Profiles, application.ApplicationId);
                            }
                        }
                    }

                    CreateEnvironmentFile();

                    void CreateEnvironmentFile()
                    {
                        if (!string.IsNullOrEmpty(options.EnvironmentFilePath) &&
                            new DirectoryInfo(options.EnvironmentFilePath).Exists)
                        {
                            postmanEnvironment.Values.Add(
                                new ValueItem
                            {
                                Enabled = true,
                                Value   = ConfigurationManager.AppSettings["selfHost:baseAddress"] ?? "http://localhost:8765/",
                                Key     = "ApiBaseUrl"
                            });

                            postmanEnvironment.Values.Add(
                                new ValueItem
                            {
                                Enabled = true,
                                Value   = ConfigurationManager.AppSettings["composites:featureIsEnabled"],
                                Key     = "CompositesFeatureIsEnabled"
                            });

                            postmanEnvironment.Values.Add(
                                new ValueItem
                            {
                                Enabled = true,
                                Value   = ConfigurationManager.AppSettings["profiles:featureIsEnabled"],
                                Key     = "ProfilesFeatureIsEnabled"
                            });

                            var jsonString = JsonConvert.SerializeObject(
                                postmanEnvironment,
                                Formatting.Indented,
                                new JsonSerializerSettings {
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            });

                            var fileName = Path.Combine(options.EnvironmentFilePath, "environment.json");

                            File.WriteAllText(fileName, jsonString);
                        }
                    }

                    string GetGuid()
                    {
                        return(Guid.NewGuid().ToString().Replace("-", "").Substring(0, 20));
                    }

                    void RegisterComponents()
                    {
                        container.Register(
                            Component
                            .For <IConfigConnectionStringsProvider>()
                            .ImplementedBy <AppConfigConnectionStringsProvider>(),
                            Component
                            .For <IConfigValueProvider>()
                            .ImplementedBy <AppConfigValueProvider>(),
                            Component
                            .For <IDatabaseEngineProvider>()
                            .ImplementedBy <DatabaseEngineProvider>(),
                            Component
                            .For <IApiConfigurationProvider>()
                            .ImplementedBy <ApiConfigurationProvider>(),
                            Component
                            .For <IUsersContextFactory>()
                            .ImplementedBy <UsersContextFactory>(),
                            Component.For <IClientAppRepo>()
                            .ImplementedBy <ClientAppRepo>(),
                            Component
                            .For <ISecurityContextFactory>()
                            .ImplementedBy <SecurityContextFactory>(),
                            Component.For <ISecurityRepository>()
                            .ImplementedBy <SecurityRepository>(),
                            Component.For <IClientCreator>()
                            .ImplementedBy <ClientCreator>(),
                            Component.For <IDefaultApplicationCreator>()
                            .ImplementedBy <DefaultApplicationCreator>(),
                            Component
                            .For <IInitializationSettingsFactory, InitializationSettingsFactory>(),
                            Component
                            .For <InitializationModel>()
                            .UsingFactoryMethod(k => k.Resolve <IInitializationSettingsFactory>().GetInitializationModel()),
                            Component
                            .For <InitializationEngine>(),
                            Component
                            .For <ISandboxProvisioner, SqlServerSandboxProvisioner>(),
                            Component.For <IDatabaseNameBuilder>()
                            .ImplementedBy <DatabaseNameBuilder>(),
                            Component.For <IDbConnectionStringBuilderAdapterFactory>()
                            .ImplementedBy <DbConnectionStringBuilderAdapterFactory>());

                        var apiConfigurationProvider = container.Resolve <IApiConfigurationProvider>();

                        if (apiConfigurationProvider.DatabaseEngine == DatabaseEngine.SqlServer)
                        {
                            container.Register(
                                Component.For <ITemplateDatabaseLeaQuery>()
                                .ImplementedBy <SqlServerTemplateDatabaseLeaQuery>());
                        }
                        else
                        {
                            container.Register(
                                Component.For <ITemplateDatabaseLeaQuery>()
                                .ImplementedBy <PostgresTemplateDatabaseLeaQuery>());
                        }
                    }
                }
            }

            return((int)ReturnStatus.Success);
        }