示例#1
0
        public static async Task CompareAndSyncProducts(string folder, Dictionary <string, Product> products, Dictionary <string, Product> authoritiveProducts)
        {
            var newProducts = authoritiveProducts.Keys.Except(products.Keys);

            foreach (var product in newProducts)
            {
                var p = authoritiveProducts[product];
                p.RefsGroups = new List <string>(); // Clear groups
                //Add New Product to Existing
                var folderPath = $"{folder}\\api-management\\products\\{product}";
                Directory.CreateDirectory(folderPath);

                var json     = JsonSerialisation.Serialise(p);
                var filePath = $"{folderPath}\\{ApimUtils.ConfigurationFileName}";
                System.Console.WriteLine($"Writing {filePath}");
                await File.WriteAllTextAsync(filePath, json);

                if (!string.IsNullOrEmpty(p.RefDescription))
                {
                    System.Console.WriteLine("Writing Ref File");
                    await File.WriteAllTextAsync(Path.Combine(folder, p.RefDescription), p.Description);
                }
            }
            var oldProducts = products.Keys.Except(authoritiveProducts.Keys);

            foreach (var product in oldProducts)
            {
                //Remove Products
                var p = products[product];
                if (!string.IsNullOrEmpty(p.RefDescription))
                {
                    var filePath = Path.Combine(folder, p.RefDescription);
                    System.Console.WriteLine($"Deleting {filePath}");
                    File.Delete(filePath);
                }
                var folderPath = Path.Combine($"{folder}\\api-management\\products\\", product);
                File.Delete(Path.Combine(folderPath, ApimUtils.ConfigurationFileName));

                Directory.Delete(folderPath);

                products.Remove(product);
            }
            foreach (var product in products)
            {
                if (!ProductSync.IsInSync(product.Value, authoritiveProducts[product.Key], false))
                {
                    System.Console.WriteLine($"OUT OF SYNC: {product.Value.Id}");
                    //Ensure it needs to be Sync'd
                    var updatedPropduct = ProductSync.Sync(product.Value, authoritiveProducts[product.Key], false);

                    //Now Save
                    var json     = JsonSerialisation.Serialise(updatedPropduct);
                    var filePath = $"{folder}\\api-management\\products\\{product.Key}\\{ApimUtils.ConfigurationFileName}"; //Path.Combine(folder1, @"\api-management\products", product.Key, "configuration.json");
                    await File.WriteAllTextAsync(filePath, json);
                }
                else
                {
                    System.Console.WriteLine($"Product {product.Value.Id} Is in sync");
                }

                //Check Ref
                if (product.Value.Description != authoritiveProducts[product.Key].Description)
                {
                    System.Console.WriteLine($"Definition Out of date");
                    var filePath = Path.Combine(folder, product.Value.RefDescription);
                    await File.WriteAllTextAsync(filePath, authoritiveProducts[product.Key].Description);
                }
            }
        }
示例#2
0
        public static async Task CompareAndSync(string folder, Dictionary <string, Api> entities, string authoritiveFolder, Dictionary <string, Api> authoritive)
        {
            var newEntities = authoritive.Keys.Except(entities.Keys);

            foreach (var entity in newEntities)
            {
                var t = authoritive[entity];

                //Add New Tag to Existing
                var folderPath = $"{folder}\\{_folderPart}\\{entity}";
                Directory.CreateDirectory(folderPath);

                var json     = JsonSerialisation.Serialise(t);
                var filePath = $"{folderPath}\\{ApimUtils.ConfigurationFileName}";
                System.Console.WriteLine($"Writing {filePath}");
                await File.WriteAllTextAsync(filePath, json);

                //Check Refs
                if (!string.IsNullOrWhiteSpace(t.RefDescription))
                {
                    //Check Description
                    await CreateRef(t.RefDescription, folder, authoritiveFolder);
                }

                //Operations
                foreach (var o in t.Operations)
                {
                    if (!string.IsNullOrWhiteSpace(o.RefDescription))
                    {
                        // Check Operation Description
                        await CreateRef(o.RefDescription, folder, authoritiveFolder);
                    }

                    if (!string.IsNullOrWhiteSpace(o.RefPolicy))
                    {
                        // Check Operation Policy
                        await CreateRef(o.RefPolicy, folder, authoritiveFolder);
                    }

                    if (!string.IsNullOrWhiteSpace(o.Request.RefDescription))
                    {
                        // Check Operation Request Desicription
                        await CreateRef(o.Request.RefDescription, folder, authoritiveFolder);
                    }

                    foreach (var r in o.Request.Representations)
                    {
                        if (!string.IsNullOrEmpty(r.RefSample))
                        {
                            // Check Operation Request Representation Samples
                            await CreateRef(r.RefSample, folder, authoritiveFolder);
                        }
                    }

                    foreach (var response in o.Responses)
                    {
                        if (!string.IsNullOrEmpty(response.RefDescription))
                        {
                            //Check Operation Response Description
                            await CreateRef(response.RefDescription, folder, authoritiveFolder);
                        }

                        foreach (var r in response.Representations)
                        {
                            if (!string.IsNullOrEmpty(r.RefSample))
                            {
                                // Check Operation Response Representation Samples
                                await CreateRef(r.RefSample, folder, authoritiveFolder);
                            }
                        }
                    }
                }

                //Check Api Scheamas
                foreach (var s in t.ApiSchemas)
                {
                    if (!string.IsNullOrEmpty(s.RefDocumentValue))
                    {
                        //Check Api Schema Document Value
                        await CreateRef(s.RefDocumentValue, folder, authoritiveFolder);
                    }
                }
            }
            var oldEntities = entities.Keys.Except(authoritive.Keys);

            foreach (var release in oldEntities)
            {
                //Remove Products
                var folderPath = Path.Combine($"{folder}\\{_folderPart}\\", release);
                File.Delete(Path.Combine(folderPath, ApimUtils.ConfigurationFileName));

                Directory.Delete(folderPath, true);

                entities.Remove(release);
            }
            foreach (var entity in entities)
            {
                var a = authoritive[entity.Key];
                var e = entity.Value;
                if (!entity.Value.Equals(a))
                {
                    System.Console.WriteLine($"OUT OF SYNC: {entity.Value.Id}");
                    //Ensure it needs to be Sync'd

                    //Todo Check for Removed Entities
                    var updatedApi = Sync(e, a);

                    //Now Save
                    var json     = JsonSerialisation.Serialise(updatedApi);
                    var filePath = $"{folder}\\{_folderPart}\\{entity.Key}\\{ApimUtils.ConfigurationFileName}"; //Path.Combine(folder1, @"\api-management\products", product.Key, ApimUtils.ConfigurationFileName);
                    await File.WriteAllTextAsync(filePath, json);

                    e = updatedApi;
                }
                else
                {
                    System.Console.WriteLine($"{entity.Value.Id} Is in sync");
                }

                //Check Refs
                if (!string.IsNullOrWhiteSpace(e.RefDescription))
                {
                    //Check Description
                    await SyncRef(e.RefDescription, folder, authoritiveFolder);
                }

                //Operations
                foreach (var o in e.Operations)
                {
                    if (!string.IsNullOrWhiteSpace(o.RefDescription))
                    {
                        // Check Operation Description
                        await SyncRef(o.RefDescription, folder, authoritiveFolder);
                    }

                    if (!string.IsNullOrWhiteSpace(o.RefPolicy))
                    {
                        // Check Operation Policy
                        await SyncRef(o.RefPolicy, folder, authoritiveFolder);
                    }

                    if (!string.IsNullOrWhiteSpace(o.Request.RefDescription))
                    {
                        // Check Operation Request Desicription
                        await SyncRef(o.Request.RefDescription, folder, authoritiveFolder);
                    }


                    foreach (var r in o.Request.Representations)
                    {
                        if (!string.IsNullOrEmpty(r.RefSample))
                        {
                            // Check Operation Request Representation Samples
                            await SyncRef(r.RefSample, folder, authoritiveFolder);
                        }
                    }

                    foreach (var response in o.Responses)
                    {
                        if (!string.IsNullOrEmpty(response.RefDescription))
                        {
                            //Check Operation Response Description
                            await SyncRef(response.RefDescription, folder, authoritiveFolder);
                        }

                        foreach (var r in response.Representations)
                        {
                            if (!string.IsNullOrEmpty(r.RefSample))
                            {
                                // Check Operation Response Representation Samples
                                await SyncRef(r.RefSample, folder, authoritiveFolder);
                            }
                        }
                    }
                }

                //Check Api Scheamas
                foreach (var s in e.ApiSchemas)
                {
                    if (!string.IsNullOrEmpty(s.RefDocumentValue))
                    {
                        //Check Api Schema Document Value
                        await SyncRef(s.RefDocumentValue, folder, authoritiveFolder);
                    }
                }
            }
        }