Skip to content

Commit 54c9ff4

Browse files
Template Specs: Expose .UIFormDefinition and support UIFormDefinition export (Azure#16088)
* Template Specs: Expose .UIFormDefinition with support for export * Update src/Resources/Resources/ChangeLog.md Co-authored-by: Beisi Zhou <[email protected]>
1 parent 511727c commit 54c9ff4

File tree

7 files changed

+72
-30
lines changed

7 files changed

+72
-30
lines changed

src/Resources/ResourceManager/Components/TemplateSpecPackagingEngine.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ public PackagedTemplate(TemplateSpecVersion versionModel)
3434
this.RootTemplate = (JObject)versionModel?.MainTemplate;
3535
this.Artifacts = versionModel?.LinkedTemplates?.ToArray()
3636
?? new LinkedTemplateArtifact[0];
37+
this.UIFormDefinition = (JObject)versionModel?.UiFormDefinition;
3738
}
3839

3940
public JObject RootTemplate { get; set; }
4041

42+
public JObject UIFormDefinition { get; set; }
43+
4144
public LinkedTemplateArtifact[] Artifacts { get; set; }
4245
}
4346

@@ -75,19 +78,34 @@ public PackingContext(string rootTemplateDirectory)
7578
/// <param name="rootTemplateFilePath">
7679
/// The path to the ARM Template .json file to pack
7780
/// </param>
78-
public static PackagedTemplate Pack(string rootTemplateFilePath)
81+
/// <param name="uiFormDefinitionFilePath">
82+
/// The path to the UI Form Definition .json to pack (if any)
83+
/// </param>
84+
public static PackagedTemplate Pack(string rootTemplateFilePath,
85+
string uiFormDefinitionFilePath)
7986
{
8087
rootTemplateFilePath = Path.GetFullPath(rootTemplateFilePath);
8188
PackingContext context = new PackingContext(
8289
Path.GetDirectoryName(rootTemplateFilePath)
8390
);
84-
91+
8592
PackArtifacts(rootTemplateFilePath, context, out JObject templateObj);
86-
return new PackagedTemplate
93+
var packagedTemplate = new PackagedTemplate
8794
{
8895
RootTemplate = templateObj,
8996
Artifacts = context.Artifacts.ToArray()
9097
};
98+
99+
// If a UI Form Definition file path was provided to us, make sure we package the
100+
// UI Form Definition as well:
101+
102+
if (!string.IsNullOrWhiteSpace(uiFormDefinitionFilePath))
103+
{
104+
string uiFormDefinitionJson = FileUtilities.DataStore.ReadFileAsText(uiFormDefinitionFilePath);
105+
packagedTemplate.UIFormDefinition = JObject.Parse(uiFormDefinitionJson);
106+
}
107+
108+
return packagedTemplate;
91109
}
92110

93111
/// <summary>
@@ -193,10 +211,15 @@ private static void PackArtifacts(
193211
/// <param name="templateFileName">
194212
/// The name of the file to use for the root template json
195213
/// </param>
214+
/// <param name="uiFormDefinitionFileName">
215+
/// The name of the file to use for the ui form definition json (if any). If set to
216+
/// null, the ui definition won't be unpacked even if present within the packaged template.
217+
/// </param>
196218
public static void Unpack(
197219
PackagedTemplate packagedTemplate,
198220
string targetDirectory,
199-
string templateFileName)
221+
string templateFileName,
222+
string uiFormDefinitionFileName)
200223
{
201224
// Ensure paths are normalized:
202225
templateFileName = Path.GetFileName(templateFileName);
@@ -254,6 +277,23 @@ public static void Unpack(
254277
FileUtilities.DataStore.WriteFile(absoluteLocalPath,
255278
((JObject)templateArtifact.Template).ToString());
256279
}
280+
281+
// Lastly, let's write the UIFormDefinition file if a UIFormDefinition is present within
282+
// the packaged template:
283+
284+
if (!string.IsNullOrWhiteSpace(uiFormDefinitionFileName) &&
285+
packagedTemplate.UIFormDefinition != null)
286+
{
287+
string absoluteUIFormDefinitionFilePath = Path.Combine(
288+
targetDirectory,
289+
uiFormDefinitionFileName
290+
);
291+
292+
FileUtilities.DataStore.WriteFile(
293+
absoluteUIFormDefinitionFilePath,
294+
packagedTemplate.UIFormDefinition.ToString()
295+
);
296+
}
257297
}
258298

