Esempio n. 1
0
        public static Copy Parse(JsonElement root, Dictionary <string, object> context, ARMFunctions functions, IInfrastructure infrastructure)
        {
            var copy          = new Copy();
            var deployContext = context[ContextKeys.ARM_CONTEXT] as DeploymentContext;

            if (root.TryGetProperty("name", out JsonElement name))
            {
                copy.Name = name.GetString();
            }
            if (root.TryGetProperty("count", out JsonElement count))
            {
                if (count.ValueKind == JsonValueKind.Number)
                {
                    copy.Count = count.GetInt32();
                }
                else if (count.ValueKind == JsonValueKind.String)
                {
                    copy.Count = (int)functions.Evaluate(count.GetString(), context);
                }
                else
                {
                    throw new Exception("the value of count property should be Number in copy node");
                }
                // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#valid-uses-1
                if (context.ContainsKey(ContextKeys.DEPENDSON))
                {
                    throw new Exception("You can't use the reference function to set the value of the count property in a copy loop.");
                }
            }
            else
            {
                throw new Exception("not find count in copy node");
            }
            if (root.TryGetProperty("mode", out JsonElement mode))
            {
                copy.Mode = mode.GetString().ToLower();
            }
            if (root.TryGetProperty("batchSize", out JsonElement batchSize))
            {
                if (batchSize.ValueKind == JsonValueKind.Number)
                {
                    copy.BatchSize = batchSize.GetInt32();
                }
                else if (batchSize.ValueKind == JsonValueKind.String)
                {
                    copy.BatchSize = (int)functions.Evaluate(batchSize.GetString(), context);
                }
            }
            if (root.TryGetProperty("input", out JsonElement input))
            {
                copy.Input = input.GetRawText();
            }
            copy.Id = functions.ResourceId(deployContext, new object[] {
                $"{infrastructure.BuiltinServiceTypes.Deployments}/{Copy.ServiceType}",
                deployContext.DeploymentName,
                copy.Name
            });
            return(copy);
        }
Esempio n. 2
0
        private static List <Resource> ParseInternal(
            JsonElement resourceElement
            , Dictionary <string, object> context
            , ARMFunctions functions
            , IInfrastructure infrastructure
            , string parentName,
            string parentType)
        {
            DeploymentContext deploymentContext = context[ContextKeys.ARM_CONTEXT] as DeploymentContext;
            List <Resource>   resources         = new List <Resource>();
            Resource          r = new Resource();

            resources.Add(r);

            if (resourceElement.TryGetProperty("condition", out JsonElement condition))
            {
                if (condition.ValueKind == JsonValueKind.False)
                {
                    r.Condition = false;
                }
                else if (condition.ValueKind == JsonValueKind.String)
                {
                    r.Condition = (bool)functions.Evaluate(condition.GetString(), context);
                }
            }

            if (resourceElement.TryGetProperty("apiVersion", out JsonElement apiVersion))
            {
                r.ApiVersion = apiVersion.GetString();
            }
            else
            {
                throw new Exception("not find apiVersion in resource node");
            }

            if (resourceElement.TryGetProperty("type", out JsonElement type))
            {
                r.Type = type.GetString();
            }
            else
            {
                throw new Exception("not find type in resource node");
            }

            if (!string.IsNullOrEmpty(parentType))
            {
                r.FullType = $"{parentType}/{r.Type}";
            }
            else
            {
                r.FullType = r.Type;
            }

            if (resourceElement.TryGetProperty("name", out JsonElement name))
            {
                r.Name = functions.Evaluate(name.GetString(), context).ToString();
            }
            else
            {
                throw new Exception("not find name in resource node");
            }

            if (!string.IsNullOrEmpty(parentName))
            {
                r.FullName = $"{parentName}/{r.Name}";
            }
            else
            {
                r.FullName = r.Name;
            }

            if (resourceElement.TryGetProperty("resourceGroup", out JsonElement resourceGroup))
            {
                r.ResourceGroup = functions.Evaluate(resourceGroup.GetString(), context).ToString();
            }
            else
            {
                r.ResourceGroup = deploymentContext.ResourceGroup;
            }

            if (resourceElement.TryGetProperty("subscriptionId", out JsonElement subscriptionId))
            {
                r.SubscriptionId = functions.Evaluate(subscriptionId.GetString(), context).ToString();
            }
            else
            {
                r.SubscriptionId = deploymentContext.SubscriptionId;
            }

            if (resourceElement.TryGetProperty("managementGroupId", out JsonElement managementGroupId))
            {
                r.ManagementGroupId = functions.Evaluate(managementGroupId.GetString(), context).ToString();
            }
            else
            {
                r.ManagementGroupId = deploymentContext.ManagementGroupId;
            }

            if (resourceElement.TryGetProperty("location", out JsonElement location))
            {
                r.Location = functions.Evaluate(location.GetString(), context).ToString();
            }

            if (resourceElement.TryGetProperty("comments", out JsonElement comments))
            {
                r.Comments = comments.GetString();
            }

            if (resourceElement.TryGetProperty("dependsOn", out JsonElement dependsOn))
            {
                using var dd = JsonDocument.Parse(dependsOn.GetRawText());
                foreach (var item in dd.RootElement.EnumerateArray())
                {
                    r.DependsOn.Add(functions.Evaluate(item.GetString(), context).ToString());
                }
            }

            #region ResouceId

            if (deploymentContext.Template.DeployLevel == DeployLevel.ResourceGroup)
            {
                List <object> pars = new List <object>
                {
                    r.SubscriptionId,
                    r.ResourceGroup,
                    r.FullType
                };
                pars.AddRange(r.FullName.Split('/'));
                r.ResourceId = functions.ResourceId(
                    deploymentContext,
                    pars.ToArray());
            }
            else if (deploymentContext.Template.DeployLevel == DeployLevel.Subscription)
            {
                List <object> pars = new List <object>
                {
                    r.SubscriptionId,
                    r.FullType
                };
                pars.AddRange(r.FullName.Split('/'));
                r.ResourceId = functions.SubscriptionResourceId(deploymentContext, pars.ToArray());
            }
            else
            {
                List <object> pars = new List <object>
                {
                    r.FullType
                };
                pars.AddRange(r.FullName.Split('/'));
                r.ResourceId = functions.TenantResourceId(pars.ToArray());
            }

            #endregion ResouceId

            if (resourceElement.TryGetProperty("sku", out JsonElement sku))
            {
                r.SKU = SKU.Parse(sku, functions, context);
            }
            else
            {
                r.SKU = new SKU()
                {
                    Name = SKU.Default
                }
            };

            if (resourceElement.TryGetProperty("kind", out JsonElement kind))
            {
                r.Kind = kind.GetString();
            }

            if (resourceElement.TryGetProperty("plan", out JsonElement plan))
            {
                r.Plan = plan.GetRawText();
            }

            if (resourceElement.TryGetProperty("zones", out JsonElement zones))
            {
                foreach (var z in zones.EnumerateArray())
                {
                    r.Zones.Add(functions.Evaluate(z.GetString(), context).ToString());
                }
            }

            if (context.ContainsKey(ContextKeys.DEPENDSON))
            {
                //https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#valid-uses-1
                throw new Exception("The reference function can only be used in the properties of a resource definition and the outputs section of a template or deployment.");
            }

            if (!r.Condition)
            {
                return(resources);
            }

            if (resourceElement.TryGetProperty("properties", out JsonElement properties))
            {
                if (r.FullType == infrastructure.BuiltinServiceTypes.Deployments)
                {
                    r.Properties = properties.GetRawText();
                }
                else
                {
                    r.Properties = properties.ExpandObject(context, functions, infrastructure);
                    // if there has Implicit dependency by reference function in properties
                    // the reference function should be evaluate at provisioning time
                    // so keep the original text
                    if (HandleDependsOn(r, context))
                    {
                        r.Properties = properties.GetRawText();
                    }
                }
            }
            if (resourceElement.TryGetProperty("resources", out JsonElement _resources))
            {
                foreach (var childres in _resources.EnumerateArray())
                {
                    //https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/child-resource-name-type
                    var childResult = Resource.Parse(childres.GetRawText(), context, functions, infrastructure, r.Name, r.Type);
                    r.Resources.Add(childResult[0].Name);
                    resources.AddRange(childResult);
                }
            }
            return(resources);
        }