VisitAction IArmVisitor <VisitAction> .VisitNestedTemplate(ArmNestedTemplate nestedTemplate) { if (ShouldStop(VisitNestedTemplate(nestedTemplate), out VisitAction parentAction)) { return(parentAction); } if (VisitDictionaryAndCheckStop(nestedTemplate, out parentAction)) { return(parentAction); } return(VisitAction.Continue); }
private async Task <ArmNestedTemplate> RunIOOperationsAsync(ArmNestedTemplate template, CancellationToken cancellationToken) { if (!NoHashTemplate) { SafeWriteVerbose("Signing template"); template = await AddHashToTemplate(template, cancellationToken).ConfigureAwait(false); } if (!NoWriteFile) { await WriteTemplate(template, cancellationToken).ConfigureAwait(false); } return(template); }
private async Task <ArmNestedTemplate> AddHashToTemplate(ArmNestedTemplate template, CancellationToken cancellationToken) { SafeWriteVerbose("Getting Azure token"); string token = GetAzureToken(cancellationToken); using var stream = new MemoryStream(); using var writer = new StreamWriter(stream, Encoding.UTF8); using var jsonWriter = new JsonTextWriter(writer); using var httpClient = Verbose ? new HttpClient(new VerboseHttpLoggingHandler(Host.UI, new HttpClientHandler())) : new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); await template.ToJson().WriteToAsync(jsonWriter, cancellationToken).ConfigureAwait(false); await jsonWriter.FlushAsync(cancellationToken); stream.Seek(0, SeekOrigin.Begin); var body = new StreamContent(stream) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }, }, }; SafeWriteVerbose("Initiating HTTP request"); using HttpResponseMessage response = await httpClient.PostAsync(TemplateSigningApiUri, body, cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); Stream responseBody = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); using var streamReader = new StreamReader(responseBody); using var jsonReader = new JsonTextReader(streamReader); string hash = await GetHashFromJsonResponse(jsonReader, cancellationToken).ConfigureAwait(false); SafeWriteVerbose($"Adding hash '{hash}' to template"); ((PSArmTopLevelTemplateMetadata)template.Metadata).GeneratorMetadata.TemplateHash = new ArmStringLiteral(hash); return(template); }
private async Task WriteTemplate(ArmNestedTemplate template, CancellationToken cancellationToken) { string outPath = GetOutPath(); FileMode writeMode = Force ? FileMode.Create : FileMode.CreateNew; using var file = new FileStream(outPath, writeMode, FileAccess.Write, FileShare.Read, bufferSize: 4096, useAsync: true); using var writer = new StreamWriter(file, Encoding.UTF8); using var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented, }; SafeWriteVerbose($"Writing template to '{outPath}'"); await template.ToJson().WriteToAsync(jsonWriter, cancellationToken).ConfigureAwait(false); await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); }
protected override void EndProcessing() { IReadOnlyDictionary <IArmString, ArmElement> armParameters = null; try { armParameters = GetTemplateParameters(); } catch (Exception e) { this.ThrowTerminatingError( e, "TemplateParameterConversionError", ErrorCategory.InvalidArgument, Parameters); } ArmNestedTemplate aggregatedTemplate = null; using (var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace)) { WriteVerbose("Building template executor"); PSArmTemplateExecutor templateExecutor = _templateExecutorBuilder.Build(pwsh); try { WriteVerbose("Finding and evaluating templates"); aggregatedTemplate = templateExecutor.EvaluatePSArmTemplates(Parameters, _cancellationSource.Token); } catch (OperationCanceledException) { return; } catch (TemplateExecutionException templateException) { ThrowTerminatingError(templateException.ErrorRecord); return; } catch (RuntimeException runtimeException) { ThrowTerminatingError( new ErrorRecord(runtimeException.ErrorRecord, runtimeException)); return; } catch (InvalidOperationException invalidOpException) { this.ThrowTerminatingError(invalidOpException, "TemplateEvaluationError", ErrorCategory.InvalidOperation, TemplatePath); return; } } // Now instantiate the template aggregatedTemplate = (ArmNestedTemplate)aggregatedTemplate.Instantiate(armParameters); // Set the PowerShell version for telemetry string psVersion = ((Hashtable)SessionState.PSVariable.GetValue("PSVersionTable"))["PSVersion"].ToString(); ((PSArmTopLevelTemplateMetadata)aggregatedTemplate.Metadata).GeneratorMetadata.PowerShellVersion = new ArmStringLiteral(psVersion); try { aggregatedTemplate = RunIOOperationsAsync(aggregatedTemplate, _cancellationSource.Token).GetAwaiter().GetResult(); } catch (OperationCanceledException) { return; } catch (HttpRequestException httpException) { this.ThrowTerminatingError( httpException, "TemplateHashRequestFailed", ErrorCategory.ConnectionError); } catch (IOException ioException) { this.ThrowTerminatingError( ioException, "TemplateFileWriteFailed", ErrorCategory.WriteError, GetOutPath()); } catch (Exception e) { this.ThrowTerminatingError( e, "TemplateCreationFailed", ErrorCategory.InvalidOperation); } if (PassThru) { WriteObject(aggregatedTemplate); } }
public virtual VisitAction VisitNestedTemplate(ArmNestedTemplate nestedTemplate) => DefaultVisit(nestedTemplate);
public object VisitNestedTemplate(ArmNestedTemplate nestedTemplate) => VisitTemplate(nestedTemplate);
public JToken VisitNestedTemplate(ArmNestedTemplate nestedTemplate) => VisitTemplate(nestedTemplate);