259299
/// <summary>

src/Resources/ResourceManager/Implementation/TemplateSpecs/ExportAzTemplateSpec.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ public override void ExecuteCmdlet()
123123
OutputFolder = ResolveUserPath(OutputFolder);
124124

125125
string mainTemplateFileName = $"{parentTemplateSpec.Name}.{specificVersion.Name}.json";
126+
string uiFormDefinitionFileName = $"{parentTemplateSpec.Name}.{specificVersion.Name}.uiformdefinition.json";
126127
string fullRootTemplateFilePath = Path.GetFullPath(
127128
Path.Combine(OutputFolder, mainTemplateFileName)
128129
);
129130

130131
if (ShouldProcess(specificVersion.Id, $"Export to '{fullRootTemplateFilePath}'"))
131132
{
132-
TemplateSpecPackagingEngine.Unpack(packagedTemplate, OutputFolder, mainTemplateFileName);
133+
TemplateSpecPackagingEngine.Unpack(
134+
packagedTemplate, OutputFolder, mainTemplateFileName, uiFormDefinitionFileName);
133135
}
134136

135137
WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", fullRootTemplateFilePath));

src/Resources/ResourceManager/Implementation/TemplateSpecs/NewAzTemplateSpec.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ public override void ExecuteCmdlet()
169169
filePath = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose);
170170
}
171171

172-
packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath);
172+
// Note: We set uiFormDefinitionFilePath to null below because we process the UIFormDefinition
173+
// specified by the cmdlet parameters later within this method...
174+
packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath, uiFormDefinitionFilePath: null);
173175
break;
174176
case FromJsonStringParameterSet:
175177

@@ -218,12 +220,7 @@ public override void ExecuteCmdlet()
218220
throw new PSNotSupportedException();
219221
}
220222

221-
JObject UIFormDefinition = new JObject();
222-
if (UIFormDefinitionFile == null && UIFormDefinitionString == null)
223-
{
224-
UIFormDefinition = null;
225-
}
226-
else if (!String.IsNullOrEmpty(UIFormDefinitionFile))
223+
if (!string.IsNullOrWhiteSpace(UIFormDefinitionFile))
227224
{
228225
string UIFormFilePath = this.TryResolvePath(UIFormDefinitionFile);
229226
if (!File.Exists(UIFormFilePath))
@@ -233,13 +230,14 @@ public override void ExecuteCmdlet()
233230
);
234231
}
235232
string UIFormJson = FileUtilities.DataStore.ReadFileAsText(UIFormDefinitionFile);
236-
UIFormDefinition = JObject.Parse(UIFormJson);
233+
packagedTemplate.UIFormDefinition = JObject.Parse(UIFormJson);
237234
}
238-
else if (!String.IsNullOrEmpty(UIFormDefinitionString))
235+
else if (!string.IsNullOrEmpty(UIFormDefinitionString))
239236
{
240-
UIFormDefinition = JObject.Parse(UIFormDefinitionString);
237+
packagedTemplate.UIFormDefinition = JObject.Parse(UIFormDefinitionString);
241238
}
242239

240+
243241
Action createOrUpdateAction = () =>
244242
{
245243
var templateSpecVersion = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpecVersion(
@@ -248,7 +246,6 @@ public override void ExecuteCmdlet()
248246
Version,
249247
Location,
250248
packagedTemplate,
251-
UIFormDefinition,
252249
templateSpecDescription: Description,
253250
templateSpecDisplayName: DisplayName,
254251
versionDescription: VersionDescription,

src/Resources/ResourceManager/Implementation/TemplateSpecs/SetAzTemplateSpec.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ public override void ExecuteCmdlet()
206206
{
207207
filePath = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose);
208208
}
209-
packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath);
209+
210+
// Note: We set uiFormDefinitionFilePath to null below because we process the UIFormDefinition
211+
// specified by the cmdlet parameters later within this method...
212+
packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath, uiFormDefinitionFilePath: null);
210213
break;
211214
case UpdateVersionByIdFromJsonParameterSet:
212215
case UpdateVersionByNameFromJsonParameterSet:
@@ -260,12 +263,7 @@ public override void ExecuteCmdlet()
260263
return;
261264
}
262265

