Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include scope & parent_id in code samples #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ To create a Microsoft.Addons/supportProviders/supportPlanTypes resource, add the

```bicep
resource symbolicname 'Microsoft.Addons/supportProviders/supportPlanTypes@2018-03-01' = {
parent: resourceSymbolicName
name: 'string'
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ To create a Microsoft.CostManagement/exports resource, add the following Bicep t

```bicep
resource symbolicname 'Microsoft.CostManagement/exports@2019-01-01' = {
scope: resourceSymbolicName or scope
name: 'string'
properties: {
definition: {
Expand Down Expand Up @@ -491,6 +492,7 @@ To create a Microsoft.CostManagement/exports resource, add the following Terrafo
resource "azapi_resource" "symbolicname" {
type = "Microsoft.CostManagement/exports@2019-01-01"
name = "string"
parent_id = "string"
body = jsonencode({
properties = {
definition = {
Expand Down
36 changes: 28 additions & 8 deletions src/TemplateRefGenerator/Generators/CodeSampleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ private static string GetStringBuilderResult(Action<StringBuilder> action)

private static GenerateResult GenerateBicep(MarkdownGenerator.ResourceMetadata resource, ImmutableArray<MarkdownGenerator.NamedType> namedTypes)
{
var mainSample = GetStringBuilderResult(sb => GenerateBicep(sb, 0, resource.Type, "", new()));
var mainSample = GetStringBuilderResult(sb => GenerateBicep(resource, sb, 0, resource.Type, "", new()));

var discriminatedSamples = new Dictionary<DiscriminatedObjectType, ImmutableArray<DiscriminatedObjectSample>>();
foreach (var discriminatedObject in namedTypes.Select(x => x.Type).OfType<DiscriminatedObjectType>())
{
var samples = new List<DiscriminatedObjectSample>();
foreach (var element in discriminatedObject.Elements)
{
var discSample = GetStringBuilderResult(sb => GenerateBicep(sb, 0, element.Value.Type, null, new()));
var discSample = GetStringBuilderResult(sb => GenerateBicep(resource, sb, 0, element.Value.Type, null, new()));
samples.Add(new(discSample, element.Key));
}

Expand All @@ -71,7 +71,7 @@ private static GenerateResult GenerateBicep(MarkdownGenerator.ResourceMetadata r
return new(mainSample, discriminatedSamples.ToImmutableDictionary());
}

private static void GenerateBicep(StringBuilder sb, int indentLevel, TypeBase type, string? path, HashSet<TypeBase> visited)
private static void GenerateBicep(MarkdownGenerator.ResourceMetadata resource, StringBuilder sb, int indentLevel, TypeBase type, string? path, HashSet<TypeBase> visited)
{
var indent = GetIndent(indentLevel);
var propIndent = GetIndent(indentLevel + 1);
Expand All @@ -96,7 +96,7 @@ void AddProperty(string name, Action writeValue)
{
case ResourceType resourceType:
sb.Append($"resource symbolicname '{resourceType.Name}' = ");
GenerateBicep(sb, 0, resourceType.Body.Type, "", visited);
GenerateBicep(resource, sb, 0, resourceType.Body.Type, "", visited);
break;

case ObjectType objectType:
Expand All @@ -110,20 +110,34 @@ void AddProperty(string name, Action writeValue)
}

sb.AppendLine("{");

if (path == "")
{
if (Utils.IsChildResource(resource.UnqualifiedResourceType))
{
AddProperty("parent", () => sb.Append($"resourceSymbolicName"));
}
else if (resource.Type.ScopeType == ScopeType.Unknown ||
resource.Type.ScopeType.HasFlag(ScopeType.Extension))
{
AddProperty("scope", () => sb.Append($"resourceSymbolicName or scope"));
}
}

foreach (var (name, prop) in MarkdownGenerator.GetOrderedWritableProperties(objectType.Properties))
{
AddProperty(name, () => GenerateBicep(sb, indentLevel + 1, prop.Type.Type, $"{path}.{name}", visited));
AddProperty(name, () => GenerateBicep(resource, sb, indentLevel + 1, prop.Type.Type, $"{path}.{name}", visited));
}
if (!props.Any() && additionalPropType is {})
{
AddProperty("{customized property}", () => GenerateBicep(sb, indentLevel + 1, additionalPropType.Type, $"{path}.*", visited));
AddProperty("{customized property}", () => GenerateBicep(resource, sb, indentLevel + 1, additionalPropType.Type, $"{path}.*", visited));
}
sb.Append(indent + "}");
break;
case ArrayType arrayType:
sb.AppendLine("[");
sb.Append(propIndent);
GenerateBicep(sb, indentLevel + 1, arrayType.ItemType.Type, $"{path}[*]", visited);
GenerateBicep(resource, sb, indentLevel + 1, arrayType.ItemType.Type, $"{path}[*]", visited);
sb.AppendLine();
sb.Append(indent + "]");
break;
Expand All @@ -145,7 +159,7 @@ void AddProperty(string name, Action writeValue)
foreach (var (name, prop) in MarkdownGenerator.GetOrderedWritableProperties(objectType.BaseProperties))
{
sb.Append($"{propIndent}{name}: ");
GenerateBicep(sb, indentLevel + 1, prop.Type.Type, $"{path}.{name}", visited);
GenerateBicep(resource, sb, indentLevel + 1, prop.Type.Type, $"{path}.{name}", visited);
sb.AppendLine();
}
sb.AppendLine($"{propIndent}{objectType.Discriminator}: 'string'");
Expand Down Expand Up @@ -349,6 +363,12 @@ void AddProperty(string name, Action writeValue)

AddProperty("type", () => sb.Append($"\"{resource.ResourceType}@{resource.ApiVersion}\""));
AddProperty("name", () => sb.Append($"\"string\""));

if (resource.Type.ScopeType == ScopeType.Unknown ||
resource.Type.ScopeType.HasFlag(ScopeType.Extension))
{
AddProperty("parent_id", () => sb.Append($"\"string\""));
}
}

var bodyProps = props.Where(x => path == "" && TerraformBodyProperties.Contains(x.Key)).ToList();
Expand Down