Skip to content

Commit 2105aac

Browse files
Support Terraform samples (#110)
1 parent ee89c99 commit 2105aac

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

docs/configuration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ For example, [remarks/microsoft.resources/remarks.json](../settings/remarks/micr
3131
* `DeploymentRemarks`: Can be used to add custom markdown with information on deploying a particular resource type. For example, explaining some of the quirks.
3232
* `ResourceRemarks`: Can be used to add custom remarks in markdown format about a particular resource type. See [here](https://github.com/Azure/bicep-refdocs-generator/blob/016143aa9375ba0ce62255fed1fad8905cfe75bf/src/TemplateRefGenerator.Tests/Files/markdown/microsoft.keyvault/2023-07-01/vaults.md?plain=1#L29-L35) for an example of what the customized output looks like.
3333
* `PropertyRemarks`: Can be used to replace the description markdown for a particular resource type property.
34-
* `BicepSamples`: A list of `.bicep` files that will be added as samples to documentation. See [here](https://github.com/Azure/bicep-refdocs-generator/blob/016143aa9375ba0ce62255fed1fad8905cfe75bf/src/TemplateRefGenerator.Tests/Files/markdown/microsoft.resources/2024-07-01/resourcegroups.md?plain=1#L61-L75) for an example of what the customized looks like.
34+
* `BicepSamples`: A list of `.bicep` files that will be added as samples to documentation. See [here](https://github.com/Azure/bicep-refdocs-generator/blob/016143aa9375ba0ce62255fed1fad8905cfe75bf/src/TemplateRefGenerator.Tests/Files/markdown/microsoft.resources/2024-07-01/resourcegroups.md?plain=1#L61-L75) for an example of what the customized looks like.
35+
* `TerraformSamples`: A list of `.tf` files that will be added as samples to documentation.

settings/schemas/remarks.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
"items": {
2929
"$ref": "#/definitions/CodeSample"
3030
}
31+
},
32+
"TerraformSamples": {
33+
"type": "array",
34+
"items": {
35+
"$ref": "#/definitions/CodeSample"
36+
}
3137
}
3238
},
3339
"additionalProperties": false,

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageVersion Include="Azure.Bicep.Types" Version="0.5.110" />
1212
<PackageVersion Include="Azure.Bicep.Types.Az" Version="0.2.727" />
1313
<PackageVersion Include="Azure.Deployments.Testing.Utilities" Version="1.473.0" />
14+
<PackageVersion Include="Microsoft.PowerPlatform.ResourceStack" Version="7.0.0.2076" />
1415
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
1516
<PackageVersion Include="MSTest" Version="3.10.2" />
1617
<PackageVersion Include="System.IO.Abstractions" Version="22.0.14" />

src/TemplateRefGenerator/Config/RemarksLoader.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public record RemarksFile(
5252
ImmutableArray<ResourceRemark>? ResourceRemarks,
5353
ImmutableArray<PropertyRemark>? PropertyRemarks,
5454
ImmutableArray<DeploymentRemark>? DeploymentRemarks,
55-
ImmutableArray<CodeSample>? BicepSamples)
55+
ImmutableArray<CodeSample>? BicepSamples,
56+
ImmutableArray<CodeSample>? TerraformSamples)
5657
{
5758
public static RemarksFile Empty
58-
=> new(null, null, null, null);
59+
=> new(null, null, null, null, null);
5960

6061
public IEnumerable<ResourceRemark> GetResourceRemarks(MarkdownGenerator.ResourceMetadata resource)
6162
=> (ResourceRemarks ?? [])
@@ -78,6 +79,11 @@ public IEnumerable<CodeSample> GetBicepSamples(MarkdownGenerator.ResourceMetadat
7879
.Where(r => StringComparer.OrdinalIgnoreCase.Equals(resource.ResourceType, r.ResourceType))
7980
.Where(r => FilterApiVersions(resource, r.ApiVersion));
8081

82+
public IEnumerable<CodeSample> GetTerraformSamples(MarkdownGenerator.ResourceMetadata resource)
83+
=> (TerraformSamples ?? [])
84+
.Where(r => StringComparer.OrdinalIgnoreCase.Equals(resource.ResourceType, r.ResourceType))
85+
.Where(r => FilterApiVersions(resource, r.ApiVersion));
86+
8187
private static bool FilterApiVersions(MarkdownGenerator.ResourceMetadata resource, ApiVersionRequirement? apiVersionRequirement)
8288
=> (apiVersionRequirement?.Minimum is null || ApiVersionComparer.Instance.Compare(apiVersionRequirement.Minimum, resource.ApiVersion) >= 0) &&
8389
(apiVersionRequirement?.Maximum is null || ApiVersionComparer.Instance.Compare(apiVersionRequirement.Maximum, resource.ApiVersion) <= 0);

src/TemplateRefGenerator/Generators/MarkdownGenerator.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ Set the **{discObjectType.Discriminator}** property to specify the type of objec
502502
return sb.ToString();
503503
}
504504

505-
private static string GetTerraformZone(ConfigLoader configLoader, ResourceMetadata resource, ImmutableArray<NamedType> namedTypes, RemarksFile remarks, int anchorIndex)
505+
private static string GetTerraformZone(ConfigLoader configLoader, RemarksLoader remarksLoader, ResourceMetadata resource, ImmutableArray<NamedType> namedTypes, RemarksFile remarks, int anchorIndex)
506506
{
507507
var sb = new StringBuilder();
508508
sb.Append($"""
@@ -591,6 +591,7 @@ Set the **{discObjectType.Discriminator}** property to specify the type of objec
591591
]));
592592

593593
sb.Append(GenerateOptionalSection("Usage Examples", [
594+
GetTerraformSamplesSection(remarksLoader, resource, remarks),
594595
GetAvmSection(configLoader.GetSamples(), AvmLinkType.Terraform, resource),
595596
]));
596597

@@ -605,9 +606,9 @@ Set the **{discObjectType.Discriminator}** property to specify the type of objec
605606

606607
private static string? GetBicepSamplesSection(RemarksLoader remarksLoader, ResourceMetadata resource, RemarksFile remarks)
607608
{
608-
var bicepSamples = remarks.GetBicepSamples(resource).ToArray();
609+
var samples = remarks.GetBicepSamples(resource).ToArray();
609610

610-
if (!bicepSamples.Any())
611+
if (!samples.Any())
611612
{
612613
return null;
613614
}
@@ -619,7 +620,7 @@ Set the **{discObjectType.Discriminator}** property to specify the type of objec
619620
620621
""");
621622

622-
foreach (var sample in bicepSamples)
623+
foreach (var sample in samples)
623624
{
624625
sb.Append($"""
625626
{sample.Description}
@@ -628,6 +629,37 @@ Set the **{discObjectType.Discriminator}** property to specify the type of objec
628629
{remarksLoader.GetCodeSample(resource.Provider, sample)}
629630
```
630631
632+
""");
633+
}
634+
635+
return sb.ToString();
636+
}
637+
638+
private static string? GetTerraformSamplesSection(RemarksLoader remarksLoader, ResourceMetadata resource, RemarksFile remarks)
639+
{
640+
var samples = remarks.GetTerraformSamples(resource).ToArray();
641+
642+
if (!samples.Any())
643+
{
644+
return null;
645+
}
646+
647+
var sb = new StringBuilder();
648+
sb.Append($"""
649+
### Terraform Samples
650+
651+
652+
""");
653+
654+
foreach (var sample in samples)
655+
{
656+
sb.Append($"""
657+
{sample.Description}
658+
659+
```tf
660+
{remarksLoader.GetCodeSample(resource.Provider, sample)}
661+
```
662+
631663
""");
632664
}
633665

@@ -1004,7 +1036,7 @@ public static string GenerateMarkdown(GroupedTypes groupedTypes, ResourceType re
10041036
{GetResourceRemarks(resource, remarks)}
10051037
{GetBicepZone(configLoader, remarksLoader, resource, namedTypes, remarks, 0)}
10061038
{GetArmTemplateZone(configLoader, resource, namedTypes, remarks, 1)}
1007-
{GetTerraformZone(configLoader, resource, namedTypes, remarks, 2)}
1039+
{GetTerraformZone(configLoader, remarksLoader, resource, namedTypes, remarks, 2)}
10081040
""";
10091041
}
10101042
}

0 commit comments

Comments
 (0)