Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 0 additions & 104 deletions src/DependencyManagement/DependencyManagementUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;

namespace Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement
{
internal class DependencyManagementUtils
{
// The PowerShellGallery uri to query for the latest module version.
private const string PowerShellGalleryFindPackagesByIdUri = "https://www.powershellgallery.com/api/v2/FindPackagesById()?id=";

/// <summary>
/// Deletes the contents at the given directory.
/// </summary>
Expand Down Expand Up @@ -51,104 +46,5 @@ internal static void EmptyDirectory(string path)
throw new InvalidOperationException(errorMsg);
}
}

/// <summary>
/// Returns the latest module version from the PSGallery for the given module name and major version.
/// </summary>
internal static string GetModuleLatestSupportedVersion(string moduleName, string majorVersion)
{
Uri address = new Uri($"{PowerShellGalleryFindPackagesByIdUri}'{moduleName}'");
int configuredRetries = 3;
int noOfRetries = 1;

string latestVersionForMajorVersion = null;

while (noOfRetries <= configuredRetries)
{
try
{
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
using (HttpWebResponse response = request?.GetResponse() as HttpWebResponse)
{
if (response != null)
{
// Load up the XML response
XmlDocument doc = new XmlDocument();
using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
{
doc.Load(reader);
}

// Add the namespaces for the gallery xml content
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ps", "http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

// Find the version information
XmlNode root = doc.DocumentElement;
var props = root.SelectNodes("//m:properties/d:Version", nsmgr);
if (props != null && props.Count > 0)
{
for (int i = 0; i < props.Count; i++)
{
if (props[i].FirstChild.Value.StartsWith(majorVersion))
{
latestVersionForMajorVersion = props[i].FirstChild.Value;
}
}
}
break;
}
}
}
catch (Exception ex)
{
WebException webEx = ex as WebException;
if (webEx == null || noOfRetries >= configuredRetries)
{
throw;
}

// Only retry the web exception
if (ShouldRetry(webEx))
{
noOfRetries++;
}
}
}

// If we could not find the latest module version error out.
if (string.IsNullOrEmpty(latestVersionForMajorVersion))
{
var errorMsg = string.Format(PowerShellWorkerStrings.CannotFindModuleVersion, moduleName, majorVersion);
var argException = new ArgumentException(errorMsg);
throw argException;
}

return latestVersionForMajorVersion;
}

/// <summary>
/// Returns true if the given WebException status matches one of the following:
/// SendFailure, ConnectFailure, UnknownError or Timeout.
/// </summary>
private static bool ShouldRetry(WebException webEx)
{
if (webEx == null)
{
return false;
}

if (webEx.Status == WebExceptionStatus.SendFailure ||
webEx.Status == WebExceptionStatus.ConnectFailure ||
webEx.Status == WebExceptionStatus.UnknownError ||
webEx.Status == WebExceptionStatus.Timeout)
{
return true;
}

return false;
}
}
}
Loading