Skip to content

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

Merged
merged 11 commits into from
Oct 27, 2020
Merged
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 @@ -25,7 +25,7 @@ public abstract class ResourceManagerCmdletBaseWithApiVersion : ResourceManagerC
/// </summary>
[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 string ApiVersion { get; set; }
public virtual string ApiVersion { get; set; }

private Dictionary<string, string> GetCmdletHeaders()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Copy link
Contributor

@filizt filizt Oct 27, 2020

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.

{
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);
Copy link
Contributor

@filizt filizt Oct 23, 2020

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand the scenario.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. We'll chat offline.


In reply to: 510976108 [](ancestors = 510976108)


var template = exportedTemplate.Template;
contents = template.ToString();

var error = exportedTemplate.Error;

if(error != null)
{
WriteWarning(string.Format("{0} : {1}", error.Code, error.Message));
foreach (var detail in error.Details)
Expand All @@ -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()
Copy link
Contributor

@filizt filizt Oct 27, 2020

Choose a reason for hiding this comment

The 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ private ResourceGroup CreateOrUpdateResourceGroup(string name, string location,
return result;
}

public ResourceGroupExportResult ExportResourceGroup(string resourceGroupName, ExportTemplateRequest properties)
{
return ResourceManagementClient.ResourceGroups.ExportTemplate(resourceGroupName, properties);
}

private void WriteVerbose(string progress)
{
if (VerboseLogger != null)
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Updated `Export-AzResourceGroup` to use the SDK.

## Version 3.0.0
* Fixed parsing bug
Expand Down