Exemplo n.º 1
0
        public static string BuildFile <T>(ScriptExecutor <T> executeScript, string bicepTemplateFilePath)
        {
            if (!IsBicepExecutable && !CheckBicepExecutable(executeScript))
            {
                throw new AzPSApplicationException(Properties.Resources.BicepNotFound);
            }

            string tempPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(bicepTemplateFilePath));

            try{
                if (Uri.IsWellFormedUriString(bicepTemplateFilePath, UriKind.Absolute))
                {
                    FileUtilities.DataStore.WriteFile(tempPath, GeneralUtilities.DownloadFile(bicepTemplateFilePath));
                }
                else if (FileUtilities.DataStore.FileExists(bicepTemplateFilePath))
                {
                    File.Copy(bicepTemplateFilePath, tempPath, true);
                }
                else
                {
                    throw new AzPSArgumentException(Properties.Resources.InvalidBicepFilePathOrUri, "TemplateFile");
                }
                executeScript($"bicep build '{tempPath}'");
                return(tempPath.Replace(".bicep", ".json"));
            }
            finally
            {
                File.Delete(tempPath);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the parameters for a given gallery template.
        /// </summary>
        /// <param name="templateIdentity">The gallery template name</param>
        /// <param name="templateParameterObject">Existing template parameter object</param>
        /// <param name="templateParameterFilePath">Path to the template parameter file if present</param>
        /// <param name="staticParameters">The existing PowerShell cmdlet parameters</param>
        /// <returns>The template parameters</returns>
        public virtual RuntimeDefinedParameterDictionary GetTemplateParametersFromGallery(string templateIdentity, Hashtable templateParameterObject, string templateParameterFilePath, string[] staticParameters)
        {
            RuntimeDefinedParameterDictionary dynamicParameters = new RuntimeDefinedParameterDictionary();
            string templateContent = null;

            templateContent = GeneralUtilities.DownloadFile(GetGalleryTemplateFile(templateIdentity));

            dynamicParameters = ParseTemplateAndExtractParameters(templateContent, templateParameterObject, templateParameterFilePath, staticParameters);
            return(dynamicParameters);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the JToken from parameter
        /// </summary>
        /// <param name="parameter">The parameter to be parsed.</param>
        private JToken GetTokenFromParameter(string parameter)
        {
            Uri outUri = null;

            if (Uri.TryCreate(parameter, UriKind.Absolute, out outUri))
            {
                if (outUri.Scheme == Uri.UriSchemeFile)
                {
                    string filePath = this.TryResolvePath(parameter);
                    if (File.Exists(filePath))
                    {
                        return(JToken.Parse(FileUtilities.DataStore.ReadFileAsText(filePath)));
                    }
                    else
                    {
                        throw new PSInvalidOperationException(string.Format(ProjectResources.InvalidFilePath, parameter));
                    }
                }
                else if (outUri.Scheme != Uri.UriSchemeHttp && outUri.Scheme != Uri.UriSchemeHttps)
                {
                    throw new PSInvalidOperationException(ProjectResources.InvalidUriScheme);
                }
                else if (!Uri.IsWellFormedUriString(outUri.AbsoluteUri, UriKind.Absolute))
                {
                    throw new PSInvalidOperationException(string.Format(ProjectResources.InvalidUriString, parameter));
                }
                else
                {
                    string contents = GeneralUtilities.DownloadFile(outUri.AbsoluteUri);
                    if (string.IsNullOrEmpty(contents))
                    {
                        throw new PSInvalidOperationException(string.Format(ProjectResources.InvalidUriContent, parameter));
                    }

                    return(JToken.Parse(contents));
                }
            }

            //for non absolute file paths
            string path = this.TryResolvePath(parameter);

            return(File.Exists(path)
                ? JToken.Parse(FileUtilities.DataStore.ReadFileAsText(path))
                : JToken.Parse(parameter));
        }
Exemplo n.º 4
0
        private static RuntimeDefinedParameterDictionary ParseTemplateAndExtractParameters(string templateContent, Hashtable templateParameterObject, string templateParameterFilePath, string[] staticParameters)
        {
            RuntimeDefinedParameterDictionary dynamicParameters = new RuntimeDefinedParameterDictionary();

            if (!string.IsNullOrEmpty(templateContent))
            {
                TemplateFile templateFile = null;

                try
                {
                    templateFile = JsonConvert.DeserializeObject <TemplateFile>(templateContent);
                    if (templateFile.Parameters == null)
                    {
                        return(dynamicParameters);
                    }
                }
                catch (Exception e)
                {
                    // Can't parse the template file, do not generate dynamic parameters
                    Debug.WriteLine("Unable to parse template file. The exception received was: " + e.Message);
                    return(dynamicParameters);
                }

                foreach (KeyValuePair <string, TemplateFileParameterV1> parameter in templateFile.Parameters)
                {
                    RuntimeDefinedParameter dynamicParameter = ConstructDynamicParameter(staticParameters, parameter);
                    dynamicParameters.Add(dynamicParameter.Name, dynamicParameter);
                }
            }
            if (templateParameterObject != null)
            {
                UpdateParametersWithObject(dynamicParameters, templateParameterObject);
            }
            if (templateParameterFilePath != null && FileUtilities.DataStore.FileExists(templateParameterFilePath))
            {
                var parametersFromFile = ParseTemplateParameterFileContents(templateParameterFilePath);
                UpdateParametersWithObject(dynamicParameters, new Hashtable(parametersFromFile));
            }
            if (templateParameterFilePath != null && Uri.IsWellFormedUriString(templateParameterFilePath, UriKind.Absolute))
            {
                var parametersFromUri = ParseTemplateParameterContent(GeneralUtilities.DownloadFile(templateParameterFilePath));
                UpdateParametersWithObject(dynamicParameters, new Hashtable(parametersFromUri));
            }
            return(dynamicParameters);
        }
Exemplo n.º 5
0
        private string GetTemplate(string templateFile)
        {
            string template = string.Empty;

            if (!string.IsNullOrEmpty(templateFile))
            {
                if (Uri.IsWellFormedUriString(templateFile, UriKind.Absolute))
                {
                    template = GeneralUtilities.DownloadFile(templateFile);
                }
                else
                {
                    template = FileUtilities.DataStore.ReadFileAsText(templateFile);
                }
            }

            return(template);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Downloads a gallery template file into specific directory.
        /// </summary>
        /// <param name="identity">The gallery template file identity</param>
        /// <param name="outputPath">The file output path</param>
        /// <param name="overwrite">Overrides existing file</param>
        /// <param name="confirmAction">The confirmation action</param>
        /// <returns>The file path</returns>
        public virtual string DownloadGalleryTemplateFile(string identity, string outputPath, bool overwrite, Action <bool, string, string, string, Action> confirmAction)
        {
            string        fileUri         = GetGalleryTemplateFile(identity);
            StringBuilder finalOutputPath = new StringBuilder();
            string        contents        = GeneralUtilities.DownloadFile(fileUri);

            if (!FileUtilities.IsValidDirectoryPath(outputPath))
            {
                // Try create the directory if it does not exist.
                FileUtilities.DataStore.CreateDirectory(Path.GetDirectoryName(outputPath));
            }

            if (FileUtilities.IsValidDirectoryPath(outputPath))
            {
                finalOutputPath.Append(Path.Combine(outputPath, identity + ".json"));
            }
            else
            {
                finalOutputPath.Append(outputPath);
                if (!outputPath.EndsWith(".json"))
                {
                    finalOutputPath.Append(".json");
                }
            }

            Action saveFile = () => FileUtilities.DataStore.WriteFile(finalOutputPath.ToString(), contents);

            if (FileUtilities.DataStore.FileExists(finalOutputPath.ToString()) && confirmAction != null)
            {
                confirmAction(
                    overwrite,
                    string.Format(ProjectResources.FileAlreadyExists, finalOutputPath.ToString()),
                    ProjectResources.OverrdingFile,
                    finalOutputPath.ToString(),
                    saveFile);
            }
            else
            {
                saveFile();
            }

            return(finalOutputPath.ToString());
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the parameters for a given template file.
        /// </summary>
        /// <param name="templateFilePath">The gallery template path (local or remote)</param>
        /// <param name="templateParameterObject">Existing template parameter object</param>
        /// <param name="templateParameterFilePath">Path to the template parameter file if present</param>
        /// <param name="staticParameters">The existing PowerShell cmdlet parameters</param>
        /// <returns>The template parameters</returns>
        public static RuntimeDefinedParameterDictionary GetTemplateParametersFromFile(string templateFilePath, Hashtable templateParameterObject, string templateParameterFilePath, string[] staticParameters)
        {
            string templateContent = null;

            if (templateFilePath != null)
            {
                if (Uri.IsWellFormedUriString(templateFilePath, UriKind.Absolute))
                {
                    templateContent = GeneralUtilities.DownloadFile(templateFilePath);
                }
                else if (FileUtilities.DataStore.FileExists(templateFilePath))
                {
                    templateContent = FileUtilities.DataStore.ReadFileAsText(templateFilePath);
                }
            }

            RuntimeDefinedParameterDictionary dynamicParameters = ParseTemplateAndExtractParameters(templateContent, templateParameterObject, templateParameterFilePath, staticParameters);

            return(dynamicParameters);
        }
Exemplo n.º 8
0
        private string GetTemplate(string templateFile, string galleryTemplateName)
        {
            string template;

            if (!string.IsNullOrEmpty(templateFile))
            {
                if (Uri.IsWellFormedUriString(templateFile, UriKind.Absolute))
                {
                    template = GeneralUtilities.DownloadFile(templateFile);
                }
                else
                {
                    template = FileUtilities.DataStore.ReadFileAsText(templateFile);
                }
            }
            else
            {
                Debug.Assert(!string.IsNullOrEmpty(galleryTemplateName));
                string templateUri = GalleryTemplatesClient.GetGalleryTemplateFile(galleryTemplateName);
                template = GeneralUtilities.DownloadFile(templateUri);
            }

            return(template);
        }