Exemplo n.º 1
0
        /// <summary>
        /// This seeding function can be used to initially seed an marketplace
        /// it is also meant to be safe to call after an marketplace has been seeded (by including seed.MarketplaceID)
        /// If a method starts with CreateOrUpdate it will update the resource every time its called based on what has been defined in SeedConstants.cs
        /// If a method starts with CreateOnlyOnce it will only create the resource once and then ignore thereafter
        /// The CreateOnlyOnce resources are likely to change after initial creation so we ignore to avoid overwriting desired changes that happen outside of seeding
        /// </summary>
        public async Task <EnvironmentSeedResponse> Seed(EnvironmentSeed seed)
        {
            OcEnv requestedEnv = validateEnvironment(seed.OrderCloudSettings.Environment);

            if (requestedEnv.environmentName == OrderCloudEnvironments.Production.environmentName && seed.MarketplaceID == null)
            {
                throw new Exception("Cannot create a production environment via the environment seed endpoint. Please contact an OrderCloud Developer to create a production marketplace.");
            }

            // lets us handle requests to multiple api environments
            _oc = new OrderCloudClient(new OrderCloudClientConfig
            {
                ApiUrl  = requestedEnv.apiUrl,
                AuthUrl = requestedEnv.apiUrl
            });

            var portalUserToken = await _portal.Login(seed.PortalUsername, seed.PortalPassword);

            var marketplace = await GetOrCreateMarketplace(portalUserToken, requestedEnv.environmentName, seed.MarketplaceName, seed.MarketplaceID);

            var marketplaceToken = await _portal.GetMarketplaceToken(marketplace.Id, portalUserToken);

            await CreateOrUpdateDefaultSellerUser(seed, marketplaceToken);

            await CreateOnlyOnceIncrementors(marketplaceToken);                       // must be before CreateBuyers
            await CreateOrUpdateMessageSendersAndAssignments(seed, marketplaceToken); // must be before CreateBuyers and CreateSuppliers

            await CreateOrUpdateSecurityProfiles(marketplaceToken);
            await CreateOnlyOnceBuyers(seed, marketplaceToken);

            await CreateOnlyOnceApiClients(seed, marketplaceToken);
            await CreateOrUpdateSecurityProfileAssignments(seed, marketplaceToken);

            await CreateOrUpdateXPIndices(marketplaceToken);
            await CreateOrUpdateAndAssignIntegrationEvents(marketplaceToken, seed);
            await CreateOrUpdateSuppliers(seed, marketplaceToken);

            await CreateOrUpdateProductFacets(marketplaceToken);

            if (seed?.StorageAccountSettings?.ConnectionString != null && seed?.StorageAccountSettings?.ContainerNameTranslations != null)
            {
                await UpdateTranslations(seed.StorageAccountSettings.ConnectionString, seed.StorageAccountSettings.ContainerNameTranslations);
            }

            var apiClients = await GetApiClients(marketplaceToken);

            return(new EnvironmentSeedResponse
            {
                Comments = "Success! Your environment is now seeded. The following clientIDs & secrets should be used to finalize the configuration of your application. The initial admin username and password can be used to sign into your admin application",
                MarketplaceName = marketplace.Name,
                MarketplaceID = marketplace.Id,
                OrderCloudEnvironment = requestedEnv.environmentName,
                ApiClients = new Dictionary <string, dynamic>
                {
                    ["Middleware"] = new
                    {
                        ClientID = apiClients.MiddlewareApiClient.ID,
                        ClientSecret = apiClients.MiddlewareApiClient.ClientSecret
                    },
                    ["Seller"] = new
                    {
                        ClientID = apiClients.AdminUiApiClient.ID
                    },
                    ["Buyer"] = new
                    {
                        ClientID = apiClients.BuyerUiApiClient.ID
                    }
                }
            });
        }