Skip to content

Commit 54d9cf0

Browse files
Merge branch 'master' of https://github.com/powershell/psresourceget into MCRAuthChallenge
2 parents 415c9f9 + 9aa8538 commit 54d9cf0

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
4747
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/"; // 0 - registry, 1 - packagename
4848
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}"; // 0 - registry, 1 - location, 2 - digest
4949
const string defaultScope = "repository:*:*";
50+
const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog"; // 0 - registry
5051

5152
#endregion
5253

@@ -77,13 +78,13 @@ public ContainerRegistryServerAPICalls(PSRepositoryInfo repository, PSCmdlet cmd
7778
public override FindResults FindAll(bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
7879
{
7980
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindAll()");
80-
errRecord = new ErrorRecord(
81-
new InvalidOperationException($"Find all is not supported for the ContainerRegistry server protocol repository '{Repository.Name}'"),
82-
"FindAllFailure",
83-
ErrorCategory.InvalidOperation,
84-
this);
81+
var findResult = FindPackages("*", includePrerelease, out errRecord);
82+
if (errRecord != null)
83+
{
84+
return emptyResponseResults;
85+
}
8586

86-
return emptyResponseResults;
87+
return findResult;
8788
}
8889

8990
/// <summary>
@@ -162,13 +163,13 @@ public override FindResults FindNameWithTag(string packageName, string[] tags, b
162163
public override FindResults FindNameGlobbing(string packageName, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
163164
{
164165
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindNameGlobbing()");
165-
errRecord = new ErrorRecord(
166-
new InvalidOperationException($"FindNameGlobbing all is not supported for the ContainerRegistry server protocol repository '{Repository.Name}'"),
167-
"FindNameGlobbingFailure",
168-
ErrorCategory.InvalidOperation,
169-
this);
166+
var findResult = FindPackages(packageName, includePrerelease, out errRecord);
167+
if (errRecord != null)
168+
{
169+
return emptyResponseResults;
170+
}
170171

171-
return emptyResponseResults;
172+
return findResult;
172173
}
173174

174175
/// <summary>
@@ -669,6 +670,20 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi
669670
return GetHttpResponseJObjectUsingDefaultHeaders(findImageUrl, HttpMethod.Get, defaultHeaders, out errRecord);
670671
}
671672

673+
/// <summary>
674+
/// Helper method to find all packages on container registry
675+
/// </summary>
676+
/// <param name="containerRegistryAccessToken"></param>
677+
/// <param name="errRecord"></param>
678+
/// <returns></returns>
679+
internal JObject FindAllRepositories(string containerRegistryAccessToken, out ErrorRecord errRecord)
680+
{
681+
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindAllRepositories()");
682+
string repositoryListUrl = string.Format(containerRegistryRepositoryListTemplate, Registry);
683+
var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken);
684+
return GetHttpResponseJObjectUsingDefaultHeaders(repositoryListUrl, HttpMethod.Get, defaultHeaders, out errRecord);
685+
}
686+
672687
/// <summary>
673688
/// Get metadata for a package version.
674689
/// </summary>
@@ -1783,12 +1798,63 @@ private string PrependMARPrefix(string packageName)
17831798

17841799
// If the repostitory is MAR and its not a wildcard search, we need to prefix the package name with MAR prefix.
17851800
string updatedPackageName = Repository.IsMARRepository() && packageName.Trim() != "*"
1786-
? string.Concat(prefix, packageName)
1801+
? packageName.StartsWith(prefix) ? packageName : string.Concat(prefix, packageName)
17871802
: packageName;
17881803

17891804
return updatedPackageName;
17901805
}
17911806

1807+
private FindResults FindPackages(string packageName, bool includePrerelease, out ErrorRecord errRecord)
1808+
{
1809+
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindPackages()");
1810+
errRecord = null;
1811+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
1812+
if (errRecord != null)
1813+
{
1814+
return emptyResponseResults;
1815+
}
1816+
1817+
var pkgResult = FindAllRepositories(containerRegistryAccessToken, out errRecord);
1818+
if (errRecord != null)
1819+
{
1820+
return emptyResponseResults;
1821+
}
1822+
1823+
List<Hashtable> repositoriesList = new List<Hashtable>();
1824+
var isMAR = Repository.IsMARRepository();
1825+
1826+
// Convert the list of repositories to a list of hashtables
1827+
foreach (var repository in pkgResult["repositories"].ToList())
1828+
{
1829+
string repositoryName = repository.ToString();
1830+
1831+
if (isMAR && !repositoryName.StartsWith(PSRepositoryInfo.MARPrefix))
1832+
{
1833+
continue;
1834+
}
1835+
1836+
// This remove the 'psresource/' prefix from the repository name for comparison with wildcard.
1837+
string moduleName = repositoryName.Substring(11);
1838+
1839+
WildcardPattern wildcardPattern = new WildcardPattern(packageName, WildcardOptions.IgnoreCase);
1840+
1841+
if (!wildcardPattern.IsMatch(moduleName))
1842+
{
1843+
continue;
1844+
}
1845+
1846+
_cmdletPassedIn.WriteDebug($"Found repository: {repositoryName}");
1847+
1848+
repositoriesList.AddRange(FindPackagesWithVersionHelper(repositoryName, VersionType.VersionRange, versionRange: VersionRange.All, requiredVersion: null, includePrerelease, getOnlyLatest: true, out errRecord));
1849+
if (errRecord != null)
1850+
{
1851+
return emptyResponseResults;
1852+
}
1853+
}
1854+
1855+
return new FindResults(stringResponse: new string[] { }, hashtableResponse: repositoriesList.ToArray(), responseType: containerRegistryFindResponseType);
1856+
}
1857+
17921858
#endregion
17931859
}
17941860
}

test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,26 @@ Describe 'Test Find-PSResource for MAR Repository' -tags 'CI' {
247247
It "Should find resource given specific Name, Version null" {
248248
$res = Find-PSResource -Name "Az.Accounts" -Repository "MAR"
249249
$res.Name | Should -Be "Az.Accounts"
250-
$res.Version | Should -Be "4.0.0"
250+
$res.Version | Should -BeGreaterThan ([Version]"4.0.0")
251251
}
252252

253253
It "Should find resource and its dependency given specific Name and Version" {
254254
$res = Find-PSResource -Name "Az.Storage" -Version "8.0.0" -Repository "MAR"
255255
$res.Dependencies.Length | Should -Be 1
256256
$res.Dependencies[0].Name | Should -Be "Az.Accounts"
257257
}
258+
259+
It "Should find resource with wildcard in Name" {
260+
$res = Find-PSResource -Name "Az.App*" -Repository "MAR"
261+
$res | Should -Not -BeNullOrEmpty
262+
$res.Count | Should -BeGreaterThan 1
263+
}
264+
265+
It "Should find all resource with wildcard in Name" {
266+
$res = Find-PSResource -Name "*" -Repository "MAR"
267+
$res | Should -Not -BeNullOrEmpty
268+
$res.Count | Should -BeGreaterThan 1
269+
}
258270
}
259271

260272
Describe 'Test Find-PSResource for unauthenticated ACR repository' -tags 'CI' {

0 commit comments

Comments
 (0)