Skip to content

Commit

Permalink
Park 1 of ACR work integration and refactor (PowerShell#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson committed Apr 2, 2024
1 parent 0c49e02 commit 881bc2c
Show file tree
Hide file tree
Showing 9 changed files with 1,358 additions and 10 deletions.
92 changes: 92 additions & 0 deletions src/code/ACRResponseUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Collections.Generic;
using System.Xml;

namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
internal class ACRResponseUtil : ResponseUtil
{
#region Members

internal override PSRepositoryInfo Repository { get; set; }

#endregion

#region Constructor

public ACRResponseUtil(PSRepositoryInfo repository) : base(repository)
{
Repository = repository;
}

#endregion

#region Overriden Methods
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
{
// in FindHelper:
// serverApi.FindName() -> return responses, and out errRecord
// check outErrorRecord
//
// v2Converter.ConvertToPSResourceInfo(responses) -> return PSResourceResult
// check resourceResult for error, write if needed
string[] responses = responseResults.StringResponse;

foreach (string response in responses)
{
var elemList = ConvertResponseToXML(response);
if (elemList.Length == 0)
{
// this indicates we got a non-empty, XML response (as noticed for V2 server) but it's not a response that's meaningful (contains 'properties')
Exception notFoundException = new ResourceNotFoundException("Package does not exist on the server");

yield return new PSResourceResult(returnedObject: null, exception: notFoundException, isTerminatingError: false);
}

foreach (var element in elemList)
{
if (!PSResourceInfo.TryConvertFromXml(element, out PSResourceInfo psGetInfo, Repository, out string errorMsg))
{
Exception parseException = new XmlParsingException(errorMsg);

yield return new PSResourceResult(returnedObject: null, exception: parseException, isTerminatingError: false);
}

// Unlisted versions will have a published year as 1900 or earlier.
if (!psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
{
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
}
}
}
}

#endregion

#region V2 Specific Methods

public XmlNode[] ConvertResponseToXML(string httpResponse)
{

//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml(httpResponse);

XmlNodeList elemList = doc.GetElementsByTagName("m:properties");

XmlNode[] nodes = new XmlNode[elemList.Count];
for (int i = 0; i < elemList.Count; i++)
{
nodes[i] = elemList[i];
}

return nodes;
}

#endregion
}
}
957 changes: 957 additions & 0 deletions src/code/ACRServerAPICalls.cs

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/code/PSRepositoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public PSRepositoryInfo(string name, Uri uri, int priority, bool trusted, PSCred

#endregion

#region Enum

public enum RepositoryProviderType
{
None,
ACR,
AzureDevOps
}

#endregion

#region Properties

/// <summary>
Expand All @@ -61,6 +72,11 @@ public PSRepositoryInfo(string name, Uri uri, int priority, bool trusted, PSCred
[ValidateRange(0, 100)]
public int Priority { get; }

/// <summary>
/// the type of repository provider (eg, AzureDevOps, ACR, etc.)
/// </summary>
public RepositoryProviderType RepositoryProvider { get; }

/// <summary>
/// the credential information for repository authentication
/// </summary>
Expand Down
26 changes: 21 additions & 5 deletions src/code/PublishPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System.Management.Automation;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Xml;

Expand Down Expand Up @@ -480,12 +482,26 @@ out string[] _

string repositoryUri = repository.Uri.AbsoluteUri;

// This call does not throw any exceptions, but it will write unsuccessful responses to the console
if (!PushNupkg(outputNupkgDir, repository.Name, repositoryUri, out ErrorRecord pushNupkgError))
if (repository.RepositoryProvider == PSRepositoryInfo.RepositoryProviderType.ACR)
{
WriteError(pushNupkgError);
// exit out of processing
return;
// TODO: Create instance of ACR server class and call PushNupkgACR
/*
if (!PushNupkgACR(outputNupkgDir, repository, out ErrorRecord pushNupkgACRError))
{
WriteError(pushNupkgACRError);
return;
}
*/
}
else
{
// This call does not throw any exceptions, but it will write unsuccessful responses to the console
if (!PushNupkg(outputNupkgDir, repository.Name, repository.Uri.ToString(), out ErrorRecord pushNupkgError))
{
WriteError(pushNupkgError);
// exit out of processing
return;
}
}
}
finally
Expand Down
24 changes: 24 additions & 0 deletions src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using static Microsoft.PowerShell.PSResourceGet.UtilClasses.PSRepositoryInfo;

namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
Expand Down Expand Up @@ -435,6 +438,7 @@ public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPrio
node.Attribute(PSCredentialInfo.SecretNameAttribute).Value);
}

RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
updatedRepo = new PSRepositoryInfo(repoName,
thisUrl,
Int32.Parse(node.Attribute("Priority").Value),
Expand Down Expand Up @@ -519,6 +523,8 @@ public static List<PSRepositoryInfo> Remove(string[] repoNames, out string[] err
}

string attributeUrlUriName = urlAttributeExists ? "Url" : "Uri";
Uri repoUri = new Uri(node.Attribute(attributeUrlUriName).Value);
RepositoryProviderType repositoryProvider= GetRepositoryProviderType(repoUri);
removedRepos.Add(
new PSRepositoryInfo(repo,
new Uri(node.Attribute(attributeUrlUriName).Value),
Expand Down Expand Up @@ -649,6 +655,7 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
continue;
}

RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(repo.Attribute("Name").Value,
thisUrl,
Int32.Parse(repo.Attribute("Priority").Value),
Expand Down Expand Up @@ -752,6 +759,7 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
continue;
}

RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(node.Attribute("Name").Value,
thisUrl,
Int32.Parse(node.Attribute("Priority").Value),
Expand Down Expand Up @@ -838,6 +846,22 @@ private static PSRepositoryInfo.APIVersion GetRepoAPIVersion(Uri repoUri)
}
}

private static RepositoryProviderType GetRepositoryProviderType(Uri repoUri)
{
string absoluteUri = repoUri.AbsoluteUri;
// We want to use contains instead of EndsWith to accomodate for trailing '/'
if (absoluteUri.Contains("azurecr.io")){
return RepositoryProviderType.ACR;
}
// TODO: add a regex for this match
// eg: *pkgs.*/_packaging/*
else if (absoluteUri.Contains("pkgs.")){
return RepositoryProviderType.AzureDevOps;
}
else {
return RepositoryProviderType.None;
}
}
#endregion
}
}
6 changes: 3 additions & 3 deletions src/code/UninstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private bool UninstallPkgHelper(out List<ErrorRecord> errRecords)

/* uninstalls a module */
private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorRecord errRecord)
{
{
WriteDebug("In UninstallPSResource::UninstallModuleHelper");
errRecord = null;
var successfullyUninstalledPkg = false;
Expand Down Expand Up @@ -324,7 +324,7 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco

/* uninstalls a script */
private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorRecord errRecord)
{
{
WriteDebug("In UninstallPSResource::UninstallScriptHelper");
errRecord = null;
var successfullyUninstalledPkg = false;
Expand Down Expand Up @@ -375,7 +375,7 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorReco
}

private bool CheckIfDependency(string pkgName, string version, out ErrorRecord errorRecord)
{
{
WriteDebug("In UninstallPSResource::CheckIfDependency");
// Checking if a specific package version is a dependency anywhere
// this is a primitive implementation
Expand Down
Loading

0 comments on commit 881bc2c

Please sign in to comment.