public static void WriteToTextWriter(TextWriter textWriter, ArmTemplate template) { var visitor = new PSArmWritingVisitor(textWriter); template.RunVisit(visitor); textWriter.Flush(); }
/// <summary> /// Get the path for the arm template file on Azure Storage /// </summary> /// <param name="offer"></param> /// <param name="plan"></param> /// <param name="subscriptionAction"></param> /// <returns>File path for the template file on blob storage</returns> public async Task <string> GetTemplatePath( Offer offer, Plan plan, FulfillmentAction subscriptionAction ) { ArmTemplate template = null; switch (subscriptionAction) { case FulfillmentAction.Activate: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.SubscribeArmTemplateName); break; case FulfillmentAction.Update: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.SubscribeArmTemplateName); break; case FulfillmentAction.Unsubscribe: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.UnsubscribeArmTemplateName); break; default: throw new InvalidEnumArgumentException(); } return(template.TemplateFilePath); }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string templateName, [FromBody] object armTemplateJSON) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (armTemplateJSON == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplate).Name), UserErrorCode.PayloadNotProvided); } ArmTemplate armTemplate = null; if (await _armTemplateService.ExistsAsync(offerName, templateName)) { // Update. Do not log armtemplatejson _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(ArmTemplateParameter).Name, templateName)); armTemplate = await _armTemplateService.UpdateAsync(offerName, templateName, armTemplateJSON); return(Ok(armTemplate)); } else { // Update. Do not log armtemplatejson _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(ArmTemplateParameter).Name, templateName)); armTemplate = await _armTemplateService.CreateAsync(offerName, templateName, armTemplateJSON); return(CreatedAtRoute(nameof(GetAsync) + nameof(ArmTemplate), new { OfferName = offerName, TemplateName = templateName }, armTemplate)); } }
public static string WriteToString(ArmTemplate template) { using (var stringWriter = new StringWriter()) { WriteToTextWriter(stringWriter, template); return(stringWriter.ToString()); } }
public static void WriteToFile(string path, ArmTemplate template, FileMode fileMode) { using (var file = new FileStream(path, fileMode, FileAccess.Write, FileShare.Read)) using (var writer = new StreamWriter(file, Encoding.UTF8)) { WriteToTextWriter(writer, template); } }
public void LoadArmTemplate_MinimalFile_Returns_ExpectedArmTemplate() { // Arrange var filePath = @"c:\templates\main\minimal.arm.template.json"; var expectedArmTemplate = new ArmTemplate(filePath, JObject.Parse(fileSystem.File.ReadAllText(filePath))); // Act var arm = sut.LoadArmTemplate(filePath); // Assert arm.Should().BeEquivalentTo(expectedArmTemplate); }
VisitAction IArmVisitor <VisitAction> .VisitTemplate(ArmTemplate template) { if (ShouldStop(VisitTemplate(template), out VisitAction parentAction)) { return(parentAction); } if (VisitDictionaryAndCheckStop(template, out parentAction)) { return(parentAction); } return(VisitAction.Continue); }
public object VisitTemplate(ArmTemplate template) { _writer.Write("Arm "); OpenBlock(); WriteParametersAndVariables(template.Parameters, template.Variables); bool needSeparator = false; WriteResources(template.Resources, ref needSeparator); WriteOutputs(template.Outputs, ref needSeparator); CloseBlock(); return(null); }
public ArmTemplate ParseJObject(string templateName, JObject templateObject) { var template = new ArmTemplate(templateName); if (templateObject.TryGetValue("$schema", out JToken schemaValue)) { template.Schema = new ArmStringLiteral(CoerceJTokenToValue <string>(schemaValue)); } else { template.Schema = s_defaultSchema; } if (templateObject.TryGetValue("contentVersion", out JToken contentVersionValue)) { template.ContentVersion = new ArmStringLiteral(CoerceJTokenToValue <string>(contentVersionValue)); } else { template.ContentVersion = s_defaultVersion; } (ArmObject <ArmParameter> parameters, ArmObject <ArmVariable> variables) = ReadParametersAndVariables(templateObject); if (parameters is not null) { template.Parameters = parameters; } if (variables is not null) { template.Variables = variables; } if (templateObject.TryGetValue("resources", out JToken resourcesValue)) { template.Resources = ReadArray((JArray)resourcesValue, new ArmArray <ArmResource>(), ReadResource); } if (templateObject.TryGetValue("outputs", out JToken outputsValue)) { template.Outputs = ReadSubobject((JObject)outputsValue, new ArmObject <ArmOutput>(), ReadOutput); } return(template); }
internal static ArmTemplate TransformTemplate(InputDashboardArmTemplate inputTemplate, TransformationDetails transformationDetails) { try { var inputJson = JObject.Parse(inputTemplate.TemplateJson); var armTemplate = new ArmTemplate(inputJson); ArmTemplateTransformer rootTransformer = CreateTransformer(transformationDetails); IArmPropertyValueResolver armPropertyValueResolver = CreatePropertyValueResolver(transformationDetails); rootTransformer.Transform(armTemplate, armPropertyValueResolver); return(armTemplate); } catch (Exception e) { throw new InvalidInputTemplateException(e); } }
/// <summary> /// Parse the ARM template to get the parameter names and build an object containing the parameter names and values /// </summary> /// <param name="offer"></param> /// <param name="plan"></param> /// <param name="subscriptionAction"></param> /// <returns>An object containing the parameter names and values</returns> public async Task <Dictionary <string, object> > GetTemplateParameters( Offer offer, Plan plan, FulfillmentAction subscriptionAction ) { ArmTemplate template = null; switch (subscriptionAction) { case FulfillmentAction.Activate: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.SubscribeArmTemplateName); break; case FulfillmentAction.Update: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.SubscribeArmTemplateName); break; case FulfillmentAction.Unsubscribe: template = await _lunaClient.GetArmTemplate(offer.OfferName, plan.UnsubscribeArmTemplateName); break; default: throw new InvalidEnumArgumentException(); } string templateContent = await _storage.DownloadToTextAsync(template.TemplateFilePath); var parameters = ARMTemplateHelper.GetArmTemplateParameters(templateContent); var parameterList = new Dictionary <string, object>(); foreach (var parameter in parameters) { ArmTemplateParameter atp = await _lunaClient.GetArmTemplateParameter(offer.OfferName, parameter.Key); parameterList.Add( atp.Name, new { Value = atp.Value } ); } return(parameterList); }
public ArmTemplateEngine(string armTemplate, string subscriptionId, string resourceGroupName, string resourceGroupLocation, string workingDirectory, string armTemplateParameters = null, int debuggerVsPid = -1, bool verboseLogging = false) { Contracts.EnsureArgumentNotNull(armTemplate, "armTemplate"); Contracts.EnsureArgumentNotNull(subscriptionId, "subscriptionId"); Contracts.EnsureArgumentNotNull(resourceGroupName, "resourceGroupName"); Contracts.EnsureArgumentNotNull(resourceGroupLocation, "resourceGroupLocation"); _armTemplateFile = armTemplate; _armTemplateParametersFile = armTemplateParameters; _armTemplate = JsonConvert.DeserializeObject <ArmTemplate>(_armTemplateFile, new ConverterWithLinfo()); _verboseLogging = verboseLogging; _armTemplateParameters = !string.IsNullOrEmpty(_armTemplateParametersFile) ? JsonConvert.DeserializeObject <ArmTemplateParameterList>(_armTemplateParametersFile, new ConverterWithLinfo()).Parameters : new Dictionary <string, ArmTemplateParameter>(); _debuggerVsPid = debuggerVsPid; _resourceGroupName = resourceGroupName; _resourceGroupLocation = resourceGroupLocation; _subscriptionId = subscriptionId; _workingDirectory = workingDirectory; SubstituteFromTemplateParametersCheckIfAllParametersAreInitialized(); }
/// <summary> /// Initializes a new instance of the <see cref="T:SeamothArms.API.CraftableSeamothArm"/> class, the basic class for any arm that can be crafted at a Vehicle Upgrade Console. /// </summary> /// <param name="techTypeName">The main internal identifier for this item. Your item's <see cref="T:TechType"/> will be created using this name.</param> /// <param name="friendlyName">The name displayed in-game for this item whether in the open world or in the inventory.</param> /// <param name="description">The description for this item. Typically seen in the PDA, inventory, or crafting screens.</param> /// <param name="iconFileName">The file name for this item's icon without ".png" extension. If this parameter null, the object receives the <see cref="T:SeamothArms.API.ArmTemplate"/> icon.</param> /// <param name="armTemplate">The template <see cref="T:SeamothArms.API.ArmTemplate"/> for cloning.</param> /// <param name="requiredForUnlock">The required <see cref="T:TechType"/> that must first be scanned or picked up to unlock the blueprint for this item.If You use fragment set this to TechType.None</param> /// <param name="fragment">The techtype fragment <see cref="T:SeamothArms.API.SeaTruckArmFragment"/>.</param> protected CraftableSeaTruckArm( string techTypeName, string friendlyName, string description, ArmTemplate armTemplate, TechType requiredForUnlock, SeaTruckArmFragment fragment ) : base(techTypeName, $"{techTypeName}.Prefab") { TechTypeName = techTypeName; FriendlyName = friendlyName; Description = description; PrefabForClone = ArmTypes[armTemplate]; RequiredForUnlock = requiredForUnlock; _Fragment = fragment; ArmTemplate = armTemplate; //IngameMenuHandler.Main.RegisterOnQuitEvent(OnQuitEvent); }
static void Main(string[] args) { var wc = new System.Net.WebClient(); var tmpl = wc.DownloadString("https://raw.githubusercontent.com/windoze/azure-china-swarm/master/azuredeploy.json"); var par = wc.DownloadString( "https://raw.githubusercontent.com/windoze/azure-china-swarm/master/azuredeploy.parameters.json"); var t = new ArmTemplate(JToken.Parse(tmpl)); // t.Resources.Resources[5].SetDependency(1, "hahaha"); var repl = new List <ARMResources>(); repl.Add(new ARMResources { Index = 5, IsMMPUI = true, Type = "Microsoft.Network/virtualNetworks", Value = "Microsoft.Network/virtualNetworkName/someValue" }); var ret = ReplaceExistingResources(repl, tmpl, par, "someResourceGroup", "someDeployment"); Console.Out.WriteLine(ret); Console.In.ReadLine(); }
/// <summary> /// Uploads the given armTemplate as a JSON file in blob storage and records the URI to the /// created resrouce in the db. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="templateName">The name of the ARM template.</param> /// <param name="armTemplateJSON">The ARM Template's raw JSON data.</param> /// <returns>The created armTemplate db record.</returns> public async Task <ArmTemplate> UpdateAsync(string offerName, string templateName, object armTemplateJSON) { if (armTemplateJSON is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplate).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does already have an armTemplate with the same templateName if (!await ExistsAsync(offerName, templateName)) { throw new LunaNotFoundUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); } ArmTemplate armTemplate = await GetAsync(offerName, templateName); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Get the container name associated with the offer var containerName = offer.ContainerName.ToString(); // Upload the armTemplateJSON as a file in blob storage and get the URL to the created resource var url = await uploadToBlobStorageAsync(containerName, GetArmTemplateFileName(templateName), armTemplateJSON.ToString()); _logger.LogInformation($"Arm template {templateName} in offer {offerName} is uploaded to {url}."); armTemplate.TemplateFilePath = url; // Add armTemplate to db _context.ArmTemplates.Update(armTemplate); await _context._SaveChangesAsync(); await UpdateArmTemplateParameters(offer, armTemplateJSON, armTemplate.Id); return(armTemplate); }
public ArmNestedTemplateBuilder AddTemplate(ArmTemplate template) { string templateName = template.TemplateName; if (_templateNameCounts.TryGetValue(templateName, out int count)) { count++; templateName = $"{templateName}_{count}"; _templateNameCounts[templateName] = count; } else { _templateNameCounts[templateName] = 0; } _templateResources.Add(new ArmTemplateResource(new ArmStringLiteral(templateName)) { Template = template, }); return(this); }
public ArmTemplate Create(IEnumerable <LinkedService> linkedServices, IEnumerable <DataSet> dataSets, IEnumerable <Pipeline> pipelines) { var name = GetAdfName(); var dfArm = new DataFactoryArm(name); foreach (var ls in linkedServices) { dfArm.AddResource(new LinkedServiceArm(name, ls)); } foreach (var ds in dataSets) { dfArm.AddResource(new DataSetArm(name, ds)); } foreach (var pl in pipelines) { dfArm.AddResource(new PipelineArm(name, pl)); } var arm = new ArmTemplate(); arm.Resources.Add(dfArm); return(arm); }
protected SpawnableArmFragment( string techTypeName, string friendlyName, ArmTemplate fragmentTemplate, LargeWorldEntity.CellLevel cellLevel = LargeWorldEntity.CellLevel.Medium, float scanTime = 3, int totalFragments = 3 ) : base( techTypeName, friendlyName, template: ArmFragmentTypes[fragmentTemplate], slotType: EntitySlot.Type.Medium, prefabZUp: false, cellLevel: cellLevel, localScale: new Vector3(0.8f, 0.8f, 0.8f), scanTime: scanTime, totalFragments: totalFragments, destroyAfterScan: true ) { ArmFragmentTemplate = fragmentTemplate; }
public static void WriteToFile(string path, ArmTemplate template) => WriteToFile(path, template, FileMode.Create);
protected override void EndProcessing() { string templateName = Name; if (templateName is null) { try { templateName = Path.GetFileName(MyInvocation.ScriptName); templateName = templateName.Substring(0, templateName.Length - PSArmTemplateExecutor.PSArmFileExtension.Length); } catch { // If we fail, just proceed with templateName = null } } ScriptBlock transformedBody; ArmObject <ArmParameter> armParameters; ArmObject <ArmVariable> armVariables; object[] psArgsArray; using (var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace)) { try { transformedBody = new TemplateScriptBlockTransformer(pwsh).GetDeparameterizedTemplateScriptBlock( Body, out armParameters, out armVariables, out psArgsArray); } catch (Exception e) { this.ThrowTerminatingError(e, "TemplateScriptBlockTransformationFailure", ErrorCategory.InvalidArgument, Body); return; } } var template = new ArmTemplate(templateName); if (armParameters is not null && armParameters.Count > 0) { template.Parameters = armParameters; } if (armVariables is not null && armVariables.Count > 0) { template.Variables = armVariables; } var templateBuilder = new ArmBuilder <ArmTemplate>(template); foreach (PSObject output in InvokeCommand.InvokeScript(useLocalScope: true, transformedBody, input: null, psArgsArray)) { if (output.BaseObject is ArmEntry armEntry) { templateBuilder.AddEntry(armEntry); } } WriteObject(template); }
static string ReplaceExistingResources(IList <ARMResources> repl, string template, string parameters, string resourceGroupName, string deploymentName) { var t = new ArmTemplate(JToken.Parse(template)); var p = JObject.Parse(parameters); var dep = new Deployment("subscription", resourceGroupName, deploymentName, t, p); var index = 0; var toBeDeleted = new List <KeyValuePair <int, string> >(); foreach (var rsrc in t.Resources.Resources) { foreach (var armResourcese in repl) { if (armResourcese.Index == index && armResourcese.Type.ToLower() == rsrc.Type.ToLower()) { if (!string.IsNullOrEmpty(armResourcese.Value)) { // To be deleted toBeDeleted.Add(new KeyValuePair <int, string>(index, armResourcese.Value)); // Replace dependency for all resources depend on this one } } } index++; } // Process in reversed order toBeDeleted.Sort(); toBeDeleted.Reverse(); // Replace dependencies foreach (var keyValuePair in toBeDeleted) { var name = (t.Resources.Resources[keyValuePair.Key].Type + "/" + dep.Evaluate(t.Resources.Resources[keyValuePair.Key].Name)).ToString().ToLower(); foreach (var resource in t.Resources.Resources) { int depIndex = 0; foreach (var dependsOn in resource.DependsOn) { try { var d = dep.Evaluate(dependsOn); if (d.ToString().ToLower() == name) { // Update dependencies resource.SetDependency(depIndex, keyValuePair.Value); } } catch (ExpressionException) { // TODO: Warning // Skip resource which is unable to be processed } depIndex++; } } } // Delete resources foreach (var keyValuePair in toBeDeleted) { t.Resources.Model.RemoveAt(keyValuePair.Key); } return(t.Model.ToString()); }
public TemplateFinalizer(string path, ArmTemplate arm) { _path = path; _arm = arm; }
public void SubscriptionDeployment(string resourceGroupName, ArmTemplate armTemplate) { SubscriptionDeployment(resourceGroupName, armTemplate.GetTemplatePath(), armTemplate.GetParametersPath(), armTemplate); }
public Task SubscriptionDeployment(String resourceGroupName, ArmTemplate armTemplate) { return(SubscriptionDeployment(resourceGroupName, armTemplate.GetTemplatePath(), armTemplate.GetParametersPath(), armTemplate)); }
public void ResourceGroupDeployment(string resourceGroupName, ArmTemplate armTemplate) { ResourceGroupDeployment(resourceGroupName, armTemplate.GetTemplatePath(), armTemplate.GetParametersPath(), armTemplate); }
public object VisitTemplate(ArmTemplate template) { throw CreateInvalidException(template); }
/// <summary> /// Uploads the given armTemplate as a JSON file in blob storage and records the URI to the /// created resrouce in the db. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="templateName">The name of the ARM template.</param> /// <param name="armTemplateJSON">The ARM Template's raw JSON data.</param> /// <returns>The created armTemplate db record.</returns> public async Task <ArmTemplate> CreateAsync(string offerName, string templateName, object armTemplateJSON) { if (armTemplateJSON is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplate).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have an armTemplate with the same templateName if (await ExistsAsync(offerName, templateName)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); } // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Get the container name associated with the offer var containerName = offer.ContainerName.ToString(); // Upload the armTemplateJSON as a file in blob storage and get the URL to the created resource var url = await uploadToBlobStorageAsync(containerName, GetArmTemplateFileName(templateName), armTemplateJSON.ToString()); _logger.LogInformation($"Arm template {templateName} in offer {offerName} is uploaded to {url}."); // Create the armTemplate to store in db ArmTemplate armTemplate = new ArmTemplate { OfferId = offer.Id, TemplateName = templateName, TemplateFilePath = url }; // Add armTemplate to db _context.ArmTemplates.Add(armTemplate); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); if (!await _armTemplateParameterService.ExistsAsync(offerName, "resourceGroupLocation")) { // Add parameter for resourceGroupLocation ArmTemplateParameter armParameter = new ArmTemplateParameter { OfferId = offer.Id, Name = "resourceGroupLocation", Type = "string", // TODO: do we need to indicate an incomplete parameter? Value = string.Empty }; await _armTemplateParameterService.CreateAsync(offerName, armTemplate.Id, armParameter); } if (!await _armTemplateParameterService.ExistsAsync(offerName, "entryPointUrl")) { // Add parameter for entryPointLink ArmTemplateParameter armParameter = new ArmTemplateParameter { OfferId = offer.Id, Name = "entryPointUrl", Type = "string", // TODO: do we need to indicate an incomplete parameter? Value = string.Empty }; await _armTemplateParameterService.CreateAsync(offerName, armTemplate.Id, armParameter); } // Add arm template parameters await CreateArmTemplateParameters(offer, armTemplateJSON, armTemplate.Id); return(armTemplate); }
public JToken VisitTemplate(ArmTemplate template) => VisitObject(template);
public virtual VisitAction VisitTemplate(ArmTemplate template) => DefaultVisit(template);