@@ -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}
0 commit comments