Skip to content

[Resources] powershell for export as bicep #27294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public class ExportTemplateParameters
/// </summary>
[JsonProperty(Required = Required.Always)]
public string[] Resources { get; set; }

/// <summary>
/// Gets or sets the output format.
/// </summary>
public string OutputFormat { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public class ExportAzureResourceGroupCmdlet : ResourceManagerCmdletBaseWithApiVe
[ValidateNotNullOrEmpty]
public override string ApiVersion { get; set; }

/// <summary>
/// Gets or sets the output format.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "The output format of the template. Allowed values are 'Json', 'Bicep'.")]
[ValidateSet(ExportTemplateOutputFormat.Json, ExportTemplateOutputFormat.Bicep, IgnoreCase = true)]
public string OutputFormat { get; set; } = ExportTemplateOutputFormat.Json;

/// <summary>
/// Executes the cmdlet.
/// </summary>
Expand All @@ -106,7 +113,6 @@ protected override void OnProcessRecord()

if (ShouldProcess(ResourceGroupName, VerbsData.Export))
{

var resourceGroupId = this.GetResourceGroupId();

if (! this.IsParameterBound(c => c.ApiVersion))
Expand All @@ -115,6 +121,7 @@ protected override void OnProcessRecord()
{
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
Options = this.GetExportOptions(),
OutputFormat = this.OutputFormat
};

var exportedTemplate = NewResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters);
Expand All @@ -139,6 +146,7 @@ protected override void OnProcessRecord()
{
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
Options = this.GetExportOptions(),
OutputFormat = this.OutputFormat
};
var apiVersion = this.ApiVersion;
var operationResult = this.GetResourcesClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public void TestExportResourceGroup()
TestRunner.RunTestScript("Test-ExportResourceGroup");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestExportResourceGroupBicep()
{
TestRunner.RunTestScript("Test-ExportResourceGroupBicep");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestExportResourceGroupAsyncRoute()
Expand Down
32 changes: 32 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/ResourceGroupTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,38 @@ function Test-ExportResourceGroup
}
}

<#
.SYNOPSIS
Tests export resource group template file as bicep.
#>
function Test-ExportResourceGroupBicep
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$rglocation = Get-Location "Microsoft.Resources" "resourceGroups" "West US"
$apiversion = "2014-04-01"
$resourceType = "Providers.Test/statefulResources"

try {
# Test
New-AzResourceGroup -Name $rgname -Location $rglocation

$r = New-AzResource -Name $rname -Location "centralus" -Tags @{ testtag = "testval" } -ResourceGroupName $rgname -ResourceType $resourceType -SkuObject @{ Name = "A0" } -ApiVersion $apiversion -Force
Assert-AreEqual $r.ResourceGroupName $rgname

$exportOutput = Export-AzResourceGroup -ResourceGroupName $rgname -OutputFormat Bicep -Force
Assert-NotNull $exportOutput
Assert-True { $exportOutput.Path.Contains($rgname + ".bicep") }
}
finally {
# Cleanup
Clean-ResourceGroup $rgname
}
}
}


<#
.SYNOPSIS
Tests async export to export resource group template file.
Expand Down