263-
JObject UIFormDefinition = new JObject();
264-
if (UIFormDefinitionFile == null && UIFormDefinitionString == null)
265-
{
266-
UIFormDefinition = null;
267-
}
268-
else if (!String.IsNullOrEmpty(UIFormDefinitionFile))
266+
if (!string.IsNullOrWhiteSpace(UIFormDefinitionFile))
269267
{
270268
string UIFormFilePath = this.TryResolvePath(UIFormDefinitionFile);
271269
if (!File.Exists(UIFormFilePath))
@@ -275,11 +273,11 @@ public override void ExecuteCmdlet()
275273
);
276274
}
277275
string UIFormJson = FileUtilities.DataStore.ReadFileAsText(UIFormDefinitionFile);
278-
UIFormDefinition = JObject.Parse(UIFormJson);
276+
packagedTemplate.UIFormDefinition = JObject.Parse(UIFormJson);
279277
}
280-
else if (!String.IsNullOrEmpty(UIFormDefinitionString))
278+
else if (!string.IsNullOrWhiteSpace(UIFormDefinitionString))
281279
{
282-
UIFormDefinition = JObject.Parse(UIFormDefinitionString);
280+
packagedTemplate.UIFormDefinition = JObject.Parse(UIFormDefinitionString);
283281
}
284282

285283
var templateSpecVersion = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpecVersion(
@@ -288,7 +286,6 @@ public override void ExecuteCmdlet()
288286
Version,
289287
Location,
290288
packagedTemplate,
291-
UIFormDefinition,
292289
templateSpecDescription: Description,
293290
templateSpecDisplayName: DisplayName,
294291
versionDescription: VersionDescription,

src/Resources/ResourceManager/SdkClient/TemplateSpecsSdkClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ public PSTemplateSpec CreateOrUpdateTemplateSpecVersion(
182182
string templateSpecVersion,
183183
string location,
184184
PackagedTemplate packagedTemplate,
185-
object UIFormDefinition,
186185
string templateSpecDisplayName = null,
187186
string templateSpecDescription = null,
188187
string versionDescription = null,
@@ -212,7 +211,7 @@ public PSTemplateSpec CreateOrUpdateTemplateSpecVersion(
212211
MainTemplate = packagedTemplate.RootTemplate,
213212
LinkedTemplates = packagedTemplate.Artifacts?.ToList(),
214213
Description = versionDescription ?? existingTemplateSpecVersion?.Description,
215-
UiFormDefinition = UIFormDefinition
214+
UiFormDefinition = packagedTemplate.UIFormDefinition
216215
};
217216

218217
// Handle our conditional tagging:

src/Resources/ResourceManager/SdkModels/TemplateSpecs/PSTemplateSpecVersion.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public class PSTemplateSpecVersion
6767
/// </summary>
6868
public DateTime? LastModifiedTime { get; private set; }
6969

70+
/// <summary>
71+
/// Gets or sets the UI Form definition (if any) for the template spec version
72+
/// </summary>
73+
public string UIFormDefinition { get; set; }
74+
7075
/// <summary>
7176
/// Converts a template spec model from the Azure SDK to the powershell
7277
/// exposed template spec model.
@@ -92,7 +97,8 @@ internal static PSTemplateSpecVersion FromAzureSDKTemplateSpecVersion(
9297
? new Dictionary<string, string>()
9398
: new Dictionary<string, string>(templateSpecVersion.Tags),
9499
// Note: Cast is redundant, but present for clarity reasons:
95-
MainTemplate = ((JToken)templateSpecVersion.MainTemplate).ToString()
100+
MainTemplate = ((JToken)templateSpecVersion.MainTemplate).ToString(),
101+
UIFormDefinition = ((JToken)templateSpecVersion.UiFormDefinition)?.ToString()
96102
};
97103

98104
if (templateSpecVersion.LinkedTemplates?.Any() == true) {

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Added property `UIFormDefinition` to Template Spec Versions, `Export-AzTemplateSpec` will now include a Template Spec Version's UIFormDefinition (if any) as part of the export.
2223

2324
## Version 4.4.0
2425
* Added a clearer error message for a case in which TemplateUri do not accept bicep file.

0 commit comments

Comments
 (0)