Skip to content

Adding powershell capability to 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 11 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
9 changes: 5 additions & 4 deletions src/Resources/ResourceManager/Utilities/FileUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public static class FileUtility
/// <param name="outputPath">The file output path</param>
/// <param name="overwrite">Overrides existing file</param>
/// <param name="shouldContinue">The confirmation action</param>
/// <param name="extension">The file extension (defaults to .json)</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")
{
StringBuilder finalOutputPath = new StringBuilder();

Expand All @@ -48,14 +49,14 @@ public static string SaveTemplateFile(string templateName, string contents, stri

if (FileUtilities.IsValidDirectoryPath(outputPath))
{
finalOutputPath.Append(Path.Combine(outputPath, templateName + ".json"));
finalOutputPath.Append(Path.Combine(outputPath, templateName + extension));
}
else
{
finalOutputPath.Append(outputPath);
if (!outputPath.EndsWith(".json"))
if (!outputPath.EndsWith(extension))
{
finalOutputPath.Append(".json");
finalOutputPath.Append(extension);
}
}

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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-->

## Upcoming Release
* Added support for exporting resource group templates as Bicep files
- Added `OutputFormat` parameter to `Export-AzResourceGroup` cmdlet
- Supported values: `Json` (default), `Bicep`

## Version 8.0.1
* Fixed empty warning output issue for cmdlet `Test-AzResourceGroupDeployment` [#27888]
Expand Down
33 changes: 31 additions & 2 deletions src/Resources/Resources/help/Export-AzResourceGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Captures a resource group as a template and saves it to a file.
```
Export-AzResourceGroup -ResourceGroupName <String> [-Path <String>] [-IncludeParameterDefaultValue]
[-IncludeComments] [-SkipResourceNameParameterization] [-SkipAllParameterization] [-Resource <String[]>]
[-Force] [-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>]
[-OutputFormat <String>] [-Force] [-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>]
[-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
The **Export-AzResourceGroup** cmdlet captures the specified resource group as a template and saves it to a JSON file.This can be useful in scenarios where you have already created some resources in your resource group, and then want to leverage the benefits of using template backed deployments.
The **Export-AzResourceGroup** cmdlet captures the specified resource group as a template and saves it to a JSON or Bicep file.This can be useful in scenarios where you have already created some resources in your resource group, and then want to leverage the benefits of using template backed deployments.
This cmdlet gives you an easy start by generating the template for your existing resources in the resource group.
There might be some cases where this cmdlet fails to generate some parts of the template.
Warning messages will inform you of the resources that failed.
Expand Down Expand Up @@ -53,6 +53,20 @@ Export-AzResourceGroup -ResourceGroupName "TestGroup" -SkipAllParameterization -

This command captures two resources from the "TestGroup" resource group as a template, and saves it to a JSON file in the current directory. The generated template will not contain any generated parameters.

### Example 4: Export a resource group as a Bicep file
```powershell
Export-AzResourceGroup -ResourceGroupName "TestGroup" -OutputFormat Bicep
```

This command captures the resource group named TestGroup as a template, and saves it to a Bicep file in the current directory.

### Example 5: Export a resource group as a Bicep file with custom path
```powershell
Export-AzResourceGroup -ResourceGroupName "TestGroup" -OutputFormat Bicep -Path "C:\Templates\MyResourceGroup.bicep"
```

This command captures the resource group named TestGroup as a template, and saves it to a Bicep file at the specified path.

## PARAMETERS

### -ApiVersion
Expand Down Expand Up @@ -146,6 +160,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -OutputFormat
Specifies the format of the exported template. Supported values are "Json" and "Bicep".

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Json
Accept pipeline input: False
Accept wildcard characters: False
```

### -Pre
Indicates that this cmdlet use pre-release API versions when automatically determining which API version to use.

Expand Down
Loading