Skip to content

Commit 39b58a3

Browse files
Add switch parameter to register MAR
1 parent 8c9b67f commit 39b58a3

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/code/RegisterPSResourceRepository.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
2929
#region Members
3030

3131
private readonly string PSGalleryRepoName = "PSGallery";
32+
private readonly string MicrosoftArtifactRegistryRepoName = "MicrosoftArtifactRegistry";
33+
private readonly string MicrosoftArtifactRegistryRepoUri = "https://artifactregistry.microsoft.com/api/v2";
3234
private readonly string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
3335
private const int DefaultPriority = 50;
3436
private const bool DefaultTrusted = false;
@@ -37,6 +39,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
3739
private const string RepositoriesParameterSet = "RepositoriesParameterSet";
3840
private Uri _uri;
3941
private CredentialProviderDynamicParameters _credentialProvider;
42+
private const string MARParameterSet = "MARParameterSet";
4043

4144
#endregion
4245

@@ -62,6 +65,13 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
6265
[Parameter(Mandatory = true, ParameterSetName = PSGalleryParameterSet, HelpMessage = "PSGallery switch used to indicate registering PSGallery repository.")]
6366
public SwitchParameter PSGallery { get; set; }
6467

68+
/// <summary>
69+
/// When specified, registers Microsoft Artifact Registry repository.
70+
/// </summary>
71+
[Parameter(Mandatory = true, ParameterSetName = MARParameterSet, HelpMessage = "Switch used to indicate registering Microsoft Artifact Registry repository.")]
72+
[Alias("MAR")]
73+
public SwitchParameter MicrosoftArtifactRegistry { get; set; }
74+
6575
/// <summary>
6676
/// Specifies a hashtable of repositories and is used to register multiple repositories at once.
6777
/// </summary>
@@ -74,6 +84,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
7484
/// </summary>
7585
[Parameter(ParameterSetName = NameParameterSet)]
7686
[Parameter(ParameterSetName = PSGalleryParameterSet)]
87+
[Parameter(ParameterSetName = MARParameterSet)]
7788
public SwitchParameter Trusted { get; set; }
7889

7990
/// <summary>
@@ -84,6 +95,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
8495
/// </summary>
8596
[Parameter(ParameterSetName = NameParameterSet)]
8697
[Parameter(ParameterSetName = PSGalleryParameterSet)]
98+
[Parameter(ParameterSetName = MARParameterSet)]
8799
[ValidateRange(0, 100)]
88100
public int Priority { get; set; } = DefaultPriority;
89101

@@ -122,6 +134,7 @@ public object GetDynamicParameters()
122134
// It should also not appear when using the 'Repositories' parameter set.
123135
if (ParameterSetName.Equals(PSGalleryParameterSet) ||
124136
ParameterSetName.Equals(RepositoriesParameterSet) ||
137+
ParameterSetName.Equals(MARParameterSet) ||
125138
PSRepositoryInfo.IsValidContainerRegistryURL(Uri))
126139
{
127140
return null;
@@ -218,6 +231,24 @@ protected override void ProcessRecord()
218231
}
219232
break;
220233

234+
case MARParameterSet:
235+
if (MicrosoftArtifactRegistry)
236+
{
237+
try
238+
{
239+
items.Add(MicrosoftArtifactRegistryParameterSetHelper(Priority, Trusted));
240+
}
241+
catch (Exception e)
242+
{
243+
ThrowTerminatingError(new ErrorRecord(
244+
new PSInvalidOperationException(e.Message),
245+
"ErrorInMARParameterSet",
246+
ErrorCategory.InvalidArgument,
247+
this));
248+
}
249+
}
250+
break;
251+
221252
default:
222253
Dbg.Assert(false, "Invalid parameter set");
223254
break;
@@ -262,6 +293,34 @@ private PSRepositoryInfo PSGalleryParameterSetHelper(int repoPriority, bool repo
262293
return addedRepo;
263294
}
264295

296+
private PSRepositoryInfo MicrosoftArtifactRegistryParameterSetHelper(int repoPriority, bool repoTrusted)
297+
{
298+
WriteDebug("In RegisterPSResourceRepository::MicrosoftArtifactRegistryParameterSetHelper()");
299+
Uri marUri = new Uri(MicrosoftArtifactRegistryRepoUri);
300+
WriteDebug("Internal name and uri values for Microsoft Artifact Registry are hardcoded and validated. Priority and trusted values, if passed in, also validated");
301+
var addedRepo = RepositorySettings.AddToRepositoryStore(MicrosoftArtifactRegistryRepoName,
302+
marUri,
303+
repoPriority,
304+
repoTrusted,
305+
apiVersion: null,
306+
repoCredentialInfo: null,
307+
credentialProvider: null,
308+
Force,
309+
this,
310+
out string errorMsg);
311+
312+
if (!string.IsNullOrEmpty(errorMsg))
313+
{
314+
ThrowTerminatingError(new ErrorRecord(
315+
new PSInvalidOperationException(errorMsg),
316+
"RepositoryCredentialSecretManagementUnavailableModule",
317+
ErrorCategory.ResourceUnavailable,
318+
this));
319+
}
320+
321+
return addedRepo;
322+
}
323+
265324
private List<PSRepositoryInfo> RepositoriesParameterSetHelper()
266325
{
267326
WriteDebug("In RegisterPSResourceRepository::RepositoriesParameterSetHelper()");

src/code/RepositorySettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static PSRepositoryInfo AddRepository(string repoName, Uri repoUri, int r
9595

9696
if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase))
9797
{
98-
errorMsg = "Cannot register MAR with -Name parameter. The MAR repository is automatically registered. Try: Reset-PSResourceRepository to restore default repositories.";
98+
errorMsg = "Cannot register MAR with -Name parameter. Try: Register-PSResourceRepository -MicrosoftArtifactRegistry.";
9999
return null;
100100
}
101101

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
8585
$res.Priority | Should -Be 50
8686
}
8787

88+
It "register repository with MicrosoftArtifactRegistry parameter (MicrosoftArtifactRegistryParameterSet)" {
89+
Unregister-PSResourceRepository -Name $MARName
90+
$res = Register-PSResourceRepository -MicrosoftArtifactRegistry -PassThru
91+
$res.Name | Should -Be $MARName
92+
$res.Uri | Should -Be $MARUri
93+
$res.Trusted | Should -Be True
94+
$res.Priority | Should -Be 40
95+
}
96+
8897
It "register repository with PSGallery switch parameter value of false (PSGalleryParameterSet)" {
8998
Unregister-PSResourceRepository -Name $PSGalleryName
9099
$res = Register-PSResourceRepository -PSGallery:$false -PassThru
@@ -412,7 +421,7 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
412421

413422
It "should throw error when trying to register repository with ApiVersion unknown" {
414423
{Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository"
415-
424+
416425
# Verify the repository was not created
417426
$repo = Get-PSResourceRepository $TestRepoName1 -ErrorAction SilentlyContinue
418427
$repo | Should -BeNullOrEmpty

0 commit comments

Comments
 (0)