@@ -47,6 +47,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
47
47
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/" ; // 0 - registry, 1 - packagename
48
48
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}" ; // 0 - registry, 1 - location, 2 - digest
49
49
const string defaultScope = "repository:*:*" ;
50
+ const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog" ; // 0 - registry
50
51
51
52
#endregion
52
53
@@ -77,13 +78,13 @@ public ContainerRegistryServerAPICalls(PSRepositoryInfo repository, PSCmdlet cmd
77
78
public override FindResults FindAll ( bool includePrerelease , ResourceType type , out ErrorRecord errRecord )
78
79
{
79
80
_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
+ }
85
86
86
- return emptyResponseResults ;
87
+ return findResult ;
87
88
}
88
89
89
90
/// <summary>
@@ -162,13 +163,13 @@ public override FindResults FindNameWithTag(string packageName, string[] tags, b
162
163
public override FindResults FindNameGlobbing ( string packageName , bool includePrerelease , ResourceType type , out ErrorRecord errRecord )
163
164
{
164
165
_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
+ }
170
171
171
- return emptyResponseResults ;
172
+ return findResult ;
172
173
}
173
174
174
175
/// <summary>
@@ -669,6 +670,20 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi
669
670
return GetHttpResponseJObjectUsingDefaultHeaders ( findImageUrl , HttpMethod . Get , defaultHeaders , out errRecord ) ;
670
671
}
671
672
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
+
672
687
/// <summary>
673
688
/// Get metadata for a package version.
674
689
/// </summary>
@@ -1783,12 +1798,63 @@ private string PrependMARPrefix(string packageName)
1783
1798
1784
1799
// If the repostitory is MAR and its not a wildcard search, we need to prefix the package name with MAR prefix.
1785
1800
string updatedPackageName = Repository . IsMARRepository ( ) && packageName . Trim ( ) != "*"
1786
- ? string . Concat ( prefix , packageName )
1801
+ ? packageName . StartsWith ( prefix ) ? packageName : string . Concat ( prefix , packageName )
1787
1802
: packageName ;
1788
1803
1789
1804
return updatedPackageName ;
1790
1805
}
1791
1806
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
+
1792
1858
#endregion
1793
1859
}
1794
1860
}
0 commit comments