-
Notifications
You must be signed in to change notification settings - Fork 4k
Updated Export-AzResourceGroup cmdlet to use the SDK. #13275
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
Changes from all commits
83792f9
89a48cf
a734307
c741602
e72e11e
4a00dbb
cc893be
0afed3c
be51822
24e6df4
fbcb08b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation | |
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ResourceIds; | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities; | ||
using Microsoft.Azure.Management.ResourceManager.Models; | ||
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; | ||
using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
@@ -93,51 +95,43 @@ public class ExportAzureResourceGroupCmdlet : ResourceManagerCmdletBaseWithApiVe | |
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")] | ||
public SwitchParameter Force { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the API version. | ||
/// </summary> | ||
[CmdletParameterBreakingChange("ApiVersion", ChangeDescription = "Parameter is being deprecated without being replaced. Using the lastest possible API version will become the default behavior.")] | ||
[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.")] | ||
[ValidateNotNullOrEmpty] | ||
public override string ApiVersion { get; set; } | ||
|
||
/// <summary> | ||
/// Executes the cmdlet. | ||
/// </summary> | ||
protected override void OnProcessRecord() | ||
{ | ||
base.OnProcessRecord(); | ||
string contents; | ||
|
||
if (ShouldProcess(ResourceGroupName, VerbsData.Export)) | ||
{ | ||
|
||
var resourceGroupId = this.GetResourceGroupId(); | ||
|
||
var apiVersion = this.ApiVersion ?? DefaultApiVersion; | ||
|
||
var parameters = new ExportTemplateParameters | ||
{ | ||
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId), | ||
Options = this.GetExportOptions(), | ||
}; | ||
|
||
var operationResult = this.GetResourcesClient() | ||
.InvokeActionOnResource<JObject>( | ||
resourceId: resourceGroupId, | ||
action: Constants.ExportTemplate, | ||
parameters: parameters.ToJToken(), | ||
apiVersion: apiVersion, | ||
cancellationToken: this.CancellationToken.Value) | ||
.Result; | ||
|
||
var managementUri = this.GetResourcesClient() | ||
.GetResourceManagementRequestUri( | ||
resourceId: resourceGroupId, | ||
apiVersion: apiVersion, | ||
action: Constants.ExportTemplate); | ||
|
||
var activity = string.Format("POST {0}", managementUri.PathAndQuery); | ||
var resultString = this.GetLongRunningOperationTracker(activityName: activity, | ||
isResourceCreateOrUpdate: false) | ||
.WaitOnOperation(operationResult: operationResult); | ||
|
||
var template = JToken.FromObject(JObject.Parse(resultString)["template"]); | ||
|
||
if (JObject.Parse(resultString)["error"] != null) | ||
if (this.IsParameterBound(c => c.ApiVersion)) | ||
{ | ||
ExtendedErrorInfo error; | ||
if (JObject.Parse(resultString)["error"].TryConvertTo(out error)) | ||
var parameters = new Management.ResourceManager.Models.ExportTemplateRequest | ||
{ | ||
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId), | ||
Options = this.GetExportOptions(), | ||
}; | ||
|
||
var exportedTemplate = ResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you had a chance to test export async route? AFAIK, the service side goes into async operation based on the time it takes to export or based on the number of resources (I think it was >= 20 resources). It might be easier to test async route by trying more than 20 resources to export rather than trying to hit the timebound. Do you mind testing this scenario, if you haven't already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand the scenario. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
var template = exportedTemplate.Template; | ||
contents = template.ToString(); | ||
|
||
var error = exportedTemplate.Error; | ||
isra-fel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(error != null) | ||
{ | ||
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message)); | ||
foreach (var detail in error.Details) | ||
|
@@ -146,10 +140,53 @@ protected override void OnProcessRecord() | |
} | ||
} | ||
} | ||
else | ||
{ | ||
var parameters = new ExportTemplateParameters | ||
{ | ||
Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId), | ||
Options = this.GetExportOptions(), | ||
}; | ||
var apiVersion = this.ApiVersion ?? DefaultApiVersion; | ||
var operationResult = this.GetResourcesClient() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also wouldn't need this line after "this.IsParameterBound(c => c.ApiVersion" |
||
.InvokeActionOnResource<JObject>( | ||
resourceId: resourceGroupId, | ||
action: Constants.ExportTemplate, | ||
parameters: parameters.ToJToken(), | ||
apiVersion: apiVersion, | ||
cancellationToken: this.CancellationToken.Value) | ||
.Result; | ||
|
||
var managementUri = this.GetResourcesClient() | ||
.GetResourceManagementRequestUri( | ||
resourceId: resourceGroupId, | ||
apiVersion: apiVersion, | ||
action: Constants.ExportTemplate); | ||
|
||
var activity = string.Format("POST {0}", managementUri.PathAndQuery); | ||
var resultString = this.GetLongRunningOperationTracker(activityName: activity, | ||
isResourceCreateOrUpdate: false) | ||
.WaitOnOperation(operationResult: operationResult); | ||
|
||
var template = JToken.FromObject(JObject.Parse(resultString)["template"]); | ||
contents = template.ToString(); | ||
|
||
if (JObject.Parse(resultString)["error"] != null) | ||
{ | ||
if (JObject.Parse(resultString)["error"].TryConvertTo(out ExtendedErrorInfo error)) | ||
{ | ||
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message)); | ||
foreach (var detail in error.Details) | ||
{ | ||
WriteWarning(string.Format("{0} : {1}", detail.Code, detail.Message)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
string path = FileUtility.SaveTemplateFile( | ||
templateName: this.ResourceGroupName, | ||
contents: template.ToString(), | ||
contents: contents, | ||
outputPath: | ||
string.IsNullOrEmpty(this.Path) | ||
? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName) | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic seems like it should be reversed - if we have an apiVersion provided we should go through the Rest API client, otherwise SDK client.