Skip to content

Commit dec1b04

Browse files
Xynoclafeisra-fel
andauthored
Updated Export-AzResourceGroup cmdlet to use the SDK. (Azure#13275)
* Export template with SDK #1 * Export template with SDK #2 * Update changelog * Added back support for -ApiVersion parameter with a deprecation notice * update changelog to avoid merge issue * Review fixes * re-record tests Co-authored-by: Yeming Liu <[email protected]>
1 parent c707f29 commit dec1b04

File tree

7 files changed

+803
-760
lines changed

7 files changed

+803
-760
lines changed

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceManagerCmdletBaseWithAPiVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class ResourceManagerCmdletBaseWithApiVersion : ResourceManagerC
2525
/// </summary>
2626
[Parameter(Mandatory = false, HelpMessage = "When set, indicates the version of the resource provider API to use. If not specified, the API version is automatically determined as the latest available.")]
2727
[ValidateNotNullOrEmpty]
28-
public string ApiVersion { get; set; }
28+
public virtual string ApiVersion { get; set; }
2929

3030
private Dictionary<string, string> GetCmdletHeaders()
3131
{

src/Resources/ResourceManager/Implementation/ResourceGroups/ExportAzureResourceGroupCmdlet.cs

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ResourceIds;
2323
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2424
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
25+
using Microsoft.Azure.Management.ResourceManager.Models;
26+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2527
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2628
using Newtonsoft.Json.Linq;
2729
using System;
@@ -93,51 +95,43 @@ public class ExportAzureResourceGroupCmdlet : ResourceManagerCmdletBaseWithApiVe
9395
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
9496
public SwitchParameter Force { get; set; }
9597

98+
/// <summary>
99+
/// Gets or sets the API version.
100+
/// </summary>
101+
[CmdletParameterBreakingChange("ApiVersion", ChangeDescription = "Parameter is being deprecated without being replaced. Using the lastest possible API version will become the default behavior.")]
102+
[Parameter(Mandatory = false, HelpMessage = "When set, indicates the version of the resource provider API to use. If not specified, the API version is automatically determined as the latest available.")]
103+
[ValidateNotNullOrEmpty]
104+
public override string ApiVersion { get; set; }
105+
96106
/// <summary>
97107
/// Executes the cmdlet.
98108
/// </summary>
99109
protected override void OnProcessRecord()
100110
{
101111
base.OnProcessRecord();
112+
string contents;
113+
102114
if (ShouldProcess(ResourceGroupName, VerbsData.Export))
103115
{
104116

105117
var resourceGroupId = this.GetResourceGroupId();
106118

107-
var apiVersion = this.ApiVersion ?? DefaultApiVersion;
108-
109-
var parameters = new ExportTemplateParameters
110-
{
111-
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
112-
Options = this.GetExportOptions(),
113-
};
114-
115-
var operationResult = this.GetResourcesClient()
116-
.InvokeActionOnResource<JObject>(
117-
resourceId: resourceGroupId,
118-
action: Constants.ExportTemplate,
119-
parameters: parameters.ToJToken(),
120-
apiVersion: apiVersion,
121-
cancellationToken: this.CancellationToken.Value)
122-
.Result;
123-
124-
var managementUri = this.GetResourcesClient()
125-
.GetResourceManagementRequestUri(
126-
resourceId: resourceGroupId,
127-
apiVersion: apiVersion,
128-
action: Constants.ExportTemplate);
129-
130-
var activity = string.Format("POST {0}", managementUri.PathAndQuery);
131-
var resultString = this.GetLongRunningOperationTracker(activityName: activity,
132-
isResourceCreateOrUpdate: false)
133-
.WaitOnOperation(operationResult: operationResult);
134-
135-
var template = JToken.FromObject(JObject.Parse(resultString)["template"]);
136-
137-
if (JObject.Parse(resultString)["error"] != null)
119+
if (this.IsParameterBound(c => c.ApiVersion))
138120
{
139-
ExtendedErrorInfo error;
140-
if (JObject.Parse(resultString)["error"].TryConvertTo(out error))
121+
var parameters = new Management.ResourceManager.Models.ExportTemplateRequest
122+
{
123+
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
124+
Options = this.GetExportOptions(),
125+
};
126+
127+
var exportedTemplate = ResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters);
128+
129+
var template = exportedTemplate.Template;
130+
contents = template.ToString();
131+
132+
var error = exportedTemplate.Error;
133+
134+
if(error != null)
141135
{
142136
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message));
143137
foreach (var detail in error.Details)
@@ -146,10 +140,53 @@ protected override void OnProcessRecord()
146140
}
147141
}
148142
}
143+
else
144+
{
145+
var parameters = new ExportTemplateParameters
146+
{
147+
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
148+
Options = this.GetExportOptions(),
149+
};
150+
var apiVersion = this.ApiVersion ?? DefaultApiVersion;
151+
var operationResult = this.GetResourcesClient()
152+
.InvokeActionOnResource<JObject>(
153+
resourceId: resourceGroupId,
154+
action: Constants.ExportTemplate,
155+
parameters: parameters.ToJToken(),
156+
apiVersion: apiVersion,
157+
cancellationToken: this.CancellationToken.Value)
158+
.Result;
159+
160+
var managementUri = this.GetResourcesClient()
161+
.GetResourceManagementRequestUri(
162+
resourceId: resourceGroupId,
163+
apiVersion: apiVersion,
164+
action: Constants.ExportTemplate);
165+
166+
var activity = string.Format("POST {0}", managementUri.PathAndQuery);
167+
var resultString = this.GetLongRunningOperationTracker(activityName: activity,
168+
isResourceCreateOrUpdate: false)
169+
.WaitOnOperation(operationResult: operationResult);
170+
171+
var template = JToken.FromObject(JObject.Parse(resultString)["template"]);
172+
contents = template.ToString();
173+
174+
if (JObject.Parse(resultString)["error"] != null)
175+
{
176+
if (JObject.Parse(resultString)["error"].TryConvertTo(out ExtendedErrorInfo error))
177+
{
178+
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message));
179+
foreach (var detail in error.Details)
180+
{
181+
WriteWarning(string.Format("{0} : {1}", detail.Code, detail.Message));
182+
}
183+
}
184+
}
185+
}
149186

150187
string path = FileUtility.SaveTemplateFile(
151188
templateName: this.ResourceGroupName,
152-
contents: template.ToString(),
189+
contents: contents,
153190
outputPath:
154191
string.IsNullOrEmpty(this.Path)
155192
? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName)

src/Resources/ResourceManager/SdkClient/ResourceManagerSdkClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ private ResourceGroup CreateOrUpdateResourceGroup(string name, string location,
178178
return result;
179179
}
180180

181+
public ResourceGroupExportResult ExportResourceGroup(string resourceGroupName, ExportTemplateRequest properties)
182+
{
183+
return ResourceManagementClient.ResourceGroups.ExportTemplate(resourceGroupName, properties);
184+
}
185+
181186
private void WriteVerbose(string progress)
182187
{
183188
if (VerboseLogger != null)

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests/TestExportResourceGroup.json

Lines changed: 146 additions & 146 deletions
Large diffs are not rendered by default.

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests/TestExportResourceGroupAsyncRoute.json

Lines changed: 421 additions & 421 deletions
Large diffs are not rendered by default.

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests/TestExportResourceGroupWithFiltering.json

Lines changed: 158 additions & 158 deletions
Large diffs are not rendered by default.

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Updated `Export-AzResourceGroup` to use the SDK.
2122

2223
## Version 3.0.0
2324
* Fixed parsing bug

0 commit comments

Comments
 (0)