Example #1
0
        static string ReplaceExistingResources(IList <ARMResources> repl, string template, string parameters,
                                               string resourceGroupName, string deploymentName)
        {
            var t           = new ArmTemplate(JToken.Parse(template));
            var p           = JObject.Parse(parameters);
            var dep         = new Deployment("subscription", resourceGroupName, deploymentName, t, p);
            var index       = 0;
            var toBeDeleted = new List <KeyValuePair <int, string> >();

            foreach (var rsrc in t.Resources.Resources)
            {
                foreach (var armResourcese in repl)
                {
                    if (armResourcese.Index == index && armResourcese.Type.ToLower() == rsrc.Type.ToLower())
                    {
                        if (!string.IsNullOrEmpty(armResourcese.Value))
                        {
                            // To be deleted
                            toBeDeleted.Add(new KeyValuePair <int, string>(index, armResourcese.Value));
                            // Replace dependency for all resources depend on this one
                        }
                    }
                }
                index++;
            }
            // Process in reversed order
            toBeDeleted.Sort();
            toBeDeleted.Reverse();

            // Replace dependencies
            foreach (var keyValuePair in toBeDeleted)
            {
                var name = (t.Resources.Resources[keyValuePair.Key].Type + "/" + dep.Evaluate(t.Resources.Resources[keyValuePair.Key].Name)).ToString().ToLower();
                foreach (var resource in t.Resources.Resources)
                {
                    int depIndex = 0;
                    foreach (var dependsOn in resource.DependsOn)
                    {
                        try
                        {
                            var d = dep.Evaluate(dependsOn);
                            if (d.ToString().ToLower() == name)
                            {
                                // Update dependencies
                                resource.SetDependency(depIndex, keyValuePair.Value);
                            }
                        }
                        catch (ExpressionException)
                        {
                            // TODO: Warning
                            // Skip resource which is unable to be processed
                        }
                        depIndex++;
                    }
                }
            }
            // Delete resources
            foreach (var keyValuePair in toBeDeleted)
            {
                t.Resources.Model.RemoveAt(keyValuePair.Key);
            }
            return(t.Model.ToString());
        }