public override void ExecuteCmdlet() { BicepUtility.PublishFile(this.TryResolvePath(this.FilePath), this.Target, this.WriteVerbose, this.WriteWarning); if (this.PassThru.IsPresent) { this.WriteObject(true); } }
public override void ExecuteCmdlet() { try { PackagedTemplate packagedTemplate; switch (ParameterSetName) { case FromJsonFileParameterSet: string filePath = this.TryResolvePath(TemplateFile); if (!File.Exists(filePath)) { throw new PSInvalidOperationException( string.Format(ProjectResources.InvalidFilePath, TemplateFile) ); } if (BicepUtility.IsBicepFile(TemplateFile)) { filePath = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose); } packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath); break; case FromJsonStringParameterSet: JObject parsedTemplate; try { parsedTemplate = JObject.Parse(TemplateJson); } catch { // Check if the user may have inadvertantly passed a file path using "-TemplateJson" // instead of using "-TemplateFile". If they did, display a warning message. Note we // do not currently automatically switch to using a file in this case because if the // JSON string is coming from an external script/source not authored directly by the // caller it could result in a sensitive template being PUT unintentionally: try { string asFilePath = this.TryResolvePath(TemplateJson); if (File.Exists(asFilePath)) { WriteWarning( $"'{TemplateJson}' was found to exist as a file. Did you mean to use '-TemplateFile' instead?" ); } } catch { // Subsequent failure in the file existence check... Ignore it } throw; } // When we're provided with a raw JSON string for the template we don't // do any special packaging... (ie: we don't pack artifacts because there // is no well known root path): packagedTemplate = new PackagedTemplate { RootTemplate = parsedTemplate, Artifacts = new LinkedTemplateArtifact[0] }; break; default: throw new PSNotSupportedException(); } JObject UIFormDefinition = new JObject(); if (UIFormDefinitionFile == null && UIFormDefinitionString == null) { UIFormDefinition = null; } else if (!String.IsNullOrEmpty(UIFormDefinitionFile)) { string UIFormFilePath = this.TryResolvePath(UIFormDefinitionFile); if (!File.Exists(UIFormFilePath)) { throw new PSInvalidOperationException( string.Format(ProjectResources.InvalidFilePath, UIFormDefinitionFile) ); } string UIFormJson = FileUtilities.DataStore.ReadFileAsText(UIFormDefinitionFile); UIFormDefinition = JObject.Parse(UIFormJson); } else if (!String.IsNullOrEmpty(UIFormDefinitionString)) { UIFormDefinition = JObject.Parse(UIFormDefinitionString); } Action createOrUpdateAction = () => { var templateSpecVersion = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpecVersion( ResourceGroupName, Name, Version, Location, packagedTemplate, UIFormDefinition, templateSpecDescription: Description, templateSpecDisplayName: DisplayName, versionDescription: VersionDescription, templateSpecTags: Tag, // Note: Only applied if template spec doesn't exist versionTags: Tag ); WriteObject(templateSpecVersion); }; if (!Force.IsPresent && TemplateSpecsSdkClient.GetAzureSdkTemplateSpecVersion( ResourceGroupName, Name, Version, throwIfNotExists: false) != null) { // The template spec version already exists and force is not present, so // let's confirm with the user that he/she wishes to overwrite (update) it: string confirmationMessage = $"Template Spec version '{Version}' already exists and this action will overwrite existing " + "data for this version. Are you sure you'd like to overwrite existing " + $"Template Spec version data for Template Spec '{Name}' version '{Version}'?"; ConfirmAction( Force.IsPresent, confirmationMessage, "Update", $"{Name}/versions/{Version}", createOrUpdateAction ); } else { if (!ShouldProcess($"{Name}/versions/{Version}", "Create")) { return; // Don't perform the actual creation/update action } createOrUpdateAction(); } } catch (Exception ex) { WriteExceptionError(ex); } }
protected void BuildAndUseBicepTemplate() { TemplateFile = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose); }
public virtual object GetDynamicParameters() { if (BicepUtility.IsBicepFile(TemplateFile)) { BuildAndUseBicepTemplate(); } if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt)) { // Resolve the static parameter names for this cmdlet: string[] staticParameterNames = this.GetStaticParameterNames(); if (TemplateObject != null && TemplateObject != templateObject) { templateObject = TemplateObject; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateFile) && !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase)) { templateFile = TemplateFile; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateUri) && !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(protectedTemplateUri)) { templateUri = TemplateUri; } else { templateUri = protectedTemplateUri; } if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateSpecId) && !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase)) { templateSpecId = TemplateSpecId; ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId); if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase)) { throw new PSArgumentException("No version found in Resource ID"); } if (!string.IsNullOrEmpty(resourceIdentifier.Subscription) && TemplateSpecsClient.SubscriptionId != resourceIdentifier.Subscription) { // The template spec is in a different subscription than our default // context. Force the client to use that subscription: TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription; } JObject templateObj = (JObject)null; try { var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get( ResourceIdUtility.GetResourceGroupName(templateSpecId), ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0], resourceIdentifier.ResourceName); if (!(templateSpecVersion.Template is JObject)) { throw new InvalidOperationException("Unexpected type."); // Sanity check } templateObj = (JObject)templateSpecVersion.Template; } catch (TemplateSpecsErrorException e) { //If the templateSpec resourceID is pointing to a non existant resource if (e.Response.StatusCode.Equals(HttpStatusCode.NotFound)) { //By returning null, we are introducing parity in the way templateURI and templateSpecId are validated. Gives a cleaner error message in line with the error message for invalid templateURI return(null); } //Throw for any other error that is not due to a 404 for the template resource. throw; } if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } } RegisterDynamicParameters(dynamicParameters); return(dynamicParameters); }
public override void ExecuteCmdlet() { try { // TODO: Update the following to use ??= when we upgrade to C# 8.0 if (ResourceGroupName == null) { ResourceGroupName = ResourceIdUtility.GetResourceGroupName(this.ResourceId); } if (Name == null) { Name = ResourceIdUtility.GetResourceName(this.ResourceId); } if (Version != null) { // This is a version specific update... PackagedTemplate packagedTemplate; switch (ParameterSetName) { case UpdateVersionByIdFromJsonFileParameterSet: case UpdateVersionByNameFromJsonFileParameterSet: string filePath = this.TryResolvePath(TemplateFile); if (!File.Exists(filePath)) { throw new PSInvalidOperationException( string.Format(ProjectResources.InvalidFilePath, TemplateFile) ); } if (BicepUtility.IsBicepFile(TemplateFile)) { filePath = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning); } // Note: We set uiFormDefinitionFilePath to null below because we process the UIFormDefinition // specified by the cmdlet parameters later within this method... packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath, uiFormDefinitionFilePath: null); break; case UpdateVersionByIdFromJsonParameterSet: case UpdateVersionByNameFromJsonParameterSet: JObject parsedTemplate; try { parsedTemplate = JObject.Parse(TemplateJson); } catch { // Check if the user may have inadvertantly passed a file path using "-TemplateJson" // instead of using "-TemplateFile". If they did, display a warning message. Note we // do not currently automatically switch to using a file in this case because if the // JSON string is coming from an external script/source not authored directly by the // caller it could result in a sensitive template being PUT unintentionally: try { string asFilePath = this.TryResolvePath(TemplateJson); if (File.Exists(asFilePath)) { WriteWarning( $"'{TemplateJson}' was found to exist as a file. Did you mean to use '-TemplateFile' instead?" ); } } catch { // Subsequent failure in the file existence check... Ignore it } throw; } // When we're provided with a raw JSON string for the template we don't // do any special packaging... (ie: we don't pack artifacts because there // is no well known root path): packagedTemplate = new PackagedTemplate { RootTemplate = JObject.Parse(TemplateJson), Artifacts = new LinkedTemplateArtifact[0] }; break; default: throw new PSNotSupportedException(); } if (!ShouldProcess($"{Name}/versions/{Version}", "Create or Update")) { return; } if (!string.IsNullOrWhiteSpace(UIFormDefinitionFile)) { string UIFormFilePath = this.TryResolvePath(UIFormDefinitionFile); if (!File.Exists(UIFormFilePath)) { throw new PSInvalidOperationException( string.Format(ProjectResources.InvalidFilePath, UIFormDefinitionFile) ); } string UIFormJson = FileUtilities.DataStore.ReadFileAsText(UIFormDefinitionFile); packagedTemplate.UIFormDefinition = JObject.Parse(UIFormJson); } else if (!string.IsNullOrWhiteSpace(UIFormDefinitionString)) { packagedTemplate.UIFormDefinition = JObject.Parse(UIFormDefinitionString); } var templateSpecVersion = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpecVersion( ResourceGroupName, Name, Version, Location, packagedTemplate, templateSpecDescription: Description, templateSpecDisplayName: DisplayName, versionDescription: VersionDescription, templateSpecTags: Tag, // Note: Only applied if template spec doesn't exist versionTags: Tag ); WriteObject(templateSpecVersion); } else { // This is an update to the root template spec only: if (!ShouldProcess(Name, "Create or Update")) { return; } var templateSpec = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpec( ResourceGroupName, Name, Location, templateSpecDescription: Description, templateSpecDisplayName: DisplayName, tags: Tag ); // As the root template spec is a seperate resource type, it won't contain version // details. To provide more information to the user, let's get the template spec // with all of its version details: var templateSpecWithVersions = TemplateSpecsSdkClient.GetTemplateSpec(templateSpec.Name, templateSpec.ResourceGroupName); WriteObject(templateSpecWithVersions); } } catch (Exception ex) { WriteExceptionError(ex); } }
public void TestIsBicepFile() { Assert.True(BicepUtility.IsBicepFile("test.bicep")); Assert.False(BicepUtility.IsBicepFile("test.json")); }
protected void BuildAndUseBicepTemplate() { TemplateFile = BicepUtility.BuildFile(this.ExecuteScript <Object>, this.ResolvePath(TemplateFile)); }
public void TestCheckBicepExecutable() { Assert.True(BicepUtility.CheckBicepExecutable(FakeTrueScriptExcutor <Object>)); Assert.False(BicepUtility.CheckBicepExecutable(FakeFalseScriptExcutor <Object>)); }