Esempio n. 1
0
 internal static PSTemplateSpecTemplateArtifact FromAzureSDKTemplateSpecTemplateArtifact(
     LinkedTemplateArtifact artifact)
 {
     return(artifact != null
         ? new PSTemplateSpecTemplateArtifact(artifact)
         : null);
 }
Esempio n. 2
0
 protected PSTemplateSpecTemplateArtifact(
     LinkedTemplateArtifact artifact
     ) : base(artifact)
 {
     // Note: Cast is redundant, but present for clarity reasons:
     this.Template = ((JToken)artifact.Template).ToString();
 }
Esempio n. 3
0
        internal static TemplateSpecVersionData DeserializeTemplateSpecVersionData(JsonElement element)
        {
            AzureLocation location = default;
            Optional <IDictionary <string, string> > tags = default;
            ResourceIdentifier id          = default;
            string             name        = default;
            ResourceType       type        = default;
            SystemData         systemData  = default;
            Optional <string>  description = default;
            Optional <IList <LinkedTemplateArtifact> > linkedTemplates = default;
            Optional <BinaryData> metadata         = default;
            Optional <BinaryData> mainTemplate     = default;
            Optional <BinaryData> uiFormDefinition = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("location"))
                {
                    location = new AzureLocation(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("tags"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    Dictionary <string, string> dictionary = new Dictionary <string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    tags = dictionary;
                    continue;
                }
                if (property.NameEquals("id"))
                {
                    id = new ResourceIdentifier(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("type"))
                {
                    type = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("systemData"))
                {
                    systemData = JsonSerializer.Deserialize <SystemData>(property.Value.ToString());
                    continue;
                }
                if (property.NameEquals("properties"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        if (property0.NameEquals("description"))
                        {
                            description = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("linkedTemplates"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            List <LinkedTemplateArtifact> array = new List <LinkedTemplateArtifact>();
                            foreach (var item in property0.Value.EnumerateArray())
                            {
                                array.Add(LinkedTemplateArtifact.DeserializeLinkedTemplateArtifact(item));
                            }
                            linkedTemplates = array;
                            continue;
                        }
                        if (property0.NameEquals("metadata"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            metadata = BinaryData.FromString(property0.Value.GetRawText());
                            continue;
                        }
                        if (property0.NameEquals("mainTemplate"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            mainTemplate = BinaryData.FromString(property0.Value.GetRawText());
                            continue;
                        }
                        if (property0.NameEquals("uiFormDefinition"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            uiFormDefinition = BinaryData.FromString(property0.Value.GetRawText());
                            continue;
                        }
                    }
                    continue;
                }
            }
            return(new TemplateSpecVersionData(id, name, type, systemData, location, Optional.ToDictionary(tags), description.Value, Optional.ToList(linkedTemplates), metadata.Value, mainTemplate.Value, uiFormDefinition.Value));
        }
Esempio n. 4
0
 protected PSTemplateSpecArtifact(LinkedTemplateArtifact artifact)
 {
     this.Path = artifact.Path;
 }
Esempio n. 5
0
        /// <summary>
        /// Recursively packs the specified template and its referenced artifacts and
        /// adds the artifacts to the current packing context.
        /// </summary>
        /// <param name="templateAbsoluteFilePath">
        /// The path to the ARM Template .json file to pack
        /// </param>
        /// <param name="context">
        /// The packing context of the current packing operation
        /// </param>
        /// <param name="artifactableTemplateObj">
        /// The packagable template object
        /// </param>
        private static void PackArtifacts(
            string templateAbsoluteFilePath,
            PackingContext context,
            out JObject artifactableTemplateObj)
        {
            string originalDirectory = context.CurrentDirectory;

            try
            {
                context.CurrentDirectory = Path.GetDirectoryName(
                    templateAbsoluteFilePath
                    );

                string  templateJson = FileUtilities.DataStore.ReadFileAsText(templateAbsoluteFilePath);
                JObject templateObj  = artifactableTemplateObj = JObject.Parse(templateJson);

                JObject[] templateLinkToArtifactObjs = GetTemplateLinksToArtifacts(
                    templateObj, includeNested: true);

                foreach (JObject templateLinkObj in templateLinkToArtifactObjs)
                {
                    string relativePath = ((string)templateLinkObj["relativePath"])?
                                          .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

                    if (string.IsNullOrWhiteSpace(relativePath))
                    {
                        continue; // Throw here?
                    }

                    // This is a templateLink to a local template... Get the absolute path of the
                    // template based on its relative path from the current template directory and
                    // make sure it exists:

                    string absoluteLocalPath = Path.Combine(context.CurrentDirectory, relativePath);
                    if (!File.Exists(absoluteLocalPath))
                    {
                        throw new FileNotFoundException(absoluteLocalPath);
                    }

                    // Let's make sure we're not referencing a file outside of our root directory
                    // heirarchy. We won't allow such references for security purposes:

                    if (!absoluteLocalPath.StartsWith(
                            context.RootTemplateDirectory + Path.DirectorySeparatorChar,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(
                                  $"Unable to handle the reference to file '{absoluteLocalPath}' from " +
                                  $"'{templateAbsoluteFilePath}' because it exists outside of the root template " +
                                  $"directory of '{context.RootTemplateDirectory}'");
                    }

                    // Convert the template relative path to one that is relative to our root
                    // directory path, and then if we haven't already processed that template into
                    // an artifact elsewhere, we'll do so here...

                    string asRelativePath = AbsoluteToRelativePath(context.RootTemplateDirectory, absoluteLocalPath);
                    if (context.Artifacts.Any(prevAddedArtifact => prevAddedArtifact.Path.Equals(
                                                  asRelativePath, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue; // We've already packed this artifact from elsewhere
                    }

                    PackArtifacts(absoluteLocalPath, context, out JObject templateObjForArtifact);

                    LinkedTemplateArtifact artifact = new LinkedTemplateArtifact
                    {
                        Path     = asRelativePath,
                        Template = templateObjForArtifact
                    };

                    context.Artifacts.Add(artifact);
                }
            }
            finally
            {
                context.CurrentDirectory = originalDirectory;
            }
        }