Skip to content

Commit e72e11e

Browse files
committed
Added back support for -ApiVersion parameter with a deprecation notice
1 parent c741602 commit e72e11e

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
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: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2424
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
2525
using Microsoft.Azure.Management.Internal.ResourceManager.Version2018_05_01.Models;
2626
using Microsoft.Azure.Management.ResourceManager.Models;
27+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2728
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2829
using Newtonsoft.Json.Linq;
2930
using System;
@@ -95,32 +96,90 @@ public class ExportAzureResourceGroupCmdlet : ResourceManagerCmdletBaseWithApiVe
9596
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
9697
public SwitchParameter Force { get; set; }
9798

99+
/// <summary>
100+
/// Gets or sets the API version.
101+
/// </summary>
102+
[CmdletParameterBreakingChange("ApiVersion", ChangeDescription = "Parameter is being deprecated without being replaced")]
103+
[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.")]
104+
[ValidateNotNullOrEmpty]
105+
public override string ApiVersion { get; set; }
106+
98107
/// <summary>
99108
/// Executes the cmdlet.
100109
/// </summary>
101110
protected override void OnProcessRecord()
102111
{
103112
base.OnProcessRecord();
113+
String contents;
114+
104115
if (ShouldProcess(ResourceGroupName, VerbsData.Export))
105116
{
106117

107118
var resourceGroupId = this.GetResourceGroupId();
108119

109-
var parameters = new Management.ResourceManager.Models.ExportTemplateRequest
120+
if (this.ApiVersion is null)
121+
{
122+
var parameters = new Management.ResourceManager.Models.ExportTemplateRequest
123+
{
124+
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
125+
Options = this.GetExportOptions(),
126+
};
127+
128+
var exportedTemplate = ResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters);
129+
130+
var template = exportedTemplate.Template;
131+
contents = template.ToString();
132+
133+
var error = exportedTemplate.Error;
134+
}
135+
else
110136
{
111-
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
112-
Options = this.GetExportOptions(),
113-
};
137+
var parameters = new ExportTemplateParameters
138+
{
139+
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
140+
Options = this.GetExportOptions(),
141+
};
142+
var apiVersion = this.ApiVersion ?? DefaultApiVersion;
143+
var operationResult = this.GetResourcesClient()
144+
.InvokeActionOnResource<JObject>(
145+
resourceId: resourceGroupId,
146+
action: Constants.ExportTemplate,
147+
parameters: parameters.ToJToken(),
148+
apiVersion: apiVersion,
149+
cancellationToken: this.CancellationToken.Value)
150+
.Result;
151+
152+
var managementUri = this.GetResourcesClient()
153+
.GetResourceManagementRequestUri(
154+
resourceId: resourceGroupId,
155+
apiVersion: apiVersion,
156+
action: Constants.ExportTemplate);
114157

115-
var exportedTemplate = ResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters);
158+
var activity = string.Format("POST {0}", managementUri.PathAndQuery);
159+
var resultString = this.GetLongRunningOperationTracker(activityName: activity,
160+
isResourceCreateOrUpdate: false)
161+
.WaitOnOperation(operationResult: operationResult);
116162

117-
var template = exportedTemplate.Template;
163+
var template = JToken.FromObject(JObject.Parse(resultString)["template"]);
164+
contents = template.ToString();
118165

119-
var error = exportedTemplate.Error;
166+
if (JObject.Parse(resultString)["error"] != null)
167+
{
168+
ExtendedErrorInfo error;
169+
if (JObject.Parse(resultString)["error"].TryConvertTo(out error))
170+
{
171+
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message));
172+
foreach (var detail in error.Details)
173+
{
174+
WriteWarning(string.Format("{0} : {1}", detail.Code, detail.Message));
175+
}
176+
}
177+
}
178+
}
120179

121180
string path = FileUtility.SaveTemplateFile(
122181
templateName: this.ResourceGroupName,
123-
contents: template.ToString(),
182+
contents: contents,
124183
outputPath:
125184
string.IsNullOrEmpty(this.Path)
126185
? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName)

0 commit comments

Comments
 (0)