Skip to content

Anapandey/bicep export #28014

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 8 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 Expand Up @@ -177,6 +185,9 @@ protected override void OnProcessRecord()
}
}

// Determine the correct file extension based on OutputFormat
string extension = OutputFormat.Equals(ExportTemplateOutputFormat.Bicep, StringComparison.OrdinalIgnoreCase) ? ".bicep" : ".json";

string path = FileUtility.SaveTemplateFile(
templateName: this.ResourceGroupName,
contents: contents,
Expand All @@ -185,7 +196,9 @@ protected override void OnProcessRecord()
? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName)
: this.TryResolvePath(this.Path),
overwrite: Force.IsPresent,
shouldContinue: ShouldContinue);
shouldContinue: ShouldContinue,
extension: extension // Pass the extension
);

WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", path));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/ResourceManager/Utilities/FileUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class FileUtility
/// <param name="overwrite">Overrides existing file</param>
/// <param name="shouldContinue">The confirmation action</param>
/// <returns>The file path</returns>
public static string SaveTemplateFile(string templateName, string contents, string outputPath, bool overwrite, Func<string, string, bool> shouldContinue)
public static string SaveTemplateFile(string templateName, string contents, string outputPath, bool overwrite, Func<string, string, bool> shouldContinue, string extension = ".json") // Added extension parameter with default
{
StringBuilder finalOutputPath = new StringBuilder();

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
31 changes: 31 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,37 @@ 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
Loading