Ejemplo n.º 1
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        protected override void OnProcessRecord()
        {
            base.OnProcessRecord();

            var resourceId = this.GetResourceId();

            var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;

            var parameters = new ExportTemplateParameters
            {
                Resources = new string [] { "*" },
                Options   = this.GetExportOptions() ?? null
            };

            var operationResult = this.GetResourcesClient()
                                  .InvokeActionOnResource <JObject>(
                resourceId: resourceId,
                action: Constants.ExportTemplate,
                parameters: parameters.ToJToken(),
                apiVersion: apiVersion,
                cancellationToken: this.CancellationToken.Value)
                                  .Result;

            var managementUri = this.GetResourcesClient()
                                .GetResourceManagementRequestUri(
                resourceId: resourceId,
                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)
            {
                ExtendedErrorInfo error;
                if (JObject.Parse(resultString)["error"].TryConvertTo(out 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(),
                outputPath: string.IsNullOrEmpty(this.Path) ? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName) : this.TryResolvePath(this.Path),
                overwrite: this.Force,
                confirmAction: ConfirmAction);

            WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", path));
        }
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        protected override void OnProcessRecord()
        {
            base.OnProcessRecord();
            string contents;

            if (ShouldProcess(ResourceGroupName, VerbsData.Export))
            {
                var resourceGroupId = this.GetResourceGroupId();

                if (this.IsParameterBound(c => c.ApiVersion))
                {
                    var parameters = new Management.ResourceManager.Models.ExportTemplateRequest
                    {
                        Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
                        Options   = this.GetExportOptions(),
                    };

                    var exportedTemplate = ResourceManagerSdkClient.ExportResourceGroup(ResourceGroupName, parameters);

                    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)
                        {
                            WriteWarning(string.Format("{0} : {1}", detail.Code, detail.Message));
                        }
                    }
                }
                else
                {
                    var parameters = new ExportTemplateParameters
                    {
                        Resources = this.GetResourcesFilter(resourceGroupId: resourceGroupId),
                        Options   = this.GetExportOptions(),
                    };
                    var apiVersion      = this.ApiVersion ?? DefaultApiVersion;
                    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"]);
                    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: contents,
                    outputPath:
                    string.IsNullOrEmpty(this.Path)
                            ? System.IO.Path.Combine(CurrentPath(), this.ResourceGroupName)
                            : this.TryResolvePath(this.Path),
                    overwrite: Force.IsPresent,
                    shouldContinue: ShouldContinue);

                WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", path));
            }
